diff --git a/schema/schema.sql b/schema/schema.sql index c29f58b458c0cf8faeaa8a5fd9fe2063425d7581..b8c239884d6b42cb0a6b5fc2ba390bd05f1b3c64 100644 --- a/schema/schema.sql +++ b/schema/schema.sql @@ -41,7 +41,7 @@ CREATE TABLE account_verification_requests ( id SERIAL PRIMARY KEY, uuid VARCHAR(36) UNIQUE NOT NULL, - user_id INT UNIQUE NOT NULL, + user_id VARCHAR(36) UNIQUE NOT NULL, status ENUM('PENDING', 'ACCEPTED', 'REJECTED'), created_at TIMESTAMP DEFAULT NOW(), @@ -64,8 +64,8 @@ CREATE TABLE report_users ( uuid VARCHAR(36) UNIQUE NOT NULL, content VARCHAR(255) NOT NULL, - reporter_id INT NOT NULL, - reported_id INT NOT NULL, + reporter_id VARCHAR(36) NOT NULL, + reported_id VARCHAR(36) NOT NULL, created_at TIMESTAMP DEFAULT NOW(), updated_at TIMESTAMP DEFAULT NOW() diff --git a/src/main/java/org/soapService/Common/HTTPStatusCode.java b/src/main/java/org/soapService/Common/HTTPStatusCode.java new file mode 100644 index 0000000000000000000000000000000000000000..1452cc689f8cff36380e03b21b2b98b9f5bacca4 --- /dev/null +++ b/src/main/java/org/soapService/Common/HTTPStatusCode.java @@ -0,0 +1,19 @@ +package org.soapService.Common; + + +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public enum HTTPStatusCode { + OK(200, "200"), + BAD_REQUEST(400, "400"), + UNAUTHORIZED(401, "401"), + FORBIDDEN(403, "403"), + NOT_FOUND(404, "404"), + INTERNAL_SERVER_ERROR(500, "500"); + + private final int codeInt; + private final String codeStr; +} diff --git a/src/main/java/org/soapService/Common/ServiceResponse.java b/src/main/java/org/soapService/Common/ServiceResponse.java new file mode 100644 index 0000000000000000000000000000000000000000..c633a06d7e71b8f9702edd19dabc5b81dcec8856 --- /dev/null +++ b/src/main/java/org/soapService/Common/ServiceResponse.java @@ -0,0 +1,19 @@ +package org.soapService.Common; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +import java.io.Serializable; +import java.util.List; + +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor +public class ServiceResponse<T> implements Serializable { + private String status; + private String message; + private List<T> data; +} diff --git a/src/main/java/org/soapService/Domain/AccountVerificationRequest.java b/src/main/java/org/soapService/Domain/AccountVerificationRequest.java index 10d934946aa076b7ad2c9e2d9a8d63d59ca59c56..398519e31904c869a0ccf7344acd5585bd67f8f2 100644 --- a/src/main/java/org/soapService/Domain/AccountVerificationRequest.java +++ b/src/main/java/org/soapService/Domain/AccountVerificationRequest.java @@ -12,7 +12,7 @@ import lombok.Setter; public class AccountVerificationRequest { private int id; private String uuid; - private int userId; + private String userId; private String status; private String createdAt; private String updatedAt; diff --git a/src/main/java/org/soapService/Domain/ReportUser.java b/src/main/java/org/soapService/Domain/ReportUser.java index fa3d37afe69ca0cc980df428f73ee5e8e3fdfbd2..5939098e9b1aff9a697517a7cdc76ce90f07d713 100644 --- a/src/main/java/org/soapService/Domain/ReportUser.java +++ b/src/main/java/org/soapService/Domain/ReportUser.java @@ -13,8 +13,8 @@ public class ReportUser { private int id; private String uuid; private String content; - private int reporterId; - private int reportedId; + private String reporterId; + private String reportedId; private String createdAt; private String updatedAt; } diff --git a/src/main/java/org/soapService/Exceptions/RequestException.java b/src/main/java/org/soapService/Exceptions/RequestException.java new file mode 100644 index 0000000000000000000000000000000000000000..ba302a786acf90e6c94b5a1dd5a83bdc839d7402 --- /dev/null +++ b/src/main/java/org/soapService/Exceptions/RequestException.java @@ -0,0 +1,28 @@ +package org.soapService.Exceptions; + +import lombok.NoArgsConstructor; + +import javax.xml.ws.WebFault; + +@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); + } + + 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 new file mode 100644 index 0000000000000000000000000000000000000000..adb0c624c7346028e37d970eee2a94cc18565fc7 --- /dev/null +++ b/src/main/java/org/soapService/Exceptions/RequestFault.java @@ -0,0 +1,11 @@ +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/Exceptions/ValidationException.java b/src/main/java/org/soapService/Exceptions/ValidationException.java new file mode 100644 index 0000000000000000000000000000000000000000..70dd8a837aff635b1bbb133f5c24e7b5dce97be2 --- /dev/null +++ b/src/main/java/org/soapService/Exceptions/ValidationException.java @@ -0,0 +1,19 @@ +package org.soapService.Exceptions; + +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Getter +@Setter +@NoArgsConstructor +public class ValidationException extends Exception { + private String status; + private String message; + + public ValidationException(String status, String message) { + super(message); + this.status = status; + this.message = message; + } +} diff --git a/src/main/java/org/soapService/Handlers/ClientHandler.java b/src/main/java/org/soapService/Handlers/ClientHandler.java index 68250a4979e7d6f5643f15ae77f5551ff79911fb..e94f6ddbdea2c8fc4b98507117c55b7ad804eb3c 100644 --- a/src/main/java/org/soapService/Handlers/ClientHandler.java +++ b/src/main/java/org/soapService/Handlers/ClientHandler.java @@ -18,6 +18,7 @@ public class ClientHandler implements SOAPHandler<SOAPMessageContext> { public boolean handleMessage(SOAPMessageContext context) { 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); diff --git a/src/main/java/org/soapService/Handlers/LogHandler.java b/src/main/java/org/soapService/Handlers/LogHandler.java index 87a70d7dbbf7c7ea14a5f0165fcf296bebba5704..4f4e2d3ef615313aa6a5ce693fdc7c5f1a926d2c 100644 --- a/src/main/java/org/soapService/Handlers/LogHandler.java +++ b/src/main/java/org/soapService/Handlers/LogHandler.java @@ -35,7 +35,7 @@ public class LogHandler implements SOAPHandler<SOAPMessageContext> { String endpoint = httpExchange.getRequestURI().toString(); String operation = ""; Map<String, String> args = new HashMap<>(); - + try { SOAPBody soapBody = context.getMessage().getSOAPBody(); diff --git a/src/main/java/org/soapService/Repository/ReportUserRepository.java b/src/main/java/org/soapService/Repository/ReportUserRepository.java index c1180fc2547618e4cc37205f6080103482350f95..1d8bf8c269fabc507f95dcc478f3fff5084064e7 100644 --- a/src/main/java/org/soapService/Repository/ReportUserRepository.java +++ b/src/main/java/org/soapService/Repository/ReportUserRepository.java @@ -1,13 +1,26 @@ package org.soapService.Repository; +import org.soapService.Config.Database; import org.soapService.Domain.ReportUser; +import java.sql.Connection; +import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.List; +import java.util.UUID; public class ReportUserRepository implements BaseRepository<ReportUser> { + private static Connection conn = Database.getConnection(); + public int add(ReportUser data) throws SQLException { - return 0; + String query = "INSERT INTO report_users(content, reporter_id, reported_id, uuid) VALUES (?, ?, ?, ?)"; + PreparedStatement ps = conn.prepareStatement(query); + ps.setString(1, data.getContent()); + ps.setString(2, data.getReporterId()); + ps.setString(3, data.getReportedId()); + ps.setString(4, UUID.randomUUID().toString()); + + return ps.executeUpdate(); } public List<ReportUser> getAll() throws SQLException { diff --git a/src/main/java/org/soapService/Services/AccountVerificationRequestService.java b/src/main/java/org/soapService/Services/AccountVerificationRequestService.java index fc7411af3903ce3e43ddaaceb7d5c733f89015c1..4595c2603acf95e41d760c899f70be4311c93b2d 100644 --- a/src/main/java/org/soapService/Services/AccountVerificationRequestService.java +++ b/src/main/java/org/soapService/Services/AccountVerificationRequestService.java @@ -1,6 +1,7 @@ package org.soapService.Services; import org.soapService.Domain.AccountVerificationRequest; +import org.soapService.Exceptions.RequestException; import javax.jws.HandlerChain; import javax.jws.WebMethod; @@ -14,21 +15,21 @@ import java.util.List; public interface AccountVerificationRequestService { @WebMethod(operationName = "GetRequests") @RequestWrapper(className = "AccountVerificationRequestService.GetRequests") - public List<AccountVerificationRequest> getRequests(); + public List<AccountVerificationRequest> getRequests() throws RequestException; @WebMethod(operationName = "CreateRequest") @RequestWrapper(className = "AccountVerificationRequestService.CreateRequest") - public boolean createRequest(); + public boolean createRequest() throws RequestException; @WebMethod(operationName = "AcceptRequest") @RequestWrapper(className = "AccountVerificationRequestService.AcceptRequest") - public boolean acceptRequest(@WebParam(name = "userId") int userId); + public boolean acceptRequest(@WebParam(name = "userId") int userId) throws RequestException; @WebMethod(operationName = "RejectRequest") @RequestWrapper(className = "AccountVerificationRequestService.RejectRequest") - public boolean rejectRequest(@WebParam(name = "userId") int userId); + public boolean rejectRequest(@WebParam(name = "userId") int userId) throws RequestException; @WebMethod(operationName = "DeleteRequest") @RequestWrapper(className = "AccountVerificationRequestService.DeleteRequest") - public boolean deleteRequest(@WebParam(name = "requestId") int requestId); + public boolean deleteRequest(@WebParam(name = "requestId") int requestId) throws RequestException; } diff --git a/src/main/java/org/soapService/Services/AccountVerificationRequestServiceImpl.java b/src/main/java/org/soapService/Services/AccountVerificationRequestServiceImpl.java index 90ed4f2d3544fdc00e0d20d32c3d32fe301611a5..83694c41e1318ede644df56a2035539c3817cea4 100644 --- a/src/main/java/org/soapService/Services/AccountVerificationRequestServiceImpl.java +++ b/src/main/java/org/soapService/Services/AccountVerificationRequestServiceImpl.java @@ -1,29 +1,30 @@ package org.soapService.Services; import org.soapService.Domain.AccountVerificationRequest; +import org.soapService.Exceptions.RequestException; import javax.jws.WebService; import java.util.List; @WebService(endpointInterface = "org.soapService.Services.AccountVerificationRequestService") public class AccountVerificationRequestServiceImpl extends BaseService implements AccountVerificationRequestService { - public List<AccountVerificationRequest> getRequests() { + public List<AccountVerificationRequest> getRequests() throws RequestException { return null; } - public boolean createRequest() { + public boolean createRequest() throws RequestException { return false; } - public boolean acceptRequest(int userId) { + public boolean acceptRequest(int userId) throws RequestException { return false; } - public boolean rejectRequest(int userId) { + public boolean rejectRequest(int userId) throws RequestException { return false; } - public boolean deleteRequest(int requestId) { + public boolean deleteRequest(int requestId) throws RequestException { return false; } } diff --git a/src/main/java/org/soapService/Services/CatalogRequestService.java b/src/main/java/org/soapService/Services/CatalogRequestService.java index dd584138b77cd8cc6c7239345291345616873aa7..a773bcd42026efe8a745d036ac3e7bb529cbf032 100644 --- a/src/main/java/org/soapService/Services/CatalogRequestService.java +++ b/src/main/java/org/soapService/Services/CatalogRequestService.java @@ -1,6 +1,8 @@ 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; @@ -19,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); + @WebParam(name = "category") String category) throws RequestException; @WebMethod(operationName = "GetRequests") @RequestWrapper(className = "CatalogRequestService.GetRequests") - public List<CatalogRequest> getRequests(); + public List<CatalogRequest> getRequests() throws RequestException; @WebMethod(operationName = "GetRequest") @RequestWrapper(className = "CatalogRequestService.GetRequest") - public CatalogRequest getRequest(@WebParam(name = "requestId") int requestId); + public CatalogRequest getRequest(@WebParam(name = "requestId") int requestId) throws RequestException; @WebMethod(operationName = "AcceptRequest") @RequestWrapper(className = "CatalogRequestService.AcceptRequest") - public boolean acceptRequest(@WebParam(name = "requestId") int requestId); + public boolean acceptRequest(@WebParam(name = "requestId") int requestId) throws RequestException; @WebMethod(operationName = "RejectRequest") @RequestWrapper(className = "CatalogRequestService.RejectRequest") - public boolean rejectRequest(@WebParam(name = "requestId") int requestId); + public boolean rejectRequest(@WebParam(name = "requestId") int requestId) throws RequestException; @WebMethod(operationName = "DeleteRequest") @RequestWrapper(className = "CatalogRequestService.DeleteRequest") - public boolean deleteRequest(@WebParam(name = "requestId") int requestId); + public boolean deleteRequest(@WebParam(name = "requestId") int requestId) throws RequestException; @WebMethod(operationName = "TestUpload") @RequestWrapper(className = "CatalogRequestService.TestUpload") - public String testUpload(@WebParam(name = "data") DataHandler data); + public ServiceResponse testUpload(@WebParam(name = "data") DataHandler data) throws RequestException; } diff --git a/src/main/java/org/soapService/Services/CatalogRequestServiceImpl.java b/src/main/java/org/soapService/Services/CatalogRequestServiceImpl.java index abcdb9e4c4ec17e7f7707db4a7098fca6e2f5362..945f937095ae79b9ff1cf921a6e7fc8a3e21455e 100644 --- a/src/main/java/org/soapService/Services/CatalogRequestServiceImpl.java +++ b/src/main/java/org/soapService/Services/CatalogRequestServiceImpl.java @@ -1,6 +1,9 @@ package org.soapService.Services; +import org.soapService.Common.HTTPStatusCode; +import org.soapService.Common.ServiceResponse; import org.soapService.Domain.CatalogRequest; +import org.soapService.Exceptions.RequestException; import org.soapService.Utils.FileType; import org.soapService.Utils.FileUploader; @@ -10,15 +13,15 @@ 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) { + public boolean createRequest(String title, String description, String poster, String trailer, String category) throws RequestException { return false; } - public List<CatalogRequest> getRequests() { + public List<CatalogRequest> getRequests() throws RequestException { return null; } - public CatalogRequest getRequest(int requestId) { + public CatalogRequest getRequest(int requestId) throws RequestException { // For testing purpose CatalogRequest cr = new CatalogRequest(); @@ -26,31 +29,36 @@ public class CatalogRequestServiceImpl extends BaseService implements CatalogReq return cr; } - public boolean acceptRequest(int requestId) { + public boolean acceptRequest(int requestId) throws RequestException { return false; } - public boolean rejectRequest(int requestId) { + public boolean rejectRequest(int requestId) throws RequestException { return false; } - public boolean deleteRequest(int requestId) { + public boolean deleteRequest(int requestId) throws RequestException { System.out.println(requestId); return false; } - public String testUpload(DataHandler dataHandler) { + public ServiceResponse testUpload(DataHandler dataHandler) throws RequestException { try { boolean valid = FileUploader.validateImage(dataHandler); if (valid) { FileUploader.upload(dataHandler, FileType.IMAGE); } else { - return "Failed"; + throw new RequestException(HTTPStatusCode.BAD_REQUEST.getCodeStr(), "Invalid data"); } - return "Success"; } catch (Exception e) { e.printStackTrace(); - return "Failed"; + throw new RequestException(HTTPStatusCode.INTERNAL_SERVER_ERROR.getCodeStr(), e.getMessage()); } + + ServiceResponse response = new ServiceResponse(); + response.setStatus(HTTPStatusCode.OK.getCodeStr()); + response.setMessage("Success"); + + return response; } } diff --git a/src/main/java/org/soapService/Services/ReportUserService.java b/src/main/java/org/soapService/Services/ReportUserService.java index cc45a43c7b05cecf6ff445e53d368e78dd967184..9cc12c0123c390fd038c64101c0e6d0fbb0e1dea 100644 --- a/src/main/java/org/soapService/Services/ReportUserService.java +++ b/src/main/java/org/soapService/Services/ReportUserService.java @@ -1,31 +1,35 @@ 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; import javax.jws.WebParam; import javax.jws.WebService; +import javax.xml.bind.annotation.XmlSeeAlso; import javax.xml.ws.RequestWrapper; -import java.util.List; @WebService +@XmlSeeAlso({ServiceResponse.class}) @HandlerChain(file = "handler-chain.xml") public interface ReportUserService { @WebMethod(operationName = "CreateReport") @RequestWrapper(className = "ReportUserService.CreateReport") - public void createReport(@WebParam(name = "content") String content, - @WebParam(name = "reportedId") int reportedId); + public ServiceResponse createReport(@WebParam(name = "content") String content, + @WebParam(name = "reportedId") String reportedId, + @WebParam(name = "reporterId") String reporterId) throws RequestException; @WebMethod(operationName = "GetReportedUsers") @RequestWrapper(className = "ReportUserService.GetReportedUsers") - public List<ReportUser> getReportedUsers(); + public ReportUser getReportedUsers() throws RequestException; @WebMethod(operationName = "BlockUser") @RequestWrapper(className = "ReportUserService.BlockUser") - public boolean blockUser(@WebParam(name = "userId") int userId); + public boolean blockUser(@WebParam(name = "userId") int userId) throws RequestException; @WebMethod(operationName = "DeleteReport") @RequestWrapper(className = "ReportUserService.DeleteReport") - public boolean deleteReport(@WebParam(name = "reportId") int reportId); + public boolean deleteReport(@WebParam(name = "reportId") int reportId) throws RequestException; } diff --git a/src/main/java/org/soapService/Services/ReportUserServiceImpl.java b/src/main/java/org/soapService/Services/ReportUserServiceImpl.java index 3683b05d72d2f617bb564f472a8eaeaa01a77bb6..9fc6f19bac2a56462bc031cc8c890687bf38aae6 100644 --- a/src/main/java/org/soapService/Services/ReportUserServiceImpl.java +++ b/src/main/java/org/soapService/Services/ReportUserServiceImpl.java @@ -1,28 +1,57 @@ package org.soapService.Services; +import org.soapService.Common.HTTPStatusCode; +import org.soapService.Common.ServiceResponse; import org.soapService.Domain.ReportUser; +import org.soapService.Exceptions.RequestException; +import org.soapService.Exceptions.ValidationException; +import org.soapService.Repository.ReportUserRepository; +import org.soapService.Validations.ReportUserServiceValidation; import javax.jws.WebService; -import java.util.List; @WebService(endpointInterface = "org.soapService.Services.ReportUserService") public class ReportUserServiceImpl extends BaseService implements ReportUserService { - - public void createReport(String content, int reportedId) { + private static ReportUserRepository reportUserRepository = new ReportUserRepository(); + private static ReportUserServiceValidation reportUserServiceValidation = new ReportUserServiceValidation(); + + public ServiceResponse createReport(String content, String reportedId, String reporterId) throws RequestException { + try { + reportUserServiceValidation.validateCreateReport(content, reportedId, reporterId); + + ReportUser reportUser = new ReportUser(); + reportUser.setContent(content); + reportUser.setReportedId(reportedId); + reportUser.setReporterId(reporterId); + + reportUserRepository.add(reportUser); + } catch (ValidationException e) { + System.out.println(e.getMessage()); + throw new RequestException(e.getStatus(), 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"); + } + + ServiceResponse<ReportUser> response = new ServiceResponse<>(); + response.setStatus(HTTPStatusCode.OK.getCodeStr()); + response.setMessage("Report successfully created"); + + return response; } - public List<ReportUser> getReportedUsers() { + public ReportUser getReportedUsers() throws RequestException { return null; } - public boolean blockUser(int userId) { + public boolean blockUser(int userId) throws RequestException { return false; } - public boolean deleteReport(int reportId) { + public boolean deleteReport(int reportId) throws RequestException { return false; } } diff --git a/src/main/java/org/soapService/Validations/ReportUserServiceValidation.java b/src/main/java/org/soapService/Validations/ReportUserServiceValidation.java new file mode 100644 index 0000000000000000000000000000000000000000..1b95ebc9dc3244821a9ed982087d1ca2f860aa2c --- /dev/null +++ b/src/main/java/org/soapService/Validations/ReportUserServiceValidation.java @@ -0,0 +1,18 @@ +package org.soapService.Validations; + +import org.soapService.Common.HTTPStatusCode; +import org.soapService.Exceptions.ValidationException; + +public class ReportUserServiceValidation { + public void validateCreateReport(String content, String reportedId, String reporterId) throws ValidationException { + if (content == null || content.trim().equals("")) { + throw new ValidationException(HTTPStatusCode.BAD_REQUEST.getCodeStr(), "Content is required"); + } + if (reportedId == null || reportedId.trim().equals("")) { + throw new ValidationException(HTTPStatusCode.BAD_REQUEST.getCodeStr(), "Reported ID is required"); + } + if (reporterId == null || reporterId.trim().equals("")) { + throw new ValidationException(HTTPStatusCode.BAD_REQUEST.getCodeStr(), "Reporter ID is required"); + } + } +}