From 543b1f3eb7e45ee992cd6b1f4c49da72920d0781 Mon Sep 17 00:00:00 2001 From: sozyGithub <thinpotatogithub@gmail.com> Date: Thu, 26 Oct 2023 18:09:55 +0700 Subject: [PATCH] add: logging --- .gitignore | 5 +- .idea/misc.xml | 25 +++-- Dockerfile | 10 ++ pom.xml | 16 ++- schema/schema.sql | 81 +++++++++++++++ .../{soapServiceV2 => soapService}/App.java | 10 +- .../Config/Database.java | 2 +- .../Domain/AccountVerificationRequest.java | 2 +- .../Domain/CatalogRequest.java | 2 +- .../Domain/Log.java | 2 +- .../Domain/ReportUser.java | 2 +- .../org/soapService/Handlers/Handler.java | 99 +++++++++++++++++++ .../AccountVerificationRequestRepository.java | 32 ++++++ .../Repository/BaseRepository.java | 19 ++++ .../Repository/CatalogReqeustRepository.java | 32 ++++++ .../soapService/Repository/LogRepository.java | 44 +++++++++ .../Repository/ReportUserRepository.java | 32 ++++++ .../AccountVerificationRequestService.java | 34 +++++++ ...AccountVerificationRequestServiceImpl.java | 12 +-- .../org/soapService/Services/BaseService.java | 40 ++++++++ .../Services/CatalogRequestService.java | 47 +++++++++ .../Services/CatalogRequestServiceImpl.java | 56 +++++++++++ .../Services/ReportUserService.java | 31 ++++++ .../Services/ReportUserServiceImpl.java | 16 ++- .../java/org/soapService/Utils/FileType.java | 6 ++ .../org/soapService/Utils/FileUploader.java | 65 ++++++++++++ .../AccountVerificationRequestRepository.java | 32 ------ .../Repository/BaseRepository.java | 19 ---- .../Repository/CatalogReqeustRepository.java | 32 ------ .../Repository/LogRepository.java | 33 ------- .../Repository/ReportUserRepository.java | 32 ------ .../AccountVerificationRequestService.java | 25 ----- .../soapServiceV2/Services/BaseService.java | 5 - .../Services/CatalogRequestService.java | 29 ------ .../Services/CatalogRequestServiceImpl.java | 44 --------- .../Services/ReportUserService.java | 22 ----- src/main/resources/handler-chain.xml | 8 ++ src/main/resources/posters/.gitkeep | 0 src/main/resources/trailers/.gitkeep | 0 39 files changed, 683 insertions(+), 320 deletions(-) create mode 100644 Dockerfile create mode 100644 schema/schema.sql rename src/main/java/org/{soapServiceV2 => soapService}/App.java (60%) rename src/main/java/org/{soapServiceV2 => soapService}/Config/Database.java (92%) rename src/main/java/org/{soapServiceV2 => soapService}/Domain/AccountVerificationRequest.java (87%) rename src/main/java/org/{soapServiceV2 => soapService}/Domain/CatalogRequest.java (88%) rename src/main/java/org/{soapServiceV2 => soapService}/Domain/Log.java (86%) rename src/main/java/org/{soapServiceV2 => soapService}/Domain/ReportUser.java (87%) create mode 100644 src/main/java/org/soapService/Handlers/Handler.java create mode 100644 src/main/java/org/soapService/Repository/AccountVerificationRequestRepository.java create mode 100644 src/main/java/org/soapService/Repository/BaseRepository.java create mode 100644 src/main/java/org/soapService/Repository/CatalogReqeustRepository.java create mode 100644 src/main/java/org/soapService/Repository/LogRepository.java create mode 100644 src/main/java/org/soapService/Repository/ReportUserRepository.java create mode 100644 src/main/java/org/soapService/Services/AccountVerificationRequestService.java rename src/main/java/org/{soapServiceV2 => soapService}/Services/AccountVerificationRequestServiceImpl.java (62%) create mode 100644 src/main/java/org/soapService/Services/BaseService.java create mode 100644 src/main/java/org/soapService/Services/CatalogRequestService.java create mode 100644 src/main/java/org/soapService/Services/CatalogRequestServiceImpl.java create mode 100644 src/main/java/org/soapService/Services/ReportUserService.java rename src/main/java/org/{soapServiceV2 => soapService}/Services/ReportUserServiceImpl.java (61%) create mode 100644 src/main/java/org/soapService/Utils/FileType.java create mode 100644 src/main/java/org/soapService/Utils/FileUploader.java delete mode 100644 src/main/java/org/soapServiceV2/Repository/AccountVerificationRequestRepository.java delete mode 100644 src/main/java/org/soapServiceV2/Repository/BaseRepository.java delete mode 100644 src/main/java/org/soapServiceV2/Repository/CatalogReqeustRepository.java delete mode 100644 src/main/java/org/soapServiceV2/Repository/LogRepository.java delete mode 100644 src/main/java/org/soapServiceV2/Repository/ReportUserRepository.java delete mode 100644 src/main/java/org/soapServiceV2/Services/AccountVerificationRequestService.java delete mode 100644 src/main/java/org/soapServiceV2/Services/BaseService.java delete mode 100644 src/main/java/org/soapServiceV2/Services/CatalogRequestService.java delete mode 100644 src/main/java/org/soapServiceV2/Services/CatalogRequestServiceImpl.java delete mode 100644 src/main/java/org/soapServiceV2/Services/ReportUserService.java create mode 100644 src/main/resources/handler-chain.xml create mode 100644 src/main/resources/posters/.gitkeep create mode 100644 src/main/resources/trailers/.gitkeep diff --git a/.gitignore b/.gitignore index 8a9d8ad..d3a936b 100644 --- a/.gitignore +++ b/.gitignore @@ -37,4 +37,7 @@ build/ ### Mac OS ### .DS_Store -.env \ No newline at end of file +.env +db +src/main/resources/posters/poster* +src/main/resources/trailers/trailer* \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 37092cb..2fce26d 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,14 +1,13 @@ -<?xml version="1.0" encoding="UTF-8"?> -<project version="4"> - <component name="ExternalStorageConfigurationManager" enabled="true" /> - <component name="MavenProjectsManager"> - <option name="originalFiles"> - <list> - <option value="$PROJECT_DIR$/pom.xml" /> - </list> - </option> - </component> - <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="corretto-1.8 (WSL)" project-jdk-type="JavaSDK"> - <output url="file://$PROJECT_DIR$/out" /> - </component> +<project version="4"> + <component name="ExternalStorageConfigurationManager" enabled="true" /> + <component name="MavenProjectsManager"> + <option name="originalFiles"> + <list> + <option value="$PROJECT_DIR$/pom.xml" /> + </list> + </option> + </component> + <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="corretto-1.8 (WSL)" project-jdk-type="JavaSDK"> + <output url="file://$PROJECT_DIR$/out" /> + </component> </project> \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..c5e0e5e --- /dev/null +++ b/Dockerfile @@ -0,0 +1,10 @@ +FROM maven:3-amazoncorretto-8 AS build +WORKDIR /app +COPY src /usr/src/app/src +COPY pom.xml /usr/src/app +RUN mvn -f /usr/src/app/pom.xml clean package + +FROM amazoncorretto:8-alpine-jdk +EXPOSE 8082 +COPY --from=build /usr/src/app/target/drawl_soap-jar-with-dependencies.jar /usr/src/app/drawl_soap.jar +CMD ["java","-jar","/usr/src/app/service_soap.jar"] \ No newline at end of file diff --git a/pom.xml b/pom.xml index 1ef90d8..e028101 100644 --- a/pom.xml +++ b/pom.xml @@ -4,9 +4,9 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> - <groupId>org.soapServiceV2</groupId> - <artifactId>soapServiceV2</artifactId> - <version>1.0-SNAPSHOT</version> + <groupId>org.soapService</groupId> + <artifactId>soapService</artifactId> + <version>2.0</version> <properties> <maven.compiler.source>8</maven.compiler.source> @@ -31,6 +31,12 @@ <artifactId>mysql-connector-j</artifactId> <version>8.1.0</version> </dependency> + <!-- https://mvnrepository.com/artifact/org.apache.tika/tika-core --> + <dependency> + <groupId>org.apache.tika</groupId> + <artifactId>tika-core</artifactId> + <version>2.9.1</version> + </dependency> </dependencies> <build> @@ -42,7 +48,7 @@ <configuration> <archive> <manifest> - <mainClass>org.soapServiceV2.App</mainClass> + <mainClass>org.soapService.App</mainClass> </manifest> </archive> <descriptorRefs> @@ -60,6 +66,6 @@ </executions> </plugin> </plugins> - <finalName>service_soap</finalName> + <finalName>drawl_soap</finalName> </build> </project> \ No newline at end of file diff --git a/schema/schema.sql b/schema/schema.sql new file mode 100644 index 0000000..c29f58b --- /dev/null +++ b/schema/schema.sql @@ -0,0 +1,81 @@ +-- LOGS + +CREATE TABLE IF NOT EXISTS logs ( + id SERIAL PRIMARY KEY, + + req_desc VARCHAR(10000) NOT NULL, + ip VARCHAR(50) NOT NULL, + endpoint_dest VARCHAR(255) NOT NULL, + + created_at TIMESTAMP DEFAULT NOW() +); + +-- CATALOG_REQUESTS + +CREATE TABLE IF NOT EXISTS catalog_requests ( + id SERIAL PRIMARY KEY, + uuid VARCHAR(36) UNIQUE NOT NULL, + + title VARCHAR(255) NOT NULL, + description VARCHAR(255), + poster VARCHAR(255) NOT NULL, + trailer VARCHAR(255), + category ENUM('ANIME', 'DRAMA', 'MIXED'), + + created_at TIMESTAMP DEFAULT NOW(), + updated_at TIMESTAMP DEFAULT NOW() +); + +DELIMITER $$ + CREATE TRIGGER IF NOT EXISTS catalog_requests_updated_at + BEFORE UPDATE ON catalog_requests FOR EACH ROW + BEGIN + SET NEW.updated_at = NOW(); + END; +$$ +DELIMITER ; + +-- ACCOUNT_VERIFICATION_REQUEST + +CREATE TABLE account_verification_requests ( + id SERIAL PRIMARY KEY, + uuid VARCHAR(36) UNIQUE NOT NULL, + + user_id INT UNIQUE NOT NULL, + status ENUM('PENDING', 'ACCEPTED', 'REJECTED'), + + created_at TIMESTAMP DEFAULT NOW(), + updated_at TIMESTAMP DEFAULT NOW() +); + +DELIMITER $$ + CREATE TRIGGER IF NOT EXISTS account_verification_requests_updated_at + BEFORE UPDATE ON account_verification_requests FOR EACH ROW + BEGIN + SET NEW.updated_at = NOW(); + END; +$$ +DELIMITER ; + +-- REPORT_USERS + +CREATE TABLE report_users ( + id SERIAL PRIMARY KEY, + uuid VARCHAR(36) UNIQUE NOT NULL, + + content VARCHAR(255) NOT NULL, + reporter_id INT NOT NULL, + reported_id INT NOT NULL, + + created_at TIMESTAMP DEFAULT NOW(), + updated_at TIMESTAMP DEFAULT NOW() +); + +DELIMITER $$ + CREATE TRIGGER IF NOT EXISTS report_users_updated_at + BEFORE UPDATE ON report_users FOR EACH ROW + BEGIN + SET NEW.updated_at = NOW(); + END; +$$ +DELIMITER ; \ No newline at end of file diff --git a/src/main/java/org/soapServiceV2/App.java b/src/main/java/org/soapService/App.java similarity index 60% rename from src/main/java/org/soapServiceV2/App.java rename to src/main/java/org/soapService/App.java index 5b8ef89..ce70d13 100644 --- a/src/main/java/org/soapServiceV2/App.java +++ b/src/main/java/org/soapService/App.java @@ -1,14 +1,14 @@ -package org.soapServiceV2; +package org.soapService; -import org.soapServiceV2.Services.AccountVerificationRequestServiceImpl; -import org.soapServiceV2.Services.CatalogRequestServiceImpl; -import org.soapServiceV2.Services.ReportUserServiceImpl; +import org.soapService.Services.AccountVerificationRequestServiceImpl; +import org.soapService.Services.CatalogRequestServiceImpl; +import org.soapService.Services.ReportUserServiceImpl; import javax.xml.ws.Endpoint; public class App { public static void main(String[] args) { - String baseUrl = "http://0.0.0.0:8082/"; + String baseUrl = "http://0.0.0.0:8083/"; // Register services; Endpoint.publish(baseUrl + "catalog-request", new CatalogRequestServiceImpl()); diff --git a/src/main/java/org/soapServiceV2/Config/Database.java b/src/main/java/org/soapService/Config/Database.java similarity index 92% rename from src/main/java/org/soapServiceV2/Config/Database.java rename to src/main/java/org/soapService/Config/Database.java index 3542f26..6df1297 100644 --- a/src/main/java/org/soapServiceV2/Config/Database.java +++ b/src/main/java/org/soapService/Config/Database.java @@ -1,4 +1,4 @@ -package org.soapServiceV2.Config; +package org.soapService.Config; import io.github.cdimascio.dotenv.Dotenv; diff --git a/src/main/java/org/soapServiceV2/Domain/AccountVerificationRequest.java b/src/main/java/org/soapService/Domain/AccountVerificationRequest.java similarity index 87% rename from src/main/java/org/soapServiceV2/Domain/AccountVerificationRequest.java rename to src/main/java/org/soapService/Domain/AccountVerificationRequest.java index 34be801..10d9349 100644 --- a/src/main/java/org/soapServiceV2/Domain/AccountVerificationRequest.java +++ b/src/main/java/org/soapService/Domain/AccountVerificationRequest.java @@ -1,4 +1,4 @@ -package org.soapServiceV2.Domain; +package org.soapService.Domain; import lombok.AllArgsConstructor; import lombok.Getter; diff --git a/src/main/java/org/soapServiceV2/Domain/CatalogRequest.java b/src/main/java/org/soapService/Domain/CatalogRequest.java similarity index 88% rename from src/main/java/org/soapServiceV2/Domain/CatalogRequest.java rename to src/main/java/org/soapService/Domain/CatalogRequest.java index aefcece..748ca79 100644 --- a/src/main/java/org/soapServiceV2/Domain/CatalogRequest.java +++ b/src/main/java/org/soapService/Domain/CatalogRequest.java @@ -1,4 +1,4 @@ -package org.soapServiceV2.Domain; +package org.soapService.Domain; import lombok.AllArgsConstructor; diff --git a/src/main/java/org/soapServiceV2/Domain/Log.java b/src/main/java/org/soapService/Domain/Log.java similarity index 86% rename from src/main/java/org/soapServiceV2/Domain/Log.java rename to src/main/java/org/soapService/Domain/Log.java index a69c8fd..d01c5f4 100644 --- a/src/main/java/org/soapServiceV2/Domain/Log.java +++ b/src/main/java/org/soapService/Domain/Log.java @@ -1,4 +1,4 @@ -package org.soapServiceV2.Domain; +package org.soapService.Domain; import lombok.AllArgsConstructor; import lombok.Getter; diff --git a/src/main/java/org/soapServiceV2/Domain/ReportUser.java b/src/main/java/org/soapService/Domain/ReportUser.java similarity index 87% rename from src/main/java/org/soapServiceV2/Domain/ReportUser.java rename to src/main/java/org/soapService/Domain/ReportUser.java index f3f5bc3..fa3d37a 100644 --- a/src/main/java/org/soapServiceV2/Domain/ReportUser.java +++ b/src/main/java/org/soapService/Domain/ReportUser.java @@ -1,4 +1,4 @@ -package org.soapServiceV2.Domain; +package org.soapService.Domain; import lombok.AllArgsConstructor; import lombok.Getter; diff --git a/src/main/java/org/soapService/Handlers/Handler.java b/src/main/java/org/soapService/Handlers/Handler.java new file mode 100644 index 0000000..47b2d75 --- /dev/null +++ b/src/main/java/org/soapService/Handlers/Handler.java @@ -0,0 +1,99 @@ +package org.soapService.Handlers; + +import com.sun.net.httpserver.HttpExchange; +import com.sun.xml.internal.ws.developer.JAXWSProperties; +import org.soapService.Domain.Log; +import org.soapService.Repository.LogRepository; +import org.w3c.dom.NodeList; + +import javax.xml.namespace.QName; +import javax.xml.soap.Node; +import javax.xml.soap.SOAPBody; +import javax.xml.ws.handler.MessageContext; +import javax.xml.ws.handler.soap.SOAPHandler; +import javax.xml.ws.handler.soap.SOAPMessageContext; +import java.sql.SQLException; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; + +public class Handler implements SOAPHandler<SOAPMessageContext> { + // public static ThreadLocal<Log> threadLocal = new ThreadLocal<>(); + private static LogRepository logRepository = new LogRepository(); + + public Set<QName> getHeaders() { + return null; + } + + public boolean handleMessage(SOAPMessageContext context) { + Boolean outboundProperty = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY); + HttpExchange httpExchange = (HttpExchange) context.get(JAXWSProperties.HTTP_EXCHANGE); + + if (!outboundProperty) { + String ip = httpExchange.getRemoteAddress().getAddress().toString().replace("/", ""); + String endpoint = httpExchange.getRequestURI().toString(); + String operation = ""; + Map<String, String> args = new HashMap<>(); + + try { + SOAPBody soapBody = context.getMessage().getSOAPBody(); + + // Get operation + Iterator bodyElements = soapBody.getChildElements(); + while (bodyElements.hasNext()) { + Object bodyElement = bodyElements.next(); + if (bodyElement instanceof Node) { + Node node = (Node) bodyElement; + if (node.getLocalName() != null) { + operation = node.getLocalName(); + NodeList nl = node.getChildNodes(); + for (int i = 0; i < nl.getLength(); i++) { + if (nl.item(i).getLocalName() != null) { + args.put(nl.item(i).getLocalName(), nl.item(i).getTextContent().substring(0, Math.min(nl.item(i).getTextContent().length(), 200))); + } + } + } + } + } + } catch (Exception e) { + e.printStackTrace(); + } + + // Format + // [ClientName] -> [arg0]: <arg0_value>; [arg1]: <arg1_value> + String requestDescription = "[ClientName] ->" + (!args.isEmpty() ? " " : " No Args"); + for (Map.Entry<String, String> entry : args.entrySet()) { + requestDescription += "[" + entry.getKey() + "]:"; + requestDescription += entry.getValue() + "; "; + } + + // Write log + Log log = new Log(); + log.setIp(ip); + log.setEndpointDest(endpoint + "." + operation); + log.setReqDesc(requestDescription); + + try { + logRepository.add(log); + } catch (SQLException e) { + e.printStackTrace(); + } + + System.out.println("\nINFORMATION: "); + System.out.println("IP: " + ip); + System.out.println("Endpoint: " + endpoint + "." + operation); + System.out.println("Request Description: " + requestDescription); + } + + return true; + } + + public boolean handleFault(SOAPMessageContext context) { + return true; + } + + public void close(MessageContext context) { + + } +} diff --git a/src/main/java/org/soapService/Repository/AccountVerificationRequestRepository.java b/src/main/java/org/soapService/Repository/AccountVerificationRequestRepository.java new file mode 100644 index 0000000..6bf7e5a --- /dev/null +++ b/src/main/java/org/soapService/Repository/AccountVerificationRequestRepository.java @@ -0,0 +1,32 @@ +package org.soapService.Repository; + +import org.soapService.Domain.AccountVerificationRequest; + +import java.sql.SQLException; +import java.util.List; + +public class AccountVerificationRequestRepository implements BaseRepository<AccountVerificationRequest> { + public int add(AccountVerificationRequest data) throws SQLException { + return 0; + } + + public List<AccountVerificationRequest> getAll() throws SQLException { + return null; + } + + public AccountVerificationRequest getById(int id) throws SQLException { + return null; + } + + public void update(int id) throws SQLException { + + } + + public void deleteAll() throws SQLException { + + } + + public void delete(int id) throws SQLException { + + } +} diff --git a/src/main/java/org/soapService/Repository/BaseRepository.java b/src/main/java/org/soapService/Repository/BaseRepository.java new file mode 100644 index 0000000..ff841a1 --- /dev/null +++ b/src/main/java/org/soapService/Repository/BaseRepository.java @@ -0,0 +1,19 @@ +package org.soapService.Repository; + +import java.sql.SQLException; +import java.util.List; + +public interface BaseRepository<T> { + public int add(T data) throws SQLException; + + public List<T> getAll() throws SQLException; + + public T getById(int id) throws SQLException; + + public void update(int id) throws SQLException; + + public void deleteAll() throws SQLException; + + public void delete(int id) throws SQLException; + +} diff --git a/src/main/java/org/soapService/Repository/CatalogReqeustRepository.java b/src/main/java/org/soapService/Repository/CatalogReqeustRepository.java new file mode 100644 index 0000000..ea45a0a --- /dev/null +++ b/src/main/java/org/soapService/Repository/CatalogReqeustRepository.java @@ -0,0 +1,32 @@ +package org.soapService.Repository; + +import org.soapService.Domain.CatalogRequest; + +import java.sql.SQLException; +import java.util.List; + +public class CatalogReqeustRepository implements BaseRepository<CatalogRequest> { + public int add(CatalogRequest data) throws SQLException { + return 0; + } + + public List<CatalogRequest> getAll() throws SQLException { + return null; + } + + public CatalogRequest getById(int id) throws SQLException { + return null; + } + + public void update(int id) throws SQLException { + + } + + public void deleteAll() throws SQLException { + + } + + public void delete(int id) throws SQLException { + + } +} diff --git a/src/main/java/org/soapService/Repository/LogRepository.java b/src/main/java/org/soapService/Repository/LogRepository.java new file mode 100644 index 0000000..c965622 --- /dev/null +++ b/src/main/java/org/soapService/Repository/LogRepository.java @@ -0,0 +1,44 @@ +package org.soapService.Repository; + +import org.soapService.Config.Database; +import org.soapService.Domain.Log; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.util.List; + +public class LogRepository implements BaseRepository<Log> { + + private static Connection conn = Database.getConnection(); + + public int add(Log data) throws SQLException { + String query = "INSERT INTO logs(req_desc, ip, endpoint_dest) VALUES (?, ?, ?)"; + PreparedStatement ps = conn.prepareStatement(query); + ps.setString(1, data.getReqDesc()); + ps.setString(2, data.getIp()); + ps.setString(3, data.getEndpointDest()); + + return ps.executeUpdate(); + } + + public List<Log> getAll() throws SQLException { + return null; + } + + public Log getById(int id) throws SQLException { + return null; + } + + public void update(int id) throws SQLException { + + } + + public void deleteAll() throws SQLException { + + } + + public void delete(int id) throws SQLException { + + } +} diff --git a/src/main/java/org/soapService/Repository/ReportUserRepository.java b/src/main/java/org/soapService/Repository/ReportUserRepository.java new file mode 100644 index 0000000..c1180fc --- /dev/null +++ b/src/main/java/org/soapService/Repository/ReportUserRepository.java @@ -0,0 +1,32 @@ +package org.soapService.Repository; + +import org.soapService.Domain.ReportUser; + +import java.sql.SQLException; +import java.util.List; + +public class ReportUserRepository implements BaseRepository<ReportUser> { + public int add(ReportUser data) throws SQLException { + return 0; + } + + public List<ReportUser> getAll() throws SQLException { + return null; + } + + public ReportUser getById(int id) throws SQLException { + return null; + } + + public void update(int id) throws SQLException { + + } + + public void deleteAll() throws SQLException { + + } + + public void delete(int id) throws SQLException { + + } +} diff --git a/src/main/java/org/soapService/Services/AccountVerificationRequestService.java b/src/main/java/org/soapService/Services/AccountVerificationRequestService.java new file mode 100644 index 0000000..fc7411a --- /dev/null +++ b/src/main/java/org/soapService/Services/AccountVerificationRequestService.java @@ -0,0 +1,34 @@ +package org.soapService.Services; + +import org.soapService.Domain.AccountVerificationRequest; + +import javax.jws.HandlerChain; +import javax.jws.WebMethod; +import javax.jws.WebParam; +import javax.jws.WebService; +import javax.xml.ws.RequestWrapper; +import java.util.List; + +@WebService +@HandlerChain(file = "handler-chain.xml") +public interface AccountVerificationRequestService { + @WebMethod(operationName = "GetRequests") + @RequestWrapper(className = "AccountVerificationRequestService.GetRequests") + public List<AccountVerificationRequest> getRequests(); + + @WebMethod(operationName = "CreateRequest") + @RequestWrapper(className = "AccountVerificationRequestService.CreateRequest") + public boolean createRequest(); + + @WebMethod(operationName = "AcceptRequest") + @RequestWrapper(className = "AccountVerificationRequestService.AcceptRequest") + public boolean acceptRequest(@WebParam(name = "userId") int userId); + + @WebMethod(operationName = "RejectRequest") + @RequestWrapper(className = "AccountVerificationRequestService.RejectRequest") + public boolean rejectRequest(@WebParam(name = "userId") int userId); + + @WebMethod(operationName = "DeleteRequest") + @RequestWrapper(className = "AccountVerificationRequestService.DeleteRequest") + public boolean deleteRequest(@WebParam(name = "requestId") int requestId); +} diff --git a/src/main/java/org/soapServiceV2/Services/AccountVerificationRequestServiceImpl.java b/src/main/java/org/soapService/Services/AccountVerificationRequestServiceImpl.java similarity index 62% rename from src/main/java/org/soapServiceV2/Services/AccountVerificationRequestServiceImpl.java rename to src/main/java/org/soapService/Services/AccountVerificationRequestServiceImpl.java index de30348..90ed4f2 100644 --- a/src/main/java/org/soapServiceV2/Services/AccountVerificationRequestServiceImpl.java +++ b/src/main/java/org/soapService/Services/AccountVerificationRequestServiceImpl.java @@ -1,34 +1,28 @@ -package org.soapServiceV2.Services; +package org.soapService.Services; -import org.soapServiceV2.Domain.AccountVerificationRequest; +import org.soapService.Domain.AccountVerificationRequest; -import javax.jws.WebMethod; import javax.jws.WebService; import java.util.List; -@WebService(endpointInterface = "org.soapServiceV2.Services.AccountVerificationRequestService") +@WebService(endpointInterface = "org.soapService.Services.AccountVerificationRequestService") public class AccountVerificationRequestServiceImpl extends BaseService implements AccountVerificationRequestService { - @WebMethod public List<AccountVerificationRequest> getRequests() { return null; } - @WebMethod public boolean createRequest() { return false; } - @WebMethod public boolean acceptRequest(int userId) { return false; } - @WebMethod public boolean rejectRequest(int userId) { return false; } - @WebMethod public boolean deleteRequest(int requestId) { return false; } diff --git a/src/main/java/org/soapService/Services/BaseService.java b/src/main/java/org/soapService/Services/BaseService.java new file mode 100644 index 0000000..b65fb3e --- /dev/null +++ b/src/main/java/org/soapService/Services/BaseService.java @@ -0,0 +1,40 @@ +package org.soapService.Services; + + +import com.sun.net.httpserver.HttpExchange; +import com.sun.xml.internal.ws.developer.JAXWSProperties; +import org.soapService.Domain.Log; +import org.soapService.Repository.LogRepository; + +import javax.annotation.Resource; +import javax.xml.ws.WebServiceContext; +import javax.xml.ws.handler.MessageContext; +import java.sql.SQLException; + +public abstract class BaseService { + @Resource + WebServiceContext wsContext; + + protected void addLog() { + MessageContext messageContext = wsContext.getMessageContext(); + HttpExchange exchange = (HttpExchange) messageContext.get(JAXWSProperties.HTTP_EXCHANGE); + + Log log = new Log(); + log.setIp(exchange.getRemoteAddress().getAddress().toString().replace("/", "")); + log.setReqDesc("desc"); + log.setEndpointDest("/"); + + LogRepository logRepository = new LogRepository(); + + + try { + logRepository.add(log); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + protected void printContext() { +// System.out.println("IP From WebService: " + (String) Handler1.threadLocal.get()); + } +} diff --git a/src/main/java/org/soapService/Services/CatalogRequestService.java b/src/main/java/org/soapService/Services/CatalogRequestService.java new file mode 100644 index 0000000..dd58413 --- /dev/null +++ b/src/main/java/org/soapService/Services/CatalogRequestService.java @@ -0,0 +1,47 @@ +package org.soapService.Services; + +import org.soapService.Domain.CatalogRequest; + +import javax.activation.DataHandler; +import javax.jws.HandlerChain; +import javax.jws.WebMethod; +import javax.jws.WebParam; +import javax.jws.WebService; +import javax.xml.ws.RequestWrapper; +import java.util.List; + +@WebService +@HandlerChain(file = "handler-chain.xml") +public interface CatalogRequestService { + @WebMethod(operationName = "CreateRequest") + @RequestWrapper(className = "CatalogRequestService.CreateRequest") + public boolean createRequest(@WebParam(name = "title") String title, + @WebParam(name = "description") String description, + @WebParam(name = "poster") String poster, + @WebParam(name = "trailer") String trailer, + @WebParam(name = "category") String category); + + @WebMethod(operationName = "GetRequests") + @RequestWrapper(className = "CatalogRequestService.GetRequests") + public List<CatalogRequest> getRequests(); + + @WebMethod(operationName = "GetRequest") + @RequestWrapper(className = "CatalogRequestService.GetRequest") + public CatalogRequest getRequest(@WebParam(name = "requestId") int requestId); + + @WebMethod(operationName = "AcceptRequest") + @RequestWrapper(className = "CatalogRequestService.AcceptRequest") + public boolean acceptRequest(@WebParam(name = "requestId") int requestId); + + @WebMethod(operationName = "RejectRequest") + @RequestWrapper(className = "CatalogRequestService.RejectRequest") + public boolean rejectRequest(@WebParam(name = "requestId") int requestId); + + @WebMethod(operationName = "DeleteRequest") + @RequestWrapper(className = "CatalogRequestService.DeleteRequest") + public boolean deleteRequest(@WebParam(name = "requestId") int requestId); + + @WebMethod(operationName = "TestUpload") + @RequestWrapper(className = "CatalogRequestService.TestUpload") + public String testUpload(@WebParam(name = "data") DataHandler data); +} diff --git a/src/main/java/org/soapService/Services/CatalogRequestServiceImpl.java b/src/main/java/org/soapService/Services/CatalogRequestServiceImpl.java new file mode 100644 index 0000000..abcdb9e --- /dev/null +++ b/src/main/java/org/soapService/Services/CatalogRequestServiceImpl.java @@ -0,0 +1,56 @@ +package org.soapService.Services; + +import org.soapService.Domain.CatalogRequest; +import org.soapService.Utils.FileType; +import org.soapService.Utils.FileUploader; + +import javax.activation.DataHandler; +import javax.jws.WebService; +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) { + return false; + } + + public List<CatalogRequest> getRequests() { + return null; + } + + public CatalogRequest getRequest(int requestId) { + // For testing purpose + + CatalogRequest cr = new CatalogRequest(); + cr.setId(requestId); + return cr; + } + + public boolean acceptRequest(int requestId) { + return false; + } + + public boolean rejectRequest(int requestId) { + return false; + } + + public boolean deleteRequest(int requestId) { + System.out.println(requestId); + return false; + } + + public String testUpload(DataHandler dataHandler) { + try { + boolean valid = FileUploader.validateImage(dataHandler); + if (valid) { + FileUploader.upload(dataHandler, FileType.IMAGE); + } else { + return "Failed"; + } + return "Success"; + } catch (Exception e) { + e.printStackTrace(); + return "Failed"; + } + } +} diff --git a/src/main/java/org/soapService/Services/ReportUserService.java b/src/main/java/org/soapService/Services/ReportUserService.java new file mode 100644 index 0000000..cc45a43 --- /dev/null +++ b/src/main/java/org/soapService/Services/ReportUserService.java @@ -0,0 +1,31 @@ +package org.soapService.Services; + +import org.soapService.Domain.ReportUser; + +import javax.jws.HandlerChain; +import javax.jws.WebMethod; +import javax.jws.WebParam; +import javax.jws.WebService; +import javax.xml.ws.RequestWrapper; +import java.util.List; + +@WebService +@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); + + @WebMethod(operationName = "GetReportedUsers") + @RequestWrapper(className = "ReportUserService.GetReportedUsers") + public List<ReportUser> getReportedUsers(); + + @WebMethod(operationName = "BlockUser") + @RequestWrapper(className = "ReportUserService.BlockUser") + public boolean blockUser(@WebParam(name = "userId") int userId); + + @WebMethod(operationName = "DeleteReport") + @RequestWrapper(className = "ReportUserService.DeleteReport") + public boolean deleteReport(@WebParam(name = "reportId") int reportId); +} diff --git a/src/main/java/org/soapServiceV2/Services/ReportUserServiceImpl.java b/src/main/java/org/soapService/Services/ReportUserServiceImpl.java similarity index 61% rename from src/main/java/org/soapServiceV2/Services/ReportUserServiceImpl.java rename to src/main/java/org/soapService/Services/ReportUserServiceImpl.java index 1ad9aff..3683b05 100644 --- a/src/main/java/org/soapServiceV2/Services/ReportUserServiceImpl.java +++ b/src/main/java/org/soapService/Services/ReportUserServiceImpl.java @@ -1,29 +1,27 @@ -package org.soapServiceV2.Services; +package org.soapService.Services; -import org.soapServiceV2.Domain.ReportUser; +import org.soapService.Domain.ReportUser; -import javax.jws.WebMethod; import javax.jws.WebService; import java.util.List; -@WebService(endpointInterface = "org.soapServiceV2.Services.ReportUserService") +@WebService(endpointInterface = "org.soapService.Services.ReportUserService") public class ReportUserServiceImpl extends BaseService implements ReportUserService { - @WebMethod - public void createReport(String content, int reportedId) { + public void createReport(String content, int reportedId) { } - @WebMethod + public List<ReportUser> getReportedUsers() { return null; } - @WebMethod + public boolean blockUser(int userId) { return false; } - @WebMethod + public boolean deleteReport(int reportId) { return false; } diff --git a/src/main/java/org/soapService/Utils/FileType.java b/src/main/java/org/soapService/Utils/FileType.java new file mode 100644 index 0000000..f300243 --- /dev/null +++ b/src/main/java/org/soapService/Utils/FileType.java @@ -0,0 +1,6 @@ +package org.soapService.Utils; + +public enum FileType { + IMAGE, + VIDEO +} diff --git a/src/main/java/org/soapService/Utils/FileUploader.java b/src/main/java/org/soapService/Utils/FileUploader.java new file mode 100644 index 0000000..99edd2f --- /dev/null +++ b/src/main/java/org/soapService/Utils/FileUploader.java @@ -0,0 +1,65 @@ +package org.soapService.Utils; + +import org.apache.commons.io.IOUtils; +import org.apache.tika.Tika; + +import javax.activation.DataHandler; +import java.io.File; +import java.io.IOException; +import java.nio.file.StandardCopyOption; +import java.util.UUID; + +public class FileUploader { + private static final int MAX_IMAGE_SIZE = 200_000; + private static final int MAX_VIDEO_SIZE = 100_000_000; + + public static boolean validateImage(DataHandler dataHandler) { + try { + Tika tika = new Tika(); + String mimeTypes = tika.detect(dataHandler.getInputStream()); + int size = IOUtils.toByteArray(dataHandler.getInputStream()).length; + + System.out.println("MIME Types: " + mimeTypes); + System.out.println("Size Image: " + size); + + if (!mimeTypes.equals("image/webp") || size > MAX_IMAGE_SIZE) { + return false; + } + + return true; + } catch (Exception e) { + return false; + } + } + + public static boolean validateVideo(DataHandler dataHandler) { + try { + Tika tika = new Tika(); + String mimeTypes = tika.detect(dataHandler.getInputStream()); + int size = IOUtils.toByteArray(dataHandler.getInputStream()).length; + + System.out.println("MIME Types: " + mimeTypes); + System.out.println("Size Image: " + size); + + if (!mimeTypes.contains("video") || size > MAX_VIDEO_SIZE) { + return false; + } + + return true; + } catch (Exception e) { + return false; + } + } + + public static void upload(DataHandler dataHandler, FileType fileType) throws IOException { + File file = null; + if (fileType == FileType.IMAGE) { + file = new File("src/main/resources/posters/poster_" + UUID.randomUUID() + ".webp"); + } else { + file = new File("src/main/resources/trailers/trailer_" + UUID.randomUUID() + ".mp4"); + } + + java.nio.file.Files.copy(dataHandler.getInputStream(), file.toPath(), StandardCopyOption.REPLACE_EXISTING); + IOUtils.closeQuietly(dataHandler.getInputStream()); + } +} diff --git a/src/main/java/org/soapServiceV2/Repository/AccountVerificationRequestRepository.java b/src/main/java/org/soapServiceV2/Repository/AccountVerificationRequestRepository.java deleted file mode 100644 index d467ea3..0000000 --- a/src/main/java/org/soapServiceV2/Repository/AccountVerificationRequestRepository.java +++ /dev/null @@ -1,32 +0,0 @@ -package org.soapServiceV2.Repository; - -import org.soapServiceV2.Domain.AccountVerificationRequest; - -import java.sql.SQLException; -import java.util.List; - -public class AccountVerificationRequestRepository implements BaseRepository<AccountVerificationRequest> { - public boolean add(AccountVerificationRequest data) throws SQLException { - return false; - } - - public List<AccountVerificationRequest> getAll() throws SQLException { - return null; - } - - public AccountVerificationRequest getById(int id) throws SQLException { - return null; - } - - public boolean update(int id) throws SQLException { - return false; - } - - public boolean deleteAll() throws SQLException { - return false; - } - - public boolean delete(int id) throws SQLException { - return false; - } -} diff --git a/src/main/java/org/soapServiceV2/Repository/BaseRepository.java b/src/main/java/org/soapServiceV2/Repository/BaseRepository.java deleted file mode 100644 index df53ecf..0000000 --- a/src/main/java/org/soapServiceV2/Repository/BaseRepository.java +++ /dev/null @@ -1,19 +0,0 @@ -package org.soapServiceV2.Repository; - -import java.sql.SQLException; -import java.util.List; - -public interface BaseRepository<T> { - public boolean add(T data) throws SQLException; - - public List<T> getAll() throws SQLException; - - public T getById(int id) throws SQLException; - - public boolean update(int id) throws SQLException; - - public boolean deleteAll() throws SQLException; - - public boolean delete(int id) throws SQLException; - -} diff --git a/src/main/java/org/soapServiceV2/Repository/CatalogReqeustRepository.java b/src/main/java/org/soapServiceV2/Repository/CatalogReqeustRepository.java deleted file mode 100644 index fb29840..0000000 --- a/src/main/java/org/soapServiceV2/Repository/CatalogReqeustRepository.java +++ /dev/null @@ -1,32 +0,0 @@ -package org.soapServiceV2.Repository; - -import org.soapServiceV2.Domain.CatalogRequest; - -import java.sql.SQLException; -import java.util.List; - -public class CatalogReqeustRepository implements BaseRepository<CatalogRequest> { - public boolean add(CatalogRequest data) throws SQLException { - return false; - } - - public List<CatalogRequest> getAll() throws SQLException { - return null; - } - - public CatalogRequest getById(int id) throws SQLException { - return null; - } - - public boolean update(int id) throws SQLException { - return false; - } - - public boolean deleteAll() throws SQLException { - return false; - } - - public boolean delete(int id) throws SQLException { - return false; - } -} diff --git a/src/main/java/org/soapServiceV2/Repository/LogRepository.java b/src/main/java/org/soapServiceV2/Repository/LogRepository.java deleted file mode 100644 index 7abfdf9..0000000 --- a/src/main/java/org/soapServiceV2/Repository/LogRepository.java +++ /dev/null @@ -1,33 +0,0 @@ -package org.soapServiceV2.Repository; - -import org.soapServiceV2.Domain.Log; - -import java.sql.SQLException; -import java.util.List; - -public class LogRepository implements BaseRepository<Log> { - - public boolean add(Log data) throws SQLException { - return false; - } - - public List<Log> getAll() throws SQLException { - return null; - } - - public Log getById(int id) throws SQLException { - return null; - } - - public boolean update(int id) throws SQLException { - return false; - } - - public boolean deleteAll() throws SQLException { - return false; - } - - public boolean delete(int id) throws SQLException { - return false; - } -} diff --git a/src/main/java/org/soapServiceV2/Repository/ReportUserRepository.java b/src/main/java/org/soapServiceV2/Repository/ReportUserRepository.java deleted file mode 100644 index c94b003..0000000 --- a/src/main/java/org/soapServiceV2/Repository/ReportUserRepository.java +++ /dev/null @@ -1,32 +0,0 @@ -package org.soapServiceV2.Repository; - -import org.soapServiceV2.Domain.ReportUser; - -import java.sql.SQLException; -import java.util.List; - -public class ReportUserRepository implements BaseRepository<ReportUser> { - public boolean add(ReportUser data) throws SQLException { - return false; - } - - public List<ReportUser> getAll() throws SQLException { - return null; - } - - public ReportUser getById(int id) throws SQLException { - return null; - } - - public boolean update(int id) throws SQLException { - return false; - } - - public boolean deleteAll() throws SQLException { - return false; - } - - public boolean delete(int id) throws SQLException { - return false; - } -} diff --git a/src/main/java/org/soapServiceV2/Services/AccountVerificationRequestService.java b/src/main/java/org/soapServiceV2/Services/AccountVerificationRequestService.java deleted file mode 100644 index 8511981..0000000 --- a/src/main/java/org/soapServiceV2/Services/AccountVerificationRequestService.java +++ /dev/null @@ -1,25 +0,0 @@ -package org.soapServiceV2.Services; - -import org.soapServiceV2.Domain.AccountVerificationRequest; - -import javax.jws.WebMethod; -import javax.jws.WebService; -import java.util.List; - -@WebService -public interface AccountVerificationRequestService { - @WebMethod - public List<AccountVerificationRequest> getRequests(); - - @WebMethod - public boolean createRequest(); - - @WebMethod - public boolean acceptRequest(int userId); - - @WebMethod - public boolean rejectRequest(int userId); - - @WebMethod - public boolean deleteRequest(int requestId); -} diff --git a/src/main/java/org/soapServiceV2/Services/BaseService.java b/src/main/java/org/soapServiceV2/Services/BaseService.java deleted file mode 100644 index 406e301..0000000 --- a/src/main/java/org/soapServiceV2/Services/BaseService.java +++ /dev/null @@ -1,5 +0,0 @@ -package org.soapServiceV2.Services; - -public abstract class BaseService { - // TODO: Implements Logging -} diff --git a/src/main/java/org/soapServiceV2/Services/CatalogRequestService.java b/src/main/java/org/soapServiceV2/Services/CatalogRequestService.java deleted file mode 100644 index 2b7e8fc..0000000 --- a/src/main/java/org/soapServiceV2/Services/CatalogRequestService.java +++ /dev/null @@ -1,29 +0,0 @@ -package org.soapServiceV2.Services; - -import org.soapServiceV2.Domain.CatalogRequest; - -import javax.jws.WebMethod; -import javax.jws.WebService; -import java.util.List; - -@WebService -public interface CatalogRequestService { - - @WebMethod - public boolean createRequest(String title, String description, String poster, String trailer, String category); - - @WebMethod - public List<CatalogRequest> getRequests(); - - @WebMethod - public CatalogRequest getRequest(int requestId); - - @WebMethod - public boolean acceptRequest(int requestId); - - @WebMethod - public boolean rejectRequest(int requestId); - - @WebMethod - public boolean deleteRequest(int requestId); -} diff --git a/src/main/java/org/soapServiceV2/Services/CatalogRequestServiceImpl.java b/src/main/java/org/soapServiceV2/Services/CatalogRequestServiceImpl.java deleted file mode 100644 index d0d0c67..0000000 --- a/src/main/java/org/soapServiceV2/Services/CatalogRequestServiceImpl.java +++ /dev/null @@ -1,44 +0,0 @@ -package org.soapServiceV2.Services; - -import org.soapServiceV2.Domain.CatalogRequest; - -import javax.jws.WebMethod; -import javax.jws.WebService; -import java.util.List; - -@WebService(endpointInterface = "org.soapServiceV2.Services.CatalogRequestService") -public class CatalogRequestServiceImpl extends BaseService implements CatalogRequestService { - - @WebMethod - public boolean createRequest(String title, String description, String poster, String trailer, String category) { - return false; - } - - @WebMethod - public List<CatalogRequest> getRequests() { - return null; - } - - @WebMethod - public CatalogRequest getRequest(int requestId) { - // For testing purpose - CatalogRequest cr = new CatalogRequest(); - cr.setId(requestId); - return cr; - } - - @WebMethod - public boolean acceptRequest(int requestId) { - return false; - } - - @WebMethod - public boolean rejectRequest(int requestId) { - return false; - } - - @WebMethod - public boolean deleteRequest(int requestId) { - return false; - } -} diff --git a/src/main/java/org/soapServiceV2/Services/ReportUserService.java b/src/main/java/org/soapServiceV2/Services/ReportUserService.java deleted file mode 100644 index 559bf42..0000000 --- a/src/main/java/org/soapServiceV2/Services/ReportUserService.java +++ /dev/null @@ -1,22 +0,0 @@ -package org.soapServiceV2.Services; - -import org.soapServiceV2.Domain.ReportUser; - -import javax.jws.WebMethod; -import javax.jws.WebService; -import java.util.List; - -@WebService -public interface ReportUserService { - @WebMethod - public void createReport(String content, int reportedId); - - @WebMethod - public List<ReportUser> getReportedUsers(); - - @WebMethod - public boolean blockUser(int userId); - - @WebMethod - public boolean deleteReport(int reportId); -} diff --git a/src/main/resources/handler-chain.xml b/src/main/resources/handler-chain.xml new file mode 100644 index 0000000..5c9fd12 --- /dev/null +++ b/src/main/resources/handler-chain.xml @@ -0,0 +1,8 @@ +<jws:handler-chains xmlns:jws="http://java.sun.com/xml/ns/javaee"> + <jws:handler-chain> + <jws:handler> + <jws:handler-name>Handler</jws:handler-name> + <jws:handler-class>org.soapService.Handlers.Handler</jws:handler-class> + </jws:handler> + </jws:handler-chain> +</jws:handler-chains> \ No newline at end of file diff --git a/src/main/resources/posters/.gitkeep b/src/main/resources/posters/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/src/main/resources/trailers/.gitkeep b/src/main/resources/trailers/.gitkeep new file mode 100644 index 0000000..e69de29 -- GitLab