From 32169dbe80d5cd271c67a5c713193025eb3b6f5e Mon Sep 17 00:00:00 2001 From: hrj Date: Sun, 18 Apr 2021 18:37:11 +0530 Subject: [PATCH] scala-fix changes Signed-off-by: hrj --- src/main/java/lc/captchas/FontFunCaptcha.java | 7 +- src/main/java/lc/captchas/GifCaptcha.java | 7 +- .../java/lc/captchas/ShadowTextCaptcha.java | 7 +- .../java/org/limium/picoserve/Server.java | 130 +++++++++++------- src/main/scala/lc/server/Server.scala | 2 +- 5 files changed, 94 insertions(+), 59 deletions(-) diff --git a/src/main/java/lc/captchas/FontFunCaptcha.java b/src/main/java/lc/captchas/FontFunCaptcha.java index 3ad134c..1007078 100644 --- a/src/main/java/lc/captchas/FontFunCaptcha.java +++ b/src/main/java/lc/captchas/FontFunCaptcha.java @@ -20,10 +20,9 @@ public class FontFunCaptcha implements ChallengeProvider { public Map> supportedParameters() { return Map.of( - "supportedLevels", List.of("medium"), - "supportedMedia", List.of("image/png"), - "supportedInputType", List.of("text") - ); + "supportedLevels", List.of("medium"), + "supportedMedia", List.of("image/png"), + "supportedInputType", List.of("text")); } public void configure(String config) { diff --git a/src/main/java/lc/captchas/GifCaptcha.java b/src/main/java/lc/captchas/GifCaptcha.java index 19b161a..0dc2a82 100644 --- a/src/main/java/lc/captchas/GifCaptcha.java +++ b/src/main/java/lc/captchas/GifCaptcha.java @@ -57,10 +57,9 @@ public class GifCaptcha implements ChallengeProvider { public Map> supportedParameters() { return Map.of( - "supportedLevels", List.of("hard"), - "supportedMedia", List.of("image/gif"), - "supportedInputType", List.of("text") - ); + "supportedLevels", List.of("hard"), + "supportedMedia", List.of("image/gif"), + "supportedInputType", List.of("text")); } public Challenge returnChallenge() { diff --git a/src/main/java/lc/captchas/ShadowTextCaptcha.java b/src/main/java/lc/captchas/ShadowTextCaptcha.java index ba26513..b510b9a 100644 --- a/src/main/java/lc/captchas/ShadowTextCaptcha.java +++ b/src/main/java/lc/captchas/ShadowTextCaptcha.java @@ -29,10 +29,9 @@ public class ShadowTextCaptcha implements ChallengeProvider { public Map> supportedParameters() { return Map.of( - "supportedLevels", List.of("easy"), - "supportedMedia", List.of("image/png"), - "supportedInputType", List.of("text") - ); + "supportedLevels", List.of("easy"), + "supportedMedia", List.of("image/png"), + "supportedInputType", List.of("text")); } public boolean checkAnswer(String secret, String answer) { diff --git a/src/main/java/org/limium/picoserve/Server.java b/src/main/java/org/limium/picoserve/Server.java index b1b2be5..4f1683c 100644 --- a/src/main/java/org/limium/picoserve/Server.java +++ b/src/main/java/org/limium/picoserve/Server.java @@ -24,7 +24,9 @@ public final class Server { public static interface Response { public int getCode(); + public byte[] getBytes(); + public Map> getResponseHeaders(); } @@ -39,14 +41,21 @@ public final class Server { this.responseHeaders = null; } - public ByteResponse(final int code, final byte[] bytes, final Map> responseHeaders) { + public ByteResponse( + final int code, final byte[] bytes, final Map> responseHeaders) { this.code = code; this.bytes = bytes; this.responseHeaders = responseHeaders; } - public int getCode() { return this.code; } - public byte[] getBytes() { return this.bytes; } + public int getCode() { + return this.code; + } + + public byte[] getBytes() { + return this.bytes; + } + public Map> getResponseHeaders() { return this.responseHeaders; } @@ -57,26 +66,31 @@ public final class Server { super(code, msg.getBytes()); } - public StringResponse(final int code, final String msg, final Map> responseHeaders) { + public StringResponse( + final int code, final String msg, final Map> responseHeaders) { super(code, msg.getBytes(), responseHeaders); } } public final class Request { final HttpExchange exchange; + Request(final HttpExchange exchange) { this.exchange = exchange; } + public String getMethod() { return exchange.getRequestMethod(); } + public Map> getQueryParams() { final var query = exchange.getRequestURI().getQuery(); final var params = parseParams(query); return params; } + public byte[] getBody() { - try(final var bodyIS = exchange.getRequestBody()) { + try (final var bodyIS = exchange.getRequestBody()) { final var bytes = bodyIS.readAllBytes(); bodyIS.close(); return bytes; @@ -84,6 +98,7 @@ public final class Server { return null; } } + public String getBodyString() { return new String(getBody()); } @@ -98,11 +113,13 @@ public final class Server { public final String path; public final Processor processor; public final String[] methods; + public Handler(final String path, final Processor processor) { this.path = path; this.processor = processor; this.methods = new String[] {}; } + public Handler(final String path, final String methods, final Processor processor) { this.path = path; this.processor = processor; @@ -110,49 +127,56 @@ public final class Server { } } - public Server(final InetSocketAddress addr, final int backlog, final List handlers, final Executor executor) throws IOException { + public Server( + final InetSocketAddress addr, + final int backlog, + final List handlers, + final Executor executor) + throws IOException { this.server = HttpServer.create(addr, backlog); this.server.setExecutor(executor); - for (final var handler: handlers) { + for (final var handler : handlers) { // System.out.println("Registering handler for " + handler.path); - this.server.createContext(handler.path, new HttpHandler() { - public void handle(final HttpExchange exchange) { - final var method = exchange.getRequestMethod(); - final Response errorResponse = checkMethods(handler.methods, method); - try(final var os = exchange.getResponseBody()) { - Response response; - if (errorResponse != null) { - response = errorResponse; - } else { - try { - response = handler.processor.process(new Request(exchange)); - } catch (final Exception e) { - e.printStackTrace(); - response = new StringResponse(500, "Error: " + e); + this.server.createContext( + handler.path, + new HttpHandler() { + public void handle(final HttpExchange exchange) { + final var method = exchange.getRequestMethod(); + final Response errorResponse = checkMethods(handler.methods, method); + try (final var os = exchange.getResponseBody()) { + Response response; + if (errorResponse != null) { + response = errorResponse; + } else { + try { + response = handler.processor.process(new Request(exchange)); + } catch (final Exception e) { + e.printStackTrace(); + response = new StringResponse(500, "Error: " + e); + } + } + final var headersToSend = response.getResponseHeaders(); + if (headersToSend != null) { + final var responseHeaders = exchange.getResponseHeaders(); + responseHeaders.putAll(headersToSend); + } + final var bytes = response.getBytes(); + final var code = response.getCode(); + exchange.sendResponseHeaders(code, bytes.length); + os.write(bytes); + os.close(); + } catch (IOException ioe) { + System.out.println("Error: " + ioe); } } - final var headersToSend = response.getResponseHeaders(); - if (headersToSend != null) { - final var responseHeaders = exchange.getResponseHeaders(); - responseHeaders.putAll(headersToSend); - } - final var bytes = response.getBytes(); - final var code = response.getCode(); - exchange.sendResponseHeaders(code, bytes.length); - os.write(bytes); - os.close(); - } catch (IOException ioe) { - System.out.println("Error: " + ioe); - } - } - }); + }); } } public static Response checkMethods(final String[] methods, final String method) { if (methods.length > 0) { var found = false; - for (var m: methods) { + for (var m : methods) { if (m.equals(method)) { found = true; break; @@ -178,24 +202,28 @@ public final class Server { } // Adapted from https://stackoverflow.com/a/37368660 - private final static Pattern ampersandPattern = Pattern.compile("&"); - private final static Pattern equalPattern = Pattern.compile("="); - private final static Map> emptyMap = Map.of(); + private static final Pattern ampersandPattern = Pattern.compile("&"); + private static final Pattern equalPattern = Pattern.compile("="); + private static final Map> emptyMap = Map.of(); + private static Map> parseParams(final String query) { if (query == null) { return emptyMap; } - final var params = ampersandPattern - .splitAsStream(query) - .map(s -> Arrays.copyOf(equalPattern.split(s, 2), 2)) - .collect(Collectors.groupingBy(s -> decode(s[0]), Collectors.mapping(s -> decode(s[1]), Collectors.toList()))); + final var params = + ampersandPattern + .splitAsStream(query) + .map(s -> Arrays.copyOf(equalPattern.split(s, 2), 2)) + .collect( + Collectors.groupingBy( + s -> decode(s[0]), Collectors.mapping(s -> decode(s[1]), Collectors.toList()))); return params; } private static String decode(final String encoded) { return Optional.ofNullable(encoded) - .map(e -> URLDecoder.decode(e, StandardCharsets.UTF_8)) - .orElse(null); + .map(e -> URLDecoder.decode(e, StandardCharsets.UTF_8)) + .orElse(null); } public static class ServerBuilder { @@ -208,42 +236,52 @@ public final class Server { mAddress = new InetSocketAddress(port); return this; } + public ServerBuilder backlog(final int backlog) { this.backlog = backlog; return this; } + public ServerBuilder address(final InetSocketAddress addr) { mAddress = addr; return this; } + public ServerBuilder handle(final Handler handler) { handlers.add(handler); return this; } + public ServerBuilder GET(final String path, final Processor processor) { handlers.add(new Handler(path, "GET", request -> processor.process(request))); return this; } + public ServerBuilder POST(final String path, final Processor processor) { handlers.add(new Handler(path, "POST", request -> processor.process(request))); return this; } + public ServerBuilder PUT(final String path, final Processor processor) { handlers.add(new Handler(path, "PUT", request -> processor.process(request))); return this; } + public ServerBuilder DELETE(final String path, final Processor processor) { handlers.add(new Handler(path, "DELETE", request -> processor.process(request))); return this; } + public ServerBuilder HEAD(final String path, final Processor processor) { handlers.add(new Handler(path, "HEAD", request -> processor.process(request))); return this; } + public ServerBuilder executor(final Executor executor) { this.executor = executor; return this; } + public Server build() throws IOException { return new Server(mAddress, backlog, handlers, executor); } diff --git a/src/main/scala/lc/server/Server.scala b/src/main/scala/lc/server/Server.scala index 7c4cf6e..849caa4 100644 --- a/src/main/scala/lc/server/Server.scala +++ b/src/main/scala/lc/server/Server.scala @@ -9,7 +9,7 @@ import org.limium.picoserve import org.limium.picoserve.Server.ByteResponse class Server(port: Int) { - val server = picoserve.Server.builder() + val server: picoserve.Server = picoserve.Server.builder() .port(8888) .POST("/v1/captcha", (request) => { val json = parse(request.getBodyString())