Skip to content

Commit 5335288

Browse files
committed
Polishing
1 parent dd7f54c commit 5335288

File tree

2 files changed

+50
-60
lines changed

2 files changed

+50
-60
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -176,7 +176,7 @@ public MultiValueMap<String, String> getQueryParams() {
176176
*/
177177
@Override
178178
public HierarchicalUriComponents encode(String encoding) throws UnsupportedEncodingException {
179-
Assert.hasLength(encoding, "'encoding' must not be empty");
179+
Assert.hasLength(encoding, "Encoding must not be empty");
180180
if (this.encoded) {
181181
return this;
182182
}
@@ -214,14 +214,14 @@ static String encodeUriComponent(String source, String encoding, Type type) thro
214214
if (source == null) {
215215
return null;
216216
}
217-
Assert.hasLength(encoding, "'encoding' must not be empty");
217+
Assert.hasLength(encoding, "Encoding must not be empty");
218218
byte[] bytes = encodeBytes(source.getBytes(encoding), type);
219219
return new String(bytes, "US-ASCII");
220220
}
221221

222222
private static byte[] encodeBytes(byte[] source, Type type) {
223-
Assert.notNull(source, "'source' must not be null");
224-
Assert.notNull(type, "'type' must not be null");
223+
Assert.notNull(source, "Source must not be null");
224+
Assert.notNull(type, "Type must not be null");
225225
ByteArrayOutputStream bos = new ByteArrayOutputStream(source.length);
226226
for (byte b : source) {
227227
if (b < 0) {
@@ -242,7 +242,7 @@ private static byte[] encodeBytes(byte[] source, Type type) {
242242
}
243243

244244
private Type getHostType() {
245-
return ((this.host != null) && this.host.startsWith("[")) ? Type.HOST_IPV6 : Type.HOST_IPV4;
245+
return (this.host != null && this.host.startsWith("[")) ? Type.HOST_IPV6 : Type.HOST_IPV4;
246246
}
247247

248248
// verifying

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

Lines changed: 44 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,14 +24,15 @@
2424
import org.springframework.util.Assert;
2525

2626
/**
27-
* Utility class for URI encoding and decoding based on RFC 3986. Offers encoding methods for the various URI
28-
* components.
27+
* Utility class for URI encoding and decoding based on RFC 3986.
28+
* Offers encoding methods for the various URI components.
2929
*
3030
* <p>All {@code encode*(String, String} methods in this class operate in a similar way:
3131
* <ul>
3232
* <li>Valid characters for the specific URI component as defined in RFC 3986 stay the same.</li>
33-
* <li>All other characters are converted into one or more bytes in the given encoding scheme. Each of the
34-
* resulting bytes is written as a hexadecimal string in the "{@code %<i>xy</i>}" format.</li>
33+
* <li>All other characters are converted into one or more bytes in the given encoding scheme.
34+
* Each of the resulting bytes is written as a hexadecimal string in the "{@code %<i>xy</i>}"
35+
* format.</li>
3536
* </ul>
3637
*
3738
* @author Arjen Poutsma
@@ -65,6 +66,7 @@ public abstract class UriUtils {
6566
"^" + HTTP_PATTERN + "(//(" + USERINFO_PATTERN + "@)?" + HOST_PATTERN + "(:" + PORT_PATTERN + ")?" + ")?" +
6667
PATH_PATTERN + "(\\?" + LAST_PATTERN + ")?");
6768

69+
6870
// encoding
6971

7072
/**
@@ -86,19 +88,18 @@ public abstract class UriUtils {
8688
*/
8789
@Deprecated
8890
public static String encodeUri(String uri, String encoding) throws UnsupportedEncodingException {
89-
Assert.notNull(uri, "'uri' must not be null");
90-
Assert.hasLength(encoding, "'encoding' must not be empty");
91-
Matcher m = URI_PATTERN.matcher(uri);
92-
if (m.matches()) {
93-
String scheme = m.group(2);
94-
String authority = m.group(3);
95-
String userinfo = m.group(5);
96-
String host = m.group(6);
97-
String port = m.group(8);
98-
String path = m.group(9);
99-
String query = m.group(11);
100-
String fragment = m.group(13);
101-
91+
Assert.notNull(uri, "URI must not be null");
92+
Assert.hasLength(encoding, "Encoding must not be empty");
93+
Matcher matcher = URI_PATTERN.matcher(uri);
94+
if (matcher.matches()) {
95+
String scheme = matcher.group(2);
96+
String authority = matcher.group(3);
97+
String userinfo = matcher.group(5);
98+
String host = matcher.group(6);
99+
String port = matcher.group(8);
100+
String path = matcher.group(9);
101+
String query = matcher.group(11);
102+
String fragment = matcher.group(13);
102103
return encodeUriComponents(scheme, authority, userinfo, host, port, path, query, fragment, encoding);
103104
}
104105
else {
@@ -127,18 +128,17 @@ public static String encodeUri(String uri, String encoding) throws UnsupportedEn
127128
*/
128129
@Deprecated
129130
public static String encodeHttpUrl(String httpUrl, String encoding) throws UnsupportedEncodingException {
130-
Assert.notNull(httpUrl, "'httpUrl' must not be null");
131-
Assert.hasLength(encoding, "'encoding' must not be empty");
132-
Matcher m = HTTP_URL_PATTERN.matcher(httpUrl);
133-
if (m.matches()) {
134-
String scheme = m.group(1);
135-
String authority = m.group(2);
136-
String userinfo = m.group(4);
137-
String host = m.group(5);
138-
String portString = m.group(7);
139-
String path = m.group(8);
140-
String query = m.group(10);
141-
131+
Assert.notNull(httpUrl, "HTTP URL must not be null");
132+
Assert.hasLength(encoding, "Encoding must not be empty");
133+
Matcher matcher = HTTP_URL_PATTERN.matcher(httpUrl);
134+
if (matcher.matches()) {
135+
String scheme = matcher.group(1);
136+
String authority = matcher.group(2);
137+
String userinfo = matcher.group(4);
138+
String host = matcher.group(5);
139+
String portString = matcher.group(7);
140+
String path = matcher.group(8);
141+
String query = matcher.group(10);
142142
return encodeUriComponents(scheme, authority, userinfo, host, portString, path, query, null, encoding);
143143
}
144144
else {
@@ -168,7 +168,7 @@ public static String encodeUriComponents(String scheme, String authority, String
168168
String host, String port, String path, String query, String fragment, String encoding)
169169
throws UnsupportedEncodingException {
170170

171-
Assert.hasLength(encoding, "'encoding' must not be empty");
171+
Assert.hasLength(encoding, "Encoding must not be empty");
172172
StringBuilder sb = new StringBuilder();
173173

174174
if (scheme != null) {
@@ -217,8 +217,7 @@ public static String encodeUriComponents(String scheme, String authority, String
217217
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
218218
*/
219219
public static String encodeScheme(String scheme, String encoding) throws UnsupportedEncodingException {
220-
return HierarchicalUriComponents.encodeUriComponent(scheme, encoding,
221-
HierarchicalUriComponents.Type.SCHEME);
220+
return HierarchicalUriComponents.encodeUriComponent(scheme, encoding, HierarchicalUriComponents.Type.SCHEME);
222221
}
223222

224223
/**
@@ -229,8 +228,7 @@ public static String encodeScheme(String scheme, String encoding) throws Unsuppo
229228
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
230229
*/
231230
public static String encodeAuthority(String authority, String encoding) throws UnsupportedEncodingException {
232-
return HierarchicalUriComponents.encodeUriComponent(authority, encoding,
233-
HierarchicalUriComponents.Type.AUTHORITY);
231+
return HierarchicalUriComponents.encodeUriComponent(authority, encoding, HierarchicalUriComponents.Type.AUTHORITY);
234232
}
235233

236234
/**
@@ -241,8 +239,7 @@ public static String encodeAuthority(String authority, String encoding) throws U
241239
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
242240
*/
243241
public static String encodeUserInfo(String userInfo, String encoding) throws UnsupportedEncodingException {
244-
return HierarchicalUriComponents.encodeUriComponent(userInfo, encoding,
245-
HierarchicalUriComponents.Type.USER_INFO);
242+
return HierarchicalUriComponents.encodeUriComponent(userInfo, encoding, HierarchicalUriComponents.Type.USER_INFO);
246243
}
247244

248245
/**
@@ -253,8 +250,7 @@ public static String encodeUserInfo(String userInfo, String encoding) throws Uns
253250
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
254251
*/
255252
public static String encodeHost(String host, String encoding) throws UnsupportedEncodingException {
256-
return HierarchicalUriComponents
257-
.encodeUriComponent(host, encoding, HierarchicalUriComponents.Type.HOST_IPV4);
253+
return HierarchicalUriComponents.encodeUriComponent(host, encoding, HierarchicalUriComponents.Type.HOST_IPV4);
258254
}
259255

260256
/**
@@ -265,8 +261,7 @@ public static String encodeHost(String host, String encoding) throws Unsupported
265261
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
266262
*/
267263
public static String encodePort(String port, String encoding) throws UnsupportedEncodingException {
268-
return HierarchicalUriComponents
269-
.encodeUriComponent(port, encoding, HierarchicalUriComponents.Type.PORT);
264+
return HierarchicalUriComponents.encodeUriComponent(port, encoding, HierarchicalUriComponents.Type.PORT);
270265
}
271266

272267
/**
@@ -277,8 +272,7 @@ public static String encodePort(String port, String encoding) throws Unsupported
277272
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
278273
*/
279274
public static String encodePath(String path, String encoding) throws UnsupportedEncodingException {
280-
return HierarchicalUriComponents
281-
.encodeUriComponent(path, encoding, HierarchicalUriComponents.Type.PATH);
275+
return HierarchicalUriComponents.encodeUriComponent(path, encoding, HierarchicalUriComponents.Type.PATH);
282276
}
283277

284278
/**
@@ -289,8 +283,7 @@ public static String encodePath(String path, String encoding) throws Unsupported
289283
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
290284
*/
291285
public static String encodePathSegment(String segment, String encoding) throws UnsupportedEncodingException {
292-
return HierarchicalUriComponents.encodeUriComponent(segment, encoding,
293-
HierarchicalUriComponents.Type.PATH_SEGMENT);
286+
return HierarchicalUriComponents.encodeUriComponent(segment, encoding, HierarchicalUriComponents.Type.PATH_SEGMENT);
294287
}
295288

296289
/**
@@ -301,8 +294,7 @@ public static String encodePathSegment(String segment, String encoding) throws U
301294
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
302295
*/
303296
public static String encodeQuery(String query, String encoding) throws UnsupportedEncodingException {
304-
return HierarchicalUriComponents
305-
.encodeUriComponent(query, encoding, HierarchicalUriComponents.Type.QUERY);
297+
return HierarchicalUriComponents.encodeUriComponent(query, encoding, HierarchicalUriComponents.Type.QUERY);
306298
}
307299

308300
/**
@@ -313,8 +305,7 @@ public static String encodeQuery(String query, String encoding) throws Unsupport
313305
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
314306
*/
315307
public static String encodeQueryParam(String queryParam, String encoding) throws UnsupportedEncodingException {
316-
return HierarchicalUriComponents.encodeUriComponent(queryParam, encoding,
317-
HierarchicalUriComponents.Type.QUERY_PARAM);
308+
return HierarchicalUriComponents.encodeUriComponent(queryParam, encoding, HierarchicalUriComponents.Type.QUERY_PARAM);
318309
}
319310

320311
/**
@@ -325,8 +316,7 @@ public static String encodeQueryParam(String queryParam, String encoding) throws
325316
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
326317
*/
327318
public static String encodeFragment(String fragment, String encoding) throws UnsupportedEncodingException {
328-
return HierarchicalUriComponents.encodeUriComponent(fragment, encoding,
329-
HierarchicalUriComponents.Type.FRAGMENT);
319+
return HierarchicalUriComponents.encodeUriComponent(fragment, encoding, HierarchicalUriComponents.Type.FRAGMENT);
330320
}
331321

332322

@@ -348,8 +338,8 @@ public static String encodeFragment(String fragment, String encoding) throws Uns
348338
* @see java.net.URLDecoder#decode(String, String)
349339
*/
350340
public static String decode(String source, String encoding) throws UnsupportedEncodingException {
351-
Assert.notNull(source, "'source' must not be null");
352-
Assert.hasLength(encoding, "'encoding' must not be empty");
341+
Assert.notNull(source, "Source must not be null");
342+
Assert.hasLength(encoding, "Encoding must not be empty");
353343
int length = source.length();
354344
ByteArrayOutputStream bos = new ByteArrayOutputStream(length);
355345
boolean changed = false;
@@ -376,7 +366,7 @@ public static String decode(String source, String encoding) throws UnsupportedEn
376366
bos.write(ch);
377367
}
378368
}
379-
return changed ? new String(bos.toByteArray(), encoding) : source;
369+
return (changed ? new String(bos.toByteArray(), encoding) : source);
380370
}
381371

382372
}

0 commit comments

Comments
 (0)