diff --git a/soap/src/main/java/com/sayit/service/Services.java b/soap/src/main/java/com/sayit/service/Services.java new file mode 100644 index 0000000000000000000000000000000000000000..4c36cbb83fd308007c1c4ba4e6a0c94fac108994 --- /dev/null +++ b/soap/src/main/java/com/sayit/service/Services.java @@ -0,0 +1,48 @@ +package com.sayit.service; + +import com.sayit.model.LoggingModel; +import com.sun.net.httpserver.HttpExchange; + +import javax.annotation.Resource; +import javax.xml.ws.WebServiceContext; +import javax.xml.ws.handler.MessageContext; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.List; + +public class Services { + @Resource + private WebServiceContext context; + + public boolean validateKey() { + MessageContext messageContext = context.getMessageContext(); + System.out.println("halo"); + HttpExchange httpExchange = (HttpExchange) messageContext.get("com.sun.xml.ws.http.exchange"); + List<String> APIKeys = httpExchange.getRequestHeaders().get("X-API-KEY"); + System.out.println(APIKeys); + System.out.println("masuk sini"); + String APIKey = (APIKeys != null && !APIKeys.isEmpty()) ? APIKeys.get(0) : null; + String[] APIValid = {"REST_KEY", "SOAP_KEY"}; + if (APIKey == null) { + return false; + } else if (APIKey.equals(APIValid[0]) || APIKey.equals(APIValid[1])) { + return true; + } else { + return false; + } + } + + public void log(String description) { + MessageContext msgContext = context.getMessageContext(); + HttpExchange httpExchange = (HttpExchange) msgContext.get("com.sun.xml.ws.http.exchange"); + String ipAddress = httpExchange.getRemoteAddress().getAddress().getHostAddress(); + String endpoint = httpExchange.getRequestURI().toString(); + + DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); + java.util.Date date = new java.util.Date(); + String time = dateFormat.format(date); + + LoggingModel loggingModel = new LoggingModel(); + loggingModel.InsertLog(ipAddress, endpoint, description, time); + } +}