From 34c0810c8013b2b6a5118e8fc4fd166b9ef2dce4 Mon Sep 17 00:00:00 2001
From: sozyGithub <thinpotatogithub@gmail.com>
Date: Sat, 4 Nov 2023 22:56:27 +0700
Subject: [PATCH] fix: web method exception

---
 .../Exceptions/RequestException.java          | 36 +++++++---------
 .../soapService/Exceptions/RequestFault.java  | 11 -----
 .../soapService/Handlers/ClientHandler.java   | 43 +++++++++++++------
 .../AccountVerificationRequestService.java    | 12 +++---
 ...AccountVerificationRequestServiceImpl.java | 12 +++---
 .../Services/CatalogRequestService.java       | 16 +++----
 .../Services/CatalogRequestServiceImpl.java   | 19 ++++----
 .../Services/ReportUserService.java           | 14 +++---
 .../Services/ReportUserServiceImpl.java       | 20 ++++++---
 9 files changed, 96 insertions(+), 87 deletions(-)
 delete mode 100644 src/main/java/org/soapService/Exceptions/RequestFault.java

diff --git a/src/main/java/org/soapService/Exceptions/RequestException.java b/src/main/java/org/soapService/Exceptions/RequestException.java
index ba302a7..b15317d 100644
--- a/src/main/java/org/soapService/Exceptions/RequestException.java
+++ b/src/main/java/org/soapService/Exceptions/RequestException.java
@@ -1,28 +1,24 @@
 package org.soapService.Exceptions;
 
-import lombok.NoArgsConstructor;
+import javax.xml.soap.SOAPFactory;
+import javax.xml.soap.SOAPFault;
+import javax.xml.ws.soap.SOAPFaultException;
 
-import javax.xml.ws.WebFault;
+public class RequestException {
+    public RequestException(String faultCode, String faultString) throws SOAPFaultException {
+        try {
+            SOAPFactory soapFactory = SOAPFactory.newInstance();
+            SOAPFault soapFault = soapFactory.createFault();
+            soapFault.setFaultCode(faultCode);
+            soapFault.setFaultString(faultString);
 
-@NoArgsConstructor
-@WebFault(name = "RequestFault", faultBean = "org.soapService.Exceptions.RequestFault")
-public class RequestException extends Exception {
-    private RequestFault fault;
-
-    protected RequestException(RequestFault fault) {
-        super(fault.getFaultString());
-        this.fault = fault;
-    }
-
-    public RequestException(String code, String message) {
-        super(message);
-        this.fault = new RequestFault();
-        this.fault.setFaultString(message);
-        this.fault.setFaultCode(code);
+            throw new SOAPFaultException(soapFault);
+        } catch (SOAPFaultException e) {
+            throw e;
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
     }
 
-    public RequestFault getFaultInfo() {
-        return fault;
-    }
 }
 
diff --git a/src/main/java/org/soapService/Exceptions/RequestFault.java b/src/main/java/org/soapService/Exceptions/RequestFault.java
deleted file mode 100644
index adb0c62..0000000
--- a/src/main/java/org/soapService/Exceptions/RequestFault.java
+++ /dev/null
@@ -1,11 +0,0 @@
-package org.soapService.Exceptions;
-
-import lombok.Getter;
-import lombok.Setter;
-
-@Getter
-@Setter
-public class RequestFault {
-    private String faultCode;
-    private String faultString;
-}
diff --git a/src/main/java/org/soapService/Handlers/ClientHandler.java b/src/main/java/org/soapService/Handlers/ClientHandler.java
index e94f6dd..e5e8c01 100644
--- a/src/main/java/org/soapService/Handlers/ClientHandler.java
+++ b/src/main/java/org/soapService/Handlers/ClientHandler.java
@@ -2,11 +2,15 @@ package org.soapService.Handlers;
 
 import at.favre.lib.crypto.bcrypt.BCrypt;
 import io.github.cdimascio.dotenv.Dotenv;
+import org.soapService.Common.HTTPStatusCode;
 
 import javax.xml.namespace.QName;
+import javax.xml.soap.SOAPFactory;
+import javax.xml.soap.SOAPFault;
 import javax.xml.ws.handler.MessageContext;
 import javax.xml.ws.handler.soap.SOAPHandler;
 import javax.xml.ws.handler.soap.SOAPMessageContext;
+import javax.xml.ws.soap.SOAPFaultException;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -20,27 +24,38 @@ public class ClientHandler implements SOAPHandler<SOAPMessageContext> {
         Boolean outboundProperty = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
         Integer httpExchange = (Integer) context.get(MessageContext.HTTP_RESPONSE_CODE);
 
-        if (!outboundProperty) {
-            Map<String, List<String>> headers = (Map<String, List<String>>) context.get(MessageContext.HTTP_REQUEST_HEADERS);
+        try {
+            if (!outboundProperty) {
+                Map<String, List<String>> headers = (Map<String, List<String>>) context.get(MessageContext.HTTP_REQUEST_HEADERS);
 
+                SOAPFactory soapFactory = SOAPFactory.newInstance();
+                SOAPFault soapFault = soapFactory.createFault();
+                soapFault.setFaultCode(HTTPStatusCode.UNAUTHORIZED.getCodeStr());
+                soapFault.setFaultString("Unauthorized");
 
-            if (headers.get("token") == null) {
-                return false;
-            }
 
-            Dotenv dotenv = Dotenv.load();
+                if (headers.get("token") == null) {
+                    throw new SOAPFaultException(soapFault);
+                }
 
-            BCrypt.Result resultREST = BCrypt.verifyer().verify(headers.get("token").get(0).toCharArray(), dotenv.get("REST_API_KEY"));
-            System.out.println("REST Client: " + resultREST.verified);
+                Dotenv dotenv = Dotenv.load();
 
-            BCrypt.Result resultPHP = BCrypt.verifyer().verify(headers.get("token").get(0).toCharArray(), dotenv.get("PHP_API_KEY"));
-            System.out.println("PHP Client: " + resultPHP.verified);
+                BCrypt.Result resultREST = BCrypt.verifyer().verify(headers.get("token").get(0).toCharArray(), dotenv.get("REST_API_KEY"));
+                System.out.println("REST Client: " + resultREST.verified);
 
-            if (!resultREST.verified && !resultPHP.verified) {
-                return false;
-            }
+                BCrypt.Result resultPHP = BCrypt.verifyer().verify(headers.get("token").get(0).toCharArray(), dotenv.get("PHP_API_KEY"));
+                System.out.println("PHP Client: " + resultPHP.verified);
 
-            context.put("client", resultREST.verified ? "REST" : "PHP");
+                if (!resultREST.verified && !resultPHP.verified) {
+                    throw new SOAPFaultException(soapFault);
+                }
+
+                context.put("client", resultREST.verified ? "REST" : "PHP");
+            }
+        } catch (SOAPFaultException soapFaultException) {
+            throw soapFaultException;
+        } catch (Exception e) {
+            e.printStackTrace();
         }
         return true;
     }
diff --git a/src/main/java/org/soapService/Services/AccountVerificationRequestService.java b/src/main/java/org/soapService/Services/AccountVerificationRequestService.java
index 4595c26..2e996e4 100644
--- a/src/main/java/org/soapService/Services/AccountVerificationRequestService.java
+++ b/src/main/java/org/soapService/Services/AccountVerificationRequestService.java
@@ -1,13 +1,13 @@
 package org.soapService.Services;
 
 import org.soapService.Domain.AccountVerificationRequest;
-import org.soapService.Exceptions.RequestException;
 
 import javax.jws.HandlerChain;
 import javax.jws.WebMethod;
 import javax.jws.WebParam;
 import javax.jws.WebService;
 import javax.xml.ws.RequestWrapper;
+import javax.xml.ws.soap.SOAPFaultException;
 import java.util.List;
 
 @WebService
@@ -15,21 +15,21 @@ import java.util.List;
 public interface AccountVerificationRequestService {
     @WebMethod(operationName = "GetRequests")
     @RequestWrapper(className = "AccountVerificationRequestService.GetRequests")
-    public List<AccountVerificationRequest> getRequests() throws RequestException;
+    public List<AccountVerificationRequest> getRequests() throws SOAPFaultException;
 
     @WebMethod(operationName = "CreateRequest")
     @RequestWrapper(className = "AccountVerificationRequestService.CreateRequest")
-    public boolean createRequest() throws RequestException;
+    public boolean createRequest() throws SOAPFaultException;
 
     @WebMethod(operationName = "AcceptRequest")
     @RequestWrapper(className = "AccountVerificationRequestService.AcceptRequest")
-    public boolean acceptRequest(@WebParam(name = "userId") int userId) throws RequestException;
+    public boolean acceptRequest(@WebParam(name = "userId") int userId) throws SOAPFaultException;
 
     @WebMethod(operationName = "RejectRequest")
     @RequestWrapper(className = "AccountVerificationRequestService.RejectRequest")
-    public boolean rejectRequest(@WebParam(name = "userId") int userId) throws RequestException;
+    public boolean rejectRequest(@WebParam(name = "userId") int userId) throws SOAPFaultException;
 
     @WebMethod(operationName = "DeleteRequest")
     @RequestWrapper(className = "AccountVerificationRequestService.DeleteRequest")
-    public boolean deleteRequest(@WebParam(name = "requestId") int requestId) throws RequestException;
+    public boolean deleteRequest(@WebParam(name = "requestId") int requestId) throws SOAPFaultException;
 }
diff --git a/src/main/java/org/soapService/Services/AccountVerificationRequestServiceImpl.java b/src/main/java/org/soapService/Services/AccountVerificationRequestServiceImpl.java
index 83694c4..2672038 100644
--- a/src/main/java/org/soapService/Services/AccountVerificationRequestServiceImpl.java
+++ b/src/main/java/org/soapService/Services/AccountVerificationRequestServiceImpl.java
@@ -1,30 +1,30 @@
 package org.soapService.Services;
 
 import org.soapService.Domain.AccountVerificationRequest;
-import org.soapService.Exceptions.RequestException;
 
 import javax.jws.WebService;
+import javax.xml.ws.soap.SOAPFaultException;
 import java.util.List;
 
 @WebService(endpointInterface = "org.soapService.Services.AccountVerificationRequestService")
 public class AccountVerificationRequestServiceImpl extends BaseService implements AccountVerificationRequestService {
-    public List<AccountVerificationRequest> getRequests() throws RequestException {
+    public List<AccountVerificationRequest> getRequests() throws SOAPFaultException {
         return null;
     }
 
-    public boolean createRequest() throws RequestException {
+    public boolean createRequest() throws SOAPFaultException {
         return false;
     }
 
-    public boolean acceptRequest(int userId) throws RequestException {
+    public boolean acceptRequest(int userId) throws SOAPFaultException {
         return false;
     }
 
-    public boolean rejectRequest(int userId) throws RequestException {
+    public boolean rejectRequest(int userId) throws SOAPFaultException {
         return false;
     }
 
-    public boolean deleteRequest(int requestId) throws RequestException {
+    public boolean deleteRequest(int requestId) throws SOAPFaultException {
         return false;
     }
 }
diff --git a/src/main/java/org/soapService/Services/CatalogRequestService.java b/src/main/java/org/soapService/Services/CatalogRequestService.java
index a773bcd..d1ea090 100644
--- a/src/main/java/org/soapService/Services/CatalogRequestService.java
+++ b/src/main/java/org/soapService/Services/CatalogRequestService.java
@@ -2,7 +2,6 @@ package org.soapService.Services;
 
 import org.soapService.Common.ServiceResponse;
 import org.soapService.Domain.CatalogRequest;
-import org.soapService.Exceptions.RequestException;
 
 import javax.activation.DataHandler;
 import javax.jws.HandlerChain;
@@ -10,6 +9,7 @@ import javax.jws.WebMethod;
 import javax.jws.WebParam;
 import javax.jws.WebService;
 import javax.xml.ws.RequestWrapper;
+import javax.xml.ws.soap.SOAPFaultException;
 import java.util.List;
 
 @WebService
@@ -21,29 +21,29 @@ public interface CatalogRequestService {
                                  @WebParam(name = "description") String description,
                                  @WebParam(name = "poster") String poster,
                                  @WebParam(name = "trailer") String trailer,
-                                 @WebParam(name = "category") String category) throws RequestException;
+                                 @WebParam(name = "category") String category) throws SOAPFaultException;
 
     @WebMethod(operationName = "GetRequests")
     @RequestWrapper(className = "CatalogRequestService.GetRequests")
-    public List<CatalogRequest> getRequests() throws RequestException;
+    public List<CatalogRequest> getRequests() throws SOAPFaultException;
 
     @WebMethod(operationName = "GetRequest")
     @RequestWrapper(className = "CatalogRequestService.GetRequest")
-    public CatalogRequest getRequest(@WebParam(name = "requestId") int requestId) throws RequestException;
+    public CatalogRequest getRequest(@WebParam(name = "requestId") int requestId) throws SOAPFaultException;
 
     @WebMethod(operationName = "AcceptRequest")
     @RequestWrapper(className = "CatalogRequestService.AcceptRequest")
-    public boolean acceptRequest(@WebParam(name = "requestId") int requestId) throws RequestException;
+    public boolean acceptRequest(@WebParam(name = "requestId") int requestId) throws SOAPFaultException;
 
     @WebMethod(operationName = "RejectRequest")
     @RequestWrapper(className = "CatalogRequestService.RejectRequest")
-    public boolean rejectRequest(@WebParam(name = "requestId") int requestId) throws RequestException;
+    public boolean rejectRequest(@WebParam(name = "requestId") int requestId) throws SOAPFaultException;
 
     @WebMethod(operationName = "DeleteRequest")
     @RequestWrapper(className = "CatalogRequestService.DeleteRequest")
-    public boolean deleteRequest(@WebParam(name = "requestId") int requestId) throws RequestException;
+    public boolean deleteRequest(@WebParam(name = "requestId") int requestId) throws SOAPFaultException;
 
     @WebMethod(operationName = "TestUpload")
     @RequestWrapper(className = "CatalogRequestService.TestUpload")
-    public ServiceResponse testUpload(@WebParam(name = "data") DataHandler data) throws RequestException;
+    public ServiceResponse testUpload(@WebParam(name = "data") DataHandler data) throws SOAPFaultException;
 }
diff --git a/src/main/java/org/soapService/Services/CatalogRequestServiceImpl.java b/src/main/java/org/soapService/Services/CatalogRequestServiceImpl.java
index 945f937..515a9c3 100644
--- a/src/main/java/org/soapService/Services/CatalogRequestServiceImpl.java
+++ b/src/main/java/org/soapService/Services/CatalogRequestServiceImpl.java
@@ -9,19 +9,20 @@ import org.soapService.Utils.FileUploader;
 
 import javax.activation.DataHandler;
 import javax.jws.WebService;
+import javax.xml.ws.soap.SOAPFaultException;
 import java.util.List;
 
 @WebService(endpointInterface = "org.soapService.Services.CatalogRequestService")
 public class CatalogRequestServiceImpl extends BaseService implements CatalogRequestService {
-    public boolean createRequest(String title, String description, String poster, String trailer, String category) throws RequestException {
+    public boolean createRequest(String title, String description, String poster, String trailer, String category) throws SOAPFaultException {
         return false;
     }
 
-    public List<CatalogRequest> getRequests() throws RequestException {
+    public List<CatalogRequest> getRequests() throws SOAPFaultException {
         return null;
     }
 
-    public CatalogRequest getRequest(int requestId) throws RequestException {
+    public CatalogRequest getRequest(int requestId) throws SOAPFaultException {
         // For testing purpose
 
         CatalogRequest cr = new CatalogRequest();
@@ -29,30 +30,30 @@ public class CatalogRequestServiceImpl extends BaseService implements CatalogReq
         return cr;
     }
 
-    public boolean acceptRequest(int requestId) throws RequestException {
+    public boolean acceptRequest(int requestId) throws SOAPFaultException {
         return false;
     }
 
-    public boolean rejectRequest(int requestId) throws RequestException {
+    public boolean rejectRequest(int requestId) throws SOAPFaultException {
         return false;
     }
 
-    public boolean deleteRequest(int requestId) throws RequestException {
+    public boolean deleteRequest(int requestId) throws SOAPFaultException {
         System.out.println(requestId);
         return false;
     }
 
-    public ServiceResponse testUpload(DataHandler dataHandler) throws RequestException {
+    public ServiceResponse testUpload(DataHandler dataHandler) throws SOAPFaultException {
         try {
             boolean valid = FileUploader.validateImage(dataHandler);
             if (valid) {
                 FileUploader.upload(dataHandler, FileType.IMAGE);
             } else {
-                throw new RequestException(HTTPStatusCode.BAD_REQUEST.getCodeStr(), "Invalid data");
+                new RequestException(HTTPStatusCode.BAD_REQUEST.getCodeStr(), "Invalid data");
             }
         } catch (Exception e) {
             e.printStackTrace();
-            throw new RequestException(HTTPStatusCode.INTERNAL_SERVER_ERROR.getCodeStr(), e.getMessage());
+            new RequestException(HTTPStatusCode.INTERNAL_SERVER_ERROR.getCodeStr(), e.getMessage());
         }
 
         ServiceResponse response = new ServiceResponse();
diff --git a/src/main/java/org/soapService/Services/ReportUserService.java b/src/main/java/org/soapService/Services/ReportUserService.java
index 9cc12c0..7549235 100644
--- a/src/main/java/org/soapService/Services/ReportUserService.java
+++ b/src/main/java/org/soapService/Services/ReportUserService.java
@@ -2,7 +2,6 @@ package org.soapService.Services;
 
 import org.soapService.Common.ServiceResponse;
 import org.soapService.Domain.ReportUser;
-import org.soapService.Exceptions.RequestException;
 
 import javax.jws.HandlerChain;
 import javax.jws.WebMethod;
@@ -10,6 +9,7 @@ import javax.jws.WebParam;
 import javax.jws.WebService;
 import javax.xml.bind.annotation.XmlSeeAlso;
 import javax.xml.ws.RequestWrapper;
+import javax.xml.ws.soap.SOAPFaultException;
 
 @WebService
 @XmlSeeAlso({ServiceResponse.class})
@@ -17,19 +17,19 @@ import javax.xml.ws.RequestWrapper;
 public interface ReportUserService {
     @WebMethod(operationName = "CreateReport")
     @RequestWrapper(className = "ReportUserService.CreateReport")
-    public ServiceResponse createReport(@WebParam(name = "content") String content,
-                                        @WebParam(name = "reportedId") String reportedId,
-                                        @WebParam(name = "reporterId") String reporterId) throws RequestException;
+    public ServiceResponse<ReportUser> createReport(@WebParam(name = "content") String content,
+                                                    @WebParam(name = "reportedId") String reportedId,
+                                                    @WebParam(name = "reporterId") String reporterId) throws SOAPFaultException;
 
     @WebMethod(operationName = "GetReportedUsers")
     @RequestWrapper(className = "ReportUserService.GetReportedUsers")
-    public ReportUser getReportedUsers() throws RequestException;
+    public ReportUser getReportedUsers() throws SOAPFaultException;
 
     @WebMethod(operationName = "BlockUser")
     @RequestWrapper(className = "ReportUserService.BlockUser")
-    public boolean blockUser(@WebParam(name = "userId") int userId) throws RequestException;
+    public boolean blockUser(@WebParam(name = "userId") int userId) throws SOAPFaultException;
 
     @WebMethod(operationName = "DeleteReport")
     @RequestWrapper(className = "ReportUserService.DeleteReport")
-    public boolean deleteReport(@WebParam(name = "reportId") int reportId) throws RequestException;
+    public boolean deleteReport(@WebParam(name = "reportId") int reportId) throws SOAPFaultException;
 }
diff --git a/src/main/java/org/soapService/Services/ReportUserServiceImpl.java b/src/main/java/org/soapService/Services/ReportUserServiceImpl.java
index 9fc6f19..cfa3326 100644
--- a/src/main/java/org/soapService/Services/ReportUserServiceImpl.java
+++ b/src/main/java/org/soapService/Services/ReportUserServiceImpl.java
@@ -9,13 +9,17 @@ import org.soapService.Repository.ReportUserRepository;
 import org.soapService.Validations.ReportUserServiceValidation;
 
 import javax.jws.WebService;
+import javax.xml.ws.soap.SOAPFaultException;
+import java.util.ArrayList;
+import java.util.List;
 
 @WebService(endpointInterface = "org.soapService.Services.ReportUserService")
 public class ReportUserServiceImpl extends BaseService implements ReportUserService {
     private static ReportUserRepository reportUserRepository = new ReportUserRepository();
     private static ReportUserServiceValidation reportUserServiceValidation = new ReportUserServiceValidation();
 
-    public ServiceResponse createReport(String content, String reportedId, String reporterId) throws RequestException {
+    public ServiceResponse<ReportUser> createReport(String content, String reportedId, String reporterId) throws SOAPFaultException {
+        List<ReportUser> lru = new ArrayList<>();
         try {
             reportUserServiceValidation.validateCreateReport(content, reportedId, reporterId);
 
@@ -24,34 +28,38 @@ public class ReportUserServiceImpl extends BaseService implements ReportUserServ
             reportUser.setReportedId(reportedId);
             reportUser.setReporterId(reporterId);
 
+            lru.add(reportUser);
+            lru.add(reportUser);
+
             reportUserRepository.add(reportUser);
         } catch (ValidationException e) {
             System.out.println(e.getMessage());
-            throw new RequestException(e.getStatus(), e.getMessage());
+            new RequestException(HTTPStatusCode.BAD_REQUEST.getCodeStr(), e.getMessage());
         } catch (Exception e) {
             System.out.println(e.getMessage());
-            throw new RequestException(HTTPStatusCode.INTERNAL_SERVER_ERROR.getCodeStr(), "Something went wrong, please try again later");
+            new RequestException(HTTPStatusCode.INTERNAL_SERVER_ERROR.getCodeStr(), "Something went wrong, please try again later");
         }
 
         ServiceResponse<ReportUser> response = new ServiceResponse<>();
         response.setStatus(HTTPStatusCode.OK.getCodeStr());
         response.setMessage("Report successfully created");
+        response.setData(lru);
 
         return response;
     }
 
 
-    public ReportUser getReportedUsers() throws RequestException {
+    public ReportUser getReportedUsers() throws SOAPFaultException {
         return null;
     }
 
 
-    public boolean blockUser(int userId) throws RequestException {
+    public boolean blockUser(int userId) throws SOAPFaultException {
         return false;
     }
 
 
-    public boolean deleteReport(int reportId) throws RequestException {
+    public boolean deleteReport(int reportId) throws SOAPFaultException {
         return false;
     }
 }
-- 
GitLab