String requestPath = request.path().toString(); if (requestPath.startsWith("/")) { requestPath = requestPath.substring(1);
private static URI requestUri(ServerRequest req) { try { // Creating a URI from a URL avoids re-encoding of characters like '%' URI partialUri = new URL(req.isSecure() ? "https" : "http", req.localAddress(), req.localPort(), req.path().absolute().toString()).toURI(); StringBuilder sb = new StringBuilder(partialUri.toString()); if (req.uri().toString().endsWith("/") && sb.charAt(sb.length() - 1) != '/') { sb.append('/'); } // unfortunately, the URI constructor encodes the 'query' and 'fragment' which is totally silly if (req.query() != null && !req.query().isEmpty()) { sb.append("?") .append(req.query()); } if (req.fragment() != null && !req.fragment().isEmpty()) { sb.append("#") .append(req.fragment()); } return new URI(sb.toString()); } catch (URISyntaxException | MalformedURLException e) { throw new IllegalStateException("Unable to create a request URI from the request info.", e); } }
private void registerContext(ServerRequest req, ServerResponse res) { Map<String, List<String>> allHeaders = new HashMap<>(req.headers().toMap()); Optional<Map> newHeaders = req.context().get(CONTEXT_ADD_HEADERS, Map.class); newHeaders.ifPresent(allHeaders::putAll); //make sure there is no context if (!req.context().get(SecurityContext.class).isPresent()) { SecurityEnvironment env = security.environmentBuilder() .targetUri(req.uri()) .path(req.path().toString()) .method(req.method().name()) .addAttribute("userIp", req.remoteAddress()) .addAttribute("userPort", req.remotePort()) .transport(req.isSecure() ? "https" : "http") .headers(allHeaders) .build(); EndpointConfig ec = EndpointConfig.builder() .build(); SecurityContext context = security.contextBuilder(String.valueOf(SECURITY_COUNTER.incrementAndGet())) .tracingSpan(req.spanContext()) .env(env) .endpointConfig(ec) .build(); req.context().register(context); req.context().register(defaultHandler); } req.next(); }
private static URI requestUri(ServerRequest req) { try { // Creating a URI from a URL avoids re-encoding of characters like '%' URI partialUri = new URL(req.isSecure() ? "https" : "http", req.localAddress(), req.localPort(), req.path().absolute().toString()).toURI(); StringBuilder sb = new StringBuilder(partialUri.toString()); if (req.uri().toString().endsWith("/") && sb.charAt(sb.length() - 1) != '/') { sb.append('/'); } // unfortunately, the URI constructor encodes the 'query' and 'fragment' which is totally silly if (req.query() != null && !req.query().isEmpty()) { sb.append("?") .append(req.query()); } if (req.fragment() != null && !req.fragment().isEmpty()) { sb.append("#") .append(req.fragment()); } return new URI(sb.toString()); } catch (URISyntaxException | MalformedURLException e) { throw new IllegalStateException("Unable to create a request URI from the request info.", e); } }
private static String basePath(ServerRequest req) { String reqPath = req.path().toString(); String absPath = req.path().absolute().toString(); String basePath = absPath.substring(0, absPath.length() - reqPath.length()); if (absPath.isEmpty() || basePath.isEmpty()) { return "/"; } else if (basePath.charAt(basePath.length() - 1) != '/') { return basePath + "/"; } else { return basePath; } }
private void registerContext(ServerRequest req, ServerResponse res) { Map<String, List<String>> allHeaders = new HashMap<>(req.headers().toMap()); Optional<Map> newHeaders = req.context().get(CONTEXT_ADD_HEADERS, Map.class); newHeaders.ifPresent(allHeaders::putAll); //make sure there is no context if (!req.context().get(SecurityContext.class).isPresent()) { SecurityEnvironment env = security.environmentBuilder() .targetUri(req.uri()) .path(req.path().toString()) .method(req.method().name()) .addAttribute("userIp", req.remoteAddress()) .addAttribute("userPort", req.remotePort()) .transport(req.isSecure() ? "https" : "http") .headers(allHeaders) .build(); EndpointConfig ec = EndpointConfig.builder() .build(); SecurityContext context = security.contextBuilder(String.valueOf(SECURITY_COUNTER.incrementAndGet())) .tracingSpan(req.spanContext()) .env(env) .endpointConfig(ec) .build(); req.context().register(context); req.context().register(defaultHandler); } req.next(); }
private void optionsOne(ServerRequest req, ServerResponse res, Registry registry) { String metricName = req.path().param("metric"); OptionalHelper.from(registry.getMetric(metricName)) .ifPresentOrElse(metric -> { if (req.headers().isAccepted(MediaType.APPLICATION_JSON)) { JsonObjectBuilder builder = JSON.createObjectBuilder(); metric.jsonMeta(builder); res.send(builder.build()); } else { res.status(Http.Status.NOT_ACCEPTABLE_406); res.send(); } }, () -> { res.status(Http.Status.NO_CONTENT_204); res.send(); }); }
private void getOne(ServerRequest req, ServerResponse res, Registry registry) { String metricName = req.path().param("metric"); OptionalHelper.from(registry.getMetric(metricName)) .ifPresentOrElse(metric -> { if (requestsJsonData(req.headers())) { JsonObjectBuilder builder = JSON.createObjectBuilder(); metric.jsonData(builder); res.send(builder.build()); } else { res.send(metric.prometheusData()); } }, () -> { res.status(Http.Status.NOT_FOUND_404); res.send(); }); }
@Override public ServerRequest.Path path() { return super.path().absolute(); } }
private static String basePath(ServerRequest req) { String reqPath = req.path().toString(); String absPath = req.path().absolute().toString(); String basePath = absPath.substring(0, absPath.length() - reqPath.length()); if (absPath.isEmpty() || basePath.isEmpty()) { return "/"; } else if (basePath.charAt(basePath.length() - 1) != '/') { return basePath + "/"; } else { return basePath; } }