@Test // SPR-17630 public void toUriStringWithCurlyBraces() { assertEquals("/path?q=%7Basa%7Dasa", UriComponentsBuilder.fromUriString("/path?q={asa}asa").toUriString()); } }
protected String getServiceUrl() { if (instance.getServiceUrl() != null) { return instance.getServiceUrl(); } return UriComponentsBuilder.fromUriString(getServiceBaseUrl()).path("/").toUriString(); }
@Override public void sendRedirect(String location) throws IOException { UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(location); UriComponents uriComponents = builder.build(); // Absolute location if (uriComponents.getScheme() != null) { super.sendRedirect(location); return; } // Network-path reference if (location.startsWith("//")) { String scheme = this.request.getScheme(); super.sendRedirect(builder.scheme(scheme).toUriString()); return; } String path = uriComponents.getPath(); if (path != null) { // Relative to Servlet container root or to current request path = (path.startsWith(FOLDER_SEPARATOR) ? path : StringUtils.applyRelativePath(this.request.getRequestURI(), path)); } String result = UriComponentsBuilder .fromHttpRequest(new ServletServerHttpRequest(this.request)) .replacePath(path) .replaceQuery(uriComponents.getQuery()) .fragment(uriComponents.getFragment()) .build().normalize().toUriString(); super.sendRedirect(result); } }
protected String getServiceBaseUrl() { String baseUrl = instance.getServiceBaseUrl(); if (!StringUtils.isEmpty(baseUrl)) { return baseUrl; } return UriComponentsBuilder.newInstance() .scheme(getScheme(server.getSsl())) .host(getServiceHost()) .port(getLocalServerPort()) .toUriString(); }
@Override protected String getServiceUrl() { if (instance.getServiceUrl() != null) { return instance.getServiceUrl(); } return UriComponentsBuilder.fromUriString(getServiceBaseUrl()) .path("/") .path(getServerContextPath()) .toUriString(); }
@Test public void fromControllerWithCustomBaseUrlViaStaticCall() { UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("http://example.org:9090/base"); UriComponents uriComponents = fromController(builder, PersonControllerImpl.class).build(); assertEquals("http://example.org:9090/base/people", uriComponents.toString()); assertEquals("http://example.org:9090/base", builder.toUriString()); }
@Test public void fromMethodNameWithCustomBaseUrlViaStaticCall() { UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("http://example.org:9090/base"); UriComponents uriComponents = fromMethodName(builder, ControllerWithMethods.class, "methodWithPathVariable", "1").build(); assertEquals("http://example.org:9090/base/something/1/foo", uriComponents.toString()); assertEquals("http://example.org:9090/base", builder.toUriString()); }
protected String getHealthUrl() { if (instance.getHealthUrl() != null) { return instance.getHealthUrl(); } return UriComponentsBuilder.fromHttpUrl(getManagementBaseUrl()) .path("/") .path(getHealthEndpointPath()) .toUriString(); }
protected String getManagementUrl() { if (instance.getManagementUrl() != null) { return instance.getManagementUrl(); } return UriComponentsBuilder.fromUriString(getManagementBaseUrl()) .path("/") .path(getEndpointsWebPath()) .toUriString(); }
@Test public void resolveUriComponentsBuilder() { MethodParameter param = this.testMethod.arg(UriComponentsBuilder.class); Object value = this.resolver.resolveArgument(param, new BindingContext(), this.exchange).block(); assertNotNull(value); assertEquals(UriComponentsBuilder.class, value.getClass()); assertEquals("http://example.org:9999/next", ((UriComponentsBuilder) value).path("/next").toUriString()); }
@Test public void fromMethodNameWithCustomBaseUrlViaInstance() { UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("http://example.org:9090/base"); MvcUriComponentsBuilder mvcBuilder = relativeTo(builder); UriComponents uriComponents = mvcBuilder.withMethodName(ControllerWithMethods.class, "methodWithPathVariable", "1").build(); assertEquals("http://example.org:9090/base/something/1/foo", uriComponents.toString()); assertEquals("http://example.org:9090/base", builder.toUriString()); }
@Test public void fromControllerWithCustomBaseUrlViaInstance() { UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("http://example.org:9090/base"); MvcUriComponentsBuilder mvcBuilder = relativeTo(builder); UriComponents uriComponents = mvcBuilder.withController(PersonControllerImpl.class).build(); assertEquals("http://example.org:9090/base/people", uriComponents.toString()); assertEquals("http://example.org:9090/base", builder.toUriString()); }
@Override public void sendRedirect(String location) throws IOException { UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(location); UriComponents uriComponents = builder.build(); // Absolute location if (uriComponents.getScheme() != null) { super.sendRedirect(location); return; } // Network-path reference if (location.startsWith("//")) { String scheme = this.request.getScheme(); super.sendRedirect(builder.scheme(scheme).toUriString()); return; } String path = uriComponents.getPath(); if (path != null) { // Relative to Servlet container root or to current request path = (path.startsWith(FOLDER_SEPARATOR) ? path : StringUtils.applyRelativePath(this.request.getRequestURI(), path)); } String result = UriComponentsBuilder .fromHttpRequest(new ServletServerHttpRequest(this.request)) .replacePath(path) .replaceQuery(uriComponents.getQuery()) .fragment(uriComponents.getFragment()) .build().normalize().toUriString(); super.sendRedirect(result); } }
@Test public void fromMethodCallWithCustomBaseUrlViaStaticCall() { UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("http://example.org:9090/base"); UriComponents uriComponents = fromMethodCall(builder, on(ControllerWithMethods.class).myMethod(null)).build(); assertEquals("http://example.org:9090/base/something/else", uriComponents.toString()); assertEquals("http://example.org:9090/base", builder.toUriString()); }
.toString()).toUriString(); String endpointLocalPath = getEndpointLocalPath(pathWithinApplication);
String uriString = template.toUriString(); logger.info("Redirecting to " + uriString);
@Test public void fromMethodCallWithCustomBaseUrlViaInstance() { UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("http://example.org:9090/base"); MvcUriComponentsBuilder mvcBuilder = relativeTo(builder); UriComponents result = mvcBuilder.withMethodCall(on(ControllerWithMethods.class).myMethod(null)).build(); assertEquals("http://example.org:9090/base/something/else", result.toString()); assertEquals("http://example.org:9090/base", builder.toUriString()); }
protected String getManagementBaseUrl() { String baseUrl = instance.getManagementBaseUrl(); if (!StringUtils.isEmpty(baseUrl)) { return baseUrl; } if (isManagementPortEqual()) { return this.getServiceUrl(); } Ssl ssl = management.getSsl() != null ? management.getSsl() : server.getSsl(); return UriComponentsBuilder.newInstance() .scheme(getScheme(ssl)) .host(getManagementHost()) .port(getLocalManagementPort()) .toUriString(); }
@Override protected String getManagementBaseUrl() { String baseUrl = instance.getManagementBaseUrl(); if (!StringUtils.isEmpty(baseUrl)) { return baseUrl; } if (isManagementPortEqual()) { return UriComponentsBuilder.fromHttpUrl(getServiceUrl()) .path("/") .path(getDispatcherServletPrefix()) .path(getManagementContextPath()) .toUriString(); } Ssl ssl = management.getSsl() != null ? management.getSsl() : server.getSsl(); return UriComponentsBuilder.newInstance() .scheme(getScheme(ssl)) .host(getManagementHost()) .port(getLocalManagementPort()) .path(getManagementContextPath()) .toUriString(); }
@Override @ResponseBody public Map<String, Map<String, Link>> links(ServerWebExchange exchange) { String requestUri = UriComponentsBuilder .fromUri(exchange.getRequest().getURI()).replaceQuery(null) .toUriString(); return Collections.singletonMap("_links", WebFluxEndpointHandlerMapping.this.linksResolver .resolveLinks(requestUri)); }