@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(); } }
@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
/** * 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()); }
/** {@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
/** * 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()); }
/** * 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()); }
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); } }
/** * 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()); }
if (requestMethod.equalsIgnoreCase("GET")) { Headers responseHeaders = exchange.getResponseHeaders(); responseHeaders.set("Content-Type", "text/plain"); exchange.sendResponseHeaders(200, 0);
/** * Handles HTTP requests: checks that a request is a HTTP GET and that url path is the expected one. * If all checks are passed it writes pre-configured file content to the HTTP response body. * * @param exchange Wrapper above the HTTP request and response. */ @Override public void handle(HttpExchange exchange) throws IOException { assert "GET".equalsIgnoreCase(exchange.getRequestMethod()); assert urlPath == null || urlPath.equals(exchange.getRequestURI().toString()); exchange.getResponseHeaders().set("Content-Type", "application/octet-stream"); exchange.sendResponseHeaders(200, 0); try (OutputStream resBody = exchange.getResponseBody()) { resBody.write(GridTestUtils.readFile(downloadFile)); } } }
rh.set("Content-Type", "application/ocsp-response"); httpExchange.sendResponseHeaders(200, responseBytes.length);
private void sendResponse(HttpExchange t, String contentType, String response) throws IOException { Headers hds = t.getResponseHeaders(); hds.set("Content-Type", contentType); t.sendResponseHeaders(200, response.length()); OutputStream os = t.getResponseBody(); try { os.write(response.getBytes()); } finally { os.close(); } }
private void handleClientStart(HttpExchange t, ConsoleProxyClient viewer, String title, String guest) throws IOException { List<String> languages = t.getRequestHeaders().get("Accept-Language"); String response = viewer.onAjaxClientStart(title, languages, guest); Headers hds = t.getResponseHeaders(); hds.set("Content-Type", "text/html"); hds.set("Cache-Control", "no-cache"); hds.set("Cache-Control", "no-store"); t.sendResponseHeaders(200, response.length()); OutputStream os = t.getResponseBody(); try { os.write(response.getBytes()); } finally { os.close(); } }
private void handleClientUpdate(HttpExchange t, ConsoleProxyClient viewer) throws IOException { String response = viewer.onAjaxClientUpdate(); Headers hds = t.getResponseHeaders(); hds.set("Content-Type", "text/javascript"); t.sendResponseHeaders(200, response.length()); OutputStream os = t.getResponseBody(); try { os.write(response.getBytes()); } finally { os.close(); } } }
@Override public void handle(final HttpExchange httpExchange) throws IOException { final String remoteClientAddress = httpExchange.getRemoteAddress().getAddress().toString().replace("/", ""); LOG.debug("Prometheus exporter received client request from: " + remoteClientAddress); String response = "Forbidden"; int responseCode = 403; if (Arrays.asList(PrometheusExporterAllowedAddresses.value().split(",")).contains(remoteClientAddress)) { prometheusExporter.updateMetrics(); response = prometheusExporter.getMetrics(); responseCode = 200; } httpExchange.getResponseHeaders().set("Content-Type", "text/plain"); httpExchange.sendResponseHeaders(responseCode, response.length()); final OutputStream os = httpExchange.getResponseBody(); os.write(response.getBytes()); os.close(); } }
private void performCorsPreflightCheck(HttpExchange pExchange) { Headers requestHeaders = pExchange.getRequestHeaders(); Map<String,String> respHeaders = requestHandler.handleCorsPreflightRequest(extractOriginOrReferer(pExchange), requestHeaders.getFirst("Access-Control-Request-Headers")); Headers responseHeaders = pExchange.getResponseHeaders(); for (Map.Entry<String,String> entry : respHeaders.entrySet()) { responseHeaders.set(entry.getKey(), entry.getValue()); } }
public void doHandle(HttpExchange t) throws Exception { String path = t.getRequestURI().getPath(); int i = path.indexOf("/", 1); String cmd = path.substring(i + 1); s_logger.info("Get CMD request for " + cmd); if (cmd.equals("getstatus")) { ConsoleProxyClientStatsCollector statsCollector = ConsoleProxy.getStatsCollector(); Headers hds = t.getResponseHeaders(); hds.set("Content-Type", "text/plain"); t.sendResponseHeaders(200, 0); OutputStreamWriter os = new OutputStreamWriter(t.getResponseBody(),"UTF-8"); statsCollector.getStatsReport(os); os.close(); } } }
private void sendStreamingResponse(HttpExchange pExchange, ParsedUri pParsedUri, JSONStreamAware pJson) throws IOException { Headers headers = pExchange.getResponseHeaders(); if (pJson != null) { headers.set("Content-Type", getMimeType(pParsedUri) + "; charset=utf-8"); pExchange.sendResponseHeaders(200, 0); Writer writer = new OutputStreamWriter(pExchange.getResponseBody(), "UTF-8"); String callback = pParsedUri.getParameter(ConfigKey.CALLBACK.getKeyValue()); IoUtil.streamResponseAndClose(writer, pJson, callback != null && MimeTypeUtil.isValidCallback(callback) ? callback : null); } else { headers.set("Content-Type", "text/plain"); pExchange.sendResponseHeaders(200,-1); } }