1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.esigate.util;
17
18 import java.net.URI;
19 import java.net.URISyntaxException;
20 import java.util.List;
21 import java.util.regex.Pattern;
22
23 import org.apache.commons.lang3.StringUtils;
24 import org.apache.http.HttpHost;
25 import org.apache.http.NameValuePair;
26 import org.apache.http.client.utils.URIUtils;
27 import org.apache.http.client.utils.URLEncodedUtils;
28 import org.esigate.Parameters;
29
30
31
32
33
34
35
36 public final class UriUtils {
37
38 private static final int CONVERSION_TABLE_SIZE = 128;
39 private static final String RESERVED_CHARACTERS = ":/?&=#%";
40 private static final String[] CONVERSION_TABLE = new String[CONVERSION_TABLE_SIZE];
41
42 static {
43 for (int i = 0; i < CONVERSION_TABLE_SIZE; i++) {
44 char character = (char) i;
45 String charString = Character.toString(character);
46 if (RESERVED_CHARACTERS.indexOf(i) == -1) {
47 charString = encode(charString);
48 }
49 CONVERSION_TABLE[i] = charString;
50 }
51 }
52
53 private UriUtils() {
54
55 }
56
57 private static String encode(char character) {
58 return Character.toString(character);
59 }
60
61 private static String encode(String charString) {
62 try {
63 return new URI(null, null, null, -1, charString, null, null).toASCIIString();
64 } catch (URISyntaxException e) {
65 throw new InvalidUriException(e);
66 }
67 }
68
69
70
71
72
73
74
75
76 public static String encodeIllegalCharacters(String uri) {
77 StringBuilder result = new StringBuilder();
78 int length = uri.length();
79 for (int i = 0; i < length; i++) {
80 char character = uri.charAt(i);
81 if (character == '%') {
82
83 if (i >= length - 2 || !isHex(uri.charAt(i + 1)) || !isHex(uri.charAt(i + 2))) {
84 result.append("%25");
85 } else {
86 result.append('%');
87 }
88 } else {
89 int j = (int) character;
90 if (j >= CONVERSION_TABLE_SIZE || j < 0) {
91 result.append(encode(character));
92 } else {
93 result.append(CONVERSION_TABLE[j]);
94 }
95 }
96 }
97 return result.toString();
98 }
99
100 private static boolean isHex(char character) {
101 return character == '0' || character == '1' || character == '2' || character == '3' || character == '4'
102 || character == '5' || character == '6' || character == '7' || character == '8' || character == '9'
103 || character == 'a' || character == 'b' || character == 'c' || character == 'd' || character == 'e'
104 || character == 'f' || character == 'A' || character == 'B' || character == 'C' || character == 'D'
105 || character == 'E' || character == 'F';
106 }
107
108 private static final class InvalidUriException extends RuntimeException {
109 private static final long serialVersionUID = 7013885420191182730L;
110
111 private InvalidUriException(URISyntaxException cause) {
112 super(cause);
113 }
114
115 }
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134 public static String createURI(final String scheme, final String host, int port, final String path,
135 final String query, final String fragment) {
136 StringBuilder buffer = new StringBuilder(Parameters.SMALL_BUFFER_SIZE);
137 if (host != null) {
138 if (scheme != null) {
139 buffer.append(scheme);
140 buffer.append("://");
141 }
142 buffer.append(host);
143 if (port > 0) {
144 buffer.append(':');
145 buffer.append(port);
146 }
147 }
148 if (path == null || !path.startsWith("/")) {
149 buffer.append('/');
150 }
151 if (path != null) {
152 buffer.append(path);
153 }
154 if (query != null) {
155 buffer.append('?');
156 buffer.append(query);
157 }
158 if (fragment != null) {
159 buffer.append('#');
160 buffer.append(fragment);
161 }
162 return buffer.toString();
163 }
164
165
166
167
168
169
170
171
172 public static String extractHostName(final String uri) {
173 return extractHost(uri).getHostName();
174 }
175
176
177
178
179
180
181
182
183 public static HttpHost extractHost(final String uri) {
184 return URIUtils.extractHost(createURI(uri));
185 }
186
187
188
189
190
191
192
193
194 public static HttpHost extractHost(final URI uri) {
195 return URIUtils.extractHost(uri);
196 }
197
198
199
200
201
202
203
204
205
206 public static URI createURI(String uri) {
207 uri = encodeIllegalCharacters(uri);
208 URI result = URI.create(uri);
209 if (result.getHost() != null && StringUtils.isEmpty(result.getPath())) {
210 result =
211 URI.create(createURI(result.getScheme(), result.getHost(), result.getPort(), "/",
212 result.getRawQuery(), result.getRawFragment()));
213 }
214 return result;
215 }
216
217
218
219
220
221
222
223
224
225
226 public static String rewriteURI(String uri, HttpHost targetHost) {
227 try {
228 return URIUtils.rewriteURI(createURI(uri), targetHost).toString();
229 } catch (URISyntaxException e) {
230 throw new InvalidUriException(e);
231 }
232 }
233
234
235
236
237
238
239
240
241
242
243 public static String removeSessionId(String sessionId, String page) {
244 String regexp = ";?jsessionid=" + Pattern.quote(sessionId);
245 return page.replaceAll(regexp, "");
246 }
247
248
249
250
251
252
253
254
255 public static String extractScheme(String uri) {
256 return extractHost(uri).getSchemeName();
257 }
258
259
260
261
262
263
264
265
266
267 public static String getRawQuery(String uri) {
268 return createURI(uri).getRawQuery();
269 }
270
271
272
273
274
275
276
277
278
279
280 public static String getPath(String uri) {
281 return createURI(uri).getPath();
282 }
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300 public static List<NameValuePair> parse(final String uri, final String charset) {
301 return URLEncodedUtils.parse(createURI(uri), charset);
302 }
303
304
305
306
307
308
309
310
311 public static boolean isAbsolute(String uri) {
312 return (uri.startsWith("http://") || uri.startsWith("https://"));
313 }
314
315
316
317
318
319
320
321
322
323
324
325
326 public static URI concatPath(URI base, String relPath) {
327 String resultPath = base.getPath() + StringUtils.stripStart(relPath, "/");
328 try {
329 return new URI(base.getScheme(), base.getUserInfo(), base.getHost(), base.getPort(), resultPath, null, null);
330 } catch (URISyntaxException e) {
331 throw new InvalidUriException(e);
332 }
333 }
334
335
336
337
338
339
340
341
342 public static URI removeServer(URI uri) {
343 try {
344 return new URI(null, null, null, -1, uri.getPath(), uri.getQuery(), uri.getFragment());
345 } catch (URISyntaxException e) {
346 throw new InvalidUriException(e);
347 }
348
349 }
350
351
352
353
354
355
356
357
358
359
360
361 public static URI resolve(String relUri, URI base) {
362 URI uri = createURI(relUri);
363 if (uri.getScheme() == null && uri.getUserInfo() == null && uri.getHost() == null && uri.getPort() == -1
364 && StringUtils.isEmpty(uri.getPath()) && uri.getQuery() != null) {
365 try {
366 return new URI(base.getScheme(), base.getUserInfo(), base.getHost(), base.getPort(), base.getPath(),
367 uri.getQuery(), uri.getFragment());
368 } catch (URISyntaxException e) {
369 throw new InvalidUriException(e);
370 }
371 } else {
372 return base.resolve(uri);
373 }
374 }
375
376
377
378
379
380
381
382
383
384 public static String removeQuerystring(String uriString) {
385 URI uri = createURI(uriString);
386 try {
387 return new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), uri.getPath(), null, null)
388 .toASCIIString();
389 } catch (URISyntaxException e) {
390 throw new InvalidUriException(e);
391 }
392 }
393
394 }