scala-fix changes

Signed-off-by: hrj <harshad.rj@gmail.com>
This commit is contained in:
hrj 2021-04-18 18:37:11 +05:30
parent 55288d3346
commit 32169dbe80
5 changed files with 94 additions and 59 deletions

View File

@ -22,8 +22,7 @@ public class FontFunCaptcha implements ChallengeProvider {
return Map.of( return Map.of(
"supportedLevels", List.of("medium"), "supportedLevels", List.of("medium"),
"supportedMedia", List.of("image/png"), "supportedMedia", List.of("image/png"),
"supportedInputType", List.of("text") "supportedInputType", List.of("text"));
);
} }
public void configure(String config) { public void configure(String config) {

View File

@ -59,8 +59,7 @@ public class GifCaptcha implements ChallengeProvider {
return Map.of( return Map.of(
"supportedLevels", List.of("hard"), "supportedLevels", List.of("hard"),
"supportedMedia", List.of("image/gif"), "supportedMedia", List.of("image/gif"),
"supportedInputType", List.of("text") "supportedInputType", List.of("text"));
);
} }
public Challenge returnChallenge() { public Challenge returnChallenge() {

View File

@ -31,8 +31,7 @@ public class ShadowTextCaptcha implements ChallengeProvider {
return Map.of( return Map.of(
"supportedLevels", List.of("easy"), "supportedLevels", List.of("easy"),
"supportedMedia", List.of("image/png"), "supportedMedia", List.of("image/png"),
"supportedInputType", List.of("text") "supportedInputType", List.of("text"));
);
} }
public boolean checkAnswer(String secret, String answer) { public boolean checkAnswer(String secret, String answer) {

View File

@ -24,7 +24,9 @@ public final class Server {
public static interface Response { public static interface Response {
public int getCode(); public int getCode();
public byte[] getBytes(); public byte[] getBytes();
public Map<String, List<String>> getResponseHeaders(); public Map<String, List<String>> getResponseHeaders();
} }
@ -39,14 +41,21 @@ public final class Server {
this.responseHeaders = null; this.responseHeaders = null;
} }
public ByteResponse(final int code, final byte[] bytes, final Map<String, List<String>> responseHeaders) { public ByteResponse(
final int code, final byte[] bytes, final Map<String, List<String>> responseHeaders) {
this.code = code; this.code = code;
this.bytes = bytes; this.bytes = bytes;
this.responseHeaders = responseHeaders; this.responseHeaders = responseHeaders;
} }
public int getCode() { return this.code; } public int getCode() {
public byte[] getBytes() { return this.bytes; } return this.code;
}
public byte[] getBytes() {
return this.bytes;
}
public Map<String, List<String>> getResponseHeaders() { public Map<String, List<String>> getResponseHeaders() {
return this.responseHeaders; return this.responseHeaders;
} }
@ -57,26 +66,31 @@ public final class Server {
super(code, msg.getBytes()); super(code, msg.getBytes());
} }
public StringResponse(final int code, final String msg, final Map<String, List<String>> responseHeaders) { public StringResponse(
final int code, final String msg, final Map<String, List<String>> responseHeaders) {
super(code, msg.getBytes(), responseHeaders); super(code, msg.getBytes(), responseHeaders);
} }
} }
public final class Request { public final class Request {
final HttpExchange exchange; final HttpExchange exchange;
Request(final HttpExchange exchange) { Request(final HttpExchange exchange) {
this.exchange = exchange; this.exchange = exchange;
} }
public String getMethod() { public String getMethod() {
return exchange.getRequestMethod(); return exchange.getRequestMethod();
} }
public Map<String, List<String>> getQueryParams() { public Map<String, List<String>> getQueryParams() {
final var query = exchange.getRequestURI().getQuery(); final var query = exchange.getRequestURI().getQuery();
final var params = parseParams(query); final var params = parseParams(query);
return params; return params;
} }
public byte[] getBody() { public byte[] getBody() {
try(final var bodyIS = exchange.getRequestBody()) { try (final var bodyIS = exchange.getRequestBody()) {
final var bytes = bodyIS.readAllBytes(); final var bytes = bodyIS.readAllBytes();
bodyIS.close(); bodyIS.close();
return bytes; return bytes;
@ -84,6 +98,7 @@ public final class Server {
return null; return null;
} }
} }
public String getBodyString() { public String getBodyString() {
return new String(getBody()); return new String(getBody());
} }
@ -98,11 +113,13 @@ public final class Server {
public final String path; public final String path;
public final Processor processor; public final Processor processor;
public final String[] methods; public final String[] methods;
public Handler(final String path, final Processor processor) { public Handler(final String path, final Processor processor) {
this.path = path; this.path = path;
this.processor = processor; this.processor = processor;
this.methods = new String[] {}; this.methods = new String[] {};
} }
public Handler(final String path, final String methods, final Processor processor) { public Handler(final String path, final String methods, final Processor processor) {
this.path = path; this.path = path;
this.processor = processor; this.processor = processor;
@ -110,16 +127,23 @@ public final class Server {
} }
} }
public Server(final InetSocketAddress addr, final int backlog, final List<Handler> handlers, final Executor executor) throws IOException { public Server(
final InetSocketAddress addr,
final int backlog,
final List<Handler> handlers,
final Executor executor)
throws IOException {
this.server = HttpServer.create(addr, backlog); this.server = HttpServer.create(addr, backlog);
this.server.setExecutor(executor); this.server.setExecutor(executor);
for (final var handler: handlers) { for (final var handler : handlers) {
// System.out.println("Registering handler for " + handler.path); // System.out.println("Registering handler for " + handler.path);
this.server.createContext(handler.path, new HttpHandler() { this.server.createContext(
handler.path,
new HttpHandler() {
public void handle(final HttpExchange exchange) { public void handle(final HttpExchange exchange) {
final var method = exchange.getRequestMethod(); final var method = exchange.getRequestMethod();
final Response errorResponse = checkMethods(handler.methods, method); final Response errorResponse = checkMethods(handler.methods, method);
try(final var os = exchange.getResponseBody()) { try (final var os = exchange.getResponseBody()) {
Response response; Response response;
if (errorResponse != null) { if (errorResponse != null) {
response = errorResponse; response = errorResponse;
@ -152,7 +176,7 @@ public final class Server {
public static Response checkMethods(final String[] methods, final String method) { public static Response checkMethods(final String[] methods, final String method) {
if (methods.length > 0) { if (methods.length > 0) {
var found = false; var found = false;
for (var m: methods) { for (var m : methods) {
if (m.equals(method)) { if (m.equals(method)) {
found = true; found = true;
break; break;
@ -178,17 +202,21 @@ public final class Server {
} }
// Adapted from https://stackoverflow.com/a/37368660 // Adapted from https://stackoverflow.com/a/37368660
private final static Pattern ampersandPattern = Pattern.compile("&"); private static final Pattern ampersandPattern = Pattern.compile("&");
private final static Pattern equalPattern = Pattern.compile("="); private static final Pattern equalPattern = Pattern.compile("=");
private final static Map<String, List<String>> emptyMap = Map.of(); private static final Map<String, List<String>> emptyMap = Map.of();
private static Map<String, List<String>> parseParams(final String query) { private static Map<String, List<String>> parseParams(final String query) {
if (query == null) { if (query == null) {
return emptyMap; return emptyMap;
} }
final var params = ampersandPattern final var params =
ampersandPattern
.splitAsStream(query) .splitAsStream(query)
.map(s -> Arrays.copyOf(equalPattern.split(s, 2), 2)) .map(s -> Arrays.copyOf(equalPattern.split(s, 2), 2))
.collect(Collectors.groupingBy(s -> decode(s[0]), Collectors.mapping(s -> decode(s[1]), Collectors.toList()))); .collect(
Collectors.groupingBy(
s -> decode(s[0]), Collectors.mapping(s -> decode(s[1]), Collectors.toList())));
return params; return params;
} }
@ -208,42 +236,52 @@ public final class Server {
mAddress = new InetSocketAddress(port); mAddress = new InetSocketAddress(port);
return this; return this;
} }
public ServerBuilder backlog(final int backlog) { public ServerBuilder backlog(final int backlog) {
this.backlog = backlog; this.backlog = backlog;
return this; return this;
} }
public ServerBuilder address(final InetSocketAddress addr) { public ServerBuilder address(final InetSocketAddress addr) {
mAddress = addr; mAddress = addr;
return this; return this;
} }
public ServerBuilder handle(final Handler handler) { public ServerBuilder handle(final Handler handler) {
handlers.add(handler); handlers.add(handler);
return this; return this;
} }
public ServerBuilder GET(final String path, final Processor processor) { public ServerBuilder GET(final String path, final Processor processor) {
handlers.add(new Handler(path, "GET", request -> processor.process(request))); handlers.add(new Handler(path, "GET", request -> processor.process(request)));
return this; return this;
} }
public ServerBuilder POST(final String path, final Processor processor) { public ServerBuilder POST(final String path, final Processor processor) {
handlers.add(new Handler(path, "POST", request -> processor.process(request))); handlers.add(new Handler(path, "POST", request -> processor.process(request)));
return this; return this;
} }
public ServerBuilder PUT(final String path, final Processor processor) { public ServerBuilder PUT(final String path, final Processor processor) {
handlers.add(new Handler(path, "PUT", request -> processor.process(request))); handlers.add(new Handler(path, "PUT", request -> processor.process(request)));
return this; return this;
} }
public ServerBuilder DELETE(final String path, final Processor processor) { public ServerBuilder DELETE(final String path, final Processor processor) {
handlers.add(new Handler(path, "DELETE", request -> processor.process(request))); handlers.add(new Handler(path, "DELETE", request -> processor.process(request)));
return this; return this;
} }
public ServerBuilder HEAD(final String path, final Processor processor) { public ServerBuilder HEAD(final String path, final Processor processor) {
handlers.add(new Handler(path, "HEAD", request -> processor.process(request))); handlers.add(new Handler(path, "HEAD", request -> processor.process(request)));
return this; return this;
} }
public ServerBuilder executor(final Executor executor) { public ServerBuilder executor(final Executor executor) {
this.executor = executor; this.executor = executor;
return this; return this;
} }
public Server build() throws IOException { public Server build() throws IOException {
return new Server(mAddress, backlog, handlers, executor); return new Server(mAddress, backlog, handlers, executor);
} }

View File

@ -9,7 +9,7 @@ import org.limium.picoserve
import org.limium.picoserve.Server.ByteResponse import org.limium.picoserve.Server.ByteResponse
class Server(port: Int) { class Server(port: Int) {
val server = picoserve.Server.builder() val server: picoserve.Server = picoserve.Server.builder()
.port(8888) .port(8888)
.POST("/v1/captcha", (request) => { .POST("/v1/captcha", (request) => {
val json = parse(request.getBodyString()) val json = parse(request.getBodyString())