From ca410fea531c5d486e034df3ab815229d7356f86 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Wed, 13 May 2015 15:03:33 -0400 Subject: [PATCH] Add component-neutral encode option in UriUtils Issue: SPR-12750, SPR-12942 --- .../web/util/HierarchicalUriComponents.java | 11 ++++++++--- .../org/springframework/web/util/UriUtils.java | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/util/HierarchicalUriComponents.java b/spring-web/src/main/java/org/springframework/web/util/HierarchicalUriComponents.java index acdd35a9c60..09a2b3ec361 100644 --- a/spring-web/src/main/java/org/springframework/web/util/HierarchicalUriComponents.java +++ b/spring-web/src/main/java/org/springframework/web/util/HierarchicalUriComponents.java @@ -61,7 +61,6 @@ final class HierarchicalUriComponents extends UriComponents { private final boolean encoded; - /** * Package-private constructor. All arguments are optional, and can be {@code null}. * @param scheme the scheme @@ -479,7 +478,7 @@ final class HierarchicalUriComponents extends UriComponents { // inner types /** - * Enumeration used to identify the parts of a URI. + * Enumeration used to identify the allowed characters per URI component. *

Contains methods to indicate whether a given character is valid in a specific URI component. * @see RFC 3986 */ @@ -555,6 +554,12 @@ final class HierarchicalUriComponents extends UriComponents { public boolean isAllowed(int c) { return isPchar(c) || '/' == c || '?' == c; } + }, + URI { + @Override + public boolean isAllowed(int c) { + return isUnreserved(c); + } }; /** @@ -600,7 +605,7 @@ final class HierarchicalUriComponents extends UriComponents { * Indicates whether the given character is in the {@code reserved} set. * @see RFC 3986, appendix A */ - protected boolean isReserved(char c) { + protected boolean isReserved(int c) { return isGenericDelimiter(c) || isSubDelimiter(c); } diff --git a/spring-web/src/main/java/org/springframework/web/util/UriUtils.java b/spring-web/src/main/java/org/springframework/web/util/UriUtils.java index e33531ba62e..16f213eacbb 100644 --- a/spring-web/src/main/java/org/springframework/web/util/UriUtils.java +++ b/spring-web/src/main/java/org/springframework/web/util/UriUtils.java @@ -177,6 +177,20 @@ public abstract class UriUtils { return HierarchicalUriComponents.encodeUriComponent(fragment, encoding, HierarchicalUriComponents.Type.FRAGMENT); } + /** + * Encode characters outside the unreserved character set as defined in + * RFC 3986 Section 2. + *

This can be used to ensure the given String will not contain any + * characters with reserved URI meaning regardless of URI component. + * @param source the string to be encoded + * @param encoding the character encoding to encode to + * @return the encoded string + * @throws UnsupportedEncodingException when the given encoding parameter is not supported + */ + public static String encode(String source, String encoding) throws UnsupportedEncodingException { + HierarchicalUriComponents.Type type = HierarchicalUriComponents.Type.URI; + return HierarchicalUriComponents.encodeUriComponent(source, encoding, type); + } // decoding