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
private void handleInstanceGET(HttpExchange httpExchange) throws IOException { Matcher matcher = Pattern.compile("/v2/instances/([^/]+)").matcher(httpExchange.getRequestURI().getPath()); if (matcher.matches()) { mapResponse(httpExchange, requestHandler.getInstance(matcher.group(1))); } else { httpExchange.sendResponseHeaders(HttpServletResponse.SC_NOT_FOUND, 0); } }
private void handleAppsDelete(HttpExchange httpExchange) throws IOException { EurekaHttpResponse<?> httpResponse; String path = httpExchange.getRequestURI().getPath(); Matcher matcher; if ((matcher = Pattern.compile("/v2/apps/([^/]+)/([^/]+)").matcher(path)).matches()) { httpResponse = requestHandler.cancel(matcher.group(1), matcher.group(2)); } else if ((matcher = Pattern.compile("/v2/apps/([^/]+)/([^/]+)/status").matcher(path)).matches()) { httpResponse = requestHandler.deleteStatusOverride(matcher.group(1), matcher.group(2), null); } else { httpExchange.sendResponseHeaders(HttpServletResponse.SC_NOT_FOUND, 0); return; } mapResponse(httpExchange, httpResponse); }
@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
private void handleSecureVipsGET(HttpExchange httpExchange) throws IOException { Matcher matcher = Pattern.compile("/v2/svips/([^/]+)").matcher(httpExchange.getRequestURI().getPath()); if (matcher.matches()) { String regions = getQueryParam(httpExchange, "regions"); EurekaHttpResponse<Applications> httpResponse = regions == null ? requestHandler.getSecureVip(matcher.group(1)) : requestHandler.getSecureVip(matcher.group(1), regions); mapResponse(httpExchange, httpResponse); } else { httpExchange.sendResponseHeaders(HttpServletResponse.SC_NOT_FOUND, 0); } }
private void handleVipsGET(HttpExchange httpExchange) throws IOException { Matcher matcher = Pattern.compile("/v2/vips/([^/]+)").matcher(httpExchange.getRequestURI().getPath()); if (matcher.matches()) { String regions = getQueryParam(httpExchange, "regions"); EurekaHttpResponse<Applications> httpResponse = regions == null ? requestHandler.getVip(matcher.group(1)) : requestHandler.getVip(matcher.group(1), regions); mapResponse(httpExchange, httpResponse); } else { httpExchange.sendResponseHeaders(HttpServletResponse.SC_NOT_FOUND, 0); } }
/** * 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()); }
@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
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(); }
/** * 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(); }
private void handleAppsPost(HttpExchange httpExchange) throws IOException { EurekaHttpResponse<?> httpResponse; String path = httpExchange.getRequestURI().getPath(); if (path.matches("/v2/apps/([^/]+)(/)?")) { InstanceInfo instance = decoder.decode(httpExchange.getRequestBody(), InstanceInfo.class); httpResponse = requestHandler.register(instance); } else { httpExchange.sendResponseHeaders(HttpServletResponse.SC_NOT_FOUND, 0); return; } mapResponse(httpExchange, httpResponse); }
public void handle(HttpExchange t) throws IOException { String response = "This is the response"; t.sendResponseHeaders(200, response.length()); OutputStream os = t.getResponseBody(); os.write(response.getBytes());
@Override public void failure(final Throwable error) { try { exchange.sendResponseHeaders(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), getResponseLength(0)); } catch (final IOException e) { LOGGER.log(Level.WARNING, LocalizationMessages.ERROR_RESPONSEWRITER_SENDING_FAILURE_RESPONSE(), e); } finally { commit(); rethrow(error); } }
/** * 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); } }
/** * 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(); } }
/** * 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()); }