|
| 1 | +package org.javawebstack.httpserver.adapter.untertow; |
| 2 | + |
| 3 | +import io.undertow.server.HttpServerExchange; |
| 4 | +import io.undertow.util.HeaderValues; |
| 5 | +import io.undertow.util.HttpString; |
| 6 | +import org.javawebstack.httpserver.HTTPMethod; |
| 7 | +import org.javawebstack.httpserver.HTTPStatus; |
| 8 | +import org.javawebstack.httpserver.adapter.IHTTPSocket; |
| 9 | + |
| 10 | +import java.io.IOException; |
| 11 | +import java.io.InputStream; |
| 12 | +import java.io.OutputStream; |
| 13 | +import java.util.*; |
| 14 | +import java.util.stream.Collectors; |
| 15 | + |
| 16 | +public class UndertowHTTPSocket implements IHTTPSocket { |
| 17 | + |
| 18 | + private final HttpServerExchange exchange; |
| 19 | + |
| 20 | + public UndertowHTTPSocket(HttpServerExchange exchange) { |
| 21 | + this.exchange = exchange; |
| 22 | + } |
| 23 | + |
| 24 | + public InputStream getInputStream() throws IOException { |
| 25 | + return exchange.getInputStream(); |
| 26 | + } |
| 27 | + |
| 28 | + public OutputStream getOutputStream() throws IOException { |
| 29 | + return exchange.getOutputStream(); |
| 30 | + } |
| 31 | + |
| 32 | + public void close() throws IOException { |
| 33 | + exchange.endExchange(); |
| 34 | + } |
| 35 | + |
| 36 | + public boolean isClosed() { |
| 37 | + return exchange.isComplete(); |
| 38 | + } |
| 39 | + |
| 40 | + public IHTTPSocket setResponseStatus(int status, String message) { |
| 41 | + exchange.setStatusCode(status); |
| 42 | + return this; |
| 43 | + } |
| 44 | + |
| 45 | + public IHTTPSocket setResponseHeader(String name, String value) { |
| 46 | + exchange.getResponseHeaders().put(new HttpString(name), value); |
| 47 | + return this; |
| 48 | + } |
| 49 | + |
| 50 | + public IHTTPSocket addResponseHeader(String name, String value) { |
| 51 | + exchange.getResponseHeaders().add(new HttpString(name), value); |
| 52 | + return this; |
| 53 | + } |
| 54 | + |
| 55 | + public HTTPMethod getRequestMethod() { |
| 56 | + return HTTPMethod.valueOf(exchange.getRequestMethod().toString()); |
| 57 | + } |
| 58 | + |
| 59 | + public String getRequestPath() { |
| 60 | + return exchange.getRequestPath(); |
| 61 | + } |
| 62 | + |
| 63 | + public String getRequestQuery() { |
| 64 | + return exchange.getQueryString(); |
| 65 | + } |
| 66 | + |
| 67 | + public String getRequestVersion() { |
| 68 | + return exchange.getProtocol().toString(); |
| 69 | + } |
| 70 | + |
| 71 | + public Set<String> getRequestHeaderNames() { |
| 72 | + return exchange.getRequestHeaders().getHeaderNames().stream().map(HttpString::toString).collect(Collectors.toSet()); |
| 73 | + } |
| 74 | + |
| 75 | + public String getRequestHeader(String name) { |
| 76 | + HeaderValues values = exchange.getRequestHeaders().get(name); |
| 77 | + if(values == null) |
| 78 | + return null; |
| 79 | + return values.getFirst(); |
| 80 | + } |
| 81 | + |
| 82 | + public List<String> getRequestHeaders(String name) { |
| 83 | + HeaderValues values = exchange.getRequestHeaders().get(name); |
| 84 | + if(values == null) |
| 85 | + return Collections.emptyList(); |
| 86 | + return new ArrayList<>(values); |
| 87 | + } |
| 88 | + |
| 89 | + public int getResponseStatus() { |
| 90 | + return exchange.getStatusCode(); |
| 91 | + } |
| 92 | + |
| 93 | + public String getResponseStatusMessage() { |
| 94 | + HTTPStatus status = HTTPStatus.byStatus(getResponseStatus()); |
| 95 | + if(status == null) |
| 96 | + return null; |
| 97 | + return status.getMessage(); |
| 98 | + } |
| 99 | + |
| 100 | + public void writeHeaders() throws IOException { |
| 101 | + exchange.getResponseSender().send(""); |
| 102 | + } |
| 103 | + |
| 104 | + public String getRemoteAddress() { |
| 105 | + return exchange.getSourceAddress().getAddress().getHostAddress(); |
| 106 | + } |
| 107 | + |
| 108 | + public HttpServerExchange getExchange() { |
| 109 | + return exchange; |
| 110 | + } |
| 111 | + |
| 112 | +} |
0 commit comments