Java Code Examples for com.sun.net.httpserver.HttpExchange#getRequestHeaders()
The following examples show how to use
com.sun.net.httpserver.HttpExchange#getRequestHeaders() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Deadlock.java From dragonwell8_jdk with GNU General Public License v2.0 | 6 votes |
@Override public void handle (HttpExchange t) throws IOException { InputStream is = t.getRequestBody(); Headers map = t.getRequestHeaders(); Headers rmap = t.getResponseHeaders(); while (is.read() != -1); is.close(); t.sendResponseHeaders(200, -1); HttpPrincipal p = t.getPrincipal(); if (!p.getUsername().equals("fred")) { error = true; } if (!p.getRealm().equals("[email protected]")) { error = true; } t.close(); }
Example 2
Source File: FileUploadDownloadClientTest.java From incubator-pinot with Apache License 2.0 | 6 votes |
@Override public void handle(HttpExchange httpExchange) throws IOException { Headers requestHeaders = httpExchange.getRequestHeaders(); String uploadTypeStr = requestHeaders.getFirst(FileUploadDownloadClient.CustomHeaders.UPLOAD_TYPE); FileUploadType uploadType = FileUploadType.valueOf(uploadTypeStr); String downloadUri = null; if (uploadType == FileUploadType.JSON) { InputStream bodyStream = httpExchange.getRequestBody(); downloadUri = JsonUtils.stringToJsonNode(IOUtils.toString(bodyStream, "UTF-8")) .get(CommonConstants.Segment.Offline.DOWNLOAD_URL).asText(); } else if (uploadType == FileUploadType.URI) { downloadUri = requestHeaders.getFirst(FileUploadDownloadClient.CustomHeaders.DOWNLOAD_URI); String crypter = requestHeaders.getFirst(FileUploadDownloadClient.CustomHeaders.CRYPTER); Assert.assertEquals(crypter, TEST_CRYPTER); } else { Assert.fail(); } Assert.assertEquals(downloadUri, TEST_URI); sendResponse(httpExchange, HttpStatus.SC_OK, "OK"); }
Example 3
Source File: Deadlock.java From jdk8u_jdk with GNU General Public License v2.0 | 6 votes |
@Override public void handle (HttpExchange t) throws IOException { InputStream is = t.getRequestBody(); Headers map = t.getRequestHeaders(); Headers rmap = t.getResponseHeaders(); while (is.read() != -1); is.close(); t.sendResponseHeaders(200, -1); HttpPrincipal p = t.getPrincipal(); if (!p.getUsername().equals("fred")) { error = true; } if (!p.getRealm().equals("[email protected]")) { error = true; } t.close(); }
Example 4
Source File: Deadlock.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
@Override public void handle (HttpExchange t) throws IOException { InputStream is = t.getRequestBody(); Headers map = t.getRequestHeaders(); Headers rmap = t.getResponseHeaders(); while (is.read() != -1); is.close(); t.sendResponseHeaders(200, -1); HttpPrincipal p = t.getPrincipal(); if (!p.getUsername().equals("fred")) { error = true; } if (!p.getRealm().equals("[email protected]")) { error = true; } t.close(); }
Example 5
Source File: Deadlock.java From openjdk-jdk8u with GNU General Public License v2.0 | 6 votes |
@Override public void handle (HttpExchange t) throws IOException { InputStream is = t.getRequestBody(); Headers map = t.getRequestHeaders(); Headers rmap = t.getResponseHeaders(); while (is.read() != -1); is.close(); t.sendResponseHeaders(200, -1); HttpPrincipal p = t.getPrincipal(); if (!p.getUsername().equals("fred")) { error = true; } if (!p.getRealm().equals("[email protected]")) { error = true; } t.close(); }
Example 6
Source File: Deadlock.java From jdk8u-jdk with GNU General Public License v2.0 | 6 votes |
@Override public void handle (HttpExchange t) throws IOException { InputStream is = t.getRequestBody(); Headers map = t.getRequestHeaders(); Headers rmap = t.getResponseHeaders(); while (is.read() != -1); is.close(); t.sendResponseHeaders(200, -1); HttpPrincipal p = t.getPrincipal(); if (!p.getUsername().equals("fred")) { error = true; } if (!p.getRealm().equals("[email protected]")) { error = true; } t.close(); }
Example 7
Source File: Deadlock.java From openjdk-8 with GNU General Public License v2.0 | 6 votes |
@Override public void handle (HttpExchange t) throws IOException { InputStream is = t.getRequestBody(); Headers map = t.getRequestHeaders(); Headers rmap = t.getResponseHeaders(); while (is.read() != -1); is.close(); t.sendResponseHeaders(200, -1); HttpPrincipal p = t.getPrincipal(); if (!p.getUsername().equals("fred")) { error = true; } if (!p.getRealm().equals("[email protected]")) { error = true; } t.close(); }
Example 8
Source File: Basic.java From hottub with GNU General Public License v2.0 | 5 votes |
public void handle(HttpExchange t) throws IOException { InputStream is = t.getRequestBody(); Headers map = t.getRequestHeaders(); Headers rmap = t.getResponseHeaders(); URI uri = t.getRequestURI(); debug("Server: received request for " + uri); String path = uri.getPath(); if (path.endsWith("a.jar")) aDotJar++; else if (path.endsWith("b.jar")) bDotJar++; else if (path.endsWith("c.jar")) cDotJar++; else System.out.println("Unexpected resource request" + path); while (is.read() != -1); is.close(); File file = new File(docsDir, path); if (!file.exists()) throw new RuntimeException("Error: request for " + file); long clen = file.length(); t.sendResponseHeaders (200, clen); OutputStream os = t.getResponseBody(); FileInputStream fis = new FileInputStream(file); try { byte[] buf = new byte [16 * 1024]; int len; while ((len=fis.read(buf)) != -1) { os.write (buf, 0, len); } } catch (IOException e) { e.printStackTrace(); } fis.close(); os.close(); }
Example 9
Source File: HttpOnly.java From openjdk-8 with GNU General Public License v2.0 | 5 votes |
@Override public void handle(HttpExchange t) throws IOException { Headers reqHeaders = t.getRequestHeaders(); // some small sanity check List<String> cookies = reqHeaders.get("Cookie"); for (String cookie : cookies) { if (!cookie.contains("JSESSIONID") || !cookie.contains("WILE_E_COYOTE")) t.sendResponseHeaders(400, -1); } // return some cookies so we can check getHeaderField(s) Headers respHeaders = t.getResponseHeaders(); List<String> values = new ArrayList<>(); values.add("ID=JOEBLOGGS; version=1; Path=" + URI_PATH); values.add("NEW_JSESSIONID=" + (SESSION_ID+1) + "; version=1; Path=" + URI_PATH +"; HttpOnly"); values.add("NEW_CUSTOMER=WILE_E_COYOTE2; version=1; Path=" + URI_PATH); respHeaders.put("Set-Cookie", values); values = new ArrayList<>(); values.add("COOKIE2_CUSTOMER=WILE_E_COYOTE2; version=1; Path=" + URI_PATH); respHeaders.put("Set-Cookie2", values); values.add("COOKIE2_JSESSIONID=" + (SESSION_ID+100) + "; version=1; Path=" + URI_PATH +"; HttpOnly"); respHeaders.put("Set-Cookie2", values); t.sendResponseHeaders(200, -1); t.close(); }
Example 10
Source File: B8185898.java From jdk8u_jdk with GNU General Public License v2.0 | 5 votes |
public void handle(HttpExchange t) throws IOException { InputStream is = t.getRequestBody(); InetSocketAddress rem = t.getRemoteAddress(); headers = t.getRequestHeaders(); // Get request headers on the server side while(is.read() != -1){} is.close(); OutputStream os = t.getResponseBody(); t.sendResponseHeaders(200, RESPONSE_BODY.length()); os.write(RESPONSE_BODY.getBytes(UTF_8)); t.close(); }
Example 11
Source File: HTTPTest.java From boon with Apache License 2.0 | 5 votes |
public void handle( HttpExchange t ) throws IOException { InputStream requestBody = t.getRequestBody(); String body = IO.read( requestBody ); Headers requestHeaders = t.getRequestHeaders(); body = body + "\n" + copy( requestHeaders ).toString(); t.sendResponseHeaders( 200, body.length() ); OutputStream os = t.getResponseBody(); os.write( body.getBytes() ); os.close(); }
Example 12
Source File: HttpOnly.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
@Override public void handle(HttpExchange t) throws IOException { Headers reqHeaders = t.getRequestHeaders(); // some small sanity check List<String> cookies = reqHeaders.get("Cookie"); for (String cookie : cookies) { if (!cookie.contains("JSESSIONID") || !cookie.contains("WILE_E_COYOTE")) t.sendResponseHeaders(400, -1); } // return some cookies so we can check getHeaderField(s) Headers respHeaders = t.getResponseHeaders(); List<String> values = new ArrayList<>(); values.add("ID=JOEBLOGGS; version=1; Path=" + URI_PATH); values.add("NEW_JSESSIONID=" + (SESSION_ID+1) + "; version=1; Path=" + URI_PATH +"; HttpOnly"); values.add("NEW_CUSTOMER=WILE_E_COYOTE2; version=1; Path=" + URI_PATH); respHeaders.put("Set-Cookie", values); values = new ArrayList<>(); values.add("COOKIE2_CUSTOMER=WILE_E_COYOTE2; version=1; Path=" + URI_PATH); respHeaders.put("Set-Cookie2", values); values.add("COOKIE2_JSESSIONID=" + (SESSION_ID+100) + "; version=1; Path=" + URI_PATH +"; HttpOnly"); respHeaders.put("Set-Cookie2", values); t.sendResponseHeaders(200, -1); t.close(); }
Example 13
Source File: SimpleHttpServer.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
void moved(HttpExchange t) throws IOException { Headers req = t.getRequestHeaders(); Headers map = t.getResponseHeaders(); URI uri = t.getRequestURI(); String host = req.getFirst("Host"); String location = "http://" + host + uri.getPath() + "/"; map.set("Content-Type", "text/html"); map.set("Location", location); t.sendResponseHeaders(301, -1); t.close(); }
Example 14
Source File: RefactoringMinerHttpServer.java From RefactoringMiner with MIT License | 5 votes |
private static void printRequestInfo(HttpExchange exchange) { System.out.println("-- headers --"); Headers requestHeaders = exchange.getRequestHeaders(); requestHeaders.entrySet().forEach(System.out::println); System.out.println("-- HTTP method --"); String requestMethod = exchange.getRequestMethod(); System.out.println(requestMethod); System.out.println("-- query --"); URI requestURI = exchange.getRequestURI(); String query = requestURI.getQuery(); System.out.println(query); }
Example 15
Source File: HttpOnly.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
@Override public void handle(HttpExchange t) throws IOException { Headers reqHeaders = t.getRequestHeaders(); // some small sanity check List<String> cookies = reqHeaders.get("Cookie"); for (String cookie : cookies) { if (!cookie.contains("JSESSIONID") || !cookie.contains("WILE_E_COYOTE")) t.sendResponseHeaders(400, -1); } // return some cookies so we can check getHeaderField(s) Headers respHeaders = t.getResponseHeaders(); List<String> values = new ArrayList<>(); values.add("ID=JOEBLOGGS; version=1; Path=" + URI_PATH); values.add("NEW_JSESSIONID=" + (SESSION_ID+1) + "; version=1; Path=" + URI_PATH +"; HttpOnly"); values.add("NEW_CUSTOMER=WILE_E_COYOTE2; version=1; Path=" + URI_PATH); respHeaders.put("Set-Cookie", values); values = new ArrayList<>(); values.add("COOKIE2_CUSTOMER=WILE_E_COYOTE2; version=1; Path=" + URI_PATH); respHeaders.put("Set-Cookie2", values); values.add("COOKIE2_JSESSIONID=" + (SESSION_ID+100) + "; version=1; Path=" + URI_PATH +"; HttpOnly"); respHeaders.put("Set-Cookie2", values); t.sendResponseHeaders(200, -1); t.close(); }
Example 16
Source File: Contexts.java From nano with Apache License 2.0 | 5 votes |
public static HttpHandler instantiate(Path scriptFile) { ScriptEngineManager sem = new ScriptEngineManager(); ScriptEngine engine = sem.getEngineByName("javascript"); try { engine.eval(new FileReader(scriptFile.toFile())); } catch (ScriptException | FileNotFoundException ex) { throw new IllegalStateException(ex); } Invocable invocable = (Invocable) engine; NanoRequest request = invocable.getInterface(NanoRequest.class); return (HttpExchange he) -> { final OutputStream responseBody = he.getResponseBody(); StringBuilder builder = new StringBuilder(); ResponseWriter writer = builder::append; final InputStream requestBody = he.getRequestBody(); String requestContent; try (BufferedReader buffer = new BufferedReader(new InputStreamReader(requestBody))) { requestContent = buffer.lines().collect(Collectors.joining("\n")); } Headers requestHeaders = he.getRequestHeaders(); Headers responseHeaders = he.getResponseHeaders(); int statusCode = request.process(he.getRequestMethod(), requestHeaders, responseHeaders, requestContent, writer); String content = builder.toString(); he.sendResponseHeaders(statusCode, content.length()); responseBody.write(content.getBytes()); responseBody.flush(); he.close(); }; }
Example 17
Source File: B8185898.java From dragonwell8_jdk with GNU General Public License v2.0 | 5 votes |
public void handle(HttpExchange t) throws IOException { InputStream is = t.getRequestBody(); InetSocketAddress rem = t.getRemoteAddress(); headers = t.getRequestHeaders(); // Get request headers on the server side while(is.read() != -1){} is.close(); OutputStream os = t.getResponseBody(); t.sendResponseHeaders(200, RESPONSE_BODY.length()); os.write(RESPONSE_BODY.getBytes(UTF_8)); t.close(); }
Example 18
Source File: HttpOnly.java From dragonwell8_jdk with GNU General Public License v2.0 | 5 votes |
@Override public void handle(HttpExchange t) throws IOException { Headers reqHeaders = t.getRequestHeaders(); // some small sanity check List<String> cookies = reqHeaders.get("Cookie"); for (String cookie : cookies) { if (!cookie.contains("JSESSIONID") || !cookie.contains("WILE_E_COYOTE")) t.sendResponseHeaders(400, -1); } // return some cookies so we can check getHeaderField(s) Headers respHeaders = t.getResponseHeaders(); List<String> values = new ArrayList<>(); values.add("ID=JOEBLOGGS; version=1; Path=" + URI_PATH); values.add("NEW_JSESSIONID=" + (SESSION_ID+1) + "; version=1; Path=" + URI_PATH +"; HttpOnly"); values.add("NEW_CUSTOMER=WILE_E_COYOTE2; version=1; Path=" + URI_PATH); respHeaders.put("Set-Cookie", values); values = new ArrayList<>(); values.add("COOKIE2_CUSTOMER=WILE_E_COYOTE2; version=1; Path=" + URI_PATH); respHeaders.put("Set-Cookie2", values); values.add("COOKIE2_JSESSIONID=" + (SESSION_ID+100) + "; version=1; Path=" + URI_PATH +"; HttpOnly"); respHeaders.put("Set-Cookie2", values); t.sendResponseHeaders(200, -1); t.close(); }
Example 19
Source File: App.java From templates with MIT License | 4 votes |
@Override public void handle(HttpExchange t) throws IOException { String requestBody = ""; String method = t.getRequestMethod(); if (method.equalsIgnoreCase("POST")) { InputStream inputStream = t.getRequestBody(); ByteArrayOutputStream result = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int length; while ((length = inputStream.read(buffer)) != -1) { result.write(buffer, 0, length); } // StandardCharsets.UTF_8.name() > JDK 7 requestBody = result.toString("UTF-8"); } // System.out.println(requestBody); Headers reqHeaders = t.getRequestHeaders(); Map<String, String> reqHeadersMap = new HashMap<String, String>(); for (Map.Entry<String, java.util.List<String>> header : reqHeaders.entrySet()) { java.util.List<String> headerValues = header.getValue(); if(headerValues.size() > 0) { reqHeadersMap.put(header.getKey(), headerValues.get(0)); } } // for(Map.Entry<String, String> entry : reqHeadersMap.entrySet()) { // System.out.println("Req header " + entry.getKey() + " " + entry.getValue()); // } IRequest req = new Request(requestBody, reqHeadersMap,t.getRequestURI().getRawQuery(), t.getRequestURI().getPath()); IResponse res = this.handler.Handle(req); String response = res.getBody(); byte[] bytesOut = response.getBytes("UTF-8"); Headers responseHeaders = t.getResponseHeaders(); String contentType = res.getContentType(); if(contentType.length() > 0) { responseHeaders.set("Content-Type", contentType); } for(Map.Entry<String, String> entry : res.getHeaders().entrySet()) { responseHeaders.set(entry.getKey(), entry.getValue()); } t.sendResponseHeaders(res.getStatusCode(), bytesOut.length); OutputStream os = t.getResponseBody(); os.write(bytesOut); os.close(); System.out.println("Request / " + Integer.toString(bytesOut.length) +" bytes written."); }
Example 20
Source File: App.java From templates with MIT License | 4 votes |
@Override public void handle(HttpExchange t) throws IOException { String requestBody = ""; String method = t.getRequestMethod(); if (method.equalsIgnoreCase("POST")) { InputStream inputStream = t.getRequestBody(); ByteArrayOutputStream result = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int length; while ((length = inputStream.read(buffer)) != -1) { result.write(buffer, 0, length); } // StandardCharsets.UTF_8.name() > JDK 7 requestBody = result.toString("UTF-8"); } // System.out.println(requestBody); Headers reqHeaders = t.getRequestHeaders(); Map<String, String> reqHeadersMap = new HashMap<String, String>(); for (Map.Entry<String, java.util.List<String>> header : reqHeaders.entrySet()) { java.util.List<String> headerValues = header.getValue(); if(headerValues.size() > 0) { reqHeadersMap.put(header.getKey(), headerValues.get(0)); } } // for(Map.Entry<String, String> entry : reqHeadersMap.entrySet()) { // System.out.println("Req header " + entry.getKey() + " " + entry.getValue()); // } IRequest req = new Request(requestBody, reqHeadersMap,t.getRequestURI().getRawQuery(), t.getRequestURI().getPath()); IResponse res = this.handler.Handle(req); String response = res.getBody(); byte[] bytesOut = response.getBytes("UTF-8"); Headers responseHeaders = t.getResponseHeaders(); String contentType = res.getContentType(); if(contentType.length() > 0) { responseHeaders.set("Content-Type", contentType); } for(Map.Entry<String, String> entry : res.getHeaders().entrySet()) { responseHeaders.set(entry.getKey(), entry.getValue()); } t.sendResponseHeaders(res.getStatusCode(), bytesOut.length); OutputStream os = t.getResponseBody(); os.write(bytesOut); os.close(); System.out.println("Request / " + Integer.toString(bytesOut.length) +" bytes written."); }