| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- import com.sun.net.httpserver.HttpServer;
- import com.sun.net.httpserver.HttpHandler;
- import com.sun.net.httpserver.HttpExchange;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.io.InputStream;
- import java.net.InetSocketAddress;
- import java.nio.charset.StandardCharsets;
- import java.util.*;
- public class RestApiServer {
- // Key yang diizinkan
- private static final String VALID_KEY = "K7tfZuRT1MLmNsftNWK9FUlGZHL4oPIa";
- // start single port
- public static void start(int port) {
- try {
- HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);
- server.createContext("/mid/bifast/inqAccount", new InqAccountHandler());
- server.setExecutor(null);
- System.out.println("✅ Dummy BI-Fast REST listening on http://0.0.0.0:" + port);
- server.start();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- // start multiple ports
- public static void start(int[] ports) {
- for (int port : ports) {
- new Thread(() -> start(port), "restapi-" + port).start();
- }
- }
- // Handler untuk BI-Fast
- static class InqAccountHandler implements HttpHandler {
- @Override
- public void handle(HttpExchange exchange) throws IOException {
- if (!"POST".equalsIgnoreCase(exchange.getRequestMethod())) {
- exchange.sendResponseHeaders(405, -1); // Method Not Allowed
- return;
- }
- // cek key dari header atau query
- String clientKey = exchange.getRequestHeaders().getFirst("key");
- if (clientKey == null) {
- clientKey = getQueryParam(exchange.getRequestURI().getQuery(), "key");
- }
- // if (!VALID_KEY.equals(clientKey)) {
- // String resp = "{\"rsp\":\"401\",\"rspdesc\":\"Unauthorized - Invalid key\"}";
- // writeResponse(exchange, 401, resp);
- // return;
- // }
- // baca body manual (karena Java 8 belum ada readAllBytes)
- InputStream is = exchange.getRequestBody();
- StringBuilder sb = new StringBuilder();
- byte[] buffer = new byte[1024];
- int len;
- while ((len = is.read(buffer)) != -1) {
- sb.append(new String(buffer, 0, len, StandardCharsets.UTF_8));
- }
- String requestBody = sb.toString();
- // ambil id dari body kalau ada (sederhana, tanpa parser JSON)
- String clientId = null;
- if (requestBody.contains("\"id\"")) {
- int idx = requestBody.indexOf("\"id\"");
- int colon = requestBody.indexOf(":", idx);
- if (colon > 0) {
- int start = requestBody.indexOf("\"", colon);
- int end = requestBody.indexOf("\"", start + 1);
- if (start > 0 && end > start) {
- clientId = requestBody.substring(start + 1, end);
- }
- }
- }
- // response dummy
- String resp = "{"
- + "\"wsref\":\"000716949164\","
- + "\"rsp\":\"000\","
- + "\"rspdesc\":\"Transaction Success (POST)\","
- + "\"id\":\"" + (clientId != null ? clientId : "AKTE NO.23") + "\","
- + "\"idType\":\"03\","
- + "\"residentStatus\":\"Y\","
- + "\"townName\":\"0391\","
- + "\"name\":\"NASABAH POST\","
- + "\"type\":\"D\""
- + "}";
- writeResponse(exchange, 200, resp);
- }
- }
- private static void writeResponse(HttpExchange exchange, int status, String resp) throws IOException {
- exchange.getResponseHeaders().add("Content-Type", "application/json");
- exchange.sendResponseHeaders(status, resp.getBytes(StandardCharsets.UTF_8).length);
- try (OutputStream os = exchange.getResponseBody()) {
- os.write(resp.getBytes(StandardCharsets.UTF_8));
- }
- }
- // helper ambil query param
- private static String getQueryParam(String query, String key) {
- if (query == null) return null;
- for (String pair : query.split("&")) {
- String[] kv = pair.split("=");
- if (kv.length == 2 && kv[0].equals(key)) {
- return kv[1];
- }
- }
- return null;
- }
- }
|