31
31
public class URIBuilder {
32
32
33
33
private String scheme ;
34
+ private String encodedSchemeSpecificPart ;
34
35
private String encodedAuthority ;
35
36
private String userInfo ;
36
37
private String encodedUserInfo ;
37
38
private String host ;
38
39
private int port ;
39
40
private String path ;
41
+ private String encodedPath ;
42
+ private String encodedQuery ;
40
43
private List <NameValuePair > queryParams ;
41
44
private String fragment ;
45
+ private String encodedFragment ;
42
46
43
47
public URIBuilder () {
44
48
this .port = -1 ;
45
49
}
46
50
47
51
public URIBuilder (final String uri ) {
48
52
try {
49
- _digestURI (new URI (uri . replace ( " " , "%20" ) ));
53
+ digestURI (new URI (uri ));
50
54
} catch (URISyntaxException e ) {
51
55
LogUtils .e (e .getMessage (), e );
52
56
}
@@ -56,30 +60,19 @@ public URIBuilder(final URI uri) {
56
60
digestURI (uri );
57
61
}
58
62
59
- private void _digestURI (final URI uri ) {
60
- this .scheme = uri .getScheme ();
61
- this .encodedAuthority = uri .getRawAuthority ();
62
- this .host = uri .getHost ();
63
- this .port = uri .getPort ();
64
- this .encodedUserInfo = uri .getRawUserInfo ();
65
- this .userInfo = uri .getUserInfo ();
66
- this .path = uri .getPath ();
67
- if (path != null ) path = path .replace ("%20" , " " );
68
- String query = uri .getRawQuery ();
69
- if (query != null ) query = query .replace ("%20" , " " );
70
- this .queryParams = parseQuery (query );
71
- this .fragment = uri .getFragment ();
72
- }
73
-
74
63
private void digestURI (final URI uri ) {
75
64
this .scheme = uri .getScheme ();
65
+ this .encodedSchemeSpecificPart = uri .getRawSchemeSpecificPart ();
76
66
this .encodedAuthority = uri .getRawAuthority ();
77
67
this .host = uri .getHost ();
78
68
this .port = uri .getPort ();
79
69
this .encodedUserInfo = uri .getRawUserInfo ();
80
70
this .userInfo = uri .getUserInfo ();
71
+ this .encodedPath = uri .getRawPath ();
81
72
this .path = uri .getPath ();
73
+ this .encodedQuery = uri .getRawQuery ();
82
74
this .queryParams = parseQuery (uri .getRawQuery ());
75
+ this .encodedFragment = uri .getRawFragment ();
83
76
this .fragment = uri .getFragment ();
84
77
}
85
78
@@ -100,39 +93,45 @@ public URI build(Charset charset) throws URISyntaxException {
100
93
}
101
94
102
95
private String buildString (Charset charset ) {
103
- StringBuilder sb = new StringBuilder ();
96
+ final StringBuilder sb = new StringBuilder ();
104
97
if (this .scheme != null ) {
105
98
sb .append (this .scheme ).append (':' );
106
99
}
107
-
108
- if (this .encodedAuthority != null ) {
109
- sb .append ("//" ).append (this .encodedAuthority );
110
- } else if (this .host != null ) {
111
- sb .append ("//" );
112
- if (this .encodedUserInfo != null ) {
113
- sb .append (this .encodedUserInfo ).append ("@" );
114
- } else if (this .userInfo != null ) {
115
- sb .append (encodeUserInfo (this .userInfo , charset )).append ("@" );
100
+ if (this .encodedSchemeSpecificPart != null ) {
101
+ sb .append (this .encodedSchemeSpecificPart );
102
+ } else {
103
+ if (this .encodedAuthority != null ) {
104
+ sb .append ("//" ).append (this .encodedAuthority );
105
+ } else if (this .host != null ) {
106
+ sb .append ("//" );
107
+ if (this .encodedUserInfo != null ) {
108
+ sb .append (this .encodedUserInfo ).append ("@" );
109
+ } else if (this .userInfo != null ) {
110
+ sb .append (encodeUserInfo (this .userInfo , charset )).append ("@" );
111
+ }
112
+ if (InetAddressUtils .isIPv6Address (this .host )) {
113
+ sb .append ("[" ).append (this .host ).append ("]" );
114
+ } else {
115
+ sb .append (this .host );
116
+ }
117
+ if (this .port >= 0 ) {
118
+ sb .append (":" ).append (this .port );
119
+ }
116
120
}
117
- if (InetAddressUtils . isIPv6Address ( this .host ) ) {
118
- sb .append ("[" ). append (this .host ). append ( "]" );
119
- } else {
120
- sb .append (this .host );
121
+ if (this .encodedPath != null ) {
122
+ sb .append (normalizePath (this .encodedPath ) );
123
+ } else if ( this . path != null ) {
124
+ sb .append (encodePath ( normalizePath ( this .path ), charset ) );
121
125
}
122
- if (this .port >= 0 ) {
123
- sb .append (":" ).append (this .port );
126
+ if (this .encodedQuery != null ) {
127
+ sb .append ("?" ).append (this .encodedQuery );
128
+ } else if (this .queryParams != null ) {
129
+ sb .append ("?" ).append (encodeQuery (this .queryParams , charset ));
124
130
}
125
131
}
126
-
127
- if (this .path != null ) {
128
- sb .append (encodePath (normalizePath (this .path ), charset ));
129
- }
130
-
131
- if (this .queryParams != null ) {
132
- sb .append ("?" ).append (encodeQuery (this .queryParams , charset ));
133
- }
134
-
135
- if (this .fragment != null ) {
132
+ if (this .encodedFragment != null ) {
133
+ sb .append ("#" ).append (this .encodedFragment );
134
+ } else if (this .fragment != null ) {
136
135
sb .append ("#" ).append (encodeFragment (this .fragment , charset ));
137
136
}
138
137
return sb .toString ();
@@ -168,6 +167,7 @@ public URIBuilder setScheme(final String scheme) {
168
167
*/
169
168
public URIBuilder setUserInfo (final String userInfo ) {
170
169
this .userInfo = userInfo ;
170
+ this .encodedSchemeSpecificPart = null ;
171
171
this .encodedAuthority = null ;
172
172
this .encodedUserInfo = null ;
173
173
return this ;
@@ -186,6 +186,7 @@ public URIBuilder setUserInfo(final String username, final String password) {
186
186
*/
187
187
public URIBuilder setHost (final String host ) {
188
188
this .host = host ;
189
+ this .encodedSchemeSpecificPart = null ;
189
190
this .encodedAuthority = null ;
190
191
return this ;
191
192
}
@@ -195,6 +196,7 @@ public URIBuilder setHost(final String host) {
195
196
*/
196
197
public URIBuilder setPort (final int port ) {
197
198
this .port = port < 0 ? -1 : port ;
199
+ this .encodedSchemeSpecificPart = null ;
198
200
this .encodedAuthority = null ;
199
201
return this ;
200
202
}
@@ -204,14 +206,8 @@ public URIBuilder setPort(final int port) {
204
206
*/
205
207
public URIBuilder setPath (final String path ) {
206
208
this .path = path ;
207
- return this ;
208
- }
209
-
210
- /**
211
- * Removes URI query.
212
- */
213
- public URIBuilder removeQuery () {
214
- this .queryParams = null ;
209
+ this .encodedSchemeSpecificPart = null ;
210
+ this .encodedPath = null ;
215
211
return this ;
216
212
}
217
213
@@ -222,6 +218,8 @@ public URIBuilder removeQuery() {
222
218
*/
223
219
public URIBuilder setQuery (final String query ) {
224
220
this .queryParams = parseQuery (query );
221
+ this .encodedQuery = null ;
222
+ this .encodedSchemeSpecificPart = null ;
225
223
return this ;
226
224
}
227
225
@@ -234,6 +232,8 @@ public URIBuilder addParameter(final String param, final String value) {
234
232
this .queryParams = new ArrayList <NameValuePair >();
235
233
}
236
234
this .queryParams .add (new BasicNameValuePair (param , value ));
235
+ this .encodedQuery = null ;
236
+ this .encodedSchemeSpecificPart = null ;
237
237
return this ;
238
238
}
239
239
@@ -246,14 +246,16 @@ public URIBuilder setParameter(final String param, final String value) {
246
246
this .queryParams = new ArrayList <NameValuePair >();
247
247
}
248
248
if (!this .queryParams .isEmpty ()) {
249
- for (Iterator <NameValuePair > it = this .queryParams .iterator (); it .hasNext (); ) {
250
- NameValuePair nvp = it .next ();
249
+ for (final Iterator <NameValuePair > it = this .queryParams .iterator (); it .hasNext (); ) {
250
+ final NameValuePair nvp = it .next ();
251
251
if (nvp .getName ().equals (param )) {
252
252
it .remove ();
253
253
}
254
254
}
255
255
}
256
256
this .queryParams .add (new BasicNameValuePair (param , value ));
257
+ this .encodedQuery = null ;
258
+ this .encodedSchemeSpecificPart = null ;
257
259
return this ;
258
260
}
259
261
@@ -263,6 +265,7 @@ public URIBuilder setParameter(final String param, final String value) {
263
265
*/
264
266
public URIBuilder setFragment (final String fragment ) {
265
267
this .fragment = fragment ;
268
+ this .encodedFragment = null ;
266
269
return this ;
267
270
}
268
271
0 commit comments