From 1c786d06f21c2d63f27fe24444ddcb307a2cff1b Mon Sep 17 00:00:00 2001 From: JanHolger Date: Sun, 3 Sep 2023 21:23:44 +0200 Subject: [PATCH] Moved undertow to a separate repository and removed HTTPServer's empty constructor --- pom.xml | 5 - .../javawebstack/httpserver/HTTPServer.java | 6 +- .../undertow/StreamSinkOutputStream.java | 29 ---- .../undertow/StreamSourceInputStream.java | 29 ---- .../adapter/undertow/UndertowHTTPSocket.java | 132 ------------------ .../undertow/UndertowHTTPSocketServer.java | 80 ----------- 6 files changed, 1 insertion(+), 280 deletions(-) delete mode 100644 src/main/java/org/javawebstack/httpserver/adapter/undertow/StreamSinkOutputStream.java delete mode 100644 src/main/java/org/javawebstack/httpserver/adapter/undertow/StreamSourceInputStream.java delete mode 100644 src/main/java/org/javawebstack/httpserver/adapter/undertow/UndertowHTTPSocket.java delete mode 100644 src/main/java/org/javawebstack/httpserver/adapter/undertow/UndertowHTTPSocketServer.java diff --git a/pom.xml b/pom.xml index f6a69f4..3ad7423 100644 --- a/pom.xml +++ b/pom.xml @@ -62,11 +62,6 @@ junit-jupiter-api 5.10.0 - - io.undertow - undertow-core - 2.3.8.Final - org.junit.jupiter junit-jupiter-engine diff --git a/src/main/java/org/javawebstack/httpserver/HTTPServer.java b/src/main/java/org/javawebstack/httpserver/HTTPServer.java index c3ee9b8..ffa5e4c 100644 --- a/src/main/java/org/javawebstack/httpserver/HTTPServer.java +++ b/src/main/java/org/javawebstack/httpserver/HTTPServer.java @@ -3,7 +3,6 @@ import org.javawebstack.abstractdata.AbstractMapper; import org.javawebstack.abstractdata.NamingPolicy; import org.javawebstack.httpserver.adapter.IHTTPSocketServer; -import org.javawebstack.httpserver.adapter.undertow.UndertowHTTPSocketServer; import org.javawebstack.httpserver.handler.*; import org.javawebstack.httpserver.router.DefaultRouteAutoInjector; import org.javawebstack.httpserver.router.Route; @@ -45,10 +44,6 @@ public class HTTPServer implements RouteParamTransformerProvider { private Function, Object> controllerInitiator = this::defaultControllerInitiator; private boolean formMethods = true; - public HTTPServer() { - this(new UndertowHTTPSocketServer()); - } - public HTTPServer(IHTTPSocketServer server) { this.server = server; routeParamTransformers.add(DefaultRouteParamTransformer.INSTANCE); @@ -424,4 +419,5 @@ public HTTPServer disableFormMethods() { formMethods = false; return this; } + } diff --git a/src/main/java/org/javawebstack/httpserver/adapter/undertow/StreamSinkOutputStream.java b/src/main/java/org/javawebstack/httpserver/adapter/undertow/StreamSinkOutputStream.java deleted file mode 100644 index 56fcb2b..0000000 --- a/src/main/java/org/javawebstack/httpserver/adapter/undertow/StreamSinkOutputStream.java +++ /dev/null @@ -1,29 +0,0 @@ -package org.javawebstack.httpserver.adapter.undertow; - -import org.xnio.channels.StreamSinkChannel; - -import java.io.IOException; -import java.io.OutputStream; -import java.nio.ByteBuffer; - -public class StreamSinkOutputStream extends OutputStream { - - private final StreamSinkChannel sink; - private final ByteBuffer byteBuffer = ByteBuffer.allocate(1); - - public StreamSinkOutputStream(StreamSinkChannel sink) { - this.sink = sink; - } - - public void write(int i) throws IOException { - byteBuffer.position(0); - byteBuffer.put((byte) i); - byteBuffer.position(0); - sink.write(byteBuffer); - } - - public void close() throws IOException { - sink.close(); - } - -} diff --git a/src/main/java/org/javawebstack/httpserver/adapter/undertow/StreamSourceInputStream.java b/src/main/java/org/javawebstack/httpserver/adapter/undertow/StreamSourceInputStream.java deleted file mode 100644 index 79ffba5..0000000 --- a/src/main/java/org/javawebstack/httpserver/adapter/undertow/StreamSourceInputStream.java +++ /dev/null @@ -1,29 +0,0 @@ -package org.javawebstack.httpserver.adapter.undertow; - -import org.xnio.channels.StreamSourceChannel; - -import java.io.IOException; -import java.io.InputStream; -import java.nio.ByteBuffer; - -public class StreamSourceInputStream extends InputStream { - - private final StreamSourceChannel source; - private final ByteBuffer byteBuffer = ByteBuffer.allocate(1); - - public StreamSourceInputStream(StreamSourceChannel source) { - this.source = source; - } - - public synchronized int read() throws IOException { - byteBuffer.position(0); - int r; - while ((r = source.read(byteBuffer)) == 0) - Thread.yield(); - if(r == -1) - return -1; - byteBuffer.position(0); - return byteBuffer.get(); - } - -} diff --git a/src/main/java/org/javawebstack/httpserver/adapter/undertow/UndertowHTTPSocket.java b/src/main/java/org/javawebstack/httpserver/adapter/undertow/UndertowHTTPSocket.java deleted file mode 100644 index 356b513..0000000 --- a/src/main/java/org/javawebstack/httpserver/adapter/undertow/UndertowHTTPSocket.java +++ /dev/null @@ -1,132 +0,0 @@ -package org.javawebstack.httpserver.adapter.undertow; - -import io.undertow.server.HttpServerExchange; -import io.undertow.util.HeaderValues; -import io.undertow.util.HttpString; -import org.javawebstack.httpserver.HTTPMethod; -import org.javawebstack.httpserver.HTTPStatus; -import org.javawebstack.httpserver.adapter.IHTTPSocket; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.util.*; -import java.util.stream.Collectors; - -public class UndertowHTTPSocket implements IHTTPSocket { - - private final HttpServerExchange exchange; - private final InputStream inputStream; - private final OutputStream outputStream; - private boolean closed; - - public UndertowHTTPSocket(HttpServerExchange exchange, InputStream inputStream, OutputStream outputStream) { - this.exchange = exchange; - InputStream is = inputStream == null ? exchange.getInputStream() : inputStream; - this.inputStream = new InputStream() { - public int read() throws IOException { - return is.read(); - } - public int available() throws IOException { - int a = is.available(); - if(a == 0 && !exchange.isRequestComplete()) - a = 1; - return a; - } - }; - this.outputStream = outputStream == null ? exchange.getOutputStream() : outputStream; - } - - public InputStream getInputStream() throws IOException { - return inputStream; - } - - public OutputStream getOutputStream() throws IOException { - return outputStream; - } - - public void close() throws IOException { - if(closed) - return; - closed = true; - exchange.getOutputStream().close(); - } - - public boolean isClosed() { - return exchange.isComplete(); - } - - public IHTTPSocket setResponseStatus(int status, String message) { - if(!exchange.isResponseStarted()) - exchange.setStatusCode(status); - return this; - } - - public IHTTPSocket setResponseHeader(String name, String value) { - exchange.getResponseHeaders().put(new HttpString(name), value); - return this; - } - - public IHTTPSocket addResponseHeader(String name, String value) { - exchange.getResponseHeaders().add(new HttpString(name), value); - return this; - } - - public HTTPMethod getRequestMethod() { - return HTTPMethod.valueOf(exchange.getRequestMethod().toString()); - } - - public String getRequestPath() { - return exchange.getRequestPath(); - } - - public String getRequestQuery() { - return exchange.getQueryString(); - } - - public String getRequestVersion() { - return exchange.getProtocol().toString(); - } - - public Set getRequestHeaderNames() { - return exchange.getRequestHeaders().getHeaderNames().stream().map(HttpString::toString).collect(Collectors.toSet()); - } - - public String getRequestHeader(String name) { - HeaderValues values = exchange.getRequestHeaders().get(name); - if(values == null) - return null; - return values.getFirst(); - } - - public List getRequestHeaders(String name) { - HeaderValues values = exchange.getRequestHeaders().get(name); - if(values == null) - return Collections.emptyList(); - return new ArrayList<>(values); - } - - public int getResponseStatus() { - return exchange.getStatusCode(); - } - - public String getResponseStatusMessage() { - HTTPStatus status = HTTPStatus.byStatus(getResponseStatus()); - if(status == null) - return null; - return status.getMessage(); - } - - public void writeHeaders() throws IOException { - exchange.getOutputStream().write(new byte[0]); - } - - public String getRemoteAddress() { - return exchange.getSourceAddress().getAddress().getHostAddress(); - } - - public HttpServerExchange getExchange() { - return exchange; - } - -} diff --git a/src/main/java/org/javawebstack/httpserver/adapter/undertow/UndertowHTTPSocketServer.java b/src/main/java/org/javawebstack/httpserver/adapter/undertow/UndertowHTTPSocketServer.java deleted file mode 100644 index 178d0d2..0000000 --- a/src/main/java/org/javawebstack/httpserver/adapter/undertow/UndertowHTTPSocketServer.java +++ /dev/null @@ -1,80 +0,0 @@ -package org.javawebstack.httpserver.adapter.undertow; - -import io.undertow.Undertow; -import io.undertow.server.handlers.BlockingHandler; -import io.undertow.websockets.core.WebSocketVersion; -import org.javawebstack.httpserver.adapter.IHTTPSocketHandler; -import org.javawebstack.httpserver.adapter.IHTTPSocketServer; -import org.javawebstack.httpserver.util.websocket.WebSocketUtil; -import org.xnio.Options; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.util.concurrent.*; - -public class UndertowHTTPSocketServer implements IHTTPSocketServer { - - private int port = 80; - private int maxThreads = 64; - private Undertow server; - private IHTTPSocketHandler handler; - private ExecutorService executorService; - - public void setPort(int port) { - this.port = port; - } - - public int getPort() { - return port; - } - - public void start() throws IOException { - executorService = new ThreadPoolExecutor(1, maxThreads, 60L, TimeUnit.SECONDS, new SynchronousQueue()); - server = Undertow.builder() - .addHttpListener(port, "0.0.0.0") - .setServerOption(Options.KEEP_ALIVE, true) - .setHandler(new BlockingHandler(httpServerExchange -> { - httpServerExchange.setDispatchExecutor(executorService); - if(httpServerExchange.getRequestHeaders().contains("sec-websocket-key")) { - httpServerExchange.upgradeChannel((streamConnection, httpServerExchange1) -> { - InputStream inputStream = new StreamSourceInputStream(streamConnection.getSourceChannel()); - OutputStream outputStream = new StreamSinkOutputStream(streamConnection.getSinkChannel()); - handler.handle(new UndertowHTTPSocket(httpServerExchange1, inputStream, outputStream)); - }); - httpServerExchange.putAttachment(WebSocketVersion.ATTACHMENT_KEY, WebSocketVersion.V13); - if(!WebSocketUtil.accept(new UndertowHTTPSocket(httpServerExchange, null, null), null)) - return; - httpServerExchange.endExchange(); - } else { - handler.handle(new UndertowHTTPSocket(httpServerExchange, null, null)); - } - })) - .build(); - server.start(); - } - - public void stop() { - executorService.shutdown(); - server.stop(); - } - - public void join() { - try { - server.getWorker().awaitTermination(); - } catch (InterruptedException e) {} - } - - public void setHandler(IHTTPSocketHandler handler) { - this.handler = handler; - } - - public void setMaxThreads(int maxThreads) { - this.maxThreads = maxThreads; - } - - public boolean isWebSocketSupported() { - return true; - } - -}