private void addRequestParams(MockHttpServletRequest request, MultiValueMap<String, String> map) { map.forEach((key, values) -> values.forEach(value -> { value = (value != null ? UriUtils.decode(value, StandardCharsets.UTF_8) : null); request.addParameter(UriUtils.decode(key, StandardCharsets.UTF_8), value); })); }
/** * Encode the given URI authority with the given encoding. * @param authority the authority to be encoded * @param charset the character encoding to encode to * @return the encoded authority * @since 5.0 */ public static String encodeAuthority(String authority, Charset charset) { return encode(authority, charset, HierarchicalUriComponents.Type.AUTHORITY); }
/** * Replace URI template variables in the target URL with encoded model * attributes or URI variables from the current request. Model attributes * referenced in the URL are removed from the model. * @param targetUrl the redirect URL * @param model a Map that contains model attributes * @param currentUriVariables current request URI variables to use * @param encodingScheme the encoding scheme to use * @throws UnsupportedEncodingException if string encoding failed */ protected StringBuilder replaceUriTemplateVariables( String targetUrl, Map<String, Object> model, Map<String, String> currentUriVariables, String encodingScheme) throws UnsupportedEncodingException { StringBuilder result = new StringBuilder(); Matcher matcher = URI_TEMPLATE_VARIABLE_PATTERN.matcher(targetUrl); int endLastMatch = 0; while (matcher.find()) { String name = matcher.group(1); Object value = (model.containsKey(name) ? model.remove(name) : currentUriVariables.get(name)); if (value == null) { throw new IllegalArgumentException("Model has no value for key '" + name + "'"); } result.append(targetUrl.substring(endLastMatch, matcher.start())); result.append(UriUtils.encodePathSegment(value.toString(), encodingScheme)); endLastMatch = matcher.end(); } result.append(targetUrl.substring(endLastMatch, targetUrl.length())); return result; }
@Override public UserDetails loadUserByUsername(String clientId) throws UsernameNotFoundException { try { String decodedClientId = UriUtils.decode(clientId, "UTF-8"); ClientDetailsEntity client = clientDetailsService.loadClientByClientId(decodedClientId); if (client != null) { String encodedPassword = UriUtils.encodePathSegment(Strings.nullToEmpty(client.getClientSecret()), "UTF-8"); if (config.isHeartMode() || // if we're running HEART mode turn off all client secrets (client.getTokenEndpointAuthMethod() != null && (client.getTokenEndpointAuthMethod().equals(AuthMethod.PRIVATE_KEY) || client.getTokenEndpointAuthMethod().equals(AuthMethod.SECRET_JWT)))) { // Issue a random password each time to prevent password auth from being used (or skipped) // for private key or shared key clients, see #715 encodedPassword = new BigInteger(512, new SecureRandom()).toString(16); } boolean enabled = true; boolean accountNonExpired = true; boolean credentialsNonExpired = true; boolean accountNonLocked = true; Collection<GrantedAuthority> authorities = new HashSet<>(client.getAuthorities()); authorities.add(ROLE_CLIENT); return new User(decodedClientId, encodedPassword, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities); } else { throw new UsernameNotFoundException("Client not found: " + clientId); } } catch (UnsupportedEncodingException | InvalidClientException e) { throw new UsernameNotFoundException("Client not found: " + clientId); } }
@Test public void encodePath() { assertEquals("Invalid encoded result", "/foo/bar", UriUtils.encodePath("/foo/bar", CHARSET)); assertEquals("Invalid encoded result", "/foo%20bar", UriUtils.encodePath("/foo bar", CHARSET)); assertEquals("Invalid encoded result", "/Z%C3%BCrich", UriUtils.encodePath("/Z\u00fcrich", CHARSET)); }
try { uri = StringUtils.replace(uri, template, (value != null ? UriUtils.encodePath(value, encoding) : "")); try { uri = StringUtils.replace(uri, template, (value != null ? UriUtils.encodePathSegment(value, encoding) : ""));
private String encode(String value) { try { //return URLEncoder.encode(value,"UTF-8"); return UriUtils.encodeQueryParam(value, "UTF-8"); } catch (UnsupportedEncodingException x) { throw new IllegalArgumentException(x); } }
sb.append(encodeScheme(scheme, encoding)); sb.append(':'); sb.append("//"); if (userInfo != null) { sb.append(encodeUserInfo(userInfo, encoding)); sb.append('@'); sb.append(encodeHost(host, encoding)); sb.append(encodePort(port, encoding)); sb.append(encodePath(path, encoding)); sb.append(encodeQuery(query, encoding)); sb.append(encodeFragment(fragment, encoding));
@Override public boolean test(ServerRequest request) { String pathExtension = UriUtils.extractFileExtension(request.path()); return this.extensionPredicate.test(pathExtension); }
@Test public void encodeFragment() { assertEquals("Invalid encoded result", "foobar", UriUtils.encodeFragment("foobar", CHARSET)); assertEquals("Invalid encoded result", "foo%20bar", UriUtils.encodeFragment("foo bar", CHARSET)); assertEquals("Invalid encoded result", "foobar/", UriUtils.encodeFragment("foobar/", CHARSET)); }
String encoding = this.pageContext.getResponse().getCharacterEncoding(); try { requestUri = UriUtils.encodePath(requestUri, encoding);
try { uri = StringUtils.replace(uri, template, (value != null ? UriUtils.encodePath(value, encoding) : "")); try { uri = StringUtils.replace(uri, template, (value != null ? UriUtils.encodePathSegment(value, encoding) : ""));
qs.append(UriUtils.encodeQueryParam(param.getName(), encoding)); if (param.getValue() != null) { qs.append("="); qs.append(UriUtils.encodeQueryParam(param.getValue(), encoding));
@Override @Nullable protected String getMediaTypeKey(NativeWebRequest webRequest) { HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class); if (request == null) { return null; } String path = this.urlPathHelper.getLookupPathForRequest(request); String extension = UriUtils.extractFileExtension(path); return (StringUtils.hasText(extension) ? extension.toLowerCase(Locale.ENGLISH) : null); }
/** * Encodes the given fragment value. * * @param source must not be {@literal null}. * @return */ public static String encodeFragment(Object source) { Assert.notNull(source, "Fragment value must not be null!"); try { return UriUtils.encodeFragment(source.toString(), ENCODING); } catch (Throwable e) { throw new IllegalStateException(e); } } }
private static MultiValueMap<String, String> parseQueryParams(URI uri) { MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<>(); String query = uri.getRawQuery(); if (query != null) { Matcher matcher = QUERY_PATTERN.matcher(query); while (matcher.find()) { String name = UriUtils.decode(matcher.group(1), StandardCharsets.UTF_8); String eq = matcher.group(2); String value = matcher.group(3); if (value != null) { value = UriUtils.decode(value, StandardCharsets.UTF_8); } else { value = (StringUtils.hasLength(eq) ? "" : null); } queryParams.add(name, value); } } return queryParams; }
/** * Encode the given URI user info with the given encoding. * @param userInfo the user info to be encoded * @param encoding the character encoding to encode to * @return the encoded user info */ public static String encodeUserInfo(String userInfo, String encoding) { return encode(userInfo, encoding, HierarchicalUriComponents.Type.USER_INFO); }
/** * Replace URI template variables in the target URL with encoded model * attributes or URI variables from the current request. Model attributes * referenced in the URL are removed from the model. * @param targetUrl the redirect URL * @param model a Map that contains model attributes * @param currentUriVariables current request URI variables to use * @param encodingScheme the encoding scheme to use * @throws UnsupportedEncodingException if string encoding failed */ protected StringBuilder replaceUriTemplateVariables( String targetUrl, Map<String, Object> model, Map<String, String> currentUriVariables, String encodingScheme) throws UnsupportedEncodingException { StringBuilder result = new StringBuilder(); Matcher matcher = URI_TEMPLATE_VARIABLE_PATTERN.matcher(targetUrl); int endLastMatch = 0; while (matcher.find()) { String name = matcher.group(1); Object value = (model.containsKey(name) ? model.remove(name) : currentUriVariables.get(name)); if (value == null) { throw new IllegalArgumentException("Model has no value for key '" + name + "'"); } result.append(targetUrl.substring(endLastMatch, matcher.start())); result.append(UriUtils.encodePathSegment(value.toString(), encodingScheme)); endLastMatch = matcher.end(); } result.append(targetUrl.substring(endLastMatch, targetUrl.length())); return result; }
String encoding = this.pageContext.getResponse().getCharacterEncoding(); try { requestUri = UriUtils.encodePath(requestUri, encoding);
usedParams.add(param.getName()); try { uri = uri.replace(template, UriUtils.encodePath(param.getValue(), encoding)); usedParams.add(param.getName()); try { uri = uri.replace(template, UriUtils.encodePathSegment(param.getValue(), encoding));