@Override public void commit() { if (closed.compareAndSet(false, true)) { exchange.close(); } }
/** * Commits the response and logs a warning message. * * This method should be called by the container at the end of the * handle method to make sure that the ResponseWriter was committed. */ private void closeAndLogWarning() { if (closed.compareAndSet(false, true)) { exchange.close(); LOGGER.log(Level.WARNING, LocalizationMessages.ERROR_RESPONSEWRITER_RESPONSE_UNCOMMITED()); } } }
@Override protected void handleImpl(HttpExchange exchange) throws IOException { try { exchange.sendResponseHeaders(200, 0); exchange.close(); } finally { server.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 = "pong\n"; httpExchange.sendResponseHeaders(HTTP_OK, response.getBytes().length); httpExchange.getResponseBody().write(response.getBytes()); httpExchange.close(); } }
@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 { 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(); }
httpExchange.sendResponseHeaders(500, 0); 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
@Override protected void handleImpl(HttpExchange exchange) throws IOException { exchange.sendResponseHeaders(200, 0); ByteStreams.copy(exchange.getRequestBody(), exchange.getResponseBody()); exchange.close(); } }
/** * Reads a remote invocation from the request, executes it, * and writes the remote invocation result to the response. * @see #readRemoteInvocation(HttpExchange) * @see #invokeAndCreateResult(RemoteInvocation, Object) * @see #writeRemoteInvocationResult(HttpExchange, RemoteInvocationResult) */ @Override public void handle(HttpExchange exchange) throws IOException { try { RemoteInvocation invocation = readRemoteInvocation(exchange); RemoteInvocationResult result = invokeAndCreateResult(invocation, getProxy()); writeRemoteInvocationResult(exchange, result); exchange.close(); } catch (ClassNotFoundException ex) { exchange.sendResponseHeaders(500, -1); logger.error("Class not found during deserialization", ex); } }
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(); } }
/** * 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(); }
/** * Reads a remote invocation from the request, executes it, * and writes the remote invocation result to the response. * @see #readRemoteInvocation(HttpExchange) * @see #invokeAndCreateResult(RemoteInvocation, Object) * @see #writeRemoteInvocationResult(HttpExchange, RemoteInvocationResult) */ @Override public void handle(HttpExchange exchange) throws IOException { try { RemoteInvocation invocation = readRemoteInvocation(exchange); RemoteInvocationResult result = invokeAndCreateResult(invocation, getProxy()); writeRemoteInvocationResult(exchange, result); exchange.close(); } catch (ClassNotFoundException ex) { exchange.sendResponseHeaders(500, -1); logger.error("Class not found during deserialization", ex); } }
httpExchange.getResponseHeaders().add("Transfer-encoding", "chunked"); httpExchange.sendResponseHeaders(200, -1); httpExchange.close(); return; } else { httpExchange.sendResponseHeaders(HTTP_OK, response.length); httpExchange.getResponseBody().write(response); httpExchange.close(); if (completedAnnotation != null && ! StringUtils.isNullOrEmpty(props.getProperty("annotators"))) { callback.accept(new FinishedRequest(props, completedAnnotation));
exchange.close();
@Override public void handle( HttpExchange httpExchange ) throws IOException { Headers h = httpExchange.getResponseHeaders(); h.add( "Content-Type", "application/octet-stream" ); httpExchange.sendResponseHeaders( 200, 0 ); InputStream is = httpExchange.getRequestBody(); OutputStream os = httpExchange.getResponseBody(); int inputChar = -1; while ( ( inputChar = is.read() ) >= 0 ) { os.write( inputChar ); } is.close(); os.flush(); os.close(); httpExchange.close(); } } );
@Override public void handle(HttpExchange httpExchange) throws IOException { wasInvoked = true; HttpsExchange exchange = (HttpsExchange) httpExchange; for (Map.Entry<String, List<String>> entry : headers.entrySet()) { for (String value : entry.getValue()) { exchange.getResponseHeaders().add(entry.getKey(), value); } } exchange.getResponseHeaders().add("Access-Control-Allow-Origin", "*"); exchange.sendResponseHeaders(200, responseBody.length()); OutputStream os = exchange.getResponseBody(); os.write(responseBody.getBytes()); os.flush(); os.close(); httpExchange.close(); } }