Add component-neutral encode option in UriUtils

Issue: SPR-12750, SPR-12942
This commit is contained in:
Rossen Stoyanchev
2015-05-13 15:03:33 -04:00
parent 3e59c244f9
commit ca410fea53
2 changed files with 22 additions and 3 deletions

View File

@@ -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.
* <p>Contains methods to indicate whether a given character is valid in a specific URI component.
* @see <a href="http://www.ietf.org/rfc/rfc3986.txt">RFC 3986</a>
*/
@@ -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 <a href="http://www.ietf.org/rfc/rfc3986.txt">RFC 3986, appendix A</a>
*/
protected boolean isReserved(char c) {
protected boolean isReserved(int c) {
return isGenericDelimiter(c) || isSubDelimiter(c);
}

View File

@@ -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
* <a href="https://tools.ietf.org/html/rfc3986#section-2">RFC 3986 Section 2</a>.
* <p>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