diff --git a/src/main/java/com/letterpaw/soap/service/UserPremiumServiceImpl.java b/src/main/java/com/letterpaw/soap/service/UserPremiumServiceImpl.java index 126740f56aa5b2d98be676a8ca4c738771660ac9..6f0ba973fbd163d288b024268eba4c97851146d2 100644 --- a/src/main/java/com/letterpaw/soap/service/UserPremiumServiceImpl.java +++ b/src/main/java/com/letterpaw/soap/service/UserPremiumServiceImpl.java @@ -6,6 +6,7 @@ import com.letterpaw.soap.enums.ServiceType; import com.letterpaw.soap.util.Auth; import com.letterpaw.soap.util.Logger; import com.letterpaw.soap.util.Mailer; +import com.letterpaw.soap.util.Request; import javax.annotation.Resource; import javax.jws.WebMethod; @@ -34,14 +35,14 @@ public class UserPremiumServiceImpl implements UserPremiumService { s.premiumStatus = UserPremium.Status.PENDING; s.save(); // Notify admin -// Request.GET(ServiceType.REST, "/users/admins", (String adminJson) -> { -// Mailer.notifyAdminNewSub( -// "There is a new premium request from user id " + userId, -// adminJson -// ); -// }); - String adminJson = "[{\"email\":\"ken35kiel@gmail.com\"}]"; - Mailer.notifyAdminNewSub("There is a new premium request from user ID " + userId, adminJson); + Request.GET(ServiceType.PHP, "/admins", (String adminJson) -> { + Mailer.notifyAdminNewSub( + "There is a new premium request from user id " + userId, + adminJson + ); + }); +// String adminJson = "[{\"email\":\"ken35kiel@gmail.com\"}]"; +// Mailer.notifyAdminNewSub("There is a new premium request from user ID " + userId, adminJson); return ResponseType.SUCCESS.toString(); // Success adding new Successfully registered for premium } return ResponseType.FAILED.toString(); // User Premium already exist User Premium already exists diff --git a/src/main/java/com/letterpaw/soap/util/Auth.java b/src/main/java/com/letterpaw/soap/util/Auth.java index a2f2648272943084bb35b21ad2d7bb36b9229ba5..50ab18be0f68c6529701af35d229d5012b4dee40 100644 --- a/src/main/java/com/letterpaw/soap/util/Auth.java +++ b/src/main/java/com/letterpaw/soap/util/Auth.java @@ -30,11 +30,11 @@ public class Auth { } else if (type == ServiceType.PHP) { key = PHPAPIKey; } -// System.out.println(auth.get(0)); -// System.out.println(key); + System.out.println(auth.get(0)); + System.out.println(key); System.out.println(!auth.isEmpty() && auth.get(0).equals("Bearer " + key)); - return true; +// return true; // Check the key validity -// return !auth.isEmpty() && auth.get(0).equals("Bearer " + key); + return !auth.isEmpty() && auth.get(0).equals("Bearer " + key); } } diff --git a/src/main/java/com/letterpaw/soap/util/Mailer.java b/src/main/java/com/letterpaw/soap/util/Mailer.java index d17365b03bcfa0f554e323f0346c2f041bf28a68..3cdef20754f9ac5c9faf7dc3cde12aa3a9cd958f 100644 --- a/src/main/java/com/letterpaw/soap/util/Mailer.java +++ b/src/main/java/com/letterpaw/soap/util/Mailer.java @@ -5,9 +5,11 @@ import jakarta.mail.*; import jakarta.mail.internet.InternetAddress; import jakarta.mail.internet.MimeMessage; +import java.util.List; import java.util.Properties; import org.json.JSONArray; +import org.json.JSONObject; public class Mailer { static Dotenv dotenv = Dotenv.configure().ignoreIfMissing().load(); @@ -62,13 +64,19 @@ public class Mailer { } public static void notifyAdminNewSub(String textMsg, String adminJson) { - JSONArray resp = new JSONArray(adminJson); + System.out.println(adminJson); + JSONObject resp = new JSONObject(adminJson); if (resp.isEmpty()) { System.out.println("No admin to notify: " + textMsg); return; } - String to = resp.getJSONObject(0).getString("email"); - sendMail(to, "New subscription request to Letterpaw SOAP", textMsg); - System.out.println("Sent email to admin <" + to + "> with message: " + textMsg); +// String adminJson = "[{\"email\":\"ken35kiel@gmail.com\"}]"; +// [{"status":200,"data":["admin@example.com","adm@email.com"]}] + JSONArray to = resp.getJSONArray("data"); + for (int i = 0; i < to.length(); i++) { + sendMail(to.getString(i), "New subscription request to Letterpaw SOAP", textMsg); + System.out.println("Sent email to admin <" + to.getString(i) + "> with message: " + textMsg); + } + } } diff --git a/src/main/java/com/letterpaw/soap/util/Request.java b/src/main/java/com/letterpaw/soap/util/Request.java index a5f4b42fa1b3a34368db36bed9ef15744960f4d9..760d5ae8927c58587335efd7e42bf51df17cac65 100644 --- a/src/main/java/com/letterpaw/soap/util/Request.java +++ b/src/main/java/com/letterpaw/soap/util/Request.java @@ -1,6 +1,7 @@ package com.letterpaw.soap.util; import com.letterpaw.soap.enums.ServiceType; +import io.github.cdimascio.dotenv.Dotenv; // Make sure to import the correct HttpClient, HttpRequest, and HttpResponse import java.io.DataOutputStream; import java.net.HttpURLConnection; @@ -10,6 +11,8 @@ import java.io.InputStreamReader; import java.util.function.Consumer; public class Request { + static Dotenv dotenv = Dotenv.configure().ignoreIfMissing().load(); + public enum RequestType { GET, POST @@ -30,9 +33,11 @@ public class Request { try { // Create connection // Get URL - URL url = new URL("http://" + - System.getenv().getOrDefault((type == ServiceType.PHP ? "PHP_HOST" : "REST_HOST"), "localhost") + + URL url = new URL("http", + dotenv.get((type == ServiceType.PHP ? "PHP_HOST" : "REST_HOST"), "localhost"), + Integer.parseInt(dotenv.get((type == ServiceType.PHP ? "PHP_PORT" : "REST_PORT"), "8008")), endpoint); + System.out.println(url.toString()); // Open connection connection = (HttpURLConnection) url.openConnection(); // Set request type