@RequestMapping(value = "/", method = RequestMethod.GET) String index(Model model, // @QuerydslPredicate(root = User.class) Predicate predicate, // @PageableDefault(sort = { "lastname", "firstname" }) Pageable pageable, // @RequestParam MultiValueMap<String, String> parameters) { ServletUriComponentsBuilder builder = ServletUriComponentsBuilder.fromCurrentRequest(); builder.replaceQueryParam("page", new Object[0]); model.addAttribute("baseUri", builder.build().toUri()); model.addAttribute("users", repository.findAll(predicate, pageable)); return "index"; } }
builder.append("<form id=\"confirmationForm\" name=\"confirmationForm\" action=\""); String requestPath = ServletUriComponentsBuilder.fromContextPath(request).build().getPath(); if (requestPath == null) { requestPath = "";
/** * Generate a full URL of the service, mostly for use in templates. * @return the app URL * @see io.spring.initializr.metadata.InitializrConfiguration.Env#isForceSsl() */ protected String generateAppUrl() { ServletUriComponentsBuilder builder = ServletUriComponentsBuilder .fromCurrentServletMapping(); if (isForceSsl()) { builder.scheme("https"); } return builder.build().toString(); }
@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 fromContextPath() { this.request.setRequestURI("/mvc-showcase/data/param"); this.request.setQueryString("foo=123"); String result = ServletUriComponentsBuilder.fromContextPath(this.request).build().toUriString(); assertEquals("http://localhost/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 public void fromRequestUri() { this.request.setRequestURI("/mvc-showcase/data/param"); this.request.setQueryString("foo=123"); String result = ServletUriComponentsBuilder.fromRequestUri(this.request).build().toUriString(); assertEquals("http://localhost/mvc-showcase/data/param", result); }
@Test public void fromServletMapping() { this.request.setRequestURI("/mvc-showcase/app/simple"); this.request.setServletPath("/app"); this.request.setQueryString("foo=123"); String result = ServletUriComponentsBuilder.fromServletMapping(this.request).build().toUriString(); assertEquals("http://localhost/mvc-showcase/app", result); }
@Test public void resolveArgument() throws Exception { this.servletRequest.setContextPath("/myapp"); this.servletRequest.setServletPath("/main"); this.servletRequest.setPathInfo("/accounts"); Object actual = this.resolver.resolveArgument(this.builderParam, new ModelAndViewContainer(), this.webRequest, null); assertNotNull(actual); assertEquals(ServletUriComponentsBuilder.class, actual.getClass()); assertEquals("http://localhost/myapp/main", ((ServletUriComponentsBuilder) actual).build().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 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 fromContextPathWithForwardedPrefix() throws Exception { this.request.addHeader("X-Forwarded-Prefix", "/prefix"); this.request.setContextPath("/mvc-showcase"); this.request.setRequestURI("/mvc-showcase/simple"); HttpServletRequest requestToUse = adaptFromForwardedHeaders(this.request); String result = ServletUriComponentsBuilder.fromContextPath(requestToUse).build().toUriString(); assertEquals("http://localhost/prefix", result); }
@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 // SPR-16650 public void fromServletMappingWithForwardedPrefix() throws Exception { this.request.addHeader("X-Forwarded-Prefix", "/prefix"); this.request.setContextPath("/mvc-showcase"); this.request.setServletPath("/app"); this.request.setRequestURI("/mvc-showcase/app/simple"); HttpServletRequest requestToUse = adaptFromForwardedHeaders(this.request); String result = ServletUriComponentsBuilder.fromServletMapping(requestToUse).build().toUriString(); assertEquals("http://localhost/prefix/app", result); }
@Test public void fromCurrentRequest() { this.request.setRequestURI("/mvc-showcase/data/param"); this.request.setQueryString("foo=123"); RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(this.request)); try { String result = ServletUriComponentsBuilder.fromCurrentRequest().build().toUriString(); assertEquals("http://localhost/mvc-showcase/data/param?foo=123", result); } finally { RequestContextHolder.resetRequestAttributes(); } }
@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()); }
protected Link getDefaultSelfLink() { return new Link(ServletUriComponentsBuilder.fromCurrentRequest().build().toUriString()); } }
/** * Returns the href for the {@link Curie} instance to be created. Will prepend the current application URI (servlet * mapping) in case the template is not an absolute one in the first place. * * @param name will never be {@literal null} or empty. * @param template will never be {@literal null}. * @return the {@link String} to be used as href in the {@link Curie} to be created, must not be {@literal null}. */ protected String getCurieHref(String name, UriTemplate template) { if (template.toString().startsWith("http")) { return template.toString(); } String applicationUri = ServletUriComponentsBuilder.fromCurrentServletMapping().build().expand().toString(); return applicationUri.concat(template.toString()); }