diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000000000000000000000000000000000000..35eb1ddfbbc029bcab630581847471d7f238ec53 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="VcsDirectoryMappings"> + <mapping directory="" vcs="Git" /> + </component> +</project> \ No newline at end of file diff --git a/pom.xml b/pom.xml index 91cf1d8b8b10fe6cbe59582cb4cbfbf2303b2a06..f15f84b0f034041f64bfe6582c44133ed43368b5 100644 --- a/pom.xml +++ b/pom.xml @@ -14,4 +14,23 @@ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> + + + <dependencies> + + <dependency> + <groupId>com.sun.net.httpserver</groupId> + <artifactId>http</artifactId> + <version>20070405</version> + </dependency> + + + <!-- https://mvnrepository.com/artifact/javax.xml.ws/jaxws-api --> + <dependency> + <groupId>javax.xml.ws</groupId> + <artifactId>jaxws-api</artifactId> + <version>2.2</version> + </dependency> + + </dependencies> </project> \ No newline at end of file diff --git a/src/main/java/org/example/Main.java b/src/main/java/org/example/Main.java deleted file mode 100644 index 3a80e56b668f8448993eb42f3302a1bb5f745609..0000000000000000000000000000000000000000 --- a/src/main/java/org/example/Main.java +++ /dev/null @@ -1,14 +0,0 @@ -package org.example; - -import javax.xml.ws.Endpoint; - -public class Main { - public static void main(String[] args) { - try{ - Endpoint.publish("http://localhost:8080/ws/test", new User()); - System.out.println("Server started"); - } catch (Exception e){ - e.printStackTrace(); - } - } -} \ No newline at end of file diff --git a/src/main/java/org/example/User.java b/src/main/java/org/example/User.java deleted file mode 100644 index 0ee6fbe57b93d7a033075210ec82e24fe3b41976..0000000000000000000000000000000000000000 --- a/src/main/java/org/example/User.java +++ /dev/null @@ -1,12 +0,0 @@ -package org.example; - -import javax.jws.WebMethod; -import javax.jws.WebService; - -@WebService -public class User { - @WebMethod - public String HelloWorld(String name){ - return "Hello " + name; - } -} diff --git a/src/main/java/org/saranghaengbok/App.java b/src/main/java/org/saranghaengbok/App.java new file mode 100644 index 0000000000000000000000000000000000000000..ad5880395d9242e1d1b1d72d2bc44b8be5d0b41e --- /dev/null +++ b/src/main/java/org/saranghaengbok/App.java @@ -0,0 +1,16 @@ +package org.saranghaengbok; + +import org.saranghaengbok.service.TransactionImpl; + +import javax.xml.ws.Endpoint; + +public class App { + public static void main(String[] args) { + try{ + System.out.println("Server: http://localhost:8080/ws/transaction"); + Endpoint.publish("http://localhost:8080/ws/transaction", new TransactionImpl()); + } catch (Exception e){ + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/src/main/java/org/saranghaengbok/core/DatabaseConnection.java b/src/main/java/org/saranghaengbok/core/DatabaseConnection.java new file mode 100644 index 0000000000000000000000000000000000000000..1b3769fda03140a7aca4138236f74aee6312b168 --- /dev/null +++ b/src/main/java/org/saranghaengbok/core/DatabaseConnection.java @@ -0,0 +1,26 @@ +package org.saranghaengbok.core; + +import java.sql.*; + +public class DatabaseConnection { + protected Connection conn; + private String db_url; + private String db_username; + private String db_password; + + public DatabaseConnection(){ + db_url = "jdbc:mysql://localhost:3306/saranghaengbok_soap"; + db_username = "root"; + db_password = "mysql"; + try{ + Class.forName("com.mysql.cj.jdbc.Driver"); + this.conn = DriverManager.getConnection(db_url, db_username, db_password); + } catch (Exception e){ + System.err.println("Error connecting to db"); + } + } + + public Connection getConnection(){ + return this.conn; + } +} diff --git a/src/main/java/org/saranghaengbok/models/Log.java b/src/main/java/org/saranghaengbok/models/Log.java new file mode 100644 index 0000000000000000000000000000000000000000..4c3bceba60f9f0dd095e8fab7bf8766436bacab6 --- /dev/null +++ b/src/main/java/org/saranghaengbok/models/Log.java @@ -0,0 +1,42 @@ +package org.saranghaengbok.models; + +import java.sql.Statement; + +import javax.xml.ws.WebServiceContext; +import javax.xml.ws.handler.MessageContext; +import javax.xml.ws.spi.http.HttpExchange; + +import org.saranghaengbok.core.DatabaseConnection; + +public class Log extends DatabaseConnection { + public Log(){ + super(); + } + + public String logging(String ip, String endpoint, String desc){ + try { + Statement statement = this.conn.createStatement(); + String query = "INSERT INTO log (ip, endpoint, desc) VALUES ('" + ip + "', '" + endpoint + "', '" + desc + "')"; + statement.executeUpdate(query); + + statement.close(); + conn.close(); + } catch (Exception e) { + return "Failed to create log"; + } + + + return "Successfully create log"; + } + + + public static void logger(String desc, String apiKey, WebServiceContext context){ + MessageContext messageContext = context.getMessageContext(); + HttpExchange exchange = (HttpExchange) messageContext.get("com.sun.xml.ws.http.exchange"); + String ip = exchange.getRemoteAddress().getAddress().getHostAddress(); + String endpoint = exchange.getRequestURI().toString(); + Log log = new Log(); + String description = apiKey + ": " + desc; + log.logging(ip, endpoint, description); + } +} diff --git a/src/main/java/org/saranghaengbok/models/soap_service.sql b/src/main/java/org/saranghaengbok/models/soap_service.sql new file mode 100644 index 0000000000000000000000000000000000000000..d8d7c81beb33b38b257280711bb88e50f4c0c58a --- /dev/null +++ b/src/main/java/org/saranghaengbok/models/soap_service.sql @@ -0,0 +1,32 @@ +CREATE SCHEMA IF NOT EXISTS `saranghaengbok_soap` DEFAULT CHARACTER SET utf8 ; +USE saranghaengbok_soap; + +CREATE TABLE IF NOT EXISTS `saranghaengbok_soap`.`log`( + `id` INT NOT NULL AUTO_INCREMENT, + `ip` VARCHAR(50) NOT NULL, + `endpoint` VARCHAR(255) NOT NULL, + `description` TEXT, + `timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; + +CREATE TABLE IF NOT EXISTS `saranghaengbok_soap`.`transaction`( + `transaction_id` INT NOT NULL, + `buyer_username` VARCHAR(45) NOT NULL, + `seller_username` VARCHAR(45) NOT NULL, + PRIMARY KEY (`transaction_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; + +CREATE TABLE IF NOT EXISTS `saranghaengbok_soap`.`transaction_items`( + `transaction_id` INT NOT NULL, + `item_id` INT NOT NULL, + `quantity` INT NOT NULL, + CONSTRAINT `fk_transaction` + FOREIGN KEY (`transaction_id`) + REFERENCES `saranghaengbok_soap`.`transaction` (`transaction_id`) + ON UPDATE CASCADE, + CONSTRAINT `fk_item` + FOREIGN KEY (`item_id`) + REFERENCES `saranghaengbok_db`.`item` (`item_id`) + ON UPDATE CASCADE +)ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; diff --git a/src/main/java/org/saranghaengbok/service/Transaction.java b/src/main/java/org/saranghaengbok/service/Transaction.java new file mode 100644 index 0000000000000000000000000000000000000000..b3e10a8d8ff301e36f38467c54cad3f6e99ad2ad --- /dev/null +++ b/src/main/java/org/saranghaengbok/service/Transaction.java @@ -0,0 +1,42 @@ +package org.saranghaengbok.service; + +import java.util.List; + +import javax.jws.WebMethod; +import javax.jws.WebService; +import javax.jws.WebParam; +import javax.jws.soap.SOAPBinding; +import javax.jws.soap.SOAPBinding.ParameterStyle; +import javax.jws.soap.SOAPBinding.Style; + +@WebService +@SOAPBinding(style = Style.DOCUMENT, parameterStyle = ParameterStyle.WRAPPED) +public interface Transaction { + @WebMethod + public String createTransaction( + @WebParam(name = "transaction_id", targetNamespace = "http://service.saranghaengbok.org/") int transaction_id, + @WebParam(name = "buyer_username", targetNamespace = "http://service.saranghaengbok.org/") String buyer_username, + @WebParam(name = "seller_username", targetNamespace = "http://service.saranghaengbok.org/") String seller_username, + @WebParam(name = "item_id", targetNamespace = "http://service.saranghaengbok.org/") List<Integer> list_item_id, + @WebParam(name = "quantity", targetNamespace = "http://service.saranghaengbok.org/") List<Integer> list_quantity + ); + + @WebMethod + public String getAllTransaction( + @WebParam(name = "page", targetNamespace = "http://service.saranghaengbok.org/") int page + ); + + @WebMethod + public String getAllBuyerTransaction( + @WebParam(name = "username", targetNamespace = "http://service.saranghaengbok.org/") String username, + @WebParam(name = "page", targetNamespace = "http://service.saranghaengbok.org/") int page + ); + + @WebMethod + public String getAllSellerTransaction( + @WebParam(name = "username", targetNamespace = "http://service.saranghaengbok.org/") String username, + @WebParam(name = "page", targetNamespace = "http://service.saranghaengbok.org/") int page + ); + + +} diff --git a/src/main/java/org/saranghaengbok/service/TransactionImpl.java b/src/main/java/org/saranghaengbok/service/TransactionImpl.java new file mode 100644 index 0000000000000000000000000000000000000000..9639a6bef54c1e2f0bfc163d8e6349a9248a0eb4 --- /dev/null +++ b/src/main/java/org/saranghaengbok/service/TransactionImpl.java @@ -0,0 +1,221 @@ +package org.saranghaengbok.service; + +import java.sql.Statement; +import java.util.List; +import java.util.Objects; +import java.sql.Connection; +import java.sql.ResultSet; + +import javax.annotation.Resource; +import javax.jws.WebService; +import javax.xml.ws.WebServiceContext; +import javax.xml.ws.handler.MessageContext; +import com.sun.net.httpserver.HttpExchange; + +import org.saranghaengbok.core.DatabaseConnection; +import org.saranghaengbok.models.Log; + +@WebService(endpointInterface = "org.saranghaengbok.service.Transaction") +public class TransactionImpl implements Transaction{ + + @Resource + public WebServiceContext context; + + private String apiKey; + + public void setAPIKey(){ + MessageContext messageContext = this.context.getMessageContext(); + HttpExchange exchange = (HttpExchange) messageContext.get("com.sun.xml.ws.http.exchange"); + this.apiKey = exchange.getRequestHeaders().getFirst("API-Key"); + } + + public boolean validateAPIKeyREST(){ + setAPIKey(); + if (apiKey == null){ + return false; + } else if (apiKey.equals("rest")){ + return true; + } else { + return false; + } + } + + public boolean validateAPIKeyPHP(){ + setAPIKey(); + if (apiKey == null){ + return false; + } else if (apiKey.equals("php")){ + return true; + } else { + return false; + } + } + + @Override + public String createTransaction(int transaction_id, String buyer_username, String seller_username, List<Integer> list_item_id, List<Integer> list_quantity) { + if (!validateAPIKeyPHP()){ + return "Invalid API Key"; + } + DatabaseConnection db = new DatabaseConnection(); + Connection connection = db.getConnection(); + if ((list_item_id.isEmpty() || Objects.isNull(list_item_id)) && + list_quantity.isEmpty() || Objects.isNull(list_quantity)){ + return "No item checked out"; + } else { + try { + Statement statement = connection.createStatement(); + String query = "INSERT INTO transaction (transaction_id, buyer_username, seller_username) VALUES ("+ transaction_id +", '"+ buyer_username + "', '" + seller_username +"')"; + statement.executeQuery(query); + for (int i = 0; i < list_item_id.size(); i++) { + String query2 = "INSERT INTO transaction_item (transaction_id, item_id, quantity) VALUES ("+ transaction_id +","+ list_item_id.get(i)+","+ list_quantity.get(i) +")"; + statement.executeQuery(query2); + System.out.printf("transaction_item: &d, item_id: %d, quantity: %d", transaction_id, list_item_id.get(i), list_quantity.get(i)); + } + Log.logger("transaction success", this.apiKey, this.context); + statement.close(); + return "transaction success"; + } catch (Exception e) { + e.printStackTrace(); + Log.logger("transaction failed", this.apiKey, this.context); + return "transaction failed"; + } finally { + try { + connection.close(); + } catch (Exception e) { + Log.logger("Unexpected error", this.apiKey, this.context); + return "Unexpected error"; + } + } + } + } + + @Override + public String getAllTransaction(int page) { + if (!validateAPIKeyPHP()){ + return "Invalid API Key"; + } + DatabaseConnection db = new DatabaseConnection(); + Connection connection = db.getConnection(); + try{ + Statement statement = connection.createStatement(); + String query = "SELECT * FROM transaction as t join transaction_items as ti on t.transaction_id = ti.transaction_id LIMIT" + (page-1)*10 + "10"; + ResultSet result = statement.executeQuery(query); + Boolean hasResult = false; + String message = "{\"data\": ["; + while (result.next()) { + message += "{\"transaction_id\": " + result.getInt("transaction_id") + + "\"buyer_username\": " + result.getString("buyer_username") + + "\"sellerusername\": " + result.getString("seller_username") + + "\"item_id\": " + result.getInt("item_id"); + hasResult = true; + } + message = message.substring(0, message.length() - 1); + message += "]}"; + if (!hasResult) { + message = "{\"data\": []}"; + } + + Log.logger(message, this.apiKey, this.context); + result.close(); + statement.close(); + return message; + } catch (Exception e){ + Log.logger("Unexpected error occur", this.apiKey, this.context); + return "Unexpected error occur"; + } finally { + try { + connection.close(); + } catch (Exception e) { + Log.logger("Unexpected error occur", this.apiKey, this.context); + return "Unexpected error occur"; + } + } + } + + @Override + public String getAllBuyerTransaction(String username, int page) { + if (!validateAPIKeyPHP()){ + return "Invalid API Key"; + } + DatabaseConnection db = new DatabaseConnection(); + Connection connection = db.getConnection(); + + try{ + Statement statement = connection.createStatement(); + String query = "SELECT * FROM transaction as t join transaction_items as ti on t.transaction_id = ti.transaction_id WHERE buyer_username = '" + username + "' LIMIT" + (page-1)*10 + "10"; + ResultSet result = statement.executeQuery(query); + Boolean hasResult = false; + String message = "{\"data\": ["; + while (result.next()) { + message += "{\"transaction_id\": " + result.getInt("transaction_id") + + "\"buyer_username\": " + result.getString("buyer_username") + + "\"sellerusername\": " + result.getString("seller_username") + + "\"item_id\": " + result.getInt("item_id"); + hasResult = true; + } + message = message.substring(0, message.length() - 1); + message += "]}"; + if (!hasResult) { + message = "{\"data\": []}"; + } + Log.logger(message, this.apiKey, this.context); + result.close(); + statement.close(); + return message; + } catch (Exception e) { + Log.logger("Unexpected error occur", this.apiKey, this.context); + return "Unexpected error occur"; + } finally { + + try { + connection.close(); + } catch (Exception e) { + Log.logger("Unexpected error occur", this.apiKey, this.context); + return "Unexpected error occur"; + } + } + } + + @Override + public String getAllSellerTransaction(String username, int page) { + if (!validateAPIKeyPHP()){ + return "Invalid API Key"; + } + DatabaseConnection db = new DatabaseConnection(); + Connection connection = db.getConnection(); + try{ + Statement statement = connection.createStatement(); + String query = "SELECT * FROM transaction as t join transaction_items as ti on t.transaction_id = ti.transaction_id WHERE seller_username = '" + username + "' LIMIT" + (page-1)*10 + "10"; + ResultSet result = statement.executeQuery(query); + Boolean hasResult = false; + String message = "{\"data\": ["; + while (result.next()) { + message += "{\"transaction_id\": " + result.getInt("transaction_id") + + "\"buyer_username\": " + result.getString("buyer_username") + + "\"sellerusername\": " + result.getString("seller_username") + + "\"item_id\": " + result.getInt("item_id"); + hasResult = true; + } + message = message.substring(0, message.length() - 1); + message += "]}"; + if (!hasResult) { + message = "{\"data\": []}"; + } + Log.logger(message, this.apiKey, this.context); + result.close(); + statement.close(); + return message; + } catch (Exception e){ + Log.logger("Unexpected error occur", this.apiKey, this.context); + return "Unexpected error occur"; + } finally { + try { + connection.close(); + } catch (Exception e) { + Log.logger("Unexpected error occur", this.apiKey, this.context); + return "Unexpected error occur"; + } + } + } + +}