Browse Source

Add component-neutral encode option in UriUtils

Issue: SPR-12750, SPR-12942
pull/763/merge
Rossen Stoyanchev 11 years ago
parent
commit
ca410fea53
  1. 11
      spring-web/src/main/java/org/springframework/web/util/HierarchicalUriComponents.java
  2. 14
      spring-web/src/main/java/org/springframework/web/util/UriUtils.java

11
spring-web/src/main/java/org/springframework/web/util/HierarchicalUriComponents.java

@ -61,7 +61,6 @@ final class HierarchicalUriComponents extends UriComponents { @@ -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 { @@ -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 { @@ -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 { @@ -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);
}

14
spring-web/src/main/java/org/springframework/web/util/UriUtils.java

@ -177,6 +177,20 @@ public abstract class UriUtils { @@ -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

Loading…
Cancel
Save