diff --git a/src/main/java/org/soapService/Exceptions/RequestException.java b/src/main/java/org/soapService/Exceptions/RequestException.java index ba302a786acf90e6c94b5a1dd5a83bdc839d7402..b15317d597379b5db74b414125b938c28f16fff4 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 adb0c624c7346028e37d970eee2a94cc18565fc7..0000000000000000000000000000000000000000 --- 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 e94f6ddbdea2c8fc4b98507117c55b7ad804eb3c..e5e8c01fba152bf70e5f1a326c6c85f26f4ce101 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 4595c2603acf95e41d760c899f70be4311c93b2d..2e996e42223e7b7ac706fa6b3abe6f7da209e6fa 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 83694c41e1318ede644df56a2035539c3817cea4..2672038520e1732a58c7f39717b4c420adbef170 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 a773bcd42026efe8a745d036ac3e7bb529cbf032..d1ea09063174b64ebba992e9f5bb3973a72c590d 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 945f937095ae79b9ff1cf921a6e7fc8a3e21455e..515a9c386e6bfd8457f2f9ee13c88b0e2803c9d7 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 9cc12c0123c390fd038c64101c0e6d0fbb0e1dea..7549235af5a581393b6b8051446504d27673cc96 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 9fc6f19bac2a56462bc031cc8c890687bf38aae6..cfa33263123fbbd5bec121045bd1c4ffb6a92983 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; } }