void sendResponse(HttpExchange t, int retCode, byte[] data) throws IOException { if (t != null) { t.sendResponseHeaders(retCode, data.length); OutputStream os = t.getResponseBody(); os.write(data); os.close(); } } }// LogHandler
@Override public void handle(HttpExchange t) throws IOException { try { TimeUnit.MILLISECONDS.sleep(100); } catch (InterruptedException e) { // ignore } String response = "This is the response"; t.sendResponseHeaders(200, response.length()); OutputStream os = t.getResponseBody(); os.write(response.getBytes()); os.close(); } });
@Override public void handle(HttpExchange httpExchange) throws IOException { httpExchange.getResponseHeaders().set("Content-type", this.contentType); ByteBuffer buffer = Charset.forName("UTF-8").encode(content); byte[] bytes = new byte[buffer.remaining()]; buffer.get(bytes); httpExchange.sendResponseHeaders(HTTP_OK, bytes.length); httpExchange.getResponseBody().write(bytes); httpExchange.close(); } } // end static class FileHandler
@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 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(); } }
private void handleShowLog(HttpExchange t, Map<String, String> paramMap) throws IOException { Pair<Long, byte[]> logPair = queryLog(t, paramMap); if (logPair == null) { return; } String size = String.format(HttpserverUtils.HTTPSERVER_LOGVIEW_PARAM_SIZE_FORMAT, logPair.getFirst()); byte[] sizeBytes = size.getBytes(); byte[] logData = logPair.getSecond(); t.sendResponseHeaders(HttpURLConnection.HTTP_OK, sizeBytes.length + logData.length); OutputStream os = t.getResponseBody(); os.write(sizeBytes); os.write(logData); os.close(); }
@Override public void handle(HttpExchange httpExchange) throws IOException { Map<String, String> urlParams = getURLParams(httpExchange.getRequestURI()); httpExchange.getResponseHeaders().set("Content-type", "text/plain"); boolean doExit = false; String response = "Invalid shutdown key\n"; if (urlParams.containsKey("key") && urlParams.get("key").equals(shutdownKey)) { response = "Shutdown successful!\n"; doExit = true; } httpExchange.sendResponseHeaders(HTTP_OK, response.getBytes().length); httpExchange.getResponseBody().write(response.getBytes()); httpExchange.close(); if (doExit) { System.exit(0); } } } // end static class ShutdownHandler
/** * A helper function to respond to a request with an error. * * @param response The description of the error to send to the user. * @param httpExchange The exchange to send the error over. * * @throws IOException Thrown if the HttpExchange cannot communicate the error. */ private static void respondError(String response, HttpExchange httpExchange) throws IOException { httpExchange.getResponseHeaders().add("Content-type", "text/plain"); httpExchange.sendResponseHeaders(HTTP_INTERNAL_ERROR, response.length()); httpExchange.getResponseBody().write(response.getBytes()); httpExchange.close(); }
/** * A helper function to respond to a request with an error specifically indicating * bad input from the user. * * @param response The description of the error to send to the user. * @param httpExchange The exchange to send the error over. * * @throws IOException Thrown if the HttpExchange cannot communicate the error. */ private static void respondBadInput(String response, HttpExchange httpExchange) throws IOException { httpExchange.getResponseHeaders().add("Content-type", "text/plain"); httpExchange.sendResponseHeaders(HTTP_BAD_REQUEST, response.length()); httpExchange.getResponseBody().write(response.getBytes()); httpExchange.close(); }
/** {@inheritDoc} */ @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; int status; if (this.serverReady.get()) { response = "ready\n"; status = HTTP_OK; } else { response = "server is not ready yet. uptime=" + Redwood.formatTimeDifference(System.currentTimeMillis() - this.startTime) + '\n'; status = HTTP_UNAVAILABLE; } httpExchange.sendResponseHeaders(status, response.getBytes().length); httpExchange.getResponseBody().write(response.getBytes()); httpExchange.close(); } } // end static class ReadyHandler
String response = "This is the response"; t.sendResponseHeaders(200, response.length()); OutputStream os = t.getResponseBody(); os.write(response.getBytes()); os.close();
@Override protected void handleImpl(HttpExchange exchange) throws IOException { exchange.sendResponseHeaders(200, 0); ByteStreams.copy(exchange.getRequestBody(), exchange.getResponseBody()); exchange.close(); } }
/** * 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(); } }
/** * Processes the incoming Hessian request and creates a Hessian response. */ @Override public void handle(HttpExchange exchange) throws IOException { if (!"POST".equals(exchange.getRequestMethod())) { exchange.getResponseHeaders().set("Allow", "POST"); exchange.sendResponseHeaders(405, -1); return; } ByteArrayOutputStream output = new ByteArrayOutputStream(1024); try { invoke(exchange.getRequestBody(), output); } catch (Throwable ex) { exchange.sendResponseHeaders(500, -1); logger.error("Hessian skeleton invocation failed", ex); return; } exchange.getResponseHeaders().set("Content-Type", CONTENT_TYPE_HESSIAN); exchange.sendResponseHeaders(200, output.size()); FileCopyUtils.copy(output.toByteArray(), exchange.getResponseBody()); }
public void sendResponse(HttpExchange httpExchange, int code, String response) throws IOException { httpExchange.sendResponseHeaders(code, response.length()); OutputStream os = httpExchange.getResponseBody(); os.write(response.getBytes()); os.close(); } }
private static void writeResponse(HttpExchange exchange, Response response) { exchange.getResponseHeaders().set("Content-type", "application/json; charset=UTF-8"); OutputStream outputStream = exchange.getResponseBody(); try { exchange.getResponseHeaders().add("Connection", "close"); exchange.sendResponseHeaders(response.code(), response.body().length()); outputStream.write(response.body().getBytes(StandardCharsets.UTF_8)); outputStream.close(); } catch (IOException e) { log.log(Level.WARNING, "IOException encountered when sending response.", e); } }
/** * A helper function to respond to a request with an error stating that the user is not authorized * to make this request. * * @param httpExchange The exchange to send the error over. * * @throws IOException Thrown if the HttpExchange cannot communicate the error. */ private static void respondUnauthorized(HttpExchange httpExchange) throws IOException { log("Responding unauthorized to " + httpExchange.getRemoteAddress()); httpExchange.getResponseHeaders().add("Content-type", "application/javascript"); byte[] content = "{\"message\": \"Unauthorized API request\"}".getBytes("utf-8"); httpExchange.sendResponseHeaders(HTTP_UNAUTHORIZED, content.length); httpExchange.getResponseBody().write(content); httpExchange.close(); }
/** * 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()); }
@BeforeClass public static void setUpClass() throws Exception { server = HttpServer.create(new InetSocketAddress(0), 0); server.createContext("/", exchange -> { byte[] content = "Hello World!".getBytes(); exchange.sendResponseHeaders(200, content.length); try (OutputStream responseBody = exchange.getResponseBody()) { responseBody.write(content); responseBody.flush(); } }); server.start(); Testcontainers.exposeHostPorts(server.getAddress().getPort()); }