From efe79f4469868f4c623d3210b234faae1ab8daf3 Mon Sep 17 00:00:00 2001 From: oscar-besga-panel Date: Wed, 9 Nov 2022 19:44:11 +0100 Subject: [PATCH] Add a builder Add an executor to the server --- .../arteam/embedhttp/EmbeddedHttpServer.java | 54 ++++++++---- .../embedhttp/EmbeddedHttpServerBuilder.java | 88 +++++++++++++++++++ .../arteam/embedhttp/HandlerConfig.java | 32 +++++++ 3 files changed, 155 insertions(+), 19 deletions(-) create mode 100644 src/main/java/com/github/arteam/embedhttp/EmbeddedHttpServerBuilder.java create mode 100644 src/main/java/com/github/arteam/embedhttp/HandlerConfig.java diff --git a/src/main/java/com/github/arteam/embedhttp/EmbeddedHttpServer.java b/src/main/java/com/github/arteam/embedhttp/EmbeddedHttpServer.java index 4c5ef73..72c8f4c 100644 --- a/src/main/java/com/github/arteam/embedhttp/EmbeddedHttpServer.java +++ b/src/main/java/com/github/arteam/embedhttp/EmbeddedHttpServer.java @@ -12,8 +12,10 @@ import java.net.InetSocketAddress; import java.nio.charset.StandardCharsets; import java.util.ArrayList; +import java.util.Collection; import java.util.List; import java.util.Map; +import java.util.concurrent.Executor; /** * Represents a simple HTTP server (a facade around {@link com.sun.net.httpserver.HttpServer for unit testing. @@ -23,7 +25,8 @@ public class EmbeddedHttpServer implements Closeable { private HttpServer sunHttpServer; - private final List handlers = new ArrayList<>(); + private final List handlers = new ArrayList<>(); + private Executor executor; /** * Adds a new handler to the server to a path. @@ -36,10 +39,31 @@ public EmbeddedHttpServer addHandler(String path, HttpHandler handler) { * Adds a new handler to the server to a path with an authenticator. */ public EmbeddedHttpServer addHandler(String path, HttpHandler handler, Authenticator authenticator) { - handlers.add(new HttpHandlerConfig(path, handler, authenticator)); + handlers.add(new HandlerConfig(path, handler, authenticator)); return this; } + /** + * Adds a new handler to the server to a path with an authenticator. + */ + EmbeddedHttpServer addHandler(HandlerConfig handler) { + handlers.add(handler); + return this; + } + + + /** + * Adds a list handler to the server to a path with an authenticator. + */ + EmbeddedHttpServer addHandlers(Collection handler) { + handlers.addAll(handler); + return this; + } + + void addExecutor(Executor executor) { + this.executor = executor; + } + /** * Starts up the current server on a free port on the localhost. */ @@ -64,12 +88,13 @@ public EmbeddedHttpServer start(InetSocketAddress address) { throw new RuntimeException(e); } - for (HttpHandlerConfig config : handlers) { - HttpContext context = sunHttpServer.createContext(config.path, httpExchange -> { + for (HandlerConfig config : handlers) { + HttpContext context = sunHttpServer.createContext(config.getPath(), httpExchange -> { try { Headers requestHeaders = httpExchange.getRequestHeaders(); HttpResponse response = new HttpResponse(); - config.httpHandler.handle(new HttpRequest(httpExchange.getRequestMethod(), + HttpHandler handler = config.getHttpHandler(); + handler.handle(new HttpRequest(httpExchange.getRequestMethod(), httpExchange.getRequestURI(), httpExchange.getProtocol(), requestHeaders, readFromStream(httpExchange.getRequestBody())), response); for (Map.Entry> e : response.getHeaders().entrySet()) { @@ -84,10 +109,13 @@ public EmbeddedHttpServer start(InetSocketAddress address) { httpExchange.close(); } }); - if (config.authenticator != null) { - context.setAuthenticator(config.authenticator); + if (config.getAuthenticator() != null) { + context.setAuthenticator(config.getAuthenticator()); } } + if (executor != null) { + sunHttpServer.setExecutor(executor); + } sunHttpServer.start(); return this; } @@ -128,16 +156,4 @@ private static String readFromStream(InputStream inputStream) throws IOException return new String(inputStream.readAllBytes(), StandardCharsets.UTF_8); } - private static class HttpHandlerConfig { - - private final String path; - private final HttpHandler httpHandler; - private final Authenticator authenticator; - - HttpHandlerConfig(String path, HttpHandler httpHandler, Authenticator authenticator) { - this.path = path; - this.httpHandler = httpHandler; - this.authenticator = authenticator; - } - } } diff --git a/src/main/java/com/github/arteam/embedhttp/EmbeddedHttpServerBuilder.java b/src/main/java/com/github/arteam/embedhttp/EmbeddedHttpServerBuilder.java new file mode 100644 index 0000000..4421af4 --- /dev/null +++ b/src/main/java/com/github/arteam/embedhttp/EmbeddedHttpServerBuilder.java @@ -0,0 +1,88 @@ +package com.github.arteam.embedhttp; + +import com.sun.net.httpserver.Authenticator; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.Executor; + +public class EmbeddedHttpServerBuilder { + + + /** + * Creates a new builder to generate the server + * @return builder + */ + public static EmbeddedHttpServerBuilder createNew() { + return new EmbeddedHttpServerBuilder(); + } + + private int port = 8080; + private List handlers = new ArrayList<>(); + private Executor executor; + + /** + * Nobody should use this + */ + private EmbeddedHttpServerBuilder() {} + + /** + * Use the server with this port + * @param port Value of port, by default 8080 + * @return Builder + */ + EmbeddedHttpServerBuilder withPort(int port) { + this.port = port; + return this; + } + + /** + * Add a handler with an autenticator for the server + * @param path Path to handle + * @param handler Functional method to execute the handling + * @param authenticator to authenticate user + * @return + */ + EmbeddedHttpServerBuilder addHandler(String path,Authenticator authenticator, HttpHandler handler) { + this.handlers.add(new HandlerConfig(path, handler, authenticator)); + return this; + } + + /** + * Add a handler for the server + * @param path Path to handle + * @param handler Functional method to execute the handling + * @return Builder + */ + EmbeddedHttpServerBuilder addHandler(String path, HttpHandler handler) { + this.handlers.add(new HandlerConfig(path, handler)); + return this; + } + + /** + * Adds a custom executor for the server + * @param executor Executor to add + * @return Builder + */ + EmbeddedHttpServerBuilder addExecutor(Executor executor) { + this.executor = executor; + return this; + } + + /** + * Finally, build the server + * The server should be running when retunrning + * @return Started server + */ + EmbeddedHttpServer buildAndRun() { + EmbeddedHttpServer embeddedHttpServer = new EmbeddedHttpServer(); + embeddedHttpServer.addHandlers(handlers); + if (executor != null) { + embeddedHttpServer.addExecutor(executor); + } + embeddedHttpServer.start(port); + return embeddedHttpServer; + } + + +} diff --git a/src/main/java/com/github/arteam/embedhttp/HandlerConfig.java b/src/main/java/com/github/arteam/embedhttp/HandlerConfig.java new file mode 100644 index 0000000..282f17a --- /dev/null +++ b/src/main/java/com/github/arteam/embedhttp/HandlerConfig.java @@ -0,0 +1,32 @@ +package com.github.arteam.embedhttp; + +import com.sun.net.httpserver.Authenticator; + +class HandlerConfig { + + private final String path; + private final HttpHandler httpHandler; + private final Authenticator authenticator; + + HandlerConfig(String path, HttpHandler httpHandler) { + this(path, httpHandler, null); + } + + HandlerConfig(String path, HttpHandler httpHandler, Authenticator authenticator) { + this.path = path; + this.httpHandler = httpHandler; + this.authenticator = authenticator; + } + + public String getPath() { + return path; + } + + public HttpHandler getHttpHandler() { + return httpHandler; + } + + public Authenticator getAuthenticator() { + return authenticator; + } +}