protected UriComponents expandAndEncode(UriComponentsBuilder builder, Object[] uriVariables) { if (!isStrictEncoding()) { return builder.buildAndExpand(uriVariables).encode(); } else { Object[] encodedUriVars = UriUtils.encodeUriVariables(uriVariables); return builder.buildAndExpand(encodedUriVars); } }
protected UriComponents expandAndEncode(UriComponentsBuilder builder, Map<String, ?> uriVariables) { if (!isStrictEncoding()) { return builder.buildAndExpand(uriVariables).encode(); } else { Map<String, ?> encodedUriVars = UriUtils.encodeUriVariables(uriVariables); return builder.buildAndExpand(encodedUriVars); } }
@Override public ListenableFuture<WebSocketSession> doHandshake(WebSocketHandler webSocketHandler, String uriTemplate, Object... uriVars) { Assert.notNull(uriTemplate, "'uriTemplate' must not be null"); URI uri = UriComponentsBuilder.fromUriString(uriTemplate).buildAndExpand(uriVars).encode().toUri(); return doHandshake(webSocketHandler, null, uri); }
@Override public ListenableFuture<WebSocketSession> doHandshake( WebSocketHandler handler, String uriTemplate, Object... uriVars) { Assert.notNull(uriTemplate, "uriTemplate must not be null"); URI uri = UriComponentsBuilder.fromUriString(uriTemplate).buildAndExpand(uriVars).encode().toUri(); return doHandshake(handler, null, uri); }
private URI getUrlToUse() { MultiValueMap<String, String> params = this.queryParamsBuilder.buildAndExpand().encode().getQueryParams(); if (!params.isEmpty()) { return UriComponentsBuilder.fromUri(this.url).queryParams(params).build(true).toUri(); } return this.url; } }
/** * Asserts the request was redirected to the given URL template. * <p>This method accepts exact matches against the expanded and encoded URL template. * @param urlTemplate a URL template; the expanded URL will be encoded * @param uriVars zero or more URI variables to populate the template * @see UriComponentsBuilder#fromUriString(String) */ public static ResultMatcher redirectedUrlTemplate(String urlTemplate, Object... uriVars) { String uri = UriComponentsBuilder.fromUriString(urlTemplate).buildAndExpand(uriVars).encode().toUriString(); return redirectedUrl(uri); }
/** * Asserts the request was forwarded to the given URL template. * <p>This method accepts exact matches against the expanded and encoded URL template. * @param urlTemplate a URL template; the expanded URL will be encoded * @param uriVars zero or more URI variables to populate the template * @see UriComponentsBuilder#fromUriString(String) */ public static ResultMatcher forwardedUrlTemplate(String urlTemplate, Object... uriVars) { String uri = UriComponentsBuilder.fromUriString(urlTemplate).buildAndExpand(uriVars).encode().toUriString(); return forwardedUrl(uri); }
@Override public ListenableFuture<WebSocketSession> doHandshake(WebSocketHandler handler, String uriTemplate, Object... uriVars) { URI uri = UriComponentsBuilder.fromUriString(uriTemplate).buildAndExpand(uriVars).encode().toUri(); return doHandshake(handler, null, uri); }
@Test public void buildAndExpandOpaque() { UriComponents result = UriComponentsBuilder.fromUriString("mailto:{user}@{domain}") .buildAndExpand("foo", "example.com"); assertEquals("mailto:foo@example.com", result.toUriString()); Map<String, String> values = new HashMap<>(); values.put("user", "foo"); values.put("domain", "example.com"); UriComponentsBuilder.fromUriString("mailto:{user}@{domain}").buildAndExpand(values); assertEquals("mailto:foo@example.com", result.toUriString()); }
@Test // SPR-16710 public void fromMethodNameWithStringReturnType() { UriComponents uriComponents = fromMethodName( BookingControllerWithString.class, "getBooking", 21L).buildAndExpand(42); assertEquals("http://localhost/hotels/42/bookings/21", uriComponents.encode().toUri().toString()); }
@Test(expected = IllegalStateException.class) // SPR-16710 public void fromMethodCallWithStringReturnType() { UriComponents uriComponents = fromMethodCall( on(BookingControllerWithString.class).getBooking(21L)).buildAndExpand(42); assertEquals("http://localhost/hotels/42/bookings/21", uriComponents.encode().toUri().toString()); }
@Test // SPR-16710 public void fromMethodCallWithObjectReturnType() { UriComponents uriComponents = fromMethodCall( on(BookingControllerWithObject.class).getBooking(21L)).buildAndExpand(42); assertEquals("http://localhost/hotels/42/bookings/21", uriComponents.encode().toUri().toString()); }
@Test // SPR-16710 public void fromMethodCallWithModelAndViewReturnType() { UriComponents uriComponents = fromMethodCall( on(BookingControllerWithModelAndView.class).getBooking(21L)).buildAndExpand(42); assertEquals("http://localhost/hotels/42/bookings/21", uriComponents.encode().toUri().toString()); }
@Test public void fromMethodNameTypeLevelPathVariable() { this.request.setContextPath("/myapp"); UriComponents uriComponents = fromMethodName( PersonsAddressesController.class, "getAddressesForCountry", "DE").buildAndExpand("1"); assertThat(uriComponents.toUriString(), is("http://localhost/myapp/people/1/addresses/DE")); }
@Test public void fromControllerUriTemplate() { UriComponents uriComponents = fromController(PersonsAddressesController.class).buildAndExpand(15); assertThat(uriComponents.toUriString(), endsWith("/people/15/addresses")); }
@Test // SPR-13719 public void arbitraryMethod() { String httpMethod = "REPort"; URI url = UriComponentsBuilder.fromPath("/foo/{bar}").buildAndExpand(42).toUri(); this.builder = new MockHttpServletRequestBuilder(httpMethod, url); MockHttpServletRequest request = this.builder.buildRequest(this.servletContext); assertEquals(httpMethod, request.getMethod()); assertEquals("/foo/42", request.getPathInfo()); }
@Test // SPR-10272 public void pathExtension() { this.request.setRequestURI("/rest/books/6.json"); ServletUriComponentsBuilder builder = ServletUriComponentsBuilder.fromRequestUri(this.request); String extension = builder.removePathExtension(); String result = builder.path("/pages/1.{ext}").buildAndExpand(extension).toUriString(); assertEquals("http://localhost/rest/books/6/pages/1.json", result); }
@Test public void fromMethodCallWithTypeLevelUriVars() { UriComponents uriComponents = fromMethodCall( on(PersonsAddressesController.class).getAddressesForCountry("DE")).buildAndExpand(15); assertThat(uriComponents.toUriString(), endsWith("/people/15/addresses/DE")); }
@Test public void fromControllerWithPrefix() { initWebApplicationContext(PathPrefixWebConfig.class); this.request.setServerName("example.org"); this.request.setServerPort(9999); this.request.setContextPath("/base"); assertEquals("http://example.org:9999/base/api/people/123/addresses", fromController(PersonsAddressesController.class).buildAndExpand("123").toString()); }
@Test public void fromMethodWithPrefix() { initWebApplicationContext(PathPrefixWebConfig.class); this.request.setServerName("example.org"); this.request.setServerPort(9999); this.request.setContextPath("/base"); assertEquals("http://example.org:9999/base/api/people/123/addresses/DE", fromMethodCall(on(PersonsAddressesController.class).getAddressesForCountry("DE")) .buildAndExpand("123").toString()); }