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", "*"); }
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. * * @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 <T> void mapResponse(HttpExchange httpExchange, EurekaHttpResponse<T> response) throws IOException { // Add headers for (Map.Entry<String, String> headerEntry : response.getHeaders().entrySet()) { httpExchange.getResponseHeaders().add(headerEntry.getKey(), headerEntry.getValue()); } if (response.getStatusCode() / 100 != 2) { httpExchange.sendResponseHeaders(response.getStatusCode(), 0); return; } // Prepare body, if any T entity = response.getEntity(); byte[] body = null; if (entity != null) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); encoder.encode(entity, bos); body = bos.toByteArray(); } // Set status and body length httpExchange.sendResponseHeaders(response.getStatusCode(), body == null ? 0 : body.length); // Send body if (body != null) { OutputStream responseStream = httpExchange.getResponseBody(); try { responseStream.write(body); responseStream.flush(); } finally { responseStream.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(); }
httpExchange.getResponseHeaders().add("Transfer-encoding", "chunked"); httpExchange.sendResponseHeaders(200, -1); httpExchange.close(); contentType += ";charset=" + options.encoding; httpExchange.getResponseHeaders().add("Content-type", contentType); httpExchange.getResponseHeaders().add("Content-length", Integer.toString(response.length)); httpExchange.sendResponseHeaders(HTTP_OK, response.length); httpExchange.getResponseBody().write(response);
@Override public OutputStream writeResponseStatusAndHeaders(final long contentLength, final ContainerResponse context) throws ContainerException { final MultivaluedMap<String, String> responseHeaders = context.getStringHeaders(); final Headers serverHeaders = exchange.getResponseHeaders(); for (final Map.Entry<String, List<String>> e : responseHeaders.entrySet()) { for (final String value : e.getValue()) { serverHeaders.add(e.getKey(), value); } } try { if (context.getStatus() == Response.Status.NO_CONTENT.getStatusCode()) { // Work around bug in LW HTTP server // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6886436 exchange.sendResponseHeaders(context.getStatus(), -1); } else { exchange.sendResponseHeaders(context.getStatus(), getResponseLength(contentLength)); } } catch (final IOException ioe) { throw new ContainerException(LocalizationMessages.ERROR_RESPONSEWRITER_WRITING_HEADERS(), ioe); } return exchange.getResponseBody(); }
@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(); } }
@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(); } } );
protected void addHeader(String name, Object value) { @SuppressWarnings(value = "unchecked") RuntimeDelegate.HeaderDelegate<Object> delegate = factory.getHeaderDelegate(value.getClass()); if (delegate != null) { //System.out.println("addResponseHeader: " + key + " " + delegate.toString(value)); exchange.getResponseHeaders().add(name, delegate.toString(value)); } else { //System.out.println("addResponseHeader: " + key + " " + value.toString()); exchange.getResponseHeaders().add(name, value.toString()); } }
private Result prepareFailure(HttpExchange pHttpExchange, String pErrorDetails, int pCode) { pHttpExchange.getResponseHeaders().add("X-Error-Details", pErrorDetails); return new Failure(pCode); }
@Override public void handle(HttpExchange exch) throws IOException { String response; int status; switch (exch.getRequestURI().getPath()) { case "/valid": response = "A test response"; status = 200; break; case "/delayed": waitFor(100); response = "A delayed test response"; status = 200; break; case "/redirect": response = "A redirect"; status = 301; exch.getResponseHeaders().add("Location", format("http://localhost:%s/valid", serverPort)); break; default: response = ""; status = 404; } exch.sendResponseHeaders(status, response.length()); OutputStream os = exch.getResponseBody(); os.write(response.getBytes()); os.close(); }
private void addCorsHeaders(HttpExchange httpExchange) { Headers headers = httpExchange.getResponseHeaders(); headers.add("Access-Control-Allow-Origin", "*"); headers.add("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT"); headers.add("Access-Control-Allow-Headers", "X-Requested-With,Content-Type"); headers.add("Access-Control-Max-Age", "86400"); headers.add("Access-Control-Allow-Credentials", "true"); }
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", "*"); }
@Override public void addResponseHeader(String name, String value) { delegate.getResponseHeaders().add(name, value); }
/** * Adds the specified cookie to the response. This method can be called multiple times to set more than one cookie. * @param name a String specifying the cookie name. * @param value a String specifying the cookie value. */ public void addCookie(final String name, final String value) { //String data = name + "=" + URLEncode(value) + "; path=/"; String data = name + "=" + value + "; path=/"; _exchange.getResponseHeaders().add("Set-Cookie", data); } }
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(); } }
@Override public void handle(HttpHeaderAttribute attribute) { Headers responseHeaders = m_exchange.getResponseHeaders(); boolean headerAlreadyPresent = responseHeaders.getFirst(attribute.getName()) != null; if (headerAlreadyPresent) { return; } responseHeaders.add(attribute.getName(), (String) attribute.getValue()); } }