@Override public DefaultUriBuilder host(@Nullable String host) { this.uriComponentsBuilder.host(host); return this; }
@Override public DefaultUriBuilder host(@Nullable String host) { this.uriComponentsBuilder.host(host); return this; }
private void adaptForwardedHost(String hostToUse) { int portSeparatorIdx = hostToUse.lastIndexOf(':'); if (portSeparatorIdx > hostToUse.lastIndexOf(']')) { host(hostToUse.substring(0, portSeparatorIdx)); port(Integer.parseInt(hostToUse.substring(portSeparatorIdx + 1))); } else { host(hostToUse); port(null); } }
private void adaptForwardedHost(String hostToUse) { int portSeparatorIdx = hostToUse.lastIndexOf(':'); if (portSeparatorIdx > hostToUse.lastIndexOf(']')) { host(hostToUse.substring(0, portSeparatorIdx)); port(Integer.parseInt(hostToUse.substring(portSeparatorIdx + 1))); } else { host(hostToUse); port(null); } }
/** * Create an instance by parsing the "Origin" header of an HTTP request. * @see <a href="https://tools.ietf.org/html/rfc6454">RFC 6454</a> */ public static UriComponentsBuilder fromOriginHeader(String origin) { Matcher matcher = URI_PATTERN.matcher(origin); if (matcher.matches()) { UriComponentsBuilder builder = new UriComponentsBuilder(); String scheme = matcher.group(2); String host = matcher.group(6); String port = matcher.group(8); if (StringUtils.hasLength(scheme)) { builder.scheme(scheme); } builder.host(host); if (StringUtils.hasLength(port)) { builder.port(port); } return builder; } else { throw new IllegalArgumentException("[" + origin + "] is not a valid \"Origin\" header value"); } }
throw new IllegalArgumentException("[" + httpUrl + "] is not a valid HTTP URL"); builder.host(host); String port = matcher.group(7); if (StringUtils.hasLength(port)) {
builder.host(host); if (StringUtils.hasLength(port)) { builder.port(port);
public static String addSubdomainToUrl(String url, String subdomain) { if (!hasText(subdomain)) { return url; } subdomain = subdomain.trim(); subdomain = subdomain.endsWith(".") ? subdomain : subdomain + "."; UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url); builder.host(subdomain + builder.build().getHost()); return builder.build().toUriString(); }
/** * Create an instance by parsing the "Origin" header of an HTTP request. * @see <a href="https://tools.ietf.org/html/rfc6454">RFC 6454</a> */ public static UriComponentsBuilder fromOriginHeader(String origin) { Matcher matcher = URI_PATTERN.matcher(origin); if (matcher.matches()) { UriComponentsBuilder builder = new UriComponentsBuilder(); String scheme = matcher.group(2); String host = matcher.group(6); String port = matcher.group(8); if (StringUtils.hasLength(scheme)) { builder.scheme(scheme); } builder.host(host); if (StringUtils.hasLength(port)) { builder.port(port); } return builder; } else { throw new IllegalArgumentException("[" + origin + "] is not a valid \"Origin\" header value"); } }
throw new IllegalArgumentException("[" + httpUrl + "] is not a valid HTTP URL"); builder.host(host); String port = matcher.group(7); if (StringUtils.hasLength(port)) {
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(); }
public static UriComponentsBuilder getURIBuilder(String path, boolean zoneSwitchPossible) { UriComponentsBuilder builder = ServletUriComponentsBuilder.fromCurrentContextPath().path(path); if (zoneSwitchPossible) { String host = builder.build().getHost(); IdentityZone current = IdentityZoneHolder.get(); if (host != null && !IdentityZoneHolder.isUaa()) { if (!host.startsWith(current.getSubdomain() + ".")) { host = current.getSubdomain() + "." + host; builder.host(host); } } } return builder; }
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(); }
public static String constructTokenEndpointUrl(String issuer) throws URISyntaxException { URI uri; if (!IdentityZoneHolder.isUaa()) { String zone_issuer = IdentityZoneHolder.get().getConfig() != null ? IdentityZoneHolder.get().getConfig().getIssuer() : null; if(zone_issuer != null) { uri = validateIssuer(zone_issuer); return UriComponentsBuilder.fromUri(uri).pathSegment("oauth/token").build().toUriString(); } } uri = validateIssuer(issuer); String hostToUse = uri.getHost(); if (hasText(IdentityZoneHolder.get().getSubdomain())) { hostToUse = IdentityZoneHolder.get().getSubdomain() + "." + hostToUse; } return UriComponentsBuilder.fromUri(uri).host(hostToUse).pathSegment("oauth/token").build().toUriString(); }
@Test public void plain() throws URISyntaxException { UriComponentsBuilder builder = UriComponentsBuilder.newInstance(); UriComponents result = builder.scheme("http").host("example.com") .path("foo").queryParam("bar").fragment("baz") .build(); assertEquals("http", result.getScheme()); assertEquals("example.com", result.getHost()); assertEquals("foo", result.getPath()); assertEquals("bar", result.getQuery()); assertEquals("baz", result.getFragment()); URI expected = new URI("http://example.com/foo?bar#baz"); assertEquals("Invalid result URI", expected, result.toUri()); }
@Test public void multipleFromSameBuilder() throws URISyntaxException { UriComponentsBuilder builder = UriComponentsBuilder.newInstance() .scheme("http").host("example.com").pathSegment("foo"); UriComponents result1 = builder.build(); builder = builder.pathSegment("foo2").queryParam("bar").fragment("baz"); UriComponents result2 = builder.build(); assertEquals("http", result1.getScheme()); assertEquals("example.com", result1.getHost()); assertEquals("/foo", result1.getPath()); URI expected = new URI("http://example.com/foo"); assertEquals("Invalid result URI", expected, result1.toUri()); assertEquals("http", result2.getScheme()); assertEquals("example.com", result2.getHost()); assertEquals("/foo/foo2", result2.getPath()); assertEquals("bar", result2.getQuery()); assertEquals("baz", result2.getFragment()); expected = new URI("http://example.com/foo/foo2?bar#baz"); assertEquals("Invalid result URI", expected, result2.toUri()); }
@Override protected void copyToUriComponentsBuilder(UriComponentsBuilder builder) { if (getScheme() != null) { builder.scheme(getScheme()); } if (getUserInfo() != null) { builder.userInfo(getUserInfo()); } if (getHost() != null) { builder.host(getHost()); } // Avoid parsing the port, may have URI variable.. if (this.port != null) { builder.port(this.port); } this.path.copyToUriComponentsBuilder(builder); if (!getQueryParams().isEmpty()) { builder.queryParams(getQueryParams()); } if (getFragment() != null) { builder.fragment(getFragment()); } }
@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 protected void copyToUriComponentsBuilder(UriComponentsBuilder builder) { if (getScheme() != null) { builder.scheme(getScheme()); } if (getUserInfo() != null) { builder.userInfo(getUserInfo()); } if (getHost() != null) { builder.host(getHost()); } // Avoid parsing the port, may have URI variable.. if (this.port != null) { builder.port(this.port); } this.path.copyToUriComponentsBuilder(builder); if (!getQueryParams().isEmpty()) { builder.queryParams(getQueryParams()); } if (getFragment() != null) { builder.fragment(getFragment()); } }
@Test public void testClone() { UriComponentsBuilder builder1 = UriComponentsBuilder.newInstance(); builder1.scheme("http").host("e1.com").path("/p1").pathSegment("ps1").queryParam("q1").fragment("f1").encode(); UriComponentsBuilder builder2 = (UriComponentsBuilder) builder1.clone(); builder2.scheme("https").host("e2.com").path("p2").pathSegment("{ps2}").queryParam("q2").fragment("f2"); UriComponents result1 = builder1.build(); assertEquals("http", result1.getScheme()); assertEquals("e1.com", result1.getHost()); assertEquals("/p1/ps1", result1.getPath()); assertEquals("q1", result1.getQuery()); assertEquals("f1", result1.getFragment()); UriComponents result2 = builder2.buildAndExpand("ps2;a"); assertEquals("https", result2.getScheme()); assertEquals("e2.com", result2.getHost()); assertEquals("/p1/ps1/p2/ps2%3Ba", result2.getPath()); assertEquals("q1&q2", result2.getQuery()); assertEquals("f2", result2.getFragment()); }