Left: | ||
Right: |
OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2010 Google Inc. | 2 * Copyright (c) 2010 Google Inc. |
3 * | 3 * |
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not u se this file except | 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not u se this file except |
5 * in compliance with the License. You may obtain a copy of the License at | 5 * in compliance with the License. You may obtain a copy of the License at |
6 * | 6 * |
7 * http://www.apache.org/licenses/LICENSE-2.0 | 7 * http://www.apache.org/licenses/LICENSE-2.0 |
8 * | 8 * |
9 * Unless required by applicable law or agreed to in writing, software distribut ed under the License | 9 * Unless required by applicable law or agreed to in writing, software distribut ed under the License |
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY K IND, either express | 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY K IND, either express |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
43 * value as the parameter value. Subclasses can declare fields for known query p arameters using the | 43 * value as the parameter value. Subclasses can declare fields for known query p arameters using the |
44 * {@link Key} annotation. {@code null} parameter names are not allowed, but {@c ode null} query | 44 * {@link Key} annotation. {@code null} parameter names are not allowed, but {@c ode null} query |
45 * values are allowed. | 45 * values are allowed. |
46 * </p> | 46 * </p> |
47 * | 47 * |
48 * <p> | 48 * <p> |
49 * Query parameter values are parsed using {@link UrlEncodedParser#parse(String, Object)}. | 49 * Query parameter values are parsed using {@link UrlEncodedParser#parse(String, Object)}. |
50 * </p> | 50 * </p> |
51 * | 51 * |
52 * <p> | 52 * <p> |
53 * Upgrade warning: in prior version 1.14 user info part in the URL was dropped, but starting with | |
yanivi
2013/04/11 21:03:57
insert "the" before "user info"
peleyal
2013/04/12 13:14:08
Done.
| |
54 * version 1.15 this URL class contains the user info, and it can be retrieved o r set with | |
55 * {@link #getUserInfo} and {@link #setUserInfo}. | |
56 * </p> | |
57 * | |
58 * <p> | |
53 * Implementation is not thread-safe. | 59 * Implementation is not thread-safe. |
54 * </p> | 60 * </p> |
55 * | 61 * |
56 * @since 1.0 | 62 * @since 1.0 |
57 * @author Yaniv Inbar | 63 * @author Yaniv Inbar |
58 */ | 64 */ |
59 public class GenericUrl extends GenericData { | 65 public class GenericUrl extends GenericData { |
60 | 66 |
61 private static final Escaper URI_FRAGMENT_ESCAPER = | 67 private static final Escaper URI_FRAGMENT_ESCAPER = |
62 new PercentEscaper("=&-_.!~*'()@:$,;/?:", false); | 68 new PercentEscaper("=&-_.!~*'()@:$,;/?:", false); |
63 | 69 |
64 /** Scheme (lowercase), for example {@code "https"}. */ | 70 /** Scheme (lowercase), for example {@code "https"}. */ |
65 private String scheme; | 71 private String scheme; |
66 | 72 |
67 /** Host, for example {@code "www.google.com"}. */ | 73 /** Host, for example {@code "www.google.com"}. */ |
68 private String host; | 74 private String host; |
69 | 75 |
76 /** User info or {@code null} for none, for example {@code "username:password" }. */ | |
77 private String userInfo; | |
78 | |
70 /** Port number or {@code -1} if undefined, for example {@code 443}. */ | 79 /** Port number or {@code -1} if undefined, for example {@code 443}. */ |
71 private int port = -1; | 80 private int port = -1; |
72 | 81 |
73 /** | 82 /** |
74 * Decoded path component by parts with each part separated by a {@code '/'} o r {@code null} for | 83 * Decoded path component by parts with each part separated by a {@code '/'} o r {@code null} for |
75 * none, for example {@code "/m8/feeds/contacts/default/full"} is represented by {@code "", "m8", | 84 * none, for example {@code "/m8/feeds/contacts/default/full"} is represented by {@code "", "m8", |
76 *"feeds", "contacts", "default", "full"}. | 85 *"feeds", "contacts", "default", "full"}. |
77 * <p> | 86 * <p> |
78 * Use {@link #appendRawPath(String)} to append to the path, which ensures tha t no extra slash is | 87 * Use {@link #appendRawPath(String)} to append to the path, which ensures tha t no extra slash is |
79 * added. | 88 * added. |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
111 * | 120 * |
112 * @param uri URI | 121 * @param uri URI |
113 * | 122 * |
114 * @since 1.14 | 123 * @since 1.14 |
115 */ | 124 */ |
116 public GenericUrl(URI uri) { | 125 public GenericUrl(URI uri) { |
117 this(uri.getScheme(), | 126 this(uri.getScheme(), |
118 uri.getHost(), | 127 uri.getHost(), |
119 uri.getPort(), | 128 uri.getPort(), |
120 uri.getRawPath(), | 129 uri.getRawPath(), |
121 uri.getFragment(), | 130 uri.getFragment(), |
yanivi
2013/04/11 21:03:57
getRawFragment() because we want the encoded form
peleyal
2013/04/12 13:14:08
Done.
| |
122 uri.getRawQuery()); | 131 uri.getRawQuery(), |
132 uri.getUserInfo()); | |
yanivi
2013/04/11 21:03:57
similarly getRawUserInfo()
peleyal
2013/04/12 13:14:08
Done.
| |
123 } | 133 } |
124 | 134 |
125 /** | 135 /** |
126 * Constructs from a URL. | 136 * Constructs from a URL. |
127 * | 137 * |
128 * @param url URL | 138 * @param url URL |
129 * | 139 * |
130 * @since 1.14 | 140 * @since 1.14 |
131 */ | 141 */ |
132 public GenericUrl(URL url) { | 142 public GenericUrl(URL url) { |
133 this(url.getProtocol(), | 143 this(url.getProtocol(), |
134 url.getHost(), | 144 url.getHost(), |
135 url.getPort(), | 145 url.getPort(), |
136 url.getPath(), | 146 url.getPath(), |
137 url.getRef(), | 147 url.getRef(), |
138 url.getQuery()); | 148 url.getQuery(), |
149 url.getUserInfo()); | |
139 } | 150 } |
140 | 151 |
141 private GenericUrl(String scheme, | 152 private GenericUrl(String scheme, |
142 String host, | 153 String host, |
143 int port, | 154 int port, |
144 String path, | 155 String path, |
145 String fragment, | 156 String fragment, |
146 String query) { | 157 String query, |
158 String userInfo) { | |
147 this.scheme = scheme.toLowerCase(); | 159 this.scheme = scheme.toLowerCase(); |
148 this.host = host; | 160 this.host = host; |
149 this.port = port; | 161 this.port = port; |
150 this.pathParts = toPathParts(path); | 162 this.pathParts = toPathParts(path); |
151 this.fragment = fragment; | 163 this.fragment = fragment; |
yanivi
2013/04/11 21:03:57
need to decode the fragment?
peleyal
2013/04/12 13:14:08
Done.
| |
152 if (query != null) { | 164 if (query != null) { |
153 UrlEncodedParser.parse(query, this); | 165 UrlEncodedParser.parse(query, this); |
154 } | 166 } |
167 this.userInfo = userInfo != null ? CharEscapers.decodeUri(userInfo) : null; | |
155 } | 168 } |
156 | 169 |
157 @Override | 170 @Override |
158 public int hashCode() { | 171 public int hashCode() { |
159 // TODO(yanivi): optimize? | 172 // TODO(yanivi): optimize? |
160 return build().hashCode(); | 173 return build().hashCode(); |
161 } | 174 } |
162 | 175 |
163 @Override | 176 @Override |
164 public boolean equals(Object obj) { | 177 public boolean equals(Object obj) { |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
222 /** | 235 /** |
223 * Sets the host, for example {@code "www.google.com"}. | 236 * Sets the host, for example {@code "www.google.com"}. |
224 * | 237 * |
225 * @since 1.5 | 238 * @since 1.5 |
226 */ | 239 */ |
227 public final void setHost(String host) { | 240 public final void setHost(String host) { |
228 this.host = Preconditions.checkNotNull(host); | 241 this.host = Preconditions.checkNotNull(host); |
229 } | 242 } |
230 | 243 |
231 /** | 244 /** |
245 * Returns the user info or {@code null} for none, for example {@code "usernam e:password"}. | |
246 * | |
247 * @since 1.15 | |
248 */ | |
249 public final String getUserInfo() { | |
250 return userInfo; | |
251 } | |
252 | |
253 /** | |
254 * Sets the user info or {@code null} for none, for example {@code "username:p assword"}. | |
255 * | |
256 * @since 1.15 | |
257 */ | |
258 public final void setUserInfo(String userInfo) { | |
259 this.userInfo = userInfo; | |
260 } | |
261 | |
262 /** | |
232 * Returns the port number or {@code -1} if undefined, for example {@code 443} . | 263 * Returns the port number or {@code -1} if undefined, for example {@code 443} . |
233 * | 264 * |
234 * @since 1.5 | 265 * @since 1.5 |
235 */ | 266 */ |
236 public int getPort() { | 267 public int getPort() { |
237 return port; | 268 return port; |
238 } | 269 } |
239 | 270 |
240 /** | 271 /** |
241 * Returns the port number or {@code -1} if undefined, for example {@code 443} . | 272 * Sets the port number, for example {@code 443}. |
242 * | 273 * |
243 * @since 1.5 | 274 * @since 1.5 |
244 */ | 275 */ |
245 public final void setPort(int port) { | 276 public final void setPort(int port) { |
246 Preconditions.checkArgument(port >= -1, "expected port >= -1"); | 277 Preconditions.checkArgument(port >= -1, "expected port >= -1"); |
247 this.port = port; | 278 this.port = port; |
248 } | 279 } |
249 | 280 |
250 /** | 281 /** |
251 * Sets the decoded path component by parts with each part separated by a {@co de '/'} or | 282 * Returns the decoded path component by parts with each part separated by a { @code '/'} or |
252 * {@code null} for none. | 283 * {@code null} for none. |
253 * | 284 * |
254 * @since 1.5 | 285 * @since 1.5 |
255 */ | 286 */ |
256 public List<String> getPathParts() { | 287 public List<String> getPathParts() { |
257 return pathParts; | 288 return pathParts; |
258 } | 289 } |
259 | 290 |
260 /** | 291 /** |
261 * Sets the decoded path component by parts with each part separated by a {@co de '/'} or | 292 * Sets the decoded path component by parts with each part separated by a {@co de '/'} or |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
304 } | 335 } |
305 | 336 |
306 /** | 337 /** |
307 * Constructs the portion of the URL containing the scheme, host and port. | 338 * Constructs the portion of the URL containing the scheme, host and port. |
308 * | 339 * |
309 * <p> | 340 * <p> |
310 * For the URL {@code "http://example.com/something?action=add"} this method w ould return | 341 * For the URL {@code "http://example.com/something?action=add"} this method w ould return |
311 * {@code "http://example.com"}. | 342 * {@code "http://example.com"}. |
312 * </p> | 343 * </p> |
313 * | 344 * |
314 * @return scheme://host[:port] | 345 * @return scheme://[user-info@]host[:port] |
315 * @since 1.9 | 346 * @since 1.9 |
316 */ | 347 */ |
317 public final String buildAuthority() { | 348 public final String buildAuthority() { |
318 // scheme, host, port | 349 // scheme, [user info], host, [port] |
319 StringBuilder buf = new StringBuilder(); | 350 StringBuilder buf = new StringBuilder(); |
320 buf.append(Preconditions.checkNotNull(scheme)); | 351 buf.append(Preconditions.checkNotNull(scheme)); |
321 buf.append("://"); | 352 buf.append("://"); |
353 if (userInfo != null) { | |
354 buf.append(CharEscapers.escapeUriUserInfo(userInfo)).append('@'); | |
355 } | |
322 buf.append(Preconditions.checkNotNull(host)); | 356 buf.append(Preconditions.checkNotNull(host)); |
323 int port = this.port; | 357 int port = this.port; |
324 if (port != -1) { | 358 if (port != -1) { |
325 buf.append(':').append(port); | 359 buf.append(':').append(port); |
326 } | 360 } |
327 return buf.toString(); | 361 return buf.toString(); |
328 } | 362 } |
329 | 363 |
330 /** | 364 /** |
331 * Constructs the portion of the URL beginning at the rooted path. | 365 * Constructs the portion of the URL beginning at the rooted path. |
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
585 * @return URI | 619 * @return URI |
586 */ | 620 */ |
587 private static URI toURI(String encodedUrl) { | 621 private static URI toURI(String encodedUrl) { |
588 try { | 622 try { |
589 return new URI(encodedUrl); | 623 return new URI(encodedUrl); |
590 } catch (URISyntaxException e) { | 624 } catch (URISyntaxException e) { |
591 throw new IllegalArgumentException(e); | 625 throw new IllegalArgumentException(e); |
592 } | 626 } |
593 } | 627 } |
594 } | 628 } |
OLD | NEW |