Java Code Examples for com.sun.net.httpserver.HttpExchange#getRequestMethod()
The following examples show how to use
com.sun.net.httpserver.HttpExchange#getRequestMethod() .
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: WSHttpHandler.java From jdk8u60 with GNU General Public License v2.0 | 6 votes |
private void handleExchange(HttpExchange msg) throws IOException { WSHTTPConnection con = new ServerConnectionImpl(adapter,msg); try { if (fineTraceEnabled) { LOGGER.log(Level.FINE, "Received HTTP request:{0}", msg.getRequestURI()); } String method = msg.getRequestMethod(); if(method.equals(GET_METHOD) || method.equals(POST_METHOD) || method.equals(HEAD_METHOD) || method.equals(PUT_METHOD) || method.equals(DELETE_METHOD)) { adapter.handle(con); } else { if (LOGGER.isLoggable(Level.WARNING)) { LOGGER.warning(HttpserverMessages.UNEXPECTED_HTTP_METHOD(method)); } } } finally { msg.close(); } }
Example 2
Source File: WSHttpHandler.java From openjdk-8 with GNU General Public License v2.0 | 6 votes |
private void handleExchange(HttpExchange msg) throws IOException { WSHTTPConnection con = new ServerConnectionImpl(adapter,msg); try { if (fineTraceEnabled) { LOGGER.log(Level.FINE, "Received HTTP request:{0}", msg.getRequestURI()); } String method = msg.getRequestMethod(); if(method.equals(GET_METHOD) || method.equals(POST_METHOD) || method.equals(HEAD_METHOD) || method.equals(PUT_METHOD) || method.equals(DELETE_METHOD)) { adapter.handle(con); } else { if (LOGGER.isLoggable(Level.WARNING)) { LOGGER.warning(HttpserverMessages.UNEXPECTED_HTTP_METHOD(method)); } } } finally { msg.close(); } }
Example 3
Source File: NacosConfigHttpHandler.java From nacos-spring-project with Apache License 2.0 | 6 votes |
@Override public void handle(HttpExchange httpExchange) throws IOException { String method = httpExchange.getRequestMethod(); if ("GET".equals(method)) { handleGetConfig(httpExchange); } else if ("POST".equals(method)) { String queryString = StreamUtils.copyToString(httpExchange.getRequestBody(), forName("UTF-8")); Map<String, String> params = parseParams(queryString); String listeningConfigs = params.get("Listening-Configs"); if (listeningConfigs != null) { handleLongPolling(httpExchange, listeningConfigs); } else { handlePublishConfig(httpExchange, params); } } else if ("DELETE".equals(method)) { handleRemoveConfig(httpExchange); } }
Example 4
Source File: WebServerTestHelper.java From WLT3Serial with MIT License | 6 votes |
public void handle(HttpExchange exchange) { try { exchange.getRequestMethod(); exchange.getRequestBody().close(); Headers respHeaders = null; if(!accessed && (source == null)) { //context hasn't been accessed before source = exchange.getRemoteAddress().getHostString(); accessed = true; respHeaders = exchange.getResponseHeaders(); respHeaders.add("Content-Type","text/plain"); exchange.sendResponseHeaders(200,path.length()); OutputStream os = exchange.getResponseBody(); os.write(path.getBytes()); os.flush(); os.close(); accessList.put(path,source); } else { respHeaders = exchange.getResponseHeaders(); exchange.sendResponseHeaders(404,0); exchange.getResponseBody().close(); } } catch(Exception e) { System.err.println("Unforeseen error occurred!"); e.printStackTrace(); } }
Example 5
Source File: WSHttpHandler.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
private void handleExchange(HttpExchange msg) throws IOException { WSHTTPConnection con = new ServerConnectionImpl(adapter,msg); try { if (fineTraceEnabled) { LOGGER.log(Level.FINE, "Received HTTP request:{0}", msg.getRequestURI()); } String method = msg.getRequestMethod(); if(method.equals(GET_METHOD) || method.equals(POST_METHOD) || method.equals(HEAD_METHOD) || method.equals(PUT_METHOD) || method.equals(DELETE_METHOD)) { adapter.handle(con); } else { if (LOGGER.isLoggable(Level.WARNING)) { LOGGER.warning(HttpserverMessages.UNEXPECTED_HTTP_METHOD(method)); } } } finally { msg.close(); } }
Example 6
Source File: ParkInetServer.java From Fourinone with Apache License 2.0 | 6 votes |
public void handle(HttpExchange exchange) throws IOException { String requestMethod = exchange.getRequestMethod(); if(requestMethod.equalsIgnoreCase("GET")) { Headers responseHeaders = exchange.getResponseHeaders(); responseHeaders.set("Content-Type", "text/plain"); exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, 0); OutputStream responseBody = exchange.getResponseBody(); Headers requestHeaders = exchange.getRequestHeaders(); Set<String> keySet = requestHeaders.keySet(); Iterator<String> iter = keySet.iterator(); while (iter.hasNext()){ String key = iter.next(); List values = requestHeaders.get(key); String s = key + " = " + values.toString() + "\n"; responseBody.write(s.getBytes()); } responseBody.close(); } }
Example 7
Source File: WSHttpHandler.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 6 votes |
private void handleExchange(HttpExchange msg) throws IOException { WSHTTPConnection con = new ServerConnectionImpl(adapter,msg); try { if (fineTraceEnabled) { LOGGER.log(Level.FINE, "Received HTTP request:{0}", msg.getRequestURI()); } String method = msg.getRequestMethod(); if(method.equals(GET_METHOD) || method.equals(POST_METHOD) || method.equals(HEAD_METHOD) || method.equals(PUT_METHOD) || method.equals(DELETE_METHOD)) { adapter.handle(con); } else { if (LOGGER.isLoggable(Level.WARNING)) { LOGGER.warning(HttpserverMessages.UNEXPECTED_HTTP_METHOD(method)); } } } finally { msg.close(); } }
Example 8
Source File: BasicAuthTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
@Override public void handle(HttpExchange he) throws IOException { String method = he.getRequestMethod(); InputStream is = he.getRequestBody(); if (method.equalsIgnoreCase("POST")) { String requestBody = new String(is.readAllBytes(), US_ASCII); if (!requestBody.equals(POST_BODY)) { he.sendResponseHeaders(500, -1); ok = false; } else { he.sendResponseHeaders(200, -1); ok = true; } } else { // GET he.sendResponseHeaders(200, RESPONSE.length()); OutputStream os = he.getResponseBody(); os.write(RESPONSE.getBytes(US_ASCII)); os.close(); ok = true; } }
Example 9
Source File: ParkInetServer.java From Fourinone with Apache License 2.0 | 5 votes |
public void handle(HttpExchange exchange) throws IOException { String uri = exchange.getRequestURI().getPath().substring(5); //System.out.println(exchange.getRemoteAddress()+" getPath:"+uri); File fl = new File(uri); //System.out.println(fl); //System.out.println(fl.exists()); String requestMethod = exchange.getRequestMethod(); if(requestMethod.equalsIgnoreCase("GET")) { Headers responseHeaders = exchange.getResponseHeaders(); responseHeaders.set("Content-Type", "application/x-download"); //responseHeaders.set("Content-Disposition", "attachment; filename=abcd.bat"); exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, fl.length());//HttpURLConnection.HTTP_OK,responseMsg.length(); OutputStream responseBody = exchange.getResponseBody(); FileAdapter fa = new FileAdapter(fl.getPath()); byte[] bts = null; long begin=0,every=FileAdapter.k(512); while((bts=fa.getReader(begin, every).readAll())!=null){ responseBody.write(bts); begin+=bts.length; } fa.close(); responseBody.close(); } }
Example 10
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 11
Source File: RequestHandler.java From Plan with GNU Lesser General Public License v3.0 | 5 votes |
private Request buildRequest(HttpExchange exchange) { String requestMethod = exchange.getRequestMethod(); URIPath path = new URIPath(exchange.getRequestURI().getPath()); URIQuery query = new URIQuery(exchange.getRequestURI().getRawQuery()); WebUser user = getWebUser(exchange); Map<String, String> headers = getRequestHeaders(exchange); return new Request(requestMethod, path, query, user, headers); }
Example 12
Source File: LocalFileRepoOverHttp.java From ant-ivy with Apache License 2.0 | 5 votes |
@Override public void handle(final HttpExchange httpExchange) throws IOException { final URI requestURI = httpExchange.getRequestURI(); Message.info("Handling " + httpExchange.getRequestMethod() + " request " + requestURI); final URI artifactURI; try { artifactURI = new URI(webContextRoot).relativize(requestURI); } catch (URISyntaxException e) { throw new IOException(e); } final Path localFilePath = localFileRepoRoot.resolve(artifactURI.toString()); if (httpExchange.getRequestMethod().equals("HEAD")) { final boolean available = this.isPresent(localFilePath); if (!available) { httpExchange.sendResponseHeaders(404, -1); } else { httpExchange.sendResponseHeaders(200, -1); } return; } if (!httpExchange.getRequestMethod().equals("GET")) { throw new IOException("Cannot handle " + httpExchange.getRequestMethod() + " HTTP method"); } final OutputStream responseStream = httpExchange.getResponseBody(); @SuppressWarnings("unused") final int numBytes = this.serve(httpExchange, localFilePath, responseStream); responseStream.close(); }
Example 13
Source File: RoutingHandler.java From validator with Apache License 2.0 | 5 votes |
@Override public void handle(HttpExchange exchange) throws IOException { final String requestMethod = exchange.getRequestMethod(); if (requestMethod.equals("POST")) { checkHandler.handle(exchange); } else if (requestMethod.equals("GET")) { guiHandler.handle(exchange); } else { error(exchange, 405, String.format("Method % not supported", requestMethod)); } }
Example 14
Source File: AuthHttpServer.java From swift-explorer with Apache License 2.0 | 5 votes |
@Override public void handle(HttpExchange exchange) throws IOException { String requestMethod = exchange.getRequestMethod(); if (requestMethod.equalsIgnoreCase("GET")) { Headers responseHeaders = exchange.getResponseHeaders(); responseHeaders.set("Content-Type", "text/plain"); exchange.sendResponseHeaders(200, 0); OutputStream responseBody = exchange.getResponseBody(); Headers requestHeaders = exchange.getRequestHeaders(); Set<String> keySet = requestHeaders.keySet(); Iterator<String> iter = keySet.iterator(); while (iter.hasNext()) { String key = iter.next(); List<String> values = requestHeaders.get(key); String s = key + " = " + values.toString() + "\n"; responseBody.write(s.getBytes()); } responseBody.close(); if (sharedQueue != null) { String query = exchange.getRequestURI().getQuery() ; try { sharedQueue.put(queryToParameterMap(query)); } catch (InterruptedException e) { logger.error("Error occurred in the server handler", e); } } } }
Example 15
Source File: HttpsSimpleServer.java From tomee with Apache License 2.0 | 5 votes |
public void handle(HttpExchange exchange) throws IOException { String requestMethod = exchange.getRequestMethod(); Headers responseHeaders = exchange.getResponseHeaders(); responseHeaders.set("Content-Type", "text/plain"); exchange.sendResponseHeaders(200, 0); OutputStream responseBody = exchange.getResponseBody(); responseBody.write("secure page".getBytes()); responseBody.close(); }
Example 16
Source File: RefactoringMinerHttpsServer.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 17
Source File: RpcHandler.java From RipplePower with Apache License 2.0 | 4 votes |
/** * Handle an HTTP request * * @param exchange * HTTP exchange * @throws IOException * Error detected while handling the request */ @Override public void handle(HttpExchange exchange) throws IOException { try { int responseCode; String responseBody; // // Get the HTTP request // InetSocketAddress requestAddress = exchange.getRemoteAddress(); String requestMethod = exchange.getRequestMethod(); Headers requestHeaders = exchange.getRequestHeaders(); String contentType = requestHeaders.getFirst("Content-Type"); Headers responseHeaders = exchange.getResponseHeaders(); BTCLoader.debug(String.format("%s request received from %s", requestMethod, requestAddress.getAddress())); if (!rpcAllowIp.contains(requestAddress.getAddress())) { responseCode = HttpURLConnection.HTTP_UNAUTHORIZED; responseBody = "Your IP address is not authorized to access this server"; responseHeaders.set("Content-Type", "text/plain"); } else if (!exchange.getRequestMethod().equals("POST")) { responseCode = HttpURLConnection.HTTP_BAD_METHOD; responseBody = String.format("%s requests are not supported", exchange.getRequestMethod()); responseHeaders.set("Content-Type", "text/plain"); } else if (contentType == null || !contentType.equals("application/json-rpc")) { responseCode = HttpURLConnection.HTTP_BAD_REQUEST; responseBody = "Content type must be application/json-rpc"; responseHeaders.set("Content-Type", "text/plain"); } else { responseBody = processRequest(exchange); responseCode = HttpURLConnection.HTTP_OK; responseHeaders.set("Content-Type", "application/json-rpc"); } // // Return the HTTP response // responseHeaders.set("Cache-Control", "no-cache, no-store, must-revalidate, private"); responseHeaders.set("Server", "JavaBitcoin"); byte[] responseBytes = responseBody.getBytes("UTF-8"); exchange.sendResponseHeaders(responseCode, responseBytes.length); try (OutputStream out = exchange.getResponseBody()) { out.write(responseBytes); } BTCLoader.debug(String.format("RPC request from %s completed", requestAddress.getAddress())); } catch (IOException exc) { BTCLoader.error("Unable to process RPC request", exc); throw exc; } }
Example 18
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 19
Source File: SimpleHttpServer.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
public void handle(HttpExchange t) throws IOException { InputStream is = t.getRequestBody(); Headers map = t.getRequestHeaders(); Headers rmap = t.getResponseHeaders(); OutputStream os = t.getResponseBody(); URI uri = t.getRequestURI(); String path = uri.getPath(); while (is.read() != -1) ; is.close(); File f = new File(_docroot, path); if (!f.exists()) { notfound(t, path); return; } String method = t.getRequestMethod(); if (method.equals("HEAD")) { rmap.set("Content-Length", Long.toString(f.length())); t.sendResponseHeaders(200, -1); t.close(); } else if (!method.equals("GET")) { t.sendResponseHeaders(405, -1); t.close(); return; } if (path.endsWith(".html") || path.endsWith(".htm")) { rmap.set("Content-Type", "text/html"); } else { rmap.set("Content-Type", "text/plain"); } t.sendResponseHeaders (200, f.length()); FileInputStream fis = new FileInputStream(f); int count = 0; try { byte[] buf = new byte[16 * 1024]; int len; while ((len = fis.read(buf)) != -1) { os.write(buf, 0, len); count += len; } } catch (IOException e) { e.printStackTrace(); } fis.close(); os.close(); }
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."); }