private static String getQueryParam(HttpExchange httpExchange, String queryParam) { String query = httpExchange.getRequestURI().getQuery(); if (query != null) { for (String part : query.split("&")) { String[] keyValue = part.split("="); if (keyValue.length > 1 && keyValue[0].equals(queryParam)) { return keyValue[1]; } } } return null; } }
private URI getRequestUri(final HttpExchange exchange, final URI baseUri) { try { return new URI(getServerAddress(baseUri) + exchange.getRequestURI()); } catch (URISyntaxException ex) { throw new IllegalArgumentException(ex); } }
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); }
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); } }
@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
private EurekaHttpRequest mapToEurekaHttpRequest(HttpExchange httpExchange) { Headers exchangeHeaders = httpExchange.getRequestHeaders(); Map<String, String> headers = new HashMap<>(); for(String key: exchangeHeaders.keySet()) { headers.put(key, exchangeHeaders.getFirst(key)); } return new EurekaHttpRequest(httpExchange.getRequestMethod(), httpExchange.getRequestURI(), headers); }
String path = httpExchange.getRequestURI().getPath(); if (path.startsWith("/v2/apps")) { if ("GET".equals(method)) {
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); }
private void handleAppsPut(HttpExchange httpExchange) throws IOException { EurekaHttpResponse<?> httpResponse; String path = httpExchange.getRequestURI().getPath(); Matcher matcher; if ((matcher = Pattern.compile("/v2/apps/([^/]+)/([^/]+)").matcher(path)).matches()) { String overriddenstatus = getQueryParam(httpExchange, "overriddenstatus"); httpResponse = requestHandler.sendHeartBeat( matcher.group(1), matcher.group(2), null, overriddenstatus == null ? null : InstanceStatus.valueOf(overriddenstatus) ); } else if ((matcher = Pattern.compile("/v2/apps/([^/]+)/([^/]+)/status").matcher(path)).matches()) { String newStatus = getQueryParam(httpExchange, "value"); httpResponse = requestHandler.statusUpdate( matcher.group(1), matcher.group(2), newStatus == null ? null : InstanceStatus.valueOf(newStatus), null ); } else { httpExchange.sendResponseHeaders(HttpServletResponse.SC_NOT_FOUND, 0); return; } mapResponse(httpExchange, httpResponse); }
public void handle(final HttpExchange httpExchange) throws IOException { // And we pass control to the service, which will (hopefully) start a new thread immediately so we can // continue the receiving thread ASAP log.fine("Received HTTP exchange: " + httpExchange.getRequestMethod() + " " + httpExchange.getRequestURI()); router.received( new HttpExchangeUpnpStream(router.getProtocolFactory(), httpExchange) { @Override protected Connection createConnection() { return new HttpServerConnection(httpExchange); } } ); } }
Map<String, String> urlParams = getURLParams(httpExchange.getRequestURI());
private void handleAppsGET(HttpExchange httpExchange) throws IOException { EurekaHttpResponse<?> httpResponse; String path = httpExchange.getRequestURI().getPath(); Matcher matcher; if (path.matches("/v2/apps[/]?")) { String regions = getQueryParam(httpExchange, "regions"); httpResponse = regions == null ? requestHandler.getApplications() : requestHandler.getApplications(regions); } else if (path.matches("/v2/apps/delta[/]?")) { String regions = getQueryParam(httpExchange, "regions"); httpResponse = regions == null ? requestHandler.getDelta() : requestHandler.getDelta(regions); } else if ((matcher = Pattern.compile("/v2/apps/([^/]+)/([^/]+)").matcher(path)).matches()) { httpResponse = requestHandler.getInstance(matcher.group(1), matcher.group(2)); } else { httpExchange.sendResponseHeaders(HttpServletResponse.SC_NOT_FOUND, 0); return; } if (httpResponse == null) { httpResponse = EurekaHttpResponse.anEurekaHttpResponse(HttpServletResponse.SC_NOT_FOUND).build(); } mapResponse(httpExchange, httpResponse); }
public void handle(HttpExchange t) throws IOException { URI uri = t.getRequestURI(); Map<String, String> paramMap = parseRawQuery(uri.getRawQuery()); LOG.info("Received command " + paramMap); String cmd = paramMap.get(HttpserverUtils.HTTPSERVER_LOGVIEW_PARAM_CMD); if (StringUtils.isBlank(cmd)) { handlFailure(t, "Bad Request, please specify command!"); return; } if (HttpserverUtils.HTTPSERVER_LOGVIEW_PARAM_CMD_SHOW.equals(cmd)) { handleShowLog(t, paramMap); } else if (HttpserverUtils.HTTPSERVER_LOGVIEW_PARAM_CMD_LIST.equals(cmd)) { handleListDir(t, paramMap); } else if (HttpserverUtils.HTTPSERVER_LOGVIEW_PARAM_CMD_JSTACK.equals(cmd)) { handleJstack(t, paramMap); } else if (HttpserverUtils.HTTPSERVER_LOGVIEW_PARAM_CMD_JSTAT.equals(cmd)) { handleJstat(t, paramMap); } else if (HttpserverUtils.HTTPSERVER_LOGVIEW_PARAM_CMD_SHOW_CONF.equals(cmd)) { handleShowConf(t, paramMap); } else if (HttpserverUtils.HTTPSERVER_LOGVIEW_PARAM_CMD_SEARCH_LOG.equals(cmd)) { handleSearchLog(t, paramMap); } else if (HttpserverUtils.HTTPSERVER_LOGVIEW_PARAM_CMD_DOWNLOAD.equals(cmd)) { handleDownloadLog(t, paramMap); } else { handlFailure(t, "Bad Request, unsupported command " + cmd); } }
return; Map<String, String> params = getURLParams(httpExchange.getRequestURI());
return; Map<String, String> params = getURLParams(httpExchange.getRequestURI());
return; Map<String, String> params = getURLParams(httpExchange.getRequestURI());
/** * 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)); } } }
URI exchangeUri = exchange.getRequestURI();