1
0

RestApiServer.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import com.sun.net.httpserver.HttpServer;
  2. import com.sun.net.httpserver.HttpHandler;
  3. import com.sun.net.httpserver.HttpExchange;
  4. import java.io.IOException;
  5. import java.io.OutputStream;
  6. import java.io.InputStream;
  7. import java.net.InetSocketAddress;
  8. import java.nio.charset.StandardCharsets;
  9. import java.util.*;
  10. public class RestApiServer {
  11. // Key yang diizinkan
  12. private static final String VALID_KEY = "K7tfZuRT1MLmNsftNWK9FUlGZHL4oPIa";
  13. // start single port
  14. public static void start(int port) {
  15. try {
  16. HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);
  17. server.createContext("/mid/bifast/inqAccount", new InqAccountHandler());
  18. server.setExecutor(null);
  19. System.out.println("✅ Dummy BI-Fast REST listening on http://0.0.0.0:" + port);
  20. server.start();
  21. } catch (IOException e) {
  22. e.printStackTrace();
  23. }
  24. }
  25. // start multiple ports
  26. public static void start(int[] ports) {
  27. for (int port : ports) {
  28. new Thread(() -> start(port), "restapi-" + port).start();
  29. }
  30. }
  31. // Handler untuk BI-Fast
  32. static class InqAccountHandler implements HttpHandler {
  33. @Override
  34. public void handle(HttpExchange exchange) throws IOException {
  35. if (!"POST".equalsIgnoreCase(exchange.getRequestMethod())) {
  36. exchange.sendResponseHeaders(405, -1); // Method Not Allowed
  37. return;
  38. }
  39. // cek key dari header atau query
  40. String clientKey = exchange.getRequestHeaders().getFirst("key");
  41. if (clientKey == null) {
  42. clientKey = getQueryParam(exchange.getRequestURI().getQuery(), "key");
  43. }
  44. // if (!VALID_KEY.equals(clientKey)) {
  45. // String resp = "{\"rsp\":\"401\",\"rspdesc\":\"Unauthorized - Invalid key\"}";
  46. // writeResponse(exchange, 401, resp);
  47. // return;
  48. // }
  49. // baca body manual (karena Java 8 belum ada readAllBytes)
  50. InputStream is = exchange.getRequestBody();
  51. StringBuilder sb = new StringBuilder();
  52. byte[] buffer = new byte[1024];
  53. int len;
  54. while ((len = is.read(buffer)) != -1) {
  55. sb.append(new String(buffer, 0, len, StandardCharsets.UTF_8));
  56. }
  57. String requestBody = sb.toString();
  58. // ambil id dari body kalau ada (sederhana, tanpa parser JSON)
  59. String clientId = null;
  60. if (requestBody.contains("\"id\"")) {
  61. int idx = requestBody.indexOf("\"id\"");
  62. int colon = requestBody.indexOf(":", idx);
  63. if (colon > 0) {
  64. int start = requestBody.indexOf("\"", colon);
  65. int end = requestBody.indexOf("\"", start + 1);
  66. if (start > 0 && end > start) {
  67. clientId = requestBody.substring(start + 1, end);
  68. }
  69. }
  70. }
  71. // response dummy
  72. String resp = "{"
  73. + "\"wsref\":\"000716949164\","
  74. + "\"rsp\":\"000\","
  75. + "\"rspdesc\":\"Transaction Success (POST)\","
  76. + "\"id\":\"" + (clientId != null ? clientId : "AKTE NO.23") + "\","
  77. + "\"idType\":\"03\","
  78. + "\"residentStatus\":\"Y\","
  79. + "\"townName\":\"0391\","
  80. + "\"name\":\"NASABAH POST\","
  81. + "\"type\":\"D\""
  82. + "}";
  83. writeResponse(exchange, 200, resp);
  84. }
  85. }
  86. private static void writeResponse(HttpExchange exchange, int status, String resp) throws IOException {
  87. exchange.getResponseHeaders().add("Content-Type", "application/json");
  88. exchange.sendResponseHeaders(status, resp.getBytes(StandardCharsets.UTF_8).length);
  89. try (OutputStream os = exchange.getResponseBody()) {
  90. os.write(resp.getBytes(StandardCharsets.UTF_8));
  91. }
  92. }
  93. // helper ambil query param
  94. private static String getQueryParam(String query, String key) {
  95. if (query == null) return null;
  96. for (String pair : query.split("&")) {
  97. String[] kv = pair.split("=");
  98. if (kv.length == 2 && kv[0].equals(key)) {
  99. return kv[1];
  100. }
  101. }
  102. return null;
  103. }
  104. }