@Override public PathContainer pathContainer() { return request().getPath(); }
@Override public RequestPath getPath() { return getDelegate().getPath(); }
@Override @Nullable public CorsConfiguration getCorsConfiguration(ServerWebExchange exchange) { PathContainer lookupPath = exchange.getRequest().getPath().pathWithinApplication(); return this.corsConfigurations.entrySet().stream() .filter(entry -> entry.getKey().matches(lookupPath)) .map(Map.Entry::getValue) .findFirst() .orElse(null); }
private String formatRequest(ServerHttpRequest request) { String rawQuery = request.getURI().getRawQuery(); String query = StringUtils.hasText(rawQuery) ? "?" + rawQuery : ""; return "HTTP " + request.getMethod() + " \"" + request.getPath() + query + "\""; }
/** * Select a default view name when a controller did not specify it. * Use the request path the leading and trailing slash stripped. */ private String getDefaultViewName(ServerWebExchange exchange) { String path = exchange.getRequest().getPath().pathWithinApplication().value(); if (path.startsWith("/")) { path = path.substring(1); } if (path.endsWith("/")) { path = path.substring(0, path.length() - 1); } return StringUtils.stripFilenameExtension(path); }
/** * Find the patterns matching the given lookup path. Invoking this method should * yield results equivalent to those of calling * {@link #getMatchingCondition(ServerWebExchange)}. * This method is provided as an alternative to be used if no request is available * (e.g. introspection, tooling, etc). * @param exchange the current exchange * @return a sorted set of matching patterns sorted with the closest match first */ private SortedSet<PathPattern> getMatchingPatterns(ServerWebExchange exchange) { PathContainer lookupPath = exchange.getRequest().getPath().pathWithinApplication(); return this.patterns.stream() .filter(pattern -> pattern.matches(lookupPath)) .collect(Collectors.toCollection(TreeSet::new)); }
@Override public Mono<Void> handle(ServerHttpRequest request, ServerHttpResponse response) { // Remove underlying context path first (e.g. Servlet container) String path = request.getPath().pathWithinApplication().value(); return this.handlerMap.entrySet().stream() .filter(entry -> path.startsWith(entry.getKey())) .findFirst() .map(entry -> { String contextPath = request.getPath().contextPath().value() + entry.getKey(); ServerHttpRequest newRequest = request.mutate().contextPath(contextPath).build(); return entry.getValue().handle(newRequest, response); }) .orElseGet(() -> { response.setStatusCode(HttpStatus.NOT_FOUND); return response.setComplete(); }); }
private String formatRequest(ServerHttpRequest request) { String rawQuery = request.getURI().getRawQuery(); String query = StringUtils.hasText(rawQuery) ? "?" + rawQuery : ""; return "HTTP " + request.getMethod() + " \"" + request.getPath() + query + "\""; }
/** * Return the context path of the current web application. This is * useful for building links to other resources within the application. * <p>Delegates to {@link ServerHttpRequest#getPath()}. */ public String getContextPath() { return this.exchange.getRequest().getPath().contextPath().value(); }
/** * Get the public resource URL for the given URI string. * <p>The URI string is expected to be a path and if it contains a query or * fragment those will be preserved in the resulting public resource URL. * @param uriString the URI string to transform * @param exchange the current exchange * @return the resolved public resource URL path, or empty if unresolved */ public final Mono<String> getForUriString(String uriString, ServerWebExchange exchange) { ServerHttpRequest request = exchange.getRequest(); int queryIndex = getQueryIndex(uriString); String lookupPath = uriString.substring(0, queryIndex); String query = uriString.substring(queryIndex); PathContainer parsedLookupPath = PathContainer.parsePath(lookupPath); return resolveResourceUrl(exchange, parsedLookupPath).map(resolvedPath -> request.getPath().contextPath().value() + resolvedPath + query); }
/** * Create the target URL and, if necessary, pre-pend the contextPath, expand * URI template variables, append the current request query, and apply the * configured {@link #getRequestDataValueProcessor() * RequestDataValueProcessor}. */ protected final String createTargetUrl(Map<String, Object> model, ServerWebExchange exchange) { String url = getUrl(); Assert.state(url != null, "'url' not set"); ServerHttpRequest request = exchange.getRequest(); StringBuilder targetUrl = new StringBuilder(); if (isContextRelative() && url.startsWith("/")) { targetUrl.append(request.getPath().contextPath().value()); } targetUrl.append(url); if (StringUtils.hasText(targetUrl)) { Map<String, String> uriVars = getCurrentUriVariables(exchange); targetUrl = expandTargetUrlTemplate(targetUrl.toString(), model, uriVars); } if (isPropagateQuery()) { targetUrl = appendCurrentRequestQuery(targetUrl.toString(), request); } String result = targetUrl.toString(); RequestDataValueProcessor processor = getRequestDataValueProcessor(); return (processor != null ? processor.processUrl(exchange, result) : result); }
@GetMapping("/test") public String handle(ServerHttpRequest request) { return "Tested in " + request.getPath().contextPath().value(); } }
@Override public Mono<Object> getHandlerInternal(ServerWebExchange exchange) { PathContainer lookupPath = exchange.getRequest().getPath().pathWithinApplication(); Object handler; try { handler = lookupHandler(lookupPath, exchange); } catch (Exception ex) { return Mono.error(ex); } return Mono.justOrEmpty(handler); }
private ResponseCookie initSessionCookie( ServerWebExchange exchange, String id, Duration maxAge) { ResponseCookie.ResponseCookieBuilder cookieBuilder = ResponseCookie.from(this.cookieName, id) .path(exchange.getRequest().getPath().contextPath().value() + "/") .maxAge(maxAge) .httpOnly(true) .secure("https".equalsIgnoreCase(exchange.getRequest().getURI().getScheme())) .sameSite("Lax"); if (this.cookieInitializer != null) { this.cookieInitializer.accept(cookieBuilder); } return cookieBuilder.build(); }
@Test public void xForwardedPrefix() throws Exception { HttpHeaders headers = new HttpHeaders(); headers.add("X-Forwarded-Prefix", "/prefix"); ServerHttpRequest request = this.requestMutator.apply(getRequest(headers)); assertEquals(new URI("http://example.com/prefix/path"), request.getURI()); assertEquals("/prefix/path", request.getPath().value()); assertForwardedHeadersRemoved(request); }
@Test public void xForwardedPrefixTrailingSlash() throws Exception { HttpHeaders headers = new HttpHeaders(); headers.add("X-Forwarded-Prefix", "/prefix////"); ServerHttpRequest request = this.requestMutator.apply(getRequest(headers)); assertEquals(new URI("http://example.com/prefix/path"), request.getURI()); assertEquals("/prefix/path", request.getPath().value()); assertForwardedHeadersRemoved(request); }
@Override protected String getMatchingMapping(String pattern, ServerWebExchange exchange) { PathContainer lookupPath = exchange.getRequest().getPath().pathWithinApplication(); PathPattern parsedPattern = this.parser.parse(pattern); return (parsedPattern.matches(lookupPath) ? pattern : null); }
private void assertInvoked(TestHttpHandler handler, String contextPath) { assertTrue(handler.wasInvoked()); assertEquals(contextPath, handler.getRequest().getPath().contextPath().value()); }
@Test public void matchWithNativeContextPath() { MockServerHttpRequest request = MockServerHttpRequest .get("/yet/another/path") .contextPath("/yet") // contextPath in underlying request .build(); TestHttpHandler handler = new TestHttpHandler(); Map<String, HttpHandler> map = Collections.singletonMap("/another/path", handler); new ContextPathCompositeHandler(map).handle(request, new MockServerHttpResponse()); assertTrue(handler.wasInvoked()); assertEquals("/yet/another/path", handler.getRequest().getPath().contextPath().value()); }
@Test public void mutateRequest() throws Exception { SslInfo sslInfo = mock(SslInfo.class); ServerHttpRequest request = createHttpRequest("/").mutate().sslInfo(sslInfo).build(); assertSame(sslInfo, request.getSslInfo()); request = createHttpRequest("/").mutate().method(HttpMethod.DELETE).build(); assertEquals(HttpMethod.DELETE, request.getMethod()); String baseUri = "http://aaa.org:8080/a"; request = createHttpRequest(baseUri).mutate().uri(URI.create("http://bbb.org:9090/b")).build(); assertEquals("http://bbb.org:9090/b", request.getURI().toString()); request = createHttpRequest(baseUri).mutate().path("/b/c/d").build(); assertEquals("http://aaa.org:8080/b/c/d", request.getURI().toString()); request = createHttpRequest(baseUri).mutate().path("/app/b/c/d").contextPath("/app").build(); assertEquals("http://aaa.org:8080/app/b/c/d", request.getURI().toString()); assertEquals("/app", request.getPath().contextPath().value()); }