Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 35 additions & 19 deletions src/main/java/com/github/arteam/embedhttp/EmbeddedHttpServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -23,7 +25,8 @@
public class EmbeddedHttpServer implements Closeable {

private HttpServer sunHttpServer;
private final List<HttpHandlerConfig> handlers = new ArrayList<>();
private final List<HandlerConfig> handlers = new ArrayList<>();
private Executor executor;

/**
* Adds a new handler to the server to a path.
Expand All @@ -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<HandlerConfig> 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.
*/
Expand All @@ -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<String, List<String>> e : response.getHeaders().entrySet()) {
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
}
}
Original file line number Diff line number Diff line change
@@ -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<HandlerConfig> 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;
}


}
32 changes: 32 additions & 0 deletions src/main/java/com/github/arteam/embedhttp/HandlerConfig.java
Original file line number Diff line number Diff line change
@@ -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;
}
}