Polishing
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -176,7 +176,7 @@ final class HierarchicalUriComponents extends UriComponents {
|
||||
*/
|
||||
@Override
|
||||
public HierarchicalUriComponents encode(String encoding) throws UnsupportedEncodingException {
|
||||
Assert.hasLength(encoding, "'encoding' must not be empty");
|
||||
Assert.hasLength(encoding, "Encoding must not be empty");
|
||||
if (this.encoded) {
|
||||
return this;
|
||||
}
|
||||
@@ -214,14 +214,14 @@ final class HierarchicalUriComponents extends UriComponents {
|
||||
if (source == null) {
|
||||
return null;
|
||||
}
|
||||
Assert.hasLength(encoding, "'encoding' must not be empty");
|
||||
Assert.hasLength(encoding, "Encoding must not be empty");
|
||||
byte[] bytes = encodeBytes(source.getBytes(encoding), type);
|
||||
return new String(bytes, "US-ASCII");
|
||||
}
|
||||
|
||||
private static byte[] encodeBytes(byte[] source, Type type) {
|
||||
Assert.notNull(source, "'source' must not be null");
|
||||
Assert.notNull(type, "'type' must not be null");
|
||||
Assert.notNull(source, "Source must not be null");
|
||||
Assert.notNull(type, "Type must not be null");
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream(source.length);
|
||||
for (byte b : source) {
|
||||
if (b < 0) {
|
||||
@@ -242,7 +242,7 @@ final class HierarchicalUriComponents extends UriComponents {
|
||||
}
|
||||
|
||||
private Type getHostType() {
|
||||
return ((this.host != null) && this.host.startsWith("[")) ? Type.HOST_IPV6 : Type.HOST_IPV4;
|
||||
return (this.host != null && this.host.startsWith("[")) ? Type.HOST_IPV6 : Type.HOST_IPV4;
|
||||
}
|
||||
|
||||
// verifying
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -24,14 +24,15 @@ import java.util.regex.Pattern;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Utility class for URI encoding and decoding based on RFC 3986. Offers encoding methods for the various URI
|
||||
* components.
|
||||
* Utility class for URI encoding and decoding based on RFC 3986.
|
||||
* Offers encoding methods for the various URI components.
|
||||
*
|
||||
* <p>All {@code encode*(String, String} methods in this class operate in a similar way:
|
||||
* <ul>
|
||||
* <li>Valid characters for the specific URI component as defined in RFC 3986 stay the same.</li>
|
||||
* <li>All other characters are converted into one or more bytes in the given encoding scheme. Each of the
|
||||
* resulting bytes is written as a hexadecimal string in the "{@code %<i>xy</i>}" format.</li>
|
||||
* <li>All other characters are converted into one or more bytes in the given encoding scheme.
|
||||
* Each of the resulting bytes is written as a hexadecimal string in the "{@code %<i>xy</i>}"
|
||||
* format.</li>
|
||||
* </ul>
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
@@ -65,6 +66,7 @@ public abstract class UriUtils {
|
||||
"^" + HTTP_PATTERN + "(//(" + USERINFO_PATTERN + "@)?" + HOST_PATTERN + "(:" + PORT_PATTERN + ")?" + ")?" +
|
||||
PATH_PATTERN + "(\\?" + LAST_PATTERN + ")?");
|
||||
|
||||
|
||||
// encoding
|
||||
|
||||
/**
|
||||
@@ -86,19 +88,18 @@ public abstract class UriUtils {
|
||||
*/
|
||||
@Deprecated
|
||||
public static String encodeUri(String uri, String encoding) throws UnsupportedEncodingException {
|
||||
Assert.notNull(uri, "'uri' must not be null");
|
||||
Assert.hasLength(encoding, "'encoding' must not be empty");
|
||||
Matcher m = URI_PATTERN.matcher(uri);
|
||||
if (m.matches()) {
|
||||
String scheme = m.group(2);
|
||||
String authority = m.group(3);
|
||||
String userinfo = m.group(5);
|
||||
String host = m.group(6);
|
||||
String port = m.group(8);
|
||||
String path = m.group(9);
|
||||
String query = m.group(11);
|
||||
String fragment = m.group(13);
|
||||
|
||||
Assert.notNull(uri, "URI must not be null");
|
||||
Assert.hasLength(encoding, "Encoding must not be empty");
|
||||
Matcher matcher = URI_PATTERN.matcher(uri);
|
||||
if (matcher.matches()) {
|
||||
String scheme = matcher.group(2);
|
||||
String authority = matcher.group(3);
|
||||
String userinfo = matcher.group(5);
|
||||
String host = matcher.group(6);
|
||||
String port = matcher.group(8);
|
||||
String path = matcher.group(9);
|
||||
String query = matcher.group(11);
|
||||
String fragment = matcher.group(13);
|
||||
return encodeUriComponents(scheme, authority, userinfo, host, port, path, query, fragment, encoding);
|
||||
}
|
||||
else {
|
||||
@@ -127,18 +128,17 @@ public abstract class UriUtils {
|
||||
*/
|
||||
@Deprecated
|
||||
public static String encodeHttpUrl(String httpUrl, String encoding) throws UnsupportedEncodingException {
|
||||
Assert.notNull(httpUrl, "'httpUrl' must not be null");
|
||||
Assert.hasLength(encoding, "'encoding' must not be empty");
|
||||
Matcher m = HTTP_URL_PATTERN.matcher(httpUrl);
|
||||
if (m.matches()) {
|
||||
String scheme = m.group(1);
|
||||
String authority = m.group(2);
|
||||
String userinfo = m.group(4);
|
||||
String host = m.group(5);
|
||||
String portString = m.group(7);
|
||||
String path = m.group(8);
|
||||
String query = m.group(10);
|
||||
|
||||
Assert.notNull(httpUrl, "HTTP URL must not be null");
|
||||
Assert.hasLength(encoding, "Encoding must not be empty");
|
||||
Matcher matcher = HTTP_URL_PATTERN.matcher(httpUrl);
|
||||
if (matcher.matches()) {
|
||||
String scheme = matcher.group(1);
|
||||
String authority = matcher.group(2);
|
||||
String userinfo = matcher.group(4);
|
||||
String host = matcher.group(5);
|
||||
String portString = matcher.group(7);
|
||||
String path = matcher.group(8);
|
||||
String query = matcher.group(10);
|
||||
return encodeUriComponents(scheme, authority, userinfo, host, portString, path, query, null, encoding);
|
||||
}
|
||||
else {
|
||||
@@ -168,7 +168,7 @@ public abstract class UriUtils {
|
||||
String host, String port, String path, String query, String fragment, String encoding)
|
||||
throws UnsupportedEncodingException {
|
||||
|
||||
Assert.hasLength(encoding, "'encoding' must not be empty");
|
||||
Assert.hasLength(encoding, "Encoding must not be empty");
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
if (scheme != null) {
|
||||
@@ -217,8 +217,7 @@ public abstract class UriUtils {
|
||||
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
|
||||
*/
|
||||
public static String encodeScheme(String scheme, String encoding) throws UnsupportedEncodingException {
|
||||
return HierarchicalUriComponents.encodeUriComponent(scheme, encoding,
|
||||
HierarchicalUriComponents.Type.SCHEME);
|
||||
return HierarchicalUriComponents.encodeUriComponent(scheme, encoding, HierarchicalUriComponents.Type.SCHEME);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -229,8 +228,7 @@ public abstract class UriUtils {
|
||||
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
|
||||
*/
|
||||
public static String encodeAuthority(String authority, String encoding) throws UnsupportedEncodingException {
|
||||
return HierarchicalUriComponents.encodeUriComponent(authority, encoding,
|
||||
HierarchicalUriComponents.Type.AUTHORITY);
|
||||
return HierarchicalUriComponents.encodeUriComponent(authority, encoding, HierarchicalUriComponents.Type.AUTHORITY);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -241,8 +239,7 @@ public abstract class UriUtils {
|
||||
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
|
||||
*/
|
||||
public static String encodeUserInfo(String userInfo, String encoding) throws UnsupportedEncodingException {
|
||||
return HierarchicalUriComponents.encodeUriComponent(userInfo, encoding,
|
||||
HierarchicalUriComponents.Type.USER_INFO);
|
||||
return HierarchicalUriComponents.encodeUriComponent(userInfo, encoding, HierarchicalUriComponents.Type.USER_INFO);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -253,8 +250,7 @@ public abstract class UriUtils {
|
||||
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
|
||||
*/
|
||||
public static String encodeHost(String host, String encoding) throws UnsupportedEncodingException {
|
||||
return HierarchicalUriComponents
|
||||
.encodeUriComponent(host, encoding, HierarchicalUriComponents.Type.HOST_IPV4);
|
||||
return HierarchicalUriComponents.encodeUriComponent(host, encoding, HierarchicalUriComponents.Type.HOST_IPV4);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -265,8 +261,7 @@ public abstract class UriUtils {
|
||||
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
|
||||
*/
|
||||
public static String encodePort(String port, String encoding) throws UnsupportedEncodingException {
|
||||
return HierarchicalUriComponents
|
||||
.encodeUriComponent(port, encoding, HierarchicalUriComponents.Type.PORT);
|
||||
return HierarchicalUriComponents.encodeUriComponent(port, encoding, HierarchicalUriComponents.Type.PORT);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -277,8 +272,7 @@ public abstract class UriUtils {
|
||||
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
|
||||
*/
|
||||
public static String encodePath(String path, String encoding) throws UnsupportedEncodingException {
|
||||
return HierarchicalUriComponents
|
||||
.encodeUriComponent(path, encoding, HierarchicalUriComponents.Type.PATH);
|
||||
return HierarchicalUriComponents.encodeUriComponent(path, encoding, HierarchicalUriComponents.Type.PATH);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -289,8 +283,7 @@ public abstract class UriUtils {
|
||||
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
|
||||
*/
|
||||
public static String encodePathSegment(String segment, String encoding) throws UnsupportedEncodingException {
|
||||
return HierarchicalUriComponents.encodeUriComponent(segment, encoding,
|
||||
HierarchicalUriComponents.Type.PATH_SEGMENT);
|
||||
return HierarchicalUriComponents.encodeUriComponent(segment, encoding, HierarchicalUriComponents.Type.PATH_SEGMENT);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -301,8 +294,7 @@ public abstract class UriUtils {
|
||||
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
|
||||
*/
|
||||
public static String encodeQuery(String query, String encoding) throws UnsupportedEncodingException {
|
||||
return HierarchicalUriComponents
|
||||
.encodeUriComponent(query, encoding, HierarchicalUriComponents.Type.QUERY);
|
||||
return HierarchicalUriComponents.encodeUriComponent(query, encoding, HierarchicalUriComponents.Type.QUERY);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -313,8 +305,7 @@ public abstract class UriUtils {
|
||||
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
|
||||
*/
|
||||
public static String encodeQueryParam(String queryParam, String encoding) throws UnsupportedEncodingException {
|
||||
return HierarchicalUriComponents.encodeUriComponent(queryParam, encoding,
|
||||
HierarchicalUriComponents.Type.QUERY_PARAM);
|
||||
return HierarchicalUriComponents.encodeUriComponent(queryParam, encoding, HierarchicalUriComponents.Type.QUERY_PARAM);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -325,8 +316,7 @@ public abstract class UriUtils {
|
||||
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
|
||||
*/
|
||||
public static String encodeFragment(String fragment, String encoding) throws UnsupportedEncodingException {
|
||||
return HierarchicalUriComponents.encodeUriComponent(fragment, encoding,
|
||||
HierarchicalUriComponents.Type.FRAGMENT);
|
||||
return HierarchicalUriComponents.encodeUriComponent(fragment, encoding, HierarchicalUriComponents.Type.FRAGMENT);
|
||||
}
|
||||
|
||||
|
||||
@@ -348,8 +338,8 @@ public abstract class UriUtils {
|
||||
* @see java.net.URLDecoder#decode(String, String)
|
||||
*/
|
||||
public static String decode(String source, String encoding) throws UnsupportedEncodingException {
|
||||
Assert.notNull(source, "'source' must not be null");
|
||||
Assert.hasLength(encoding, "'encoding' must not be empty");
|
||||
Assert.notNull(source, "Source must not be null");
|
||||
Assert.hasLength(encoding, "Encoding must not be empty");
|
||||
int length = source.length();
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream(length);
|
||||
boolean changed = false;
|
||||
@@ -376,7 +366,7 @@ public abstract class UriUtils {
|
||||
bos.write(ch);
|
||||
}
|
||||
}
|
||||
return changed ? new String(bos.toByteArray(), encoding) : source;
|
||||
return (changed ? new String(bos.toByteArray(), encoding) : source);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user