private static void setHttpExchangeResponseHeaders(HttpExchange httpExchange) { // Set common response headers httpExchange.getResponseHeaders().add("Access-Control-Allow-Origin", "*"); httpExchange.getResponseHeaders().add("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS"); httpExchange.getResponseHeaders().add("Access-Control-Allow-Headers", "*"); httpExchange.getResponseHeaders().add("Access-Control-Allow-Credentials", "true"); httpExchange.getResponseHeaders().add("Access-Control-Allow-Credentials-Header", "*"); }
public void shutdown() { httpServer.stop(0); }
/** * Write the given RemoteInvocationResult to the given HTTP response. * @param exchange current HTTP request/response * @param result the RemoteInvocationResult object * @throws java.io.IOException in case of I/O failure */ protected void writeRemoteInvocationResult(HttpExchange exchange, RemoteInvocationResult result) throws IOException { exchange.getResponseHeaders().set("Content-Type", getContentType()); exchange.sendResponseHeaders(200, 0); writeRemoteInvocationResult(exchange, result, exchange.getResponseBody()); }
private static void sendAndGetResponse(HttpExchange httpExchange, byte[] response) throws IOException { if (response.length > 0) { httpExchange.getResponseHeaders().add("Content-type", "application/json"); httpExchange.getResponseHeaders().add("Content-length", Integer.toString(response.length)); httpExchange.sendResponseHeaders(HTTP_OK, response.length); httpExchange.getResponseBody().write(response); httpExchange.close(); } }
private EurekaHttpRequest mapToEurekaHttpRequest(HttpExchange httpExchange) { Headers exchangeHeaders = httpExchange.getRequestHeaders(); Map<String, String> headers = new HashMap<>(); for(String key: exchangeHeaders.keySet()) { headers.put(key, exchangeHeaders.getFirst(key)); } return new EurekaHttpRequest(httpExchange.getRequestMethod(), httpExchange.getRequestURI(), headers); }
/** * Build the HttpContext for the given endpoint. * @param endpoint the JAX-WS Provider Endpoint object * @param serviceName the given service name * @return the fully populated HttpContext */ protected HttpContext buildHttpContext(Endpoint endpoint, String serviceName) { Assert.state(this.server != null, "No HttpServer available"); String fullPath = calculateEndpointPath(endpoint, serviceName); HttpContext httpContext = this.server.createContext(fullPath); if (this.filters != null) { httpContext.getFilters().addAll(this.filters); } if (this.authenticator != null) { httpContext.setAuthenticator(this.authenticator); } return httpContext; }
@Override public void run() { server.start(); } }).start();
/** * Read a RemoteInvocation from the given HTTP request. * <p>Delegates to {@link #readRemoteInvocation(HttpExchange, InputStream)} * with the {@link HttpExchange#getRequestBody()} request's input stream}. * @param exchange current HTTP request/response * @return the RemoteInvocation object * @throws java.io.IOException in case of I/O failure * @throws ClassNotFoundException if thrown by deserialization */ protected RemoteInvocation readRemoteInvocation(HttpExchange exchange) throws IOException, ClassNotFoundException { return readRemoteInvocation(exchange, exchange.getRequestBody()); }
@Override public InetSocketAddress getAddress() { return delegate.getAddress(); } };
@Override public HttpContext createContext(final String s, final HttpHandler httpHandler) { return delegate.createContext(s, httpHandler); }
@Override public InetSocketAddress getAddress() { return delegate.getAddress(); } };
@Override public void removeContext(final String s) throws IllegalArgumentException { delegate.removeContext(s); }
@Override public void handle(HttpExchange httpExchange) throws IOException { // Return a simple text message that says pong. httpExchange.getResponseHeaders().set("Content-type", "text/plain"); String response = "pong\n"; httpExchange.sendResponseHeaders(HTTP_OK, response.getBytes().length); httpExchange.getResponseBody().write(response.getBytes()); httpExchange.close(); } }
@Override public void run() { server.stop(0); } }));
@Override public HttpContext createContext(final String s) { return delegate.createContext(s); }
@Override public void handle(HttpExchange httpExchange) throws IOException { // Return a simple text message that says pong. httpExchange.getResponseHeaders().set("Content-type", "text/plain"); String response = "live\n"; httpExchange.sendResponseHeaders(HTTP_OK, response.getBytes().length); httpExchange.getResponseBody().write(response.getBytes()); httpExchange.close(); } } // end static class LiveHandler
@Override public void destroy() { logger.info("Stopping HttpServer"); this.server.stop(this.shutdownDelay); }
/** * Write the given RemoteInvocationResult to the given HTTP response. * @param exchange current HTTP request/response * @param result the RemoteInvocationResult object * @throws java.io.IOException in case of I/O failure */ protected void writeRemoteInvocationResult(HttpExchange exchange, RemoteInvocationResult result) throws IOException { exchange.getResponseHeaders().set("Content-Type", getContentType()); exchange.sendResponseHeaders(200, 0); writeRemoteInvocationResult(exchange, result, exchange.getResponseBody()); }
@Override public void destroy() { logger.info("Stopping HttpServer"); this.server.stop(this.shutdownDelay); }
@Override public void destroy() { super.destroy(); if (this.server != null && this.localServer) { logger.info("Stopping HttpServer"); this.server.stop(this.shutdownDelay); } }