/** * Same as {@link #fromRequest(HttpServletRequest)} except the * request is obtained through {@link RequestContextHolder}. */ public static ServletUriComponentsBuilder fromCurrentRequest() { return fromRequest(getCurrentRequest()); }
/** * Same as {@link #fromRequest(HttpServletRequest)} except the * request is obtained through {@link RequestContextHolder}. */ public static ServletUriComponentsBuilder fromCurrentRequest() { return fromRequest(getCurrentRequest()); }
throws UnsupportedEncodingException { ServletUriComponentsBuilder builder = ServletUriComponentsBuilder .fromRequest(request);
@Test public void fromRequestEncodedPath() { this.request.setRequestURI("/mvc-showcase/data/foo%20bar"); String result = ServletUriComponentsBuilder.fromRequest(this.request).build().toUriString(); assertEquals("http://localhost/mvc-showcase/data/foo%20bar", result); }
@Test public void fromRequestAtypicalHttpPort() { this.request.setServerPort(8080); String result = ServletUriComponentsBuilder.fromRequest(this.request).build().toUriString(); assertEquals("http://localhost:8080/mvc-showcase", result); }
@Test public void fromRequest() { this.request.setRequestURI("/mvc-showcase/data/param"); this.request.setQueryString("foo=123"); String result = ServletUriComponentsBuilder.fromRequest(this.request).build().toUriString(); assertEquals("http://localhost/mvc-showcase/data/param?foo=123", result); }
@Test public void fromRequestAtypicalHttpsPort() { this.request.setScheme("https"); this.request.setServerPort(9043); String result = ServletUriComponentsBuilder.fromRequest(this.request).build().toUriString(); assertEquals("https://localhost:9043/mvc-showcase", result); }
@Test // SPR-16650 public void fromRequestWithForwardedPrefixRoot() throws Exception { this.request.addHeader("X-Forwarded-Prefix", "/"); this.request.setContextPath("/mvc-showcase"); this.request.setRequestURI("/mvc-showcase/bar"); HttpServletRequest requestToUse = adaptFromForwardedHeaders(this.request); UriComponents result = ServletUriComponentsBuilder.fromRequest(requestToUse).build(); assertEquals("http://localhost/bar", result.toUriString()); }
@Test // SPR-16650 public void fromRequestWithForwardedPrefix() throws Exception { this.request.addHeader("X-Forwarded-Prefix", "/prefix"); this.request.setContextPath("/mvc-showcase"); this.request.setRequestURI("/mvc-showcase/bar"); HttpServletRequest requestToUse = adaptFromForwardedHeaders(this.request); UriComponents result = ServletUriComponentsBuilder.fromRequest(requestToUse).build(); assertEquals("http://localhost/prefix/bar", result.toUriString()); }
@Test // SPR-16650 public void fromRequestWithForwardedPrefixTrailingSlash() throws Exception { this.request.addHeader("X-Forwarded-Prefix", "/foo/"); this.request.setContextPath("/spring-mvc-showcase"); this.request.setRequestURI("/spring-mvc-showcase/bar"); HttpServletRequest requestToUse = adaptFromForwardedHeaders(this.request); UriComponents result = ServletUriComponentsBuilder.fromRequest(requestToUse).build(); assertEquals("http://localhost/foo/bar", result.toUriString()); }
@Test public void fromRequestWithForwardedHostAndPort() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); request.setScheme("http"); request.setServerName("localhost"); request.setServerPort(80); request.setRequestURI("/mvc-showcase"); request.addHeader("X-Forwarded-Proto", "https"); request.addHeader("X-Forwarded-Host", "84.198.58.199"); request.addHeader("X-Forwarded-Port", "443"); HttpServletRequest requestToUse = adaptFromForwardedHeaders(request); UriComponents result = ServletUriComponentsBuilder.fromRequest(requestToUse).build(); assertEquals("https://84.198.58.199/mvc-showcase", result.toString()); }
/** * Returns the View to redirect to to access the HAL browser. * * @param request must not be {@literal null}. * @param browserRelative * @return */ private View getRedirectView(HttpServletRequest request, boolean browserRelative) { ServletUriComponentsBuilder builder = ServletUriComponentsBuilder.fromRequest(request); UriComponents components = builder.build(); String path = components.getPath() == null ? "" : components.getPath(); if (!browserRelative) { builder.path(BROWSER); } builder.path(INDEX); builder.fragment(browserRelative ? path.substring(0, path.lastIndexOf("/browser")) : path); return new RedirectView(builder.build().toUriString()); } }
/** * Same as {@link #fromRequest(HttpServletRequest)} except the * request is obtained through {@link RequestContextHolder}. * <p><strong>Note:</strong> This method extracts values from "Forwarded" * and "X-Forwarded-*" headers if found. See class-level docs. * <p>As of 4.3.15, this method replaces the contextPath with the value * of "X-Forwarded-Prefix" rather than prepending, thus aligning with * {@code ForwardedHeaderFilter}. */ public static ServletUriComponentsBuilder fromCurrentRequest() { return fromRequest(getCurrentRequest()); }
@GetMapping public void getConsoleNoSlash(@PathVariable(name = "repoName") String repoName, HttpServletRequest request, HttpServletResponse response) { ServletUriComponentsBuilder builder = ServletUriComponentsBuilder.fromRequest(request); String location = builder.build().toString() + "/"; response.setStatus(HttpStatus.MOVED_PERMANENTLY.value()); response.setHeader("Location", location); }
protected String getIndexHtmlRedirect(final HttpServletRequest request) { UriComponentsBuilder builder = ServletUriComponentsBuilder.fromRequest(request); if (request.getServletContext().getContextPath() != null) { String path = new UrlPathHelper().getPathWithinApplication(request); builder.replacePath(path); } // append "/index.html" to the current path builder.path("/index.html"); UriComponents uriComponents = builder.build(); String path = uriComponents.getPath(); return "forward:" + path; } }
protected String getIndexHtmlRedirect(final HttpServletRequest request) { UriComponentsBuilder builder = ServletUriComponentsBuilder.fromRequest(request); if (request.getServletContext().getContextPath() != null) { String path = new UrlPathHelper().getPathWithinApplication(request); builder.replacePath(path); } // append "/index.html" to the current path builder.path("/index.html"); UriComponents uriComponents = builder.build(); String path = uriComponents.getPath(); return "forward:" + path; } }
throws UnsupportedEncodingException { ServletUriComponentsBuilder builder = ServletUriComponentsBuilder .fromRequest(request);
/** * Returns the View to redirect to to access the HAL browser. * * @param request must not be {@literal null}. * @param browserRelative * @return */ private View getRedirectView(HttpServletRequest request, boolean browserRelative) { ServletUriComponentsBuilder builder = ServletUriComponentsBuilder.fromRequest(request); UriComponents components = builder.build(); String path = components.getPath() == null ? "" : components.getPath(); if (!browserRelative) { builder.path(BROWSER); } builder.path(INDEX); builder.fragment(browserRelative ? path.substring(0, path.lastIndexOf("/browser")) : path); return new RedirectView(builder.build().toUriString()); } }