@Override public DefaultUriBuilder replaceQuery(@Nullable String query) { this.uriComponentsBuilder.replaceQuery(query); return this; }
@Override public DefaultUriBuilder replaceQuery(@Nullable String query) { this.uriComponentsBuilder.replaceQuery(query); return this; }
@Test public void replaceQuery() { UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("http://example.com/foo?foo=bar&baz=qux"); builder.replaceQuery("baz=42"); UriComponents result = builder.build(); assertEquals("http://example.com/foo?baz=42", result.toUriString()); builder = UriComponentsBuilder.fromUriString("http://example.com/foo?foo=bar&baz=qux"); builder.replaceQuery(null); result = builder.build(); assertEquals("http://example.com/foo", result.toUriString()); }
.replaceQuery(query).build().toString();
private String expandRedirectUri(HttpServletRequest request, ClientRegistration clientRegistration, String action) { // Supported URI variables -> baseUrl, action, registrationId // Used in -> CommonOAuth2Provider.DEFAULT_REDIRECT_URL = "{baseUrl}/{action}/oauth2/code/{registrationId}" Map<String, String> uriVariables = new HashMap<>(); uriVariables.put("registrationId", clientRegistration.getRegistrationId()); String baseUrl = UriComponentsBuilder.fromHttpUrl(UrlUtils.buildFullRequestUrl(request)) .replaceQuery(null) .replacePath(request.getContextPath()) .build() .toUriString(); uriVariables.put("baseUrl", baseUrl); if (action != null) { uriVariables.put("action", action); } return UriComponentsBuilder.fromUriString(clientRegistration.getRedirectUriTemplate()) .buildAndExpand(uriVariables) .toUriString(); } }
@Override public GatewayFilter apply(NameValueConfig config) { return (exchange, chain) -> { URI uri = exchange.getRequest().getURI(); StringBuilder query = new StringBuilder(); String originalQuery = uri.getRawQuery(); if (StringUtils.hasText(originalQuery)) { query.append(originalQuery); if (originalQuery.charAt(originalQuery.length() - 1) != '&') { query.append('&'); } } //TODO urlencode? query.append(config.getName()); query.append('='); query.append(config.getValue()); try { URI newUri = UriComponentsBuilder.fromUri(uri) .replaceQuery(query.toString()) .build(true) .toUri(); ServerHttpRequest request = exchange.getRequest().mutate().uri(newUri).build(); return chain.filter(exchange.mutate().request(request).build()); } catch (RuntimeException ex) { throw new IllegalStateException("Invalid URI query: \"" + query.toString() + "\""); } }; }
return UriComponentsBuilder.fromUri(uri).replacePath(null).replaceQuery(null);
@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); } }
private String expandRedirectUri(ServerHttpRequest request, ClientRegistration clientRegistration) { // Supported URI variables -> baseUrl, action, registrationId // Used in -> CommonOAuth2Provider.DEFAULT_REDIRECT_URL = "{baseUrl}/{action}/oauth2/code/{registrationId}" Map<String, String> uriVariables = new HashMap<>(); uriVariables.put("registrationId", clientRegistration.getRegistrationId()); String baseUrl = UriComponentsBuilder.fromHttpRequest(new ServerHttpRequestDecorator(request)) .replacePath(request.getPath().contextPath().value()) .replaceQuery(null) .build() .toUriString(); uriVariables.put("baseUrl", baseUrl); if (AuthorizationGrantType.AUTHORIZATION_CODE.equals(clientRegistration.getAuthorizationGrantType())) { String loginAction = "login"; uriVariables.put("action", loginAction); } return UriComponentsBuilder.fromUriString(clientRegistration.getRedirectUriTemplate()) .buildAndExpand(uriVariables) .toUriString(); } }
private String expandRedirectUri(HttpServletRequest request, ClientRegistration clientRegistration) { String baseUrl = UriComponentsBuilder.fromHttpUrl(UrlUtils.buildFullRequestUrl(request)) .replaceQuery(null) .replacePath(request.getContextPath()) .build() .toUriString(); Map<String, String> uriVariables = new HashMap<>(); uriVariables.put("baseUrl", baseUrl); uriVariables.put("action", "login"); uriVariables.put("registrationId", clientRegistration.getRegistrationId()); return UriComponentsBuilder.fromUriString(clientRegistration.getRedirectUriTemplate()) .buildAndExpand(uriVariables) .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); } }
.replaceQuery(null) .build() .toUriString();
.replaceQuery(null) .build() .toUriString();
private URI upgradeConnection(URI uri, String scheme) { UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUri(uri).scheme(scheme); if (uri.getRawQuery() != null) { // When building the URI, UriComponentsBuilder verify the allowed characters and does not // support the '+' so we replace it for its equivalent '%20'. // See issue https://jira.spring.io/browse/SPR-10172 uriComponentsBuilder.replaceQuery(uri.getRawQuery().replace("+", "%20")); } return uriComponentsBuilder.build(true).toUri(); }
@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)); }
@Override public DefaultUriBuilder replaceQuery(@Nullable String query) { this.uriComponentsBuilder.replaceQuery(query); return this; }
@Override public DefaultUriBuilder replaceQuery(@Nullable String query) { this.uriComponentsBuilder.replaceQuery(query); return this; }
private static URI upgradeConnection(URI uri, String scheme) { UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUri(uri).scheme(scheme); if (uri.getRawQuery() != null) { // When building the URI, UriComponentsBuilder verify the allowed characters and does not // support the '+' so we replace it for its equivalent '%20'. // See issue https://jira.spring.io/browse/SPR-10172 uriComponentsBuilder.replaceQuery(uri.getRawQuery().replace("+", "%20")); } return uriComponentsBuilder.build(true).toUri(); } }
@Override URI resolve(final URI baseUrl, final URI uri) { final UriComponentsBuilder builder = UriComponentsBuilder.fromUri(baseUrl); @Nullable final String path = uri.getRawPath(); if (!nullToEmpty(path).isEmpty()) { builder.path("/").path(path); } return builder .replaceQuery(uri.getRawQuery()) .build() .toUri(); } };
@Override URI resolve(final URI baseUrl, final URI uri) { final UriComponentsBuilder builder = UriComponentsBuilder.fromUri(baseUrl); @Nullable final String path = uri.getRawPath(); if (!nullToEmpty(path).isEmpty()) { builder.path("/").path(path); } return builder .replaceQuery(uri.getRawQuery()) .build() .toUri(); } };