Skip to content
Snippets Groups Projects
Commit d154b49d authored by Haidar's avatar Haidar
Browse files

feat: add get currencyserviceimplement

parent 1ed0689a
Branches
Tags
No related merge requests found
package org.example.service;
import javax.annotation.Resource;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.xml.ws.WebServiceContext;
import javax.xml.ws.handler.MessageContext;
import com.sun.net.httpserver.HttpExchange;
import java.sql.PreparedStatement;
import com.sun.xml.internal.ws.developer.JAXWSProperties;
import org.example.model.Challenge;
import org.example.model.Currency;
import org.example.core.Database;
import org.example.model.Log;
import org.w3c.dom.Element;
import java.security.Principal;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
@WebService
public class getCurrencyServiceImpl implements getCurrencyService {
@Resource
public WebServiceContext wsContext;
public Boolean checkApiKey() {
String[] API_KEYS = { "RestClient" , "PHPClient" };
MessageContext msgContext = wsContext.getMessageContext();
HttpExchange httpExchange = (HttpExchange) msgContext.get("com.sun.xml.internal.ws.http.exchange");
String apiKey = httpExchange.getRequestHeaders().getFirst("X-API-Key");
if (apiKey == null) {
return false;
} else if (apiKey.equals(API_KEYS[0]) || apiKey.equals(API_KEYS[1])) {
return true;
} else {
return false;
}
}
public void log(String description) {
try {
MessageContext msgContext = wsContext.getMessageContext();
HttpExchange httpExchange = (HttpExchange) msgContext.get("com.sun.xml.internal.ws.http.exchange");
String ip = httpExchange.getRemoteAddress().getAddress().getHostAddress();
String endpoint = httpExchange.getRequestURI().toString();
System.out.println(ip);
System.out.println(endpoint);
Log log = new Log();
String apiKey = httpExchange.getRequestHeaders().getFirst("X-API-Key");
String desc = apiKey + ": " + description;
log.InsertLog(desc, ip, endpoint);
} catch (Exception e) {
System.out.println("Internal Server Error: " + e.getMessage());
}
}
@WebMethod
@Override
public Currency getCurrency(int restId) {
if (!checkApiKey()) {
return null;
}
Database db = new Database();
Connection connection = db.getConnection();
System.out.println(restId);
Currency currency = null;
try {
String query = "SELECT * FROM soap_connector WHERE user_id_Rest = ?";
PreparedStatement preparedStatement = connection.prepareStatement(query);
preparedStatement.setInt(1, restId);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
currency = new Currency (
resultSet.getInt("point"),
resultSet.getInt("uang")
);
}
preparedStatement.close();
connection.close();
log("get currency with user id " + restId);
return currency;
} catch (Exception e) {
e.printStackTrace();
log("Error when get currency with user id " + restId);
return null;
}
}
}
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment