1
1
/*
2
- * Copyright 2002-2012 the original author or authors.
2
+ * Copyright 2002-2014 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
24
24
import org .springframework .util .Assert ;
25
25
26
26
/**
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.
29
29
*
30
30
* <p>All {@code encode*(String, String} methods in this class operate in a similar way:
31
31
* <ul>
32
32
* <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>
35
36
* </ul>
36
37
*
37
38
* @author Arjen Poutsma
@@ -65,6 +66,7 @@ public abstract class UriUtils {
65
66
"^" + HTTP_PATTERN + "(//(" + USERINFO_PATTERN + "@)?" + HOST_PATTERN + "(:" + PORT_PATTERN + ")?" + ")?" +
66
67
PATH_PATTERN + "(\\ ?" + LAST_PATTERN + ")?" );
67
68
69
+
68
70
// encoding
69
71
70
72
/**
@@ -86,19 +88,18 @@ public abstract class UriUtils {
86
88
*/
87
89
@ Deprecated
88
90
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 );
102
103
return encodeUriComponents (scheme , authority , userinfo , host , port , path , query , fragment , encoding );
103
104
}
104
105
else {
@@ -127,18 +128,17 @@ public static String encodeUri(String uri, String encoding) throws UnsupportedEn
127
128
*/
128
129
@ Deprecated
129
130
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 );
142
142
return encodeUriComponents (scheme , authority , userinfo , host , portString , path , query , null , encoding );
143
143
}
144
144
else {
@@ -168,7 +168,7 @@ public static String encodeUriComponents(String scheme, String authority, String
168
168
String host , String port , String path , String query , String fragment , String encoding )
169
169
throws UnsupportedEncodingException {
170
170
171
- Assert .hasLength (encoding , "'encoding' must not be empty" );
171
+ Assert .hasLength (encoding , "Encoding must not be empty" );
172
172
StringBuilder sb = new StringBuilder ();
173
173
174
174
if (scheme != null ) {
@@ -217,8 +217,7 @@ public static String encodeUriComponents(String scheme, String authority, String
217
217
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
218
218
*/
219
219
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 );
222
221
}
223
222
224
223
/**
@@ -229,8 +228,7 @@ public static String encodeScheme(String scheme, String encoding) throws Unsuppo
229
228
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
230
229
*/
231
230
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 );
234
232
}
235
233
236
234
/**
@@ -241,8 +239,7 @@ public static String encodeAuthority(String authority, String encoding) throws U
241
239
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
242
240
*/
243
241
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 );
246
243
}
247
244
248
245
/**
@@ -253,8 +250,7 @@ public static String encodeUserInfo(String userInfo, String encoding) throws Uns
253
250
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
254
251
*/
255
252
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 );
258
254
}
259
255
260
256
/**
@@ -265,8 +261,7 @@ public static String encodeHost(String host, String encoding) throws Unsupported
265
261
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
266
262
*/
267
263
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 );
270
265
}
271
266
272
267
/**
@@ -277,8 +272,7 @@ public static String encodePort(String port, String encoding) throws Unsupported
277
272
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
278
273
*/
279
274
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 );
282
276
}
283
277
284
278
/**
@@ -289,8 +283,7 @@ public static String encodePath(String path, String encoding) throws Unsupported
289
283
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
290
284
*/
291
285
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 );
294
287
}
295
288
296
289
/**
@@ -301,8 +294,7 @@ public static String encodePathSegment(String segment, String encoding) throws U
301
294
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
302
295
*/
303
296
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 );
306
298
}
307
299
308
300
/**
@@ -313,8 +305,7 @@ public static String encodeQuery(String query, String encoding) throws Unsupport
313
305
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
314
306
*/
315
307
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 );
318
309
}
319
310
320
311
/**
@@ -325,8 +316,7 @@ public static String encodeQueryParam(String queryParam, String encoding) throws
325
316
* @throws UnsupportedEncodingException when the given encoding parameter is not supported
326
317
*/
327
318
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 );
330
320
}
331
321
332
322
@@ -348,8 +338,8 @@ public static String encodeFragment(String fragment, String encoding) throws Uns
348
338
* @see java.net.URLDecoder#decode(String, String)
349
339
*/
350
340
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" );
353
343
int length = source .length ();
354
344
ByteArrayOutputStream bos = new ByteArrayOutputStream (length );
355
345
boolean changed = false ;
@@ -376,7 +366,7 @@ public static String decode(String source, String encoding) throws UnsupportedEn
376
366
bos .write (ch );
377
367
}
378
368
}
379
- return changed ? new String (bos .toByteArray (), encoding ) : source ;
369
+ return ( changed ? new String (bos .toByteArray (), encoding ) : source ) ;
380
370
}
381
371
382
372
}
0 commit comments