author | chegar |
Thu, 17 Oct 2019 20:54:25 +0100 | |
branch | datagramsocketimpl-branch |
changeset 58679 | 9c3209ff7550 |
parent 58678 | 9cf78a70fa4f |
parent 58631 | 36c5e85b8597 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
54696
0907dce4b90e
8217364: Custom URLStreamHandler for jrt or file protocol can override default handler
coffeys
parents:
53125
diff
changeset
|
2 |
* Copyright (c) 1995, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.net; |
|
27 |
||
52944
d75110673dc9
8215008: Clear confusion between URL/URI paths and file system paths
dfuchs
parents:
52700
diff
changeset
|
28 |
import java.io.File; |
2 | 29 |
import java.io.IOException; |
30 |
import java.io.InputStream; |
|
29097 | 31 |
import java.net.spi.URLStreamHandlerProvider; |
52944
d75110673dc9
8215008: Clear confusion between URL/URI paths and file system paths
dfuchs
parents:
52700
diff
changeset
|
32 |
import java.nio.file.Path; |
29097 | 33 |
import java.security.AccessController; |
34 |
import java.security.PrivilegedAction; |
|
2 | 35 |
import java.util.Hashtable; |
35280 | 36 |
import java.io.InvalidObjectException; |
37 |
import java.io.ObjectStreamException; |
|
38 |
import java.io.ObjectStreamField; |
|
39 |
import java.io.ObjectInputStream.GetField; |
|
29097 | 40 |
import java.util.Iterator; |
35381
2545709d9a45
8147962: URL should handle lower-casing of protocol locale-independently
redestad
parents:
35280
diff
changeset
|
41 |
import java.util.Locale; |
29097 | 42 |
import java.util.NoSuchElementException; |
43 |
import java.util.ServiceConfigurationError; |
|
44 |
import java.util.ServiceLoader; |
|
45 |
||
52427
3c6aa484536c
8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents:
49528
diff
changeset
|
46 |
import jdk.internal.access.JavaNetURLAccess; |
3c6aa484536c
8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents:
49528
diff
changeset
|
47 |
import jdk.internal.access.SharedSecrets; |
55711 | 48 |
import sun.net.util.IPAddressUtil; |
2 | 49 |
import sun.security.util.SecurityConstants; |
37593
824750ada3d6
8154231: Simplify access to System properties from JDK code
redestad
parents:
36511
diff
changeset
|
50 |
import sun.security.action.GetPropertyAction; |
2 | 51 |
|
52 |
/** |
|
19069 | 53 |
* Class {@code URL} represents a Uniform Resource |
2 | 54 |
* Locator, a pointer to a "resource" on the World |
55 |
* Wide Web. A resource can be something as simple as a file or a |
|
56 |
* directory, or it can be a reference to a more complicated object, |
|
57 |
* such as a query to a database or to a search engine. More |
|
58 |
* information on the types of URLs and their formats can be found at: |
|
15309
b585dca79d0b
7017962: Obsolete link is used in URL class level spec
khazra
parents:
14342
diff
changeset
|
59 |
* <a href= |
b585dca79d0b
7017962: Obsolete link is used in URL class level spec
khazra
parents:
14342
diff
changeset
|
60 |
* "http://web.archive.org/web/20051219043731/http://archive.ncsa.uiuc.edu/SDG/Software/Mosaic/Demo/url-primer.html"> |
b585dca79d0b
7017962: Obsolete link is used in URL class level spec
khazra
parents:
14342
diff
changeset
|
61 |
* <i>Types of URL</i></a> |
2 | 62 |
* <p> |
15309
b585dca79d0b
7017962: Obsolete link is used in URL class level spec
khazra
parents:
14342
diff
changeset
|
63 |
* In general, a URL can be broken into several parts. Consider the |
b585dca79d0b
7017962: Obsolete link is used in URL class level spec
khazra
parents:
14342
diff
changeset
|
64 |
* following example: |
b585dca79d0b
7017962: Obsolete link is used in URL class level spec
khazra
parents:
14342
diff
changeset
|
65 |
* <blockquote><pre> |
b585dca79d0b
7017962: Obsolete link is used in URL class level spec
khazra
parents:
14342
diff
changeset
|
66 |
* http://www.example.com/docs/resource1.html |
b585dca79d0b
7017962: Obsolete link is used in URL class level spec
khazra
parents:
14342
diff
changeset
|
67 |
* </pre></blockquote> |
b585dca79d0b
7017962: Obsolete link is used in URL class level spec
khazra
parents:
14342
diff
changeset
|
68 |
* <p> |
b585dca79d0b
7017962: Obsolete link is used in URL class level spec
khazra
parents:
14342
diff
changeset
|
69 |
* The URL above indicates that the protocol to use is |
19069 | 70 |
* {@code http} (HyperText Transfer Protocol) and that the |
2 | 71 |
* information resides on a host machine named |
19069 | 72 |
* {@code www.example.com}. The information on that host |
73 |
* machine is named {@code /docs/resource1.html}. The exact |
|
2 | 74 |
* meaning of this name on the host machine is both protocol |
75 |
* dependent and host dependent. The information normally resides in |
|
76 |
* a file, but it could be generated on the fly. This component of |
|
77 |
* the URL is called the <i>path</i> component. |
|
78 |
* <p> |
|
79 |
* A URL can optionally specify a "port", which is the |
|
80 |
* port number to which the TCP connection is made on the remote host |
|
81 |
* machine. If the port is not specified, the default port for |
|
82 |
* the protocol is used instead. For example, the default port for |
|
19069 | 83 |
* {@code http} is {@code 80}. An alternative port could be |
2 | 84 |
* specified as: |
85 |
* <blockquote><pre> |
|
15309
b585dca79d0b
7017962: Obsolete link is used in URL class level spec
khazra
parents:
14342
diff
changeset
|
86 |
* http://www.example.com:1080/docs/resource1.html |
2 | 87 |
* </pre></blockquote> |
88 |
* <p> |
|
19069 | 89 |
* The syntax of {@code URL} is defined by <a |
708 | 90 |
* href="http://www.ietf.org/rfc/rfc2396.txt"><i>RFC 2396: Uniform |
2 | 91 |
* Resource Identifiers (URI): Generic Syntax</i></a>, amended by <a |
92 |
* href="http://www.ietf.org/rfc/rfc2732.txt"><i>RFC 2732: Format for |
|
93 |
* Literal IPv6 Addresses in URLs</i></a>. The Literal IPv6 address format |
|
94 |
* also supports scope_ids. The syntax and usage of scope_ids is described |
|
95 |
* <a href="Inet6Address.html#scoped">here</a>. |
|
96 |
* <p> |
|
97 |
* A URL may have appended to it a "fragment", also known |
|
98 |
* as a "ref" or a "reference". The fragment is indicated by the sharp |
|
99 |
* sign character "#" followed by more characters. For example, |
|
100 |
* <blockquote><pre> |
|
52700
b206bdfb9fe2
8213911: Use example.com in java.net and other examples
darcy
parents:
52694
diff
changeset
|
101 |
* http://www.example.com/index.html#chapter1 |
2 | 102 |
* </pre></blockquote> |
103 |
* <p> |
|
104 |
* This fragment is not technically part of the URL. Rather, it |
|
105 |
* indicates that after the specified resource is retrieved, the |
|
106 |
* application is specifically interested in that part of the |
|
19069 | 107 |
* document that has the tag {@code chapter1} attached to it. The |
2 | 108 |
* meaning of a tag is resource specific. |
109 |
* <p> |
|
110 |
* An application can also specify a "relative URL", |
|
111 |
* which contains only enough information to reach the resource |
|
112 |
* relative to another URL. Relative URLs are frequently used within |
|
113 |
* HTML pages. For example, if the contents of the URL: |
|
114 |
* <blockquote><pre> |
|
52700
b206bdfb9fe2
8213911: Use example.com in java.net and other examples
darcy
parents:
52694
diff
changeset
|
115 |
* http://www.example.com/index.html |
2 | 116 |
* </pre></blockquote> |
117 |
* contained within it the relative URL: |
|
118 |
* <blockquote><pre> |
|
119 |
* FAQ.html |
|
120 |
* </pre></blockquote> |
|
121 |
* it would be a shorthand for: |
|
122 |
* <blockquote><pre> |
|
52700
b206bdfb9fe2
8213911: Use example.com in java.net and other examples
darcy
parents:
52694
diff
changeset
|
123 |
* http://www.example.com/FAQ.html |
2 | 124 |
* </pre></blockquote> |
125 |
* <p> |
|
126 |
* The relative URL need not specify all the components of a URL. If |
|
127 |
* the protocol, host name, or port number is missing, the value is |
|
128 |
* inherited from the fully specified URL. The file component must be |
|
129 |
* specified. The optional fragment is not inherited. |
|
130 |
* <p> |
|
131 |
* The URL class does not itself encode or decode any URL components |
|
132 |
* according to the escaping mechanism defined in RFC2396. It is the |
|
133 |
* responsibility of the caller to encode any fields, which need to be |
|
134 |
* escaped prior to calling URL, and also to decode any escaped fields, |
|
135 |
* that are returned from URL. Furthermore, because URL has no knowledge |
|
136 |
* of URL escaping, it does not recognise equivalence between the encoded |
|
137 |
* or decoded form of the same URL. For example, the two URLs:<br> |
|
138 |
* <pre> http://foo.com/hello world/ and http://foo.com/hello%20world</pre> |
|
139 |
* would be considered not equal to each other. |
|
140 |
* <p> |
|
141 |
* Note, the {@link java.net.URI} class does perform escaping of its |
|
142 |
* component fields in certain circumstances. The recommended way |
|
143 |
* to manage the encoding and decoding of URLs is to use {@link java.net.URI}, |
|
144 |
* and to convert between these two classes using {@link #toURI()} and |
|
145 |
* {@link URI#toURL()}. |
|
146 |
* <p> |
|
147 |
* The {@link URLEncoder} and {@link URLDecoder} classes can also be |
|
148 |
* used, but only for HTML form encoding, which is not the same |
|
149 |
* as the encoding scheme defined in RFC2396. |
|
150 |
* |
|
52944
d75110673dc9
8215008: Clear confusion between URL/URI paths and file system paths
dfuchs
parents:
52700
diff
changeset
|
151 |
* @apiNote |
d75110673dc9
8215008: Clear confusion between URL/URI paths and file system paths
dfuchs
parents:
52700
diff
changeset
|
152 |
* |
d75110673dc9
8215008: Clear confusion between URL/URI paths and file system paths
dfuchs
parents:
52700
diff
changeset
|
153 |
* Applications working with file paths and file URIs should take great |
d75110673dc9
8215008: Clear confusion between URL/URI paths and file system paths
dfuchs
parents:
52700
diff
changeset
|
154 |
* care to use the appropriate methods to convert between the two. |
d75110673dc9
8215008: Clear confusion between URL/URI paths and file system paths
dfuchs
parents:
52700
diff
changeset
|
155 |
* The {@link Path#of(URI)} factory method and the {@link File#File(URI)} |
d75110673dc9
8215008: Clear confusion between URL/URI paths and file system paths
dfuchs
parents:
52700
diff
changeset
|
156 |
* constructor can be used to create {@link Path} or {@link File} |
d75110673dc9
8215008: Clear confusion between URL/URI paths and file system paths
dfuchs
parents:
52700
diff
changeset
|
157 |
* objects from a file URI. {@link Path#toUri()} and {@link File#toURI()} |
d75110673dc9
8215008: Clear confusion between URL/URI paths and file system paths
dfuchs
parents:
52700
diff
changeset
|
158 |
* can be used to create a {@link URI} from a file path, which can be |
d75110673dc9
8215008: Clear confusion between URL/URI paths and file system paths
dfuchs
parents:
52700
diff
changeset
|
159 |
* converted to URL using {@link URI#toURL()}. |
d75110673dc9
8215008: Clear confusion between URL/URI paths and file system paths
dfuchs
parents:
52700
diff
changeset
|
160 |
* Applications should never try to {@linkplain #URL(String, String, String) |
d75110673dc9
8215008: Clear confusion between URL/URI paths and file system paths
dfuchs
parents:
52700
diff
changeset
|
161 |
* construct} or {@linkplain #URL(String) parse} a {@code URL} |
d75110673dc9
8215008: Clear confusion between URL/URI paths and file system paths
dfuchs
parents:
52700
diff
changeset
|
162 |
* from the direct string representation of a {@code File} or {@code Path} |
d75110673dc9
8215008: Clear confusion between URL/URI paths and file system paths
dfuchs
parents:
52700
diff
changeset
|
163 |
* instance. |
d75110673dc9
8215008: Clear confusion between URL/URI paths and file system paths
dfuchs
parents:
52700
diff
changeset
|
164 |
* <p> |
d75110673dc9
8215008: Clear confusion between URL/URI paths and file system paths
dfuchs
parents:
52700
diff
changeset
|
165 |
* Some components of a URL or URI, such as <i>userinfo</i>, may |
d75110673dc9
8215008: Clear confusion between URL/URI paths and file system paths
dfuchs
parents:
52700
diff
changeset
|
166 |
* be abused to construct misleading URLs or URIs. Applications |
d75110673dc9
8215008: Clear confusion between URL/URI paths and file system paths
dfuchs
parents:
52700
diff
changeset
|
167 |
* that deal with URLs or URIs should take into account |
d75110673dc9
8215008: Clear confusion between URL/URI paths and file system paths
dfuchs
parents:
52700
diff
changeset
|
168 |
* the recommendations advised in <a |
d75110673dc9
8215008: Clear confusion between URL/URI paths and file system paths
dfuchs
parents:
52700
diff
changeset
|
169 |
* href="https://tools.ietf.org/html/rfc3986#section-7">RFC3986, |
d75110673dc9
8215008: Clear confusion between URL/URI paths and file system paths
dfuchs
parents:
52700
diff
changeset
|
170 |
* Section 7, Security Considerations</a>. |
d75110673dc9
8215008: Clear confusion between URL/URI paths and file system paths
dfuchs
parents:
52700
diff
changeset
|
171 |
* |
2 | 172 |
* @author James Gosling |
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
24260
diff
changeset
|
173 |
* @since 1.0 |
2 | 174 |
*/ |
175 |
public final class URL implements java.io.Serializable { |
|
176 |
||
35280 | 177 |
static final String BUILTIN_HANDLERS_PREFIX = "sun.net.www.protocol"; |
57956
e0b8b019d2f5
8229997: Apply java.io.Serial annotations in java.base
darcy
parents:
55711
diff
changeset
|
178 |
@java.io.Serial |
2 | 179 |
static final long serialVersionUID = -7627629688361524110L; |
180 |
||
181 |
/** |
|
182 |
* The property which specifies the package prefix list to be scanned |
|
183 |
* for protocol handlers. The value of this property (if any) should |
|
184 |
* be a vertical bar delimited list of package names to search through |
|
185 |
* for a protocol handler to load. The policy of this class is that |
|
186 |
* all protocol handlers will be in a class called <protocolname>.Handler, |
|
187 |
* and each package in the list is examined in turn for a matching |
|
188 |
* handler. If none are found (or the property is not specified), the |
|
189 |
* default package prefix, sun.net.www.protocol, is used. The search |
|
190 |
* proceeds from the first package in the list to the last and stops |
|
191 |
* when a match is found. |
|
192 |
*/ |
|
193 |
private static final String protocolPathProp = "java.protocol.handler.pkgs"; |
|
194 |
||
195 |
/** |
|
196 |
* The protocol to use (ftp, http, nntp, ... etc.) . |
|
197 |
* @serial |
|
198 |
*/ |
|
199 |
private String protocol; |
|
200 |
||
201 |
/** |
|
202 |
* The host name to connect to. |
|
203 |
* @serial |
|
204 |
*/ |
|
205 |
private String host; |
|
206 |
||
207 |
/** |
|
208 |
* The protocol port to connect to. |
|
209 |
* @serial |
|
210 |
*/ |
|
211 |
private int port = -1; |
|
212 |
||
213 |
/** |
|
19069 | 214 |
* The specified file name on that host. {@code file} is |
215 |
* defined as {@code path[?query]} |
|
2 | 216 |
* @serial |
217 |
*/ |
|
218 |
private String file; |
|
219 |
||
220 |
/** |
|
221 |
* The query part of this URL. |
|
222 |
*/ |
|
223 |
private transient String query; |
|
224 |
||
225 |
/** |
|
226 |
* The authority part of this URL. |
|
227 |
* @serial |
|
228 |
*/ |
|
229 |
private String authority; |
|
230 |
||
231 |
/** |
|
232 |
* The path part of this URL. |
|
233 |
*/ |
|
234 |
private transient String path; |
|
235 |
||
236 |
/** |
|
237 |
* The userinfo part of this URL. |
|
238 |
*/ |
|
239 |
private transient String userInfo; |
|
240 |
||
241 |
/** |
|
242 |
* # reference. |
|
243 |
* @serial |
|
244 |
*/ |
|
245 |
private String ref; |
|
246 |
||
247 |
/** |
|
248 |
* The host's IP address, used in equals and hashCode. |
|
249 |
* Computed on demand. An uninitialized or unknown hostAddress is null. |
|
250 |
*/ |
|
251 |
transient InetAddress hostAddress; |
|
252 |
||
253 |
/** |
|
254 |
* The URLStreamHandler for this URL. |
|
255 |
*/ |
|
256 |
transient URLStreamHandler handler; |
|
257 |
||
258 |
/* Our hash code. |
|
259 |
* @serial |
|
260 |
*/ |
|
261 |
private int hashCode = -1; |
|
262 |
||
35280 | 263 |
private transient UrlDeserializedState tempState; |
264 |
||
2 | 265 |
/** |
19069 | 266 |
* Creates a {@code URL} object from the specified |
267 |
* {@code protocol}, {@code host}, {@code port} |
|
268 |
* number, and {@code file}.<p> |
|
2 | 269 |
* |
19069 | 270 |
* {@code host} can be expressed as a host name or a literal |
2 | 271 |
* IP address. If IPv6 literal address is used, it should be |
19069 | 272 |
* enclosed in square brackets ({@code '['} and {@code ']'}), as |
2 | 273 |
* specified by <a |
274 |
* href="http://www.ietf.org/rfc/rfc2732.txt">RFC 2732</a>; |
|
275 |
* However, the literal IPv6 address format defined in <a |
|
276 |
* href="http://www.ietf.org/rfc/rfc2373.txt"><i>RFC 2373: IP |
|
277 |
* Version 6 Addressing Architecture</i></a> is also accepted.<p> |
|
278 |
* |
|
19069 | 279 |
* Specifying a {@code port} number of {@code -1} |
2 | 280 |
* indicates that the URL should use the default port for the |
281 |
* protocol.<p> |
|
282 |
* |
|
283 |
* If this is the first URL object being created with the specified |
|
284 |
* protocol, a <i>stream protocol handler</i> object, an instance of |
|
19069 | 285 |
* class {@code URLStreamHandler}, is created for that protocol: |
2 | 286 |
* <ol> |
287 |
* <li>If the application has previously set up an instance of |
|
19069 | 288 |
* {@code URLStreamHandlerFactory} as the stream handler factory, |
289 |
* then the {@code createURLStreamHandler} method of that instance |
|
2 | 290 |
* is called with the protocol string as an argument to create the |
291 |
* stream protocol handler. |
|
19069 | 292 |
* <li>If no {@code URLStreamHandlerFactory} has yet been set up, |
293 |
* or if the factory's {@code createURLStreamHandler} method |
|
29097 | 294 |
* returns {@code null}, then the {@linkplain java.util.ServiceLoader |
295 |
* ServiceLoader} mechanism is used to locate {@linkplain |
|
296 |
* java.net.spi.URLStreamHandlerProvider URLStreamHandlerProvider} |
|
297 |
* implementations using the system class |
|
298 |
* loader. The order that providers are located is implementation |
|
299 |
* specific, and an implementation is free to cache the located |
|
300 |
* providers. A {@linkplain java.util.ServiceConfigurationError |
|
301 |
* ServiceConfigurationError}, {@code Error} or {@code RuntimeException} |
|
302 |
* thrown from the {@code createURLStreamHandler}, if encountered, will |
|
303 |
* be propagated to the calling thread. The {@code |
|
304 |
* createURLStreamHandler} method of each provider, if instantiated, is |
|
305 |
* invoked, with the protocol string, until a provider returns non-null, |
|
306 |
* or all providers have been exhausted. |
|
29977
ad35f9986f75
8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
29098
diff
changeset
|
307 |
* <li>If the previous step fails to find a protocol handler, the |
ad35f9986f75
8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
29098
diff
changeset
|
308 |
* constructor reads the value of the system property: |
53120
de9fd809bb47
8214567: Use {@systemProperty} for definitions of system properties
rpatil
parents:
53018
diff
changeset
|
309 |
* <blockquote>{@systemProperty |
29977
ad35f9986f75
8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
29098
diff
changeset
|
310 |
* java.protocol.handler.pkgs |
ad35f9986f75
8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
29098
diff
changeset
|
311 |
* }</blockquote> |
ad35f9986f75
8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
29098
diff
changeset
|
312 |
* If the value of that system property is not {@code null}, |
ad35f9986f75
8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
29098
diff
changeset
|
313 |
* it is interpreted as a list of packages separated by a vertical |
ad35f9986f75
8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
29098
diff
changeset
|
314 |
* slash character '{@code |}'. The constructor tries to load |
ad35f9986f75
8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
29098
diff
changeset
|
315 |
* the class named: |
ad35f9986f75
8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
29098
diff
changeset
|
316 |
* <blockquote>{@code |
ad35f9986f75
8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
29098
diff
changeset
|
317 |
* <package>.<protocol>.Handler |
ad35f9986f75
8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
29098
diff
changeset
|
318 |
* }</blockquote> |
ad35f9986f75
8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
29098
diff
changeset
|
319 |
* where {@code <package>} is replaced by the name of the package |
ad35f9986f75
8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
29098
diff
changeset
|
320 |
* and {@code <protocol>} is replaced by the name of the protocol. |
ad35f9986f75
8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
29098
diff
changeset
|
321 |
* If this class does not exist, or if the class exists but it is not |
ad35f9986f75
8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
29098
diff
changeset
|
322 |
* a subclass of {@code URLStreamHandler}, then the next package |
ad35f9986f75
8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
29098
diff
changeset
|
323 |
* in the list is tried. |
2 | 324 |
* <li>If the previous step fails to find a protocol handler, then the |
24260
55a72a7e0cf9
8039470: java.net Content Handler API incorrectly specifies implementation specific location of handler classes
chegar
parents:
23720
diff
changeset
|
325 |
* constructor tries to load a built-in protocol handler. |
2 | 326 |
* If this class does not exist, or if the class exists but it is not a |
19069 | 327 |
* subclass of {@code URLStreamHandler}, then a |
328 |
* {@code MalformedURLException} is thrown. |
|
2 | 329 |
* </ol> |
330 |
* |
|
331 |
* <p>Protocol handlers for the following protocols are guaranteed |
|
58053
1a296c9064dc
8177389: Hyphen "-" should be removed in URL class documentation
jpai
parents:
57956
diff
changeset
|
332 |
* to exist on the search path: |
1a296c9064dc
8177389: Hyphen "-" should be removed in URL class documentation
jpai
parents:
57956
diff
changeset
|
333 |
* <ul> |
1a296c9064dc
8177389: Hyphen "-" should be removed in URL class documentation
jpai
parents:
57956
diff
changeset
|
334 |
* <li>{@code http}</li> |
1a296c9064dc
8177389: Hyphen "-" should be removed in URL class documentation
jpai
parents:
57956
diff
changeset
|
335 |
* <li>{@code https}</li> |
1a296c9064dc
8177389: Hyphen "-" should be removed in URL class documentation
jpai
parents:
57956
diff
changeset
|
336 |
* <li>{@code file}</li> |
1a296c9064dc
8177389: Hyphen "-" should be removed in URL class documentation
jpai
parents:
57956
diff
changeset
|
337 |
* <li>{@code jar}</li> |
1a296c9064dc
8177389: Hyphen "-" should be removed in URL class documentation
jpai
parents:
57956
diff
changeset
|
338 |
* </ul> |
29097 | 339 |
* Protocol handlers for additional protocols may also be available. |
340 |
* Some protocol handlers, for example those used for loading platform |
|
341 |
* classes or classes on the class path, may not be overridden. The details |
|
342 |
* of such restrictions, and when those restrictions apply (during |
|
343 |
* initialization of the runtime for example), are implementation specific |
|
344 |
* and therefore not specified |
|
2 | 345 |
* |
346 |
* <p>No validation of the inputs is performed by this constructor. |
|
347 |
* |
|
348 |
* @param protocol the name of the protocol to use. |
|
349 |
* @param host the name of the host. |
|
350 |
* @param port the port number on the host. |
|
351 |
* @param file the file on the host |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
58053
diff
changeset
|
352 |
* @throws MalformedURLException if an unknown protocol or the port |
34776
9229b3f76c40
4906983: java.net.URL constructors throw MalformedURLException in undocumented way
sebastian
parents:
29986
diff
changeset
|
353 |
* is a negative number other than -1 |
2 | 354 |
* @see java.lang.System#getProperty(java.lang.String) |
355 |
* @see java.net.URL#setURLStreamHandlerFactory( |
|
356 |
* java.net.URLStreamHandlerFactory) |
|
357 |
* @see java.net.URLStreamHandler |
|
358 |
* @see java.net.URLStreamHandlerFactory#createURLStreamHandler( |
|
359 |
* java.lang.String) |
|
360 |
*/ |
|
361 |
public URL(String protocol, String host, int port, String file) |
|
362 |
throws MalformedURLException |
|
363 |
{ |
|
364 |
this(protocol, host, port, file, null); |
|
365 |
} |
|
366 |
||
367 |
/** |
|
19069 | 368 |
* Creates a URL from the specified {@code protocol} |
369 |
* name, {@code host} name, and {@code file} name. The |
|
2 | 370 |
* default port for the specified protocol is used. |
371 |
* <p> |
|
34776
9229b3f76c40
4906983: java.net.URL constructors throw MalformedURLException in undocumented way
sebastian
parents:
29986
diff
changeset
|
372 |
* This constructor is equivalent to the four-argument |
9229b3f76c40
4906983: java.net.URL constructors throw MalformedURLException in undocumented way
sebastian
parents:
29986
diff
changeset
|
373 |
* constructor with the only difference of using the |
9229b3f76c40
4906983: java.net.URL constructors throw MalformedURLException in undocumented way
sebastian
parents:
29986
diff
changeset
|
374 |
* default port for the specified protocol. |
2 | 375 |
* |
376 |
* No validation of the inputs is performed by this constructor. |
|
377 |
* |
|
378 |
* @param protocol the name of the protocol to use. |
|
379 |
* @param host the name of the host. |
|
380 |
* @param file the file on the host. |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
58053
diff
changeset
|
381 |
* @throws MalformedURLException if an unknown protocol is specified. |
2 | 382 |
* @see java.net.URL#URL(java.lang.String, java.lang.String, |
383 |
* int, java.lang.String) |
|
384 |
*/ |
|
385 |
public URL(String protocol, String host, String file) |
|
386 |
throws MalformedURLException { |
|
387 |
this(protocol, host, -1, file); |
|
388 |
} |
|
389 |
||
390 |
/** |
|
19069 | 391 |
* Creates a {@code URL} object from the specified |
392 |
* {@code protocol}, {@code host}, {@code port} |
|
393 |
* number, {@code file}, and {@code handler}. Specifying |
|
394 |
* a {@code port} number of {@code -1} indicates that |
|
2 | 395 |
* the URL should use the default port for the protocol. Specifying |
19069 | 396 |
* a {@code handler} of {@code null} indicates that the URL |
2 | 397 |
* should use a default stream handler for the protocol, as outlined |
398 |
* for: |
|
399 |
* java.net.URL#URL(java.lang.String, java.lang.String, int, |
|
400 |
* java.lang.String) |
|
401 |
* |
|
402 |
* <p>If the handler is not null and there is a security manager, |
|
19069 | 403 |
* the security manager's {@code checkPermission} |
2 | 404 |
* method is called with a |
19069 | 405 |
* {@code NetPermission("specifyStreamHandler")} permission. |
2 | 406 |
* This may result in a SecurityException. |
407 |
* |
|
408 |
* No validation of the inputs is performed by this constructor. |
|
409 |
* |
|
410 |
* @param protocol the name of the protocol to use. |
|
411 |
* @param host the name of the host. |
|
412 |
* @param port the port number on the host. |
|
413 |
* @param file the file on the host |
|
414 |
* @param handler the stream handler for the URL. |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
58053
diff
changeset
|
415 |
* @throws MalformedURLException if an unknown protocol or the port |
34776
9229b3f76c40
4906983: java.net.URL constructors throw MalformedURLException in undocumented way
sebastian
parents:
29986
diff
changeset
|
416 |
is a negative number other than -1 |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
58053
diff
changeset
|
417 |
* @throws SecurityException |
2 | 418 |
* if a security manager exists and its |
19069 | 419 |
* {@code checkPermission} method doesn't allow |
2 | 420 |
* specifying a stream handler explicitly. |
421 |
* @see java.lang.System#getProperty(java.lang.String) |
|
422 |
* @see java.net.URL#setURLStreamHandlerFactory( |
|
423 |
* java.net.URLStreamHandlerFactory) |
|
424 |
* @see java.net.URLStreamHandler |
|
425 |
* @see java.net.URLStreamHandlerFactory#createURLStreamHandler( |
|
426 |
* java.lang.String) |
|
427 |
* @see SecurityManager#checkPermission |
|
428 |
* @see java.net.NetPermission |
|
429 |
*/ |
|
430 |
public URL(String protocol, String host, int port, String file, |
|
431 |
URLStreamHandler handler) throws MalformedURLException { |
|
432 |
if (handler != null) { |
|
433 |
SecurityManager sm = System.getSecurityManager(); |
|
434 |
if (sm != null) { |
|
435 |
// check for permission to specify a handler |
|
436 |
checkSpecifyHandler(sm); |
|
437 |
} |
|
438 |
} |
|
439 |
||
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
440 |
protocol = toLowerCase(protocol); |
2 | 441 |
this.protocol = protocol; |
442 |
if (host != null) { |
|
443 |
||
444 |
/** |
|
445 |
* if host is a literal IPv6 address, |
|
446 |
* we will make it conform to RFC 2732 |
|
447 |
*/ |
|
448 |
if (host.indexOf(':') >= 0 && !host.startsWith("[")) { |
|
449 |
host = "["+host+"]"; |
|
450 |
} |
|
451 |
this.host = host; |
|
452 |
||
453 |
if (port < -1) { |
|
454 |
throw new MalformedURLException("Invalid port number :" + |
|
455 |
port); |
|
456 |
} |
|
457 |
this.port = port; |
|
458 |
authority = (port == -1) ? host : host + ":" + port; |
|
459 |
} |
|
460 |
||
39057 | 461 |
int index = file.indexOf('#'); |
462 |
this.ref = index < 0 ? null : file.substring(index + 1); |
|
463 |
file = index < 0 ? file : file.substring(0, index); |
|
464 |
int q = file.lastIndexOf('?'); |
|
465 |
if (q != -1) { |
|
466 |
this.query = file.substring(q + 1); |
|
467 |
this.path = file.substring(0, q); |
|
2 | 468 |
this.file = path + "?" + query; |
469 |
} else { |
|
39057 | 470 |
this.path = file; |
2 | 471 |
this.file = path; |
472 |
} |
|
473 |
||
55711 | 474 |
// Note: we don't do full validation of the URL here. Too risky to change |
2 | 475 |
// right now, but worth considering for future reference. -br |
476 |
if (handler == null && |
|
477 |
(handler = getURLStreamHandler(protocol)) == null) { |
|
478 |
throw new MalformedURLException("unknown protocol: " + protocol); |
|
479 |
} |
|
480 |
this.handler = handler; |
|
55711 | 481 |
if (host != null && isBuiltinStreamHandler(handler)) { |
482 |
String s = IPAddressUtil.checkExternalForm(this); |
|
483 |
if (s != null) { |
|
484 |
throw new MalformedURLException(s); |
|
485 |
} |
|
486 |
} |
|
58631 | 487 |
if ("jar".equalsIgnoreCase(protocol)) { |
488 |
if (handler instanceof sun.net.www.protocol.jar.Handler) { |
|
489 |
// URL.openConnection() would throw a confusing exception |
|
490 |
// so generate a better exception here instead. |
|
491 |
String s = ((sun.net.www.protocol.jar.Handler) handler).checkNestedProtocol(file); |
|
492 |
if (s != null) { |
|
493 |
throw new MalformedURLException(s); |
|
494 |
} |
|
495 |
} |
|
496 |
} |
|
2 | 497 |
} |
498 |
||
499 |
/** |
|
19069 | 500 |
* Creates a {@code URL} object from the {@code String} |
2 | 501 |
* representation. |
502 |
* <p> |
|
503 |
* This constructor is equivalent to a call to the two-argument |
|
19069 | 504 |
* constructor with a {@code null} first argument. |
2 | 505 |
* |
19069 | 506 |
* @param spec the {@code String} to parse as a URL. |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
58053
diff
changeset
|
507 |
* @throws MalformedURLException if no protocol is specified, or an |
34776
9229b3f76c40
4906983: java.net.URL constructors throw MalformedURLException in undocumented way
sebastian
parents:
29986
diff
changeset
|
508 |
* unknown protocol is found, or {@code spec} is {@code null}, |
9229b3f76c40
4906983: java.net.URL constructors throw MalformedURLException in undocumented way
sebastian
parents:
29986
diff
changeset
|
509 |
* or the parsed URL fails to comply with the specific syntax |
9229b3f76c40
4906983: java.net.URL constructors throw MalformedURLException in undocumented way
sebastian
parents:
29986
diff
changeset
|
510 |
* of the associated protocol. |
2 | 511 |
* @see java.net.URL#URL(java.net.URL, java.lang.String) |
512 |
*/ |
|
513 |
public URL(String spec) throws MalformedURLException { |
|
514 |
this(null, spec); |
|
515 |
} |
|
516 |
||
517 |
/** |
|
518 |
* Creates a URL by parsing the given spec within a specified context. |
|
519 |
* |
|
520 |
* The new URL is created from the given context URL and the spec |
|
521 |
* argument as described in |
|
522 |
* RFC2396 "Uniform Resource Identifiers : Generic * Syntax" : |
|
523 |
* <blockquote><pre> |
|
524 |
* <scheme>://<authority><path>?<query>#<fragment> |
|
525 |
* </pre></blockquote> |
|
526 |
* The reference is parsed into the scheme, authority, path, query and |
|
527 |
* fragment parts. If the path component is empty and the scheme, |
|
528 |
* authority, and query components are undefined, then the new URL is a |
|
529 |
* reference to the current document. Otherwise, the fragment and query |
|
530 |
* parts present in the spec are used in the new URL. |
|
531 |
* <p> |
|
532 |
* If the scheme component is defined in the given spec and does not match |
|
533 |
* the scheme of the context, then the new URL is created as an absolute |
|
534 |
* URL based on the spec alone. Otherwise the scheme component is inherited |
|
535 |
* from the context URL. |
|
536 |
* <p> |
|
537 |
* If the authority component is present in the spec then the spec is |
|
538 |
* treated as absolute and the spec authority and path will replace the |
|
539 |
* context authority and path. If the authority component is absent in the |
|
540 |
* spec then the authority of the new URL will be inherited from the |
|
541 |
* context. |
|
542 |
* <p> |
|
543 |
* If the spec's path component begins with a slash character |
|
544 |
* "/" then the |
|
545 |
* path is treated as absolute and the spec path replaces the context path. |
|
546 |
* <p> |
|
547 |
* Otherwise, the path is treated as a relative path and is appended to the |
|
548 |
* context path, as described in RFC2396. Also, in this case, |
|
549 |
* the path is canonicalized through the removal of directory |
|
21278 | 550 |
* changes made by occurrences of ".." and ".". |
2 | 551 |
* <p> |
552 |
* For a more detailed description of URL parsing, refer to RFC2396. |
|
553 |
* |
|
554 |
* @param context the context in which to parse the specification. |
|
19069 | 555 |
* @param spec the {@code String} to parse as a URL. |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
58053
diff
changeset
|
556 |
* @throws MalformedURLException if no protocol is specified, or an |
34776
9229b3f76c40
4906983: java.net.URL constructors throw MalformedURLException in undocumented way
sebastian
parents:
29986
diff
changeset
|
557 |
* unknown protocol is found, or {@code spec} is {@code null}, |
9229b3f76c40
4906983: java.net.URL constructors throw MalformedURLException in undocumented way
sebastian
parents:
29986
diff
changeset
|
558 |
* or the parsed URL fails to comply with the specific syntax |
9229b3f76c40
4906983: java.net.URL constructors throw MalformedURLException in undocumented way
sebastian
parents:
29986
diff
changeset
|
559 |
* of the associated protocol. |
2 | 560 |
* @see java.net.URL#URL(java.lang.String, java.lang.String, |
561 |
* int, java.lang.String) |
|
562 |
* @see java.net.URLStreamHandler |
|
563 |
* @see java.net.URLStreamHandler#parseURL(java.net.URL, |
|
564 |
* java.lang.String, int, int) |
|
565 |
*/ |
|
566 |
public URL(URL context, String spec) throws MalformedURLException { |
|
567 |
this(context, spec, null); |
|
568 |
} |
|
569 |
||
570 |
/** |
|
571 |
* Creates a URL by parsing the given spec with the specified handler |
|
572 |
* within a specified context. If the handler is null, the parsing |
|
573 |
* occurs as with the two argument constructor. |
|
574 |
* |
|
575 |
* @param context the context in which to parse the specification. |
|
19069 | 576 |
* @param spec the {@code String} to parse as a URL. |
2 | 577 |
* @param handler the stream handler for the URL. |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
58053
diff
changeset
|
578 |
* @throws MalformedURLException if no protocol is specified, or an |
34776
9229b3f76c40
4906983: java.net.URL constructors throw MalformedURLException in undocumented way
sebastian
parents:
29986
diff
changeset
|
579 |
* unknown protocol is found, or {@code spec} is {@code null}, |
9229b3f76c40
4906983: java.net.URL constructors throw MalformedURLException in undocumented way
sebastian
parents:
29986
diff
changeset
|
580 |
* or the parsed URL fails to comply with the specific syntax |
9229b3f76c40
4906983: java.net.URL constructors throw MalformedURLException in undocumented way
sebastian
parents:
29986
diff
changeset
|
581 |
* of the associated protocol. |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
58053
diff
changeset
|
582 |
* @throws SecurityException |
2 | 583 |
* if a security manager exists and its |
19069 | 584 |
* {@code checkPermission} method doesn't allow |
2 | 585 |
* specifying a stream handler. |
586 |
* @see java.net.URL#URL(java.lang.String, java.lang.String, |
|
587 |
* int, java.lang.String) |
|
588 |
* @see java.net.URLStreamHandler |
|
589 |
* @see java.net.URLStreamHandler#parseURL(java.net.URL, |
|
590 |
* java.lang.String, int, int) |
|
591 |
*/ |
|
592 |
public URL(URL context, String spec, URLStreamHandler handler) |
|
593 |
throws MalformedURLException |
|
594 |
{ |
|
595 |
String original = spec; |
|
596 |
int i, limit, c; |
|
597 |
int start = 0; |
|
598 |
String newProtocol = null; |
|
599 |
boolean aRef=false; |
|
600 |
boolean isRelative = false; |
|
601 |
||
602 |
// Check for permission to specify a handler |
|
603 |
if (handler != null) { |
|
604 |
SecurityManager sm = System.getSecurityManager(); |
|
605 |
if (sm != null) { |
|
606 |
checkSpecifyHandler(sm); |
|
607 |
} |
|
608 |
} |
|
609 |
||
610 |
try { |
|
611 |
limit = spec.length(); |
|
612 |
while ((limit > 0) && (spec.charAt(limit - 1) <= ' ')) { |
|
613 |
limit--; //eliminate trailing whitespace |
|
614 |
} |
|
615 |
while ((start < limit) && (spec.charAt(start) <= ' ')) { |
|
616 |
start++; // eliminate leading whitespace |
|
617 |
} |
|
618 |
||
619 |
if (spec.regionMatches(true, start, "url:", 0, 4)) { |
|
620 |
start += 4; |
|
621 |
} |
|
622 |
if (start < spec.length() && spec.charAt(start) == '#') { |
|
623 |
/* we're assuming this is a ref relative to the context URL. |
|
624 |
* This means protocols cannot start w/ '#', but we must parse |
|
625 |
* ref URL's like: "hello:there" w/ a ':' in them. |
|
626 |
*/ |
|
627 |
aRef=true; |
|
628 |
} |
|
629 |
for (i = start ; !aRef && (i < limit) && |
|
630 |
((c = spec.charAt(i)) != '/') ; i++) { |
|
631 |
if (c == ':') { |
|
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
632 |
String s = toLowerCase(spec.substring(start, i)); |
2 | 633 |
if (isValidProtocol(s)) { |
634 |
newProtocol = s; |
|
635 |
start = i + 1; |
|
636 |
} |
|
637 |
break; |
|
638 |
} |
|
639 |
} |
|
640 |
||
641 |
// Only use our context if the protocols match. |
|
642 |
protocol = newProtocol; |
|
643 |
if ((context != null) && ((newProtocol == null) || |
|
644 |
newProtocol.equalsIgnoreCase(context.protocol))) { |
|
645 |
// inherit the protocol handler from the context |
|
646 |
// if not specified to the constructor |
|
647 |
if (handler == null) { |
|
648 |
handler = context.handler; |
|
649 |
} |
|
650 |
||
651 |
// If the context is a hierarchical URL scheme and the spec |
|
652 |
// contains a matching scheme then maintain backwards |
|
653 |
// compatibility and treat it as if the spec didn't contain |
|
654 |
// the scheme; see 5.2.3 of RFC2396 |
|
655 |
if (context.path != null && context.path.startsWith("/")) |
|
656 |
newProtocol = null; |
|
657 |
||
658 |
if (newProtocol == null) { |
|
659 |
protocol = context.protocol; |
|
660 |
authority = context.authority; |
|
661 |
userInfo = context.userInfo; |
|
662 |
host = context.host; |
|
663 |
port = context.port; |
|
664 |
file = context.file; |
|
665 |
path = context.path; |
|
666 |
isRelative = true; |
|
667 |
} |
|
668 |
} |
|
669 |
||
670 |
if (protocol == null) { |
|
671 |
throw new MalformedURLException("no protocol: "+original); |
|
672 |
} |
|
673 |
||
674 |
// Get the protocol handler if not specified or the protocol |
|
675 |
// of the context could not be used |
|
676 |
if (handler == null && |
|
677 |
(handler = getURLStreamHandler(protocol)) == null) { |
|
678 |
throw new MalformedURLException("unknown protocol: "+protocol); |
|
679 |
} |
|
680 |
||
681 |
this.handler = handler; |
|
682 |
||
683 |
i = spec.indexOf('#', start); |
|
684 |
if (i >= 0) { |
|
685 |
ref = spec.substring(i + 1, limit); |
|
686 |
limit = i; |
|
687 |
} |
|
688 |
||
689 |
/* |
|
690 |
* Handle special case inheritance of query and fragment |
|
691 |
* implied by RFC2396 section 5.2.2. |
|
692 |
*/ |
|
693 |
if (isRelative && start == limit) { |
|
694 |
query = context.query; |
|
695 |
if (ref == null) { |
|
696 |
ref = context.ref; |
|
697 |
} |
|
698 |
} |
|
699 |
||
700 |
handler.parseURL(this, spec, start, limit); |
|
701 |
||
702 |
} catch(MalformedURLException e) { |
|
703 |
throw e; |
|
704 |
} catch(Exception e) { |
|
705 |
MalformedURLException exception = new MalformedURLException(e.getMessage()); |
|
706 |
exception.initCause(e); |
|
707 |
throw exception; |
|
708 |
} |
|
709 |
} |
|
710 |
||
35393
12d55e1947f7
8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents:
35381
diff
changeset
|
711 |
/** |
12d55e1947f7
8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents:
35381
diff
changeset
|
712 |
* Creates a URL from a URI, as if by invoking {@code uri.toURL()}. |
12d55e1947f7
8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents:
35381
diff
changeset
|
713 |
* |
12d55e1947f7
8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents:
35381
diff
changeset
|
714 |
* @see java.net.URI#toURL() |
12d55e1947f7
8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents:
35381
diff
changeset
|
715 |
*/ |
12d55e1947f7
8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents:
35381
diff
changeset
|
716 |
static URL fromURI(URI uri) throws MalformedURLException { |
12d55e1947f7
8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents:
35381
diff
changeset
|
717 |
if (!uri.isAbsolute()) { |
12d55e1947f7
8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents:
35381
diff
changeset
|
718 |
throw new IllegalArgumentException("URI is not absolute"); |
12d55e1947f7
8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents:
35381
diff
changeset
|
719 |
} |
12d55e1947f7
8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents:
35381
diff
changeset
|
720 |
String protocol = uri.getScheme(); |
35399
babca4eef83f
8148626: URI.toURL needs to use protocol Handler to parse file URIs
redestad
parents:
35393
diff
changeset
|
721 |
|
babca4eef83f
8148626: URI.toURL needs to use protocol Handler to parse file URIs
redestad
parents:
35393
diff
changeset
|
722 |
// In general we need to go via Handler.parseURL, but for the jrt |
babca4eef83f
8148626: URI.toURL needs to use protocol Handler to parse file URIs
redestad
parents:
35393
diff
changeset
|
723 |
// protocol we enforce that the Handler is not overrideable and can |
babca4eef83f
8148626: URI.toURL needs to use protocol Handler to parse file URIs
redestad
parents:
35393
diff
changeset
|
724 |
// optimize URI to URL conversion. |
babca4eef83f
8148626: URI.toURL needs to use protocol Handler to parse file URIs
redestad
parents:
35393
diff
changeset
|
725 |
// |
babca4eef83f
8148626: URI.toURL needs to use protocol Handler to parse file URIs
redestad
parents:
35393
diff
changeset
|
726 |
// Case-sensitive comparison for performance; malformed protocols will |
babca4eef83f
8148626: URI.toURL needs to use protocol Handler to parse file URIs
redestad
parents:
35393
diff
changeset
|
727 |
// be handled correctly by the slow path. |
babca4eef83f
8148626: URI.toURL needs to use protocol Handler to parse file URIs
redestad
parents:
35393
diff
changeset
|
728 |
if (protocol.equals("jrt") && !uri.isOpaque() |
babca4eef83f
8148626: URI.toURL needs to use protocol Handler to parse file URIs
redestad
parents:
35393
diff
changeset
|
729 |
&& uri.getRawFragment() == null) { |
35393
12d55e1947f7
8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents:
35381
diff
changeset
|
730 |
|
12d55e1947f7
8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents:
35381
diff
changeset
|
731 |
String query = uri.getRawQuery(); |
12d55e1947f7
8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents:
35381
diff
changeset
|
732 |
String path = uri.getRawPath(); |
12d55e1947f7
8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents:
35381
diff
changeset
|
733 |
String file = (query == null) ? path : path + "?" + query; |
12d55e1947f7
8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents:
35381
diff
changeset
|
734 |
|
12d55e1947f7
8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents:
35381
diff
changeset
|
735 |
// URL represent undefined host as empty string while URI use null |
12d55e1947f7
8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents:
35381
diff
changeset
|
736 |
String host = uri.getHost(); |
12d55e1947f7
8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents:
35381
diff
changeset
|
737 |
if (host == null) { |
12d55e1947f7
8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents:
35381
diff
changeset
|
738 |
host = ""; |
12d55e1947f7
8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents:
35381
diff
changeset
|
739 |
} |
12d55e1947f7
8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents:
35381
diff
changeset
|
740 |
|
12d55e1947f7
8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents:
35381
diff
changeset
|
741 |
int port = uri.getPort(); |
12d55e1947f7
8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents:
35381
diff
changeset
|
742 |
|
35399
babca4eef83f
8148626: URI.toURL needs to use protocol Handler to parse file URIs
redestad
parents:
35393
diff
changeset
|
743 |
return new URL("jrt", host, port, file, null); |
35393
12d55e1947f7
8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents:
35381
diff
changeset
|
744 |
} else { |
12d55e1947f7
8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents:
35381
diff
changeset
|
745 |
return new URL((URL)null, uri.toString(), null); |
12d55e1947f7
8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents:
35381
diff
changeset
|
746 |
} |
12d55e1947f7
8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents:
35381
diff
changeset
|
747 |
} |
12d55e1947f7
8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents:
35381
diff
changeset
|
748 |
|
2 | 749 |
/* |
750 |
* Returns true if specified string is a valid protocol name. |
|
751 |
*/ |
|
752 |
private boolean isValidProtocol(String protocol) { |
|
753 |
int len = protocol.length(); |
|
754 |
if (len < 1) |
|
755 |
return false; |
|
756 |
char c = protocol.charAt(0); |
|
757 |
if (!Character.isLetter(c)) |
|
758 |
return false; |
|
759 |
for (int i = 1; i < len; i++) { |
|
760 |
c = protocol.charAt(i); |
|
761 |
if (!Character.isLetterOrDigit(c) && c != '.' && c != '+' && |
|
762 |
c != '-') { |
|
763 |
return false; |
|
764 |
} |
|
765 |
} |
|
766 |
return true; |
|
767 |
} |
|
768 |
||
769 |
/* |
|
770 |
* Checks for permission to specify a stream handler. |
|
771 |
*/ |
|
772 |
private void checkSpecifyHandler(SecurityManager sm) { |
|
773 |
sm.checkPermission(SecurityConstants.SPECIFY_HANDLER_PERMISSION); |
|
774 |
} |
|
775 |
||
776 |
/** |
|
777 |
* Sets the fields of the URL. This is not a public method so that |
|
778 |
* only URLStreamHandlers can modify URL fields. URLs are |
|
779 |
* otherwise constant. |
|
780 |
* |
|
781 |
* @param protocol the name of the protocol to use |
|
782 |
* @param host the name of the host |
|
783 |
@param port the port number on the host |
|
784 |
* @param file the file on the host |
|
785 |
* @param ref the internal reference in the URL |
|
786 |
*/ |
|
16055 | 787 |
void set(String protocol, String host, int port, |
788 |
String file, String ref) { |
|
2 | 789 |
synchronized (this) { |
790 |
this.protocol = protocol; |
|
791 |
this.host = host; |
|
792 |
authority = port == -1 ? host : host + ":" + port; |
|
793 |
this.port = port; |
|
794 |
this.file = file; |
|
795 |
this.ref = ref; |
|
796 |
/* This is very important. We must recompute this after the |
|
797 |
* URL has been changed. */ |
|
798 |
hashCode = -1; |
|
799 |
hostAddress = null; |
|
800 |
int q = file.lastIndexOf('?'); |
|
801 |
if (q != -1) { |
|
802 |
query = file.substring(q+1); |
|
803 |
path = file.substring(0, q); |
|
804 |
} else |
|
805 |
path = file; |
|
806 |
} |
|
807 |
} |
|
808 |
||
809 |
/** |
|
810 |
* Sets the specified 8 fields of the URL. This is not a public method so |
|
811 |
* that only URLStreamHandlers can modify URL fields. URLs are otherwise |
|
812 |
* constant. |
|
813 |
* |
|
814 |
* @param protocol the name of the protocol to use |
|
815 |
* @param host the name of the host |
|
816 |
* @param port the port number on the host |
|
817 |
* @param authority the authority part for the url |
|
818 |
* @param userInfo the username and password |
|
819 |
* @param path the file on the host |
|
820 |
* @param ref the internal reference in the URL |
|
821 |
* @param query the query part of this URL |
|
822 |
* @since 1.3 |
|
823 |
*/ |
|
16055 | 824 |
void set(String protocol, String host, int port, |
825 |
String authority, String userInfo, String path, |
|
826 |
String query, String ref) { |
|
2 | 827 |
synchronized (this) { |
828 |
this.protocol = protocol; |
|
829 |
this.host = host; |
|
830 |
this.port = port; |
|
831 |
this.file = query == null ? path : path + "?" + query; |
|
832 |
this.userInfo = userInfo; |
|
833 |
this.path = path; |
|
834 |
this.ref = ref; |
|
835 |
/* This is very important. We must recompute this after the |
|
836 |
* URL has been changed. */ |
|
837 |
hashCode = -1; |
|
838 |
hostAddress = null; |
|
839 |
this.query = query; |
|
840 |
this.authority = authority; |
|
841 |
} |
|
842 |
} |
|
843 |
||
844 |
/** |
|
19069 | 845 |
* Gets the query part of this {@code URL}. |
2 | 846 |
* |
19069 | 847 |
* @return the query part of this {@code URL}, |
2 | 848 |
* or <CODE>null</CODE> if one does not exist |
849 |
* @since 1.3 |
|
850 |
*/ |
|
851 |
public String getQuery() { |
|
852 |
return query; |
|
853 |
} |
|
854 |
||
855 |
/** |
|
19069 | 856 |
* Gets the path part of this {@code URL}. |
2 | 857 |
* |
19069 | 858 |
* @return the path part of this {@code URL}, or an |
2 | 859 |
* empty string if one does not exist |
860 |
* @since 1.3 |
|
861 |
*/ |
|
862 |
public String getPath() { |
|
863 |
return path; |
|
864 |
} |
|
865 |
||
866 |
/** |
|
19069 | 867 |
* Gets the userInfo part of this {@code URL}. |
2 | 868 |
* |
19069 | 869 |
* @return the userInfo part of this {@code URL}, or |
2 | 870 |
* <CODE>null</CODE> if one does not exist |
871 |
* @since 1.3 |
|
872 |
*/ |
|
873 |
public String getUserInfo() { |
|
874 |
return userInfo; |
|
875 |
} |
|
876 |
||
877 |
/** |
|
19069 | 878 |
* Gets the authority part of this {@code URL}. |
2 | 879 |
* |
19069 | 880 |
* @return the authority part of this {@code URL} |
2 | 881 |
* @since 1.3 |
882 |
*/ |
|
883 |
public String getAuthority() { |
|
884 |
return authority; |
|
885 |
} |
|
886 |
||
887 |
/** |
|
19069 | 888 |
* Gets the port number of this {@code URL}. |
2 | 889 |
* |
890 |
* @return the port number, or -1 if the port is not set |
|
891 |
*/ |
|
892 |
public int getPort() { |
|
893 |
return port; |
|
894 |
} |
|
895 |
||
896 |
/** |
|
897 |
* Gets the default port number of the protocol associated |
|
19069 | 898 |
* with this {@code URL}. If the URL scheme or the URLStreamHandler |
2 | 899 |
* for the URL do not define a default port number, |
900 |
* then -1 is returned. |
|
901 |
* |
|
902 |
* @return the port number |
|
903 |
* @since 1.4 |
|
904 |
*/ |
|
905 |
public int getDefaultPort() { |
|
906 |
return handler.getDefaultPort(); |
|
907 |
} |
|
908 |
||
909 |
/** |
|
19069 | 910 |
* Gets the protocol name of this {@code URL}. |
2 | 911 |
* |
19069 | 912 |
* @return the protocol of this {@code URL}. |
2 | 913 |
*/ |
914 |
public String getProtocol() { |
|
915 |
return protocol; |
|
916 |
} |
|
917 |
||
918 |
/** |
|
19069 | 919 |
* Gets the host name of this {@code URL}, if applicable. |
2 | 920 |
* The format of the host conforms to RFC 2732, i.e. for a |
921 |
* literal IPv6 address, this method will return the IPv6 address |
|
19069 | 922 |
* enclosed in square brackets ({@code '['} and {@code ']'}). |
2 | 923 |
* |
19069 | 924 |
* @return the host name of this {@code URL}. |
2 | 925 |
*/ |
926 |
public String getHost() { |
|
927 |
return host; |
|
928 |
} |
|
929 |
||
930 |
/** |
|
19069 | 931 |
* Gets the file name of this {@code URL}. |
2 | 932 |
* The returned file portion will be |
933 |
* the same as <CODE>getPath()</CODE>, plus the concatenation of |
|
934 |
* the value of <CODE>getQuery()</CODE>, if any. If there is |
|
935 |
* no query portion, this method and <CODE>getPath()</CODE> will |
|
936 |
* return identical results. |
|
937 |
* |
|
19069 | 938 |
* @return the file name of this {@code URL}, |
2 | 939 |
* or an empty string if one does not exist |
940 |
*/ |
|
941 |
public String getFile() { |
|
942 |
return file; |
|
943 |
} |
|
944 |
||
945 |
/** |
|
946 |
* Gets the anchor (also known as the "reference") of this |
|
19069 | 947 |
* {@code URL}. |
2 | 948 |
* |
949 |
* @return the anchor (also known as the "reference") of this |
|
19069 | 950 |
* {@code URL}, or <CODE>null</CODE> if one does not exist |
2 | 951 |
*/ |
952 |
public String getRef() { |
|
953 |
return ref; |
|
954 |
} |
|
955 |
||
956 |
/** |
|
957 |
* Compares this URL for equality with another object.<p> |
|
958 |
* |
|
959 |
* If the given object is not a URL then this method immediately returns |
|
19069 | 960 |
* {@code false}.<p> |
2 | 961 |
* |
962 |
* Two URL objects are equal if they have the same protocol, reference |
|
963 |
* equivalent hosts, have the same port number on the host, and the same |
|
964 |
* file and fragment of the file.<p> |
|
965 |
* |
|
966 |
* Two hosts are considered equivalent if both host names can be resolved |
|
967 |
* into the same IP addresses; else if either host name can't be |
|
968 |
* resolved, the host names must be equal without regard to case; or both |
|
969 |
* host names equal to null.<p> |
|
970 |
* |
|
971 |
* Since hosts comparison requires name resolution, this operation is a |
|
972 |
* blocking operation. <p> |
|
973 |
* |
|
19069 | 974 |
* Note: The defined behavior for {@code equals} is known to |
2 | 975 |
* be inconsistent with virtual hosting in HTTP. |
976 |
* |
|
977 |
* @param obj the URL to compare against. |
|
19069 | 978 |
* @return {@code true} if the objects are the same; |
979 |
* {@code false} otherwise. |
|
2 | 980 |
*/ |
981 |
public boolean equals(Object obj) { |
|
982 |
if (!(obj instanceof URL)) |
|
983 |
return false; |
|
984 |
URL u2 = (URL)obj; |
|
985 |
||
986 |
return handler.equals(this, u2); |
|
987 |
} |
|
988 |
||
989 |
/** |
|
990 |
* Creates an integer suitable for hash table indexing.<p> |
|
991 |
* |
|
992 |
* The hash code is based upon all the URL components relevant for URL |
|
23720
7d5147c21927
8039172: Tidy warnings cleanup for java.net, java.math, java.time, java.rmi
yan
parents:
22119
diff
changeset
|
993 |
* comparison. As such, this operation is a blocking operation. |
2 | 994 |
* |
19069 | 995 |
* @return a hash code for this {@code URL}. |
2 | 996 |
*/ |
997 |
public synchronized int hashCode() { |
|
998 |
if (hashCode != -1) |
|
999 |
return hashCode; |
|
1000 |
||
1001 |
hashCode = handler.hashCode(this); |
|
1002 |
return hashCode; |
|
1003 |
} |
|
1004 |
||
1005 |
/** |
|
1006 |
* Compares two URLs, excluding the fragment component.<p> |
|
1007 |
* |
|
19069 | 1008 |
* Returns {@code true} if this {@code URL} and the |
1009 |
* {@code other} argument are equal without taking the |
|
2 | 1010 |
* fragment component into consideration. |
1011 |
* |
|
19069 | 1012 |
* @param other the {@code URL} to compare against. |
1013 |
* @return {@code true} if they reference the same remote object; |
|
1014 |
* {@code false} otherwise. |
|
2 | 1015 |
*/ |
1016 |
public boolean sameFile(URL other) { |
|
1017 |
return handler.sameFile(this, other); |
|
1018 |
} |
|
1019 |
||
1020 |
/** |
|
19069 | 1021 |
* Constructs a string representation of this {@code URL}. The |
1022 |
* string is created by calling the {@code toExternalForm} |
|
2 | 1023 |
* method of the stream protocol handler for this object. |
1024 |
* |
|
1025 |
* @return a string representation of this object. |
|
1026 |
* @see java.net.URL#URL(java.lang.String, java.lang.String, int, |
|
1027 |
* java.lang.String) |
|
1028 |
* @see java.net.URLStreamHandler#toExternalForm(java.net.URL) |
|
1029 |
*/ |
|
1030 |
public String toString() { |
|
1031 |
return toExternalForm(); |
|
1032 |
} |
|
1033 |
||
1034 |
/** |
|
19069 | 1035 |
* Constructs a string representation of this {@code URL}. The |
1036 |
* string is created by calling the {@code toExternalForm} |
|
2 | 1037 |
* method of the stream protocol handler for this object. |
1038 |
* |
|
1039 |
* @return a string representation of this object. |
|
1040 |
* @see java.net.URL#URL(java.lang.String, java.lang.String, |
|
1041 |
* int, java.lang.String) |
|
1042 |
* @see java.net.URLStreamHandler#toExternalForm(java.net.URL) |
|
1043 |
*/ |
|
1044 |
public String toExternalForm() { |
|
1045 |
return handler.toExternalForm(this); |
|
1046 |
} |
|
1047 |
||
1048 |
/** |
|
1049 |
* Returns a {@link java.net.URI} equivalent to this URL. |
|
19069 | 1050 |
* This method functions in the same way as {@code new URI (this.toString())}. |
2 | 1051 |
* <p>Note, any URL instance that complies with RFC 2396 can be converted |
1052 |
* to a URI. However, some URLs that are not strictly in compliance |
|
1053 |
* can not be converted to a URI. |
|
1054 |
* |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
58053
diff
changeset
|
1055 |
* @throws URISyntaxException if this URL is not formatted strictly according to |
52499
768b1c612100
8213490: Networking area typos and inconsistencies cleanup
prappo
parents:
52427
diff
changeset
|
1056 |
* RFC2396 and cannot be converted to a URI. |
2 | 1057 |
* |
1058 |
* @return a URI instance equivalent to this URL. |
|
1059 |
* @since 1.5 |
|
1060 |
*/ |
|
1061 |
public URI toURI() throws URISyntaxException { |
|
55711 | 1062 |
URI uri = new URI(toString()); |
1063 |
if (authority != null && isBuiltinStreamHandler(handler)) { |
|
1064 |
String s = IPAddressUtil.checkAuthority(this); |
|
1065 |
if (s != null) throw new URISyntaxException(authority, s); |
|
1066 |
} |
|
1067 |
return uri; |
|
2 | 1068 |
} |
1069 |
||
1070 |
/** |
|
1071 |
* Returns a {@link java.net.URLConnection URLConnection} instance that |
|
1072 |
* represents a connection to the remote object referred to by the |
|
1073 |
* {@code URL}. |
|
1074 |
* |
|
1075 |
* <P>A new instance of {@linkplain java.net.URLConnection URLConnection} is |
|
1076 |
* created every time when invoking the |
|
1077 |
* {@linkplain java.net.URLStreamHandler#openConnection(URL) |
|
1078 |
* URLStreamHandler.openConnection(URL)} method of the protocol handler for |
|
1079 |
* this URL.</P> |
|
1080 |
* |
|
1081 |
* <P>It should be noted that a URLConnection instance does not establish |
|
1082 |
* the actual network connection on creation. This will happen only when |
|
1083 |
* calling {@linkplain java.net.URLConnection#connect() URLConnection.connect()}.</P> |
|
1084 |
* |
|
1085 |
* <P>If for the URL's protocol (such as HTTP or JAR), there |
|
1086 |
* exists a public, specialized URLConnection subclass belonging |
|
1087 |
* to one of the following packages or one of their subpackages: |
|
1088 |
* java.lang, java.io, java.util, java.net, the connection |
|
1089 |
* returned will be of that subclass. For example, for HTTP an |
|
1090 |
* HttpURLConnection will be returned, and for JAR a |
|
1091 |
* JarURLConnection will be returned.</P> |
|
1092 |
* |
|
1093 |
* @return a {@link java.net.URLConnection URLConnection} linking |
|
1094 |
* to the URL. |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
58053
diff
changeset
|
1095 |
* @throws IOException if an I/O exception occurs. |
2 | 1096 |
* @see java.net.URL#URL(java.lang.String, java.lang.String, |
1097 |
* int, java.lang.String) |
|
1098 |
*/ |
|
1099 |
public URLConnection openConnection() throws java.io.IOException { |
|
1100 |
return handler.openConnection(this); |
|
1101 |
} |
|
1102 |
||
1103 |
/** |
|
1104 |
* Same as {@link #openConnection()}, except that the connection will be |
|
1105 |
* made through the specified proxy; Protocol handlers that do not |
|
52499
768b1c612100
8213490: Networking area typos and inconsistencies cleanup
prappo
parents:
52427
diff
changeset
|
1106 |
* support proxying will ignore the proxy parameter and make a |
2 | 1107 |
* normal connection. |
1108 |
* |
|
22064
e3c4398c54cd
7018010: References to ProxySelector is without link
rriggs
parents:
21278
diff
changeset
|
1109 |
* Invoking this method preempts the system's default |
e3c4398c54cd
7018010: References to ProxySelector is without link
rriggs
parents:
21278
diff
changeset
|
1110 |
* {@link java.net.ProxySelector ProxySelector} settings. |
2 | 1111 |
* |
1112 |
* @param proxy the Proxy through which this connection |
|
1113 |
* will be made. If direct connection is desired, |
|
1114 |
* Proxy.NO_PROXY should be specified. |
|
19069 | 1115 |
* @return a {@code URLConnection} to the URL. |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
58053
diff
changeset
|
1116 |
* @throws IOException if an I/O exception occurs. |
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
58053
diff
changeset
|
1117 |
* @throws SecurityException if a security manager is present |
2 | 1118 |
* and the caller doesn't have permission to connect |
1119 |
* to the proxy. |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
58053
diff
changeset
|
1120 |
* @throws IllegalArgumentException will be thrown if proxy is null, |
2 | 1121 |
* or proxy has the wrong type |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
58053
diff
changeset
|
1122 |
* @throws UnsupportedOperationException if the subclass that |
2 | 1123 |
* implements the protocol handler doesn't support |
1124 |
* this method. |
|
1125 |
* @see java.net.URL#URL(java.lang.String, java.lang.String, |
|
1126 |
* int, java.lang.String) |
|
1127 |
* @see java.net.URLConnection |
|
1128 |
* @see java.net.URLStreamHandler#openConnection(java.net.URL, |
|
1129 |
* java.net.Proxy) |
|
1130 |
* @since 1.5 |
|
1131 |
*/ |
|
1132 |
public URLConnection openConnection(Proxy proxy) |
|
1133 |
throws java.io.IOException { |
|
1134 |
if (proxy == null) { |
|
1135 |
throw new IllegalArgumentException("proxy can not be null"); |
|
1136 |
} |
|
1137 |
||
3442
02df74328591
6801497: Proxy is assumed to be immutable but is non-final
jccollet
parents:
715
diff
changeset
|
1138 |
// Create a copy of Proxy as a security measure |
3450
2f08a8bb9b83
6801071: Remote sites can compromise user privacy and possibly hijack web sessions
chegar
parents:
3442
diff
changeset
|
1139 |
Proxy p = proxy == Proxy.NO_PROXY ? Proxy.NO_PROXY : sun.net.ApplicationProxy.create(proxy); |
2 | 1140 |
SecurityManager sm = System.getSecurityManager(); |
3442
02df74328591
6801497: Proxy is assumed to be immutable but is non-final
jccollet
parents:
715
diff
changeset
|
1141 |
if (p.type() != Proxy.Type.DIRECT && sm != null) { |
02df74328591
6801497: Proxy is assumed to be immutable but is non-final
jccollet
parents:
715
diff
changeset
|
1142 |
InetSocketAddress epoint = (InetSocketAddress) p.address(); |
2 | 1143 |
if (epoint.isUnresolved()) |
1144 |
sm.checkConnect(epoint.getHostName(), epoint.getPort()); |
|
1145 |
else |
|
1146 |
sm.checkConnect(epoint.getAddress().getHostAddress(), |
|
1147 |
epoint.getPort()); |
|
1148 |
} |
|
3442
02df74328591
6801497: Proxy is assumed to be immutable but is non-final
jccollet
parents:
715
diff
changeset
|
1149 |
return handler.openConnection(this, p); |
2 | 1150 |
} |
1151 |
||
1152 |
/** |
|
19069 | 1153 |
* Opens a connection to this {@code URL} and returns an |
1154 |
* {@code InputStream} for reading from that connection. This |
|
2 | 1155 |
* method is a shorthand for: |
1156 |
* <blockquote><pre> |
|
1157 |
* openConnection().getInputStream() |
|
1158 |
* </pre></blockquote> |
|
1159 |
* |
|
1160 |
* @return an input stream for reading from the URL connection. |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
58053
diff
changeset
|
1161 |
* @throws IOException if an I/O exception occurs. |
2 | 1162 |
* @see java.net.URL#openConnection() |
1163 |
* @see java.net.URLConnection#getInputStream() |
|
1164 |
*/ |
|
1165 |
public final InputStream openStream() throws java.io.IOException { |
|
1166 |
return openConnection().getInputStream(); |
|
1167 |
} |
|
1168 |
||
1169 |
/** |
|
1170 |
* Gets the contents of this URL. This method is a shorthand for: |
|
1171 |
* <blockquote><pre> |
|
1172 |
* openConnection().getContent() |
|
1173 |
* </pre></blockquote> |
|
1174 |
* |
|
1175 |
* @return the contents of this URL. |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
58053
diff
changeset
|
1176 |
* @throws IOException if an I/O exception occurs. |
2 | 1177 |
* @see java.net.URLConnection#getContent() |
1178 |
*/ |
|
1179 |
public final Object getContent() throws java.io.IOException { |
|
1180 |
return openConnection().getContent(); |
|
1181 |
} |
|
1182 |
||
1183 |
/** |
|
1184 |
* Gets the contents of this URL. This method is a shorthand for: |
|
1185 |
* <blockquote><pre> |
|
22119
da8b8a10483f
8031326: Use Class<?> rather than Class in java.net method signatures
darcy
parents:
22064
diff
changeset
|
1186 |
* openConnection().getContent(classes) |
2 | 1187 |
* </pre></blockquote> |
1188 |
* |
|
1189 |
* @param classes an array of Java types |
|
1190 |
* @return the content object of this URL that is the first match of |
|
1191 |
* the types specified in the classes array. |
|
1192 |
* null if none of the requested types are supported. |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
58053
diff
changeset
|
1193 |
* @throws IOException if an I/O exception occurs. |
2 | 1194 |
* @see java.net.URLConnection#getContent(Class[]) |
1195 |
* @since 1.3 |
|
1196 |
*/ |
|
22119
da8b8a10483f
8031326: Use Class<?> rather than Class in java.net method signatures
darcy
parents:
22064
diff
changeset
|
1197 |
public final Object getContent(Class<?>[] classes) |
2 | 1198 |
throws java.io.IOException { |
1199 |
return openConnection().getContent(classes); |
|
1200 |
} |
|
1201 |
||
1202 |
/** |
|
1203 |
* The URLStreamHandler factory. |
|
1204 |
*/ |
|
25398 | 1205 |
private static volatile URLStreamHandlerFactory factory; |
2 | 1206 |
|
1207 |
/** |
|
19069 | 1208 |
* Sets an application's {@code URLStreamHandlerFactory}. |
2 | 1209 |
* This method can be called at most once in a given Java Virtual |
1210 |
* Machine. |
|
1211 |
* |
|
19069 | 1212 |
*<p> The {@code URLStreamHandlerFactory} instance is used to |
2 | 1213 |
*construct a stream protocol handler from a protocol name. |
1214 |
* |
|
1215 |
* <p> If there is a security manager, this method first calls |
|
19069 | 1216 |
* the security manager's {@code checkSetFactory} method |
2 | 1217 |
* to ensure the operation is allowed. |
1218 |
* This could result in a SecurityException. |
|
1219 |
* |
|
1220 |
* @param fac the desired factory. |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
58053
diff
changeset
|
1221 |
* @throws Error if the application has already set a factory. |
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
58053
diff
changeset
|
1222 |
* @throws SecurityException if a security manager exists and its |
19069 | 1223 |
* {@code checkSetFactory} method doesn't allow |
2 | 1224 |
* the operation. |
1225 |
* @see java.net.URL#URL(java.lang.String, java.lang.String, |
|
1226 |
* int, java.lang.String) |
|
1227 |
* @see java.net.URLStreamHandlerFactory |
|
1228 |
* @see SecurityManager#checkSetFactory |
|
1229 |
*/ |
|
1230 |
public static void setURLStreamHandlerFactory(URLStreamHandlerFactory fac) { |
|
1231 |
synchronized (streamHandlerLock) { |
|
1232 |
if (factory != null) { |
|
1233 |
throw new Error("factory already defined"); |
|
1234 |
} |
|
1235 |
SecurityManager security = System.getSecurityManager(); |
|
1236 |
if (security != null) { |
|
1237 |
security.checkSetFactory(); |
|
1238 |
} |
|
1239 |
handlers.clear(); |
|
27565 | 1240 |
|
25398 | 1241 |
// safe publication of URLStreamHandlerFactory with volatile write |
2 | 1242 |
factory = fac; |
1243 |
} |
|
1244 |
} |
|
1245 |
||
29097 | 1246 |
private static final URLStreamHandlerFactory defaultFactory = new DefaultFactory(); |
1247 |
||
1248 |
private static class DefaultFactory implements URLStreamHandlerFactory { |
|
53125
dee9426ef417
8215990: Avoid using reflection to create common default URLStreamHandlers
redestad
parents:
53120
diff
changeset
|
1249 |
private static String PREFIX = "sun.net.www.protocol."; |
29097 | 1250 |
|
1251 |
public URLStreamHandler createURLStreamHandler(String protocol) { |
|
53125
dee9426ef417
8215990: Avoid using reflection to create common default URLStreamHandlers
redestad
parents:
53120
diff
changeset
|
1252 |
// Avoid using reflection during bootstrap |
dee9426ef417
8215990: Avoid using reflection to create common default URLStreamHandlers
redestad
parents:
53120
diff
changeset
|
1253 |
switch (protocol) { |
dee9426ef417
8215990: Avoid using reflection to create common default URLStreamHandlers
redestad
parents:
53120
diff
changeset
|
1254 |
case "file": |
dee9426ef417
8215990: Avoid using reflection to create common default URLStreamHandlers
redestad
parents:
53120
diff
changeset
|
1255 |
return new sun.net.www.protocol.file.Handler(); |
dee9426ef417
8215990: Avoid using reflection to create common default URLStreamHandlers
redestad
parents:
53120
diff
changeset
|
1256 |
case "jar": |
dee9426ef417
8215990: Avoid using reflection to create common default URLStreamHandlers
redestad
parents:
53120
diff
changeset
|
1257 |
return new sun.net.www.protocol.jar.Handler(); |
dee9426ef417
8215990: Avoid using reflection to create common default URLStreamHandlers
redestad
parents:
53120
diff
changeset
|
1258 |
case "jrt": |
dee9426ef417
8215990: Avoid using reflection to create common default URLStreamHandlers
redestad
parents:
53120
diff
changeset
|
1259 |
return new sun.net.www.protocol.jrt.Handler(); |
dee9426ef417
8215990: Avoid using reflection to create common default URLStreamHandlers
redestad
parents:
53120
diff
changeset
|
1260 |
} |
dee9426ef417
8215990: Avoid using reflection to create common default URLStreamHandlers
redestad
parents:
53120
diff
changeset
|
1261 |
String name = PREFIX + protocol + ".Handler"; |
29097 | 1262 |
try { |
53125
dee9426ef417
8215990: Avoid using reflection to create common default URLStreamHandlers
redestad
parents:
53120
diff
changeset
|
1263 |
Object o = Class.forName(name).getDeclaredConstructor().newInstance(); |
37782
ad8fe7507ecc
6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents:
37781
diff
changeset
|
1264 |
return (URLStreamHandler)o; |
29097 | 1265 |
} catch (Exception e) { |
1266 |
// For compatibility, all Exceptions are ignored. |
|
1267 |
// any number of exceptions can get thrown here |
|
1268 |
} |
|
1269 |
return null; |
|
1270 |
} |
|
1271 |
} |
|
1272 |
||
29977
ad35f9986f75
8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
29098
diff
changeset
|
1273 |
private static URLStreamHandler lookupViaProperty(String protocol) { |
37593
824750ada3d6
8154231: Simplify access to System properties from JDK code
redestad
parents:
36511
diff
changeset
|
1274 |
String packagePrefixList = |
37781
71ed5645f17c
8155775: Re-examine naming of privileged methods to access System properties
redestad
parents:
37593
diff
changeset
|
1275 |
GetPropertyAction.privilegedGetProperty(protocolPathProp); |
36511 | 1276 |
if (packagePrefixList == null) { |
1277 |
// not set |
|
1278 |
return null; |
|
1279 |
} |
|
1280 |
||
29977
ad35f9986f75
8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
29098
diff
changeset
|
1281 |
String[] packagePrefixes = packagePrefixList.split("\\|"); |
ad35f9986f75
8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
29098
diff
changeset
|
1282 |
URLStreamHandler handler = null; |
ad35f9986f75
8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
29098
diff
changeset
|
1283 |
for (int i=0; handler == null && i<packagePrefixes.length; i++) { |
ad35f9986f75
8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
29098
diff
changeset
|
1284 |
String packagePrefix = packagePrefixes[i].trim(); |
ad35f9986f75
8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
29098
diff
changeset
|
1285 |
try { |
ad35f9986f75
8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
29098
diff
changeset
|
1286 |
String clsName = packagePrefix + "." + protocol + ".Handler"; |
ad35f9986f75
8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
29098
diff
changeset
|
1287 |
Class<?> cls = null; |
ad35f9986f75
8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
29098
diff
changeset
|
1288 |
try { |
ad35f9986f75
8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
29098
diff
changeset
|
1289 |
cls = Class.forName(clsName); |
ad35f9986f75
8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
29098
diff
changeset
|
1290 |
} catch (ClassNotFoundException e) { |
ad35f9986f75
8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
29098
diff
changeset
|
1291 |
ClassLoader cl = ClassLoader.getSystemClassLoader(); |
ad35f9986f75
8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
29098
diff
changeset
|
1292 |
if (cl != null) { |
ad35f9986f75
8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
29098
diff
changeset
|
1293 |
cls = cl.loadClass(clsName); |
ad35f9986f75
8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
29098
diff
changeset
|
1294 |
} |
ad35f9986f75
8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
29098
diff
changeset
|
1295 |
} |
ad35f9986f75
8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
29098
diff
changeset
|
1296 |
if (cls != null) { |
37782
ad8fe7507ecc
6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents:
37781
diff
changeset
|
1297 |
@SuppressWarnings("deprecation") |
ad8fe7507ecc
6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents:
37781
diff
changeset
|
1298 |
Object tmp = cls.newInstance(); |
ad8fe7507ecc
6850612: Deprecate Class.newInstance since it violates the checked exception language contract
darcy
parents:
37781
diff
changeset
|
1299 |
handler = (URLStreamHandler)tmp; |
29977
ad35f9986f75
8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
29098
diff
changeset
|
1300 |
} |
ad35f9986f75
8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
29098
diff
changeset
|
1301 |
} catch (Exception e) { |
ad35f9986f75
8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
29098
diff
changeset
|
1302 |
// any number of exceptions can get thrown here |
ad35f9986f75
8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
29098
diff
changeset
|
1303 |
} |
ad35f9986f75
8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
29098
diff
changeset
|
1304 |
} |
ad35f9986f75
8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
29098
diff
changeset
|
1305 |
return handler; |
ad35f9986f75
8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
29098
diff
changeset
|
1306 |
} |
ad35f9986f75
8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
29098
diff
changeset
|
1307 |
|
29097 | 1308 |
private static Iterator<URLStreamHandlerProvider> providers() { |
29986
97167d851fc4
8078467: Update core libraries to use diamond with anonymous classes
darcy
parents:
29977
diff
changeset
|
1309 |
return new Iterator<>() { |
29097 | 1310 |
|
1311 |
ClassLoader cl = ClassLoader.getSystemClassLoader(); |
|
1312 |
ServiceLoader<URLStreamHandlerProvider> sl = |
|
1313 |
ServiceLoader.load(URLStreamHandlerProvider.class, cl); |
|
1314 |
Iterator<URLStreamHandlerProvider> i = sl.iterator(); |
|
1315 |
||
1316 |
URLStreamHandlerProvider next = null; |
|
1317 |
||
1318 |
private boolean getNext() { |
|
1319 |
while (next == null) { |
|
1320 |
try { |
|
1321 |
if (!i.hasNext()) |
|
1322 |
return false; |
|
1323 |
next = i.next(); |
|
1324 |
} catch (ServiceConfigurationError sce) { |
|
1325 |
if (sce.getCause() instanceof SecurityException) { |
|
1326 |
// Ignore security exceptions |
|
1327 |
continue; |
|
1328 |
} |
|
1329 |
throw sce; |
|
1330 |
} |
|
1331 |
} |
|
1332 |
return true; |
|
1333 |
} |
|
1334 |
||
1335 |
public boolean hasNext() { |
|
1336 |
return getNext(); |
|
1337 |
} |
|
1338 |
||
1339 |
public URLStreamHandlerProvider next() { |
|
1340 |
if (!getNext()) |
|
1341 |
throw new NoSuchElementException(); |
|
1342 |
URLStreamHandlerProvider n = next; |
|
1343 |
next = null; |
|
1344 |
return n; |
|
1345 |
} |
|
1346 |
}; |
|
1347 |
} |
|
1348 |
||
1349 |
// Thread-local gate to prevent recursive provider lookups |
|
1350 |
private static ThreadLocal<Object> gate = new ThreadLocal<>(); |
|
1351 |
||
1352 |
private static URLStreamHandler lookupViaProviders(final String protocol) { |
|
1353 |
if (gate.get() != null) |
|
1354 |
throw new Error("Circular loading of URL stream handler providers detected"); |
|
1355 |
||
1356 |
gate.set(gate); |
|
1357 |
try { |
|
1358 |
return AccessController.doPrivileged( |
|
29986
97167d851fc4
8078467: Update core libraries to use diamond with anonymous classes
darcy
parents:
29977
diff
changeset
|
1359 |
new PrivilegedAction<>() { |
29097 | 1360 |
public URLStreamHandler run() { |
1361 |
Iterator<URLStreamHandlerProvider> itr = providers(); |
|
1362 |
while (itr.hasNext()) { |
|
1363 |
URLStreamHandlerProvider f = itr.next(); |
|
1364 |
URLStreamHandler h = f.createURLStreamHandler(protocol); |
|
1365 |
if (h != null) |
|
1366 |
return h; |
|
1367 |
} |
|
1368 |
return null; |
|
1369 |
} |
|
1370 |
}); |
|
1371 |
} finally { |
|
1372 |
gate.set(null); |
|
1373 |
} |
|
1374 |
} |
|
1375 |
||
46096
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
1376 |
/** |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
1377 |
* Returns the protocol in lower case. Special cases known protocols |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
1378 |
* to avoid loading locale classes during startup. |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
1379 |
*/ |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
1380 |
static String toLowerCase(String protocol) { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
1381 |
if (protocol.equals("jrt") || protocol.equals("file") || protocol.equals("jar")) { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
1382 |
return protocol; |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
1383 |
} else { |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
1384 |
return protocol.toLowerCase(Locale.ROOT); |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
1385 |
} |
62c77b334012
8185853: Generate readability graph at link time and other startup improvements
alanb
parents:
43800
diff
changeset
|
1386 |
} |
35393
12d55e1947f7
8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents:
35381
diff
changeset
|
1387 |
|
12d55e1947f7
8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents:
35381
diff
changeset
|
1388 |
/** |
12d55e1947f7
8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents:
35381
diff
changeset
|
1389 |
* Non-overrideable protocols: "jrt" and "file" |
12d55e1947f7
8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents:
35381
diff
changeset
|
1390 |
* |
12d55e1947f7
8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents:
35381
diff
changeset
|
1391 |
* Character-based comparison for performance reasons; also ensures |
12d55e1947f7
8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents:
35381
diff
changeset
|
1392 |
* case-insensitive comparison in a locale-independent fashion. |
12d55e1947f7
8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents:
35381
diff
changeset
|
1393 |
*/ |
12d55e1947f7
8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents:
35381
diff
changeset
|
1394 |
static boolean isOverrideable(String protocol) { |
12d55e1947f7
8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents:
35381
diff
changeset
|
1395 |
if (protocol.length() == 3) { |
12d55e1947f7
8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents:
35381
diff
changeset
|
1396 |
if ((Character.toLowerCase(protocol.charAt(0)) == 'j') && |
12d55e1947f7
8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents:
35381
diff
changeset
|
1397 |
(Character.toLowerCase(protocol.charAt(1)) == 'r') && |
12d55e1947f7
8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents:
35381
diff
changeset
|
1398 |
(Character.toLowerCase(protocol.charAt(2)) == 't')) { |
29097 | 1399 |
return false; |
35393
12d55e1947f7
8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents:
35381
diff
changeset
|
1400 |
} |
12d55e1947f7
8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents:
35381
diff
changeset
|
1401 |
} else if (protocol.length() == 4) { |
12d55e1947f7
8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents:
35381
diff
changeset
|
1402 |
if ((Character.toLowerCase(protocol.charAt(0)) == 'f') && |
12d55e1947f7
8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents:
35381
diff
changeset
|
1403 |
(Character.toLowerCase(protocol.charAt(1)) == 'i') && |
12d55e1947f7
8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents:
35381
diff
changeset
|
1404 |
(Character.toLowerCase(protocol.charAt(2)) == 'l') && |
12d55e1947f7
8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents:
35381
diff
changeset
|
1405 |
(Character.toLowerCase(protocol.charAt(3)) == 'e')) { |
12d55e1947f7
8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents:
35381
diff
changeset
|
1406 |
return false; |
12d55e1947f7
8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents:
35381
diff
changeset
|
1407 |
} |
12d55e1947f7
8147462: URI.toURL could be more efficient for most non-opaque URIs
redestad
parents:
35381
diff
changeset
|
1408 |
} |
29097 | 1409 |
return true; |
1410 |
} |
|
1411 |
||
2 | 1412 |
/** |
1413 |
* A table of protocol handlers. |
|
1414 |
*/ |
|
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
5506
diff
changeset
|
1415 |
static Hashtable<String,URLStreamHandler> handlers = new Hashtable<>(); |
29097 | 1416 |
private static final Object streamHandlerLock = new Object(); |
2 | 1417 |
|
1418 |
/** |
|
1419 |
* Returns the Stream Handler. |
|
1420 |
* @param protocol the protocol to use |
|
1421 |
*/ |
|
1422 |
static URLStreamHandler getURLStreamHandler(String protocol) { |
|
1423 |
||
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
5506
diff
changeset
|
1424 |
URLStreamHandler handler = handlers.get(protocol); |
29097 | 1425 |
|
1426 |
if (handler != null) { |
|
1427 |
return handler; |
|
1428 |
} |
|
2 | 1429 |
|
29097 | 1430 |
URLStreamHandlerFactory fac; |
1431 |
boolean checkedWithFactory = false; |
|
54696
0907dce4b90e
8217364: Custom URLStreamHandler for jrt or file protocol can override default handler
coffeys
parents:
53125
diff
changeset
|
1432 |
boolean overrideableProtocol = isOverrideable(protocol); |
2 | 1433 |
|
54696
0907dce4b90e
8217364: Custom URLStreamHandler for jrt or file protocol can override default handler
coffeys
parents:
53125
diff
changeset
|
1434 |
if (overrideableProtocol && jdk.internal.misc.VM.isBooted()) { |
25398 | 1435 |
// Use the factory (if any). Volatile read makes |
1436 |
// URLStreamHandlerFactory appear fully initialized to current thread. |
|
29097 | 1437 |
fac = factory; |
25398 | 1438 |
if (fac != null) { |
1439 |
handler = fac.createURLStreamHandler(protocol); |
|
2 | 1440 |
checkedWithFactory = true; |
1441 |
} |
|
1442 |
||
29098
fd65e2764b56
8073667: URL should not use service loader to lookup the jar protocol handler
chegar
parents:
29097
diff
changeset
|
1443 |
if (handler == null && !protocol.equalsIgnoreCase("jar")) { |
29097 | 1444 |
handler = lookupViaProviders(protocol); |
1445 |
} |
|
29977
ad35f9986f75
8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
29098
diff
changeset
|
1446 |
|
ad35f9986f75
8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
29098
diff
changeset
|
1447 |
if (handler == null) { |
ad35f9986f75
8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
29098
diff
changeset
|
1448 |
handler = lookupViaProperty(protocol); |
ad35f9986f75
8075139: Restore java.protocol.handler.pkgs to work as a fallback for migration
chegar
parents:
29098
diff
changeset
|
1449 |
} |
29097 | 1450 |
} |
2 | 1451 |
|
52638 | 1452 |
if (handler == null) { |
1453 |
// Try the built-in protocol handler |
|
1454 |
handler = defaultFactory.createURLStreamHandler(protocol); |
|
1455 |
} |
|
2 | 1456 |
|
52638 | 1457 |
synchronized (streamHandlerLock) { |
1458 |
URLStreamHandler handler2 = null; |
|
2 | 1459 |
|
52638 | 1460 |
// Check again with hashtable just in case another |
1461 |
// thread created a handler since we last checked |
|
1462 |
handler2 = handlers.get(protocol); |
|
2 | 1463 |
|
52638 | 1464 |
if (handler2 != null) { |
1465 |
return handler2; |
|
29097 | 1466 |
} |
2 | 1467 |
|
52638 | 1468 |
// Check with factory if another thread set a |
1469 |
// factory since our last check |
|
54696
0907dce4b90e
8217364: Custom URLStreamHandler for jrt or file protocol can override default handler
coffeys
parents:
53125
diff
changeset
|
1470 |
if (overrideableProtocol && !checkedWithFactory && |
0907dce4b90e
8217364: Custom URLStreamHandler for jrt or file protocol can override default handler
coffeys
parents:
53125
diff
changeset
|
1471 |
(fac = factory) != null) { |
52638 | 1472 |
handler2 = fac.createURLStreamHandler(protocol); |
2 | 1473 |
} |
52638 | 1474 |
|
1475 |
if (handler2 != null) { |
|
1476 |
// The handler from the factory must be given more |
|
1477 |
// importance. Discard the default handler that |
|
1478 |
// this thread created. |
|
1479 |
handler = handler2; |
|
1480 |
} |
|
52694
93afbb11063b
8214295: Populate handlers while holding streamHandlerLock
coffeys
parents:
52638
diff
changeset
|
1481 |
|
93afbb11063b
8214295: Populate handlers while holding streamHandlerLock
coffeys
parents:
52638
diff
changeset
|
1482 |
// Insert this handler into the hashtable |
93afbb11063b
8214295: Populate handlers while holding streamHandlerLock
coffeys
parents:
52638
diff
changeset
|
1483 |
if (handler != null) { |
93afbb11063b
8214295: Populate handlers while holding streamHandlerLock
coffeys
parents:
52638
diff
changeset
|
1484 |
handlers.put(protocol, handler); |
93afbb11063b
8214295: Populate handlers while holding streamHandlerLock
coffeys
parents:
52638
diff
changeset
|
1485 |
} |
52638 | 1486 |
} |
2 | 1487 |
return handler; |
1488 |
} |
|
1489 |
||
1490 |
/** |
|
35280 | 1491 |
* @serialField protocol String |
1492 |
* |
|
1493 |
* @serialField host String |
|
1494 |
* |
|
1495 |
* @serialField port int |
|
1496 |
* |
|
1497 |
* @serialField authority String |
|
1498 |
* |
|
1499 |
* @serialField file String |
|
1500 |
* |
|
1501 |
* @serialField ref String |
|
1502 |
* |
|
1503 |
* @serialField hashCode int |
|
1504 |
* |
|
1505 |
*/ |
|
57956
e0b8b019d2f5
8229997: Apply java.io.Serial annotations in java.base
darcy
parents:
55711
diff
changeset
|
1506 |
@java.io.Serial |
35280 | 1507 |
private static final ObjectStreamField[] serialPersistentFields = { |
1508 |
new ObjectStreamField("protocol", String.class), |
|
1509 |
new ObjectStreamField("host", String.class), |
|
1510 |
new ObjectStreamField("port", int.class), |
|
1511 |
new ObjectStreamField("authority", String.class), |
|
1512 |
new ObjectStreamField("file", String.class), |
|
1513 |
new ObjectStreamField("ref", String.class), |
|
1514 |
new ObjectStreamField("hashCode", int.class), }; |
|
1515 |
||
1516 |
/** |
|
2 | 1517 |
* WriteObject is called to save the state of the URL to an |
1518 |
* ObjectOutputStream. The handler is not saved since it is |
|
1519 |
* specific to this system. |
|
1520 |
* |
|
1521 |
* @serialData the default write object value. When read back in, |
|
1522 |
* the reader must ensure that calling getURLStreamHandler with |
|
1523 |
* the protocol variable returns a valid URLStreamHandler and |
|
1524 |
* throw an IOException if it does not. |
|
1525 |
*/ |
|
57956
e0b8b019d2f5
8229997: Apply java.io.Serial annotations in java.base
darcy
parents:
55711
diff
changeset
|
1526 |
@java.io.Serial |
2 | 1527 |
private synchronized void writeObject(java.io.ObjectOutputStream s) |
1528 |
throws IOException |
|
1529 |
{ |
|
1530 |
s.defaultWriteObject(); // write the fields |
|
1531 |
} |
|
1532 |
||
1533 |
/** |
|
1534 |
* readObject is called to restore the state of the URL from the |
|
1535 |
* stream. It reads the components of the URL and finds the local |
|
1536 |
* stream handler. |
|
1537 |
*/ |
|
57956
e0b8b019d2f5
8229997: Apply java.io.Serial annotations in java.base
darcy
parents:
55711
diff
changeset
|
1538 |
@java.io.Serial |
2 | 1539 |
private synchronized void readObject(java.io.ObjectInputStream s) |
35280 | 1540 |
throws IOException, ClassNotFoundException { |
1541 |
GetField gf = s.readFields(); |
|
1542 |
String protocol = (String)gf.get("protocol", null); |
|
1543 |
if (getURLStreamHandler(protocol) == null) { |
|
2 | 1544 |
throw new IOException("unknown protocol: " + protocol); |
1545 |
} |
|
35280 | 1546 |
String host = (String)gf.get("host", null); |
1547 |
int port = gf.get("port", -1); |
|
1548 |
String authority = (String)gf.get("authority", null); |
|
1549 |
String file = (String)gf.get("file", null); |
|
1550 |
String ref = (String)gf.get("ref", null); |
|
1551 |
int hashCode = gf.get("hashCode", -1); |
|
1552 |
if (authority == null |
|
53018
8bf9268df0e2
8215281: Use String.isEmpty() when applicable in java.base
redestad
parents:
52944
diff
changeset
|
1553 |
&& ((host != null && !host.isEmpty()) || port != -1)) { |
35280 | 1554 |
if (host == null) |
1555 |
host = ""; |
|
1556 |
authority = (port == -1) ? host : host + ":" + port; |
|
1557 |
} |
|
1558 |
tempState = new UrlDeserializedState(protocol, host, port, authority, |
|
1559 |
file, ref, hashCode); |
|
1560 |
} |
|
1561 |
||
1562 |
/** |
|
1563 |
* Replaces the de-serialized object with an URL object. |
|
1564 |
* |
|
1565 |
* @return a newly created object from deserialized data |
|
1566 |
* |
|
1567 |
* @throws ObjectStreamException if a new object replacing this |
|
1568 |
* object could not be created |
|
1569 |
*/ |
|
57956
e0b8b019d2f5
8229997: Apply java.io.Serial annotations in java.base
darcy
parents:
55711
diff
changeset
|
1570 |
@java.io.Serial |
35280 | 1571 |
private Object readResolve() throws ObjectStreamException { |
1572 |
||
1573 |
URLStreamHandler handler = null; |
|
1574 |
// already been checked in readObject |
|
1575 |
handler = getURLStreamHandler(tempState.getProtocol()); |
|
1576 |
||
1577 |
URL replacementURL = null; |
|
1578 |
if (isBuiltinStreamHandler(handler.getClass().getName())) { |
|
1579 |
replacementURL = fabricateNewURL(); |
|
1580 |
} else { |
|
1581 |
replacementURL = setDeserializedFields(handler); |
|
1582 |
} |
|
1583 |
return replacementURL; |
|
1584 |
} |
|
1585 |
||
1586 |
private URL setDeserializedFields(URLStreamHandler handler) { |
|
1587 |
URL replacementURL; |
|
1588 |
String userInfo = null; |
|
1589 |
String protocol = tempState.getProtocol(); |
|
1590 |
String host = tempState.getHost(); |
|
1591 |
int port = tempState.getPort(); |
|
1592 |
String authority = tempState.getAuthority(); |
|
1593 |
String file = tempState.getFile(); |
|
1594 |
String ref = tempState.getRef(); |
|
1595 |
int hashCode = tempState.getHashCode(); |
|
1596 |
||
2 | 1597 |
|
1598 |
// Construct authority part |
|
35280 | 1599 |
if (authority == null |
53018
8bf9268df0e2
8215281: Use String.isEmpty() when applicable in java.base
redestad
parents:
52944
diff
changeset
|
1600 |
&& ((host != null && !host.isEmpty()) || port != -1)) { |
2 | 1601 |
if (host == null) |
1602 |
host = ""; |
|
1603 |
authority = (port == -1) ? host : host + ":" + port; |
|
1604 |
||
1605 |
// Handle hosts with userInfo in them |
|
1606 |
int at = host.lastIndexOf('@'); |
|
1607 |
if (at != -1) { |
|
1608 |
userInfo = host.substring(0, at); |
|
1609 |
host = host.substring(at+1); |
|
1610 |
} |
|
1611 |
} else if (authority != null) { |
|
1612 |
// Construct user info part |
|
1613 |
int ind = authority.indexOf('@'); |
|
1614 |
if (ind != -1) |
|
1615 |
userInfo = authority.substring(0, ind); |
|
1616 |
} |
|
1617 |
||
1618 |
// Construct path and query part |
|
35280 | 1619 |
String path = null; |
1620 |
String query = null; |
|
2 | 1621 |
if (file != null) { |
1622 |
// Fix: only do this if hierarchical? |
|
1623 |
int q = file.lastIndexOf('?'); |
|
1624 |
if (q != -1) { |
|
1625 |
query = file.substring(q+1); |
|
1626 |
path = file.substring(0, q); |
|
1627 |
} else |
|
1628 |
path = file; |
|
1629 |
} |
|
35280 | 1630 |
|
1631 |
// Set the object fields. |
|
1632 |
this.protocol = protocol; |
|
1633 |
this.host = host; |
|
1634 |
this.port = port; |
|
1635 |
this.file = file; |
|
1636 |
this.authority = authority; |
|
1637 |
this.ref = ref; |
|
1638 |
this.hashCode = hashCode; |
|
1639 |
this.handler = handler; |
|
1640 |
this.query = query; |
|
1641 |
this.path = path; |
|
1642 |
this.userInfo = userInfo; |
|
1643 |
replacementURL = this; |
|
1644 |
return replacementURL; |
|
1645 |
} |
|
1646 |
||
1647 |
private URL fabricateNewURL() |
|
1648 |
throws InvalidObjectException { |
|
1649 |
// create URL string from deserialized object |
|
1650 |
URL replacementURL = null; |
|
1651 |
String urlString = tempState.reconstituteUrlString(); |
|
1652 |
||
1653 |
try { |
|
1654 |
replacementURL = new URL(urlString); |
|
1655 |
} catch (MalformedURLException mEx) { |
|
1656 |
resetState(); |
|
1657 |
InvalidObjectException invoEx = new InvalidObjectException( |
|
1658 |
"Malformed URL: " + urlString); |
|
1659 |
invoEx.initCause(mEx); |
|
1660 |
throw invoEx; |
|
1661 |
} |
|
1662 |
replacementURL.setSerializedHashCode(tempState.getHashCode()); |
|
1663 |
resetState(); |
|
1664 |
return replacementURL; |
|
1665 |
} |
|
1666 |
||
55711 | 1667 |
boolean isBuiltinStreamHandler(URLStreamHandler handler) { |
1668 |
return isBuiltinStreamHandler(handler.getClass().getName()); |
|
1669 |
} |
|
1670 |
||
35280 | 1671 |
private boolean isBuiltinStreamHandler(String handlerClassName) { |
1672 |
return (handlerClassName.startsWith(BUILTIN_HANDLERS_PREFIX)); |
|
1673 |
} |
|
1674 |
||
1675 |
private void resetState() { |
|
1676 |
this.protocol = null; |
|
1677 |
this.host = null; |
|
1678 |
this.port = -1; |
|
1679 |
this.file = null; |
|
1680 |
this.authority = null; |
|
1681 |
this.ref = null; |
|
1682 |
this.hashCode = -1; |
|
1683 |
this.handler = null; |
|
1684 |
this.query = null; |
|
1685 |
this.path = null; |
|
1686 |
this.userInfo = null; |
|
1687 |
this.tempState = null; |
|
1688 |
} |
|
1689 |
||
1690 |
private void setSerializedHashCode(int hc) { |
|
1691 |
this.hashCode = hc; |
|
2 | 1692 |
} |
43800
1dc22b50717a
8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents:
43199
diff
changeset
|
1693 |
|
1dc22b50717a
8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents:
43199
diff
changeset
|
1694 |
static { |
1dc22b50717a
8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents:
43199
diff
changeset
|
1695 |
SharedSecrets.setJavaNetURLAccess( |
1dc22b50717a
8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents:
43199
diff
changeset
|
1696 |
new JavaNetURLAccess() { |
1dc22b50717a
8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents:
43199
diff
changeset
|
1697 |
@Override |
1dc22b50717a
8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents:
43199
diff
changeset
|
1698 |
public URLStreamHandler getHandler(URL u) { |
1dc22b50717a
8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents:
43199
diff
changeset
|
1699 |
return u.handler; |
1dc22b50717a
8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents:
43199
diff
changeset
|
1700 |
} |
1dc22b50717a
8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents:
43199
diff
changeset
|
1701 |
} |
1dc22b50717a
8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents:
43199
diff
changeset
|
1702 |
); |
1dc22b50717a
8174151: URLClassLoader no longer uses custom URLStreamHandler for jar URLs
psandoz
parents:
43199
diff
changeset
|
1703 |
} |
2 | 1704 |
} |
1705 |
||
35280 | 1706 |
final class UrlDeserializedState { |
1707 |
private final String protocol; |
|
1708 |
private final String host; |
|
1709 |
private final int port; |
|
1710 |
private final String authority; |
|
1711 |
private final String file; |
|
1712 |
private final String ref; |
|
1713 |
private final int hashCode; |
|
1714 |
||
1715 |
public UrlDeserializedState(String protocol, |
|
1716 |
String host, int port, |
|
1717 |
String authority, String file, |
|
1718 |
String ref, int hashCode) { |
|
1719 |
this.protocol = protocol; |
|
1720 |
this.host = host; |
|
1721 |
this.port = port; |
|
1722 |
this.authority = authority; |
|
1723 |
this.file = file; |
|
1724 |
this.ref = ref; |
|
1725 |
this.hashCode = hashCode; |
|
1726 |
} |
|
1727 |
||
1728 |
String getProtocol() { |
|
1729 |
return protocol; |
|
1730 |
} |
|
1731 |
||
1732 |
String getHost() { |
|
1733 |
return host; |
|
1734 |
} |
|
1735 |
||
1736 |
String getAuthority () { |
|
1737 |
return authority; |
|
1738 |
} |
|
1739 |
||
1740 |
int getPort() { |
|
1741 |
return port; |
|
1742 |
} |
|
1743 |
||
1744 |
String getFile () { |
|
1745 |
return file; |
|
1746 |
} |
|
1747 |
||
1748 |
String getRef () { |
|
1749 |
return ref; |
|
1750 |
} |
|
1751 |
||
1752 |
int getHashCode () { |
|
1753 |
return hashCode; |
|
1754 |
} |
|
1755 |
||
1756 |
String reconstituteUrlString() { |
|
1757 |
||
1758 |
// pre-compute length of StringBuffer |
|
1759 |
int len = protocol.length() + 1; |
|
53018
8bf9268df0e2
8215281: Use String.isEmpty() when applicable in java.base
redestad
parents:
52944
diff
changeset
|
1760 |
if (authority != null && !authority.isEmpty()) |
35280 | 1761 |
len += 2 + authority.length(); |
1762 |
if (file != null) { |
|
1763 |
len += file.length(); |
|
1764 |
} |
|
1765 |
if (ref != null) |
|
1766 |
len += 1 + ref.length(); |
|
1767 |
StringBuilder result = new StringBuilder(len); |
|
1768 |
result.append(protocol); |
|
1769 |
result.append(":"); |
|
53018
8bf9268df0e2
8215281: Use String.isEmpty() when applicable in java.base
redestad
parents:
52944
diff
changeset
|
1770 |
if (authority != null && !authority.isEmpty()) { |
35280 | 1771 |
result.append("//"); |
1772 |
result.append(authority); |
|
1773 |
} |
|
1774 |
if (file != null) { |
|
1775 |
result.append(file); |
|
1776 |
} |
|
1777 |
if (ref != null) { |
|
1778 |
result.append("#"); |
|
1779 |
result.append(ref); |
|
1780 |
} |
|
1781 |
return result.toString(); |
|
1782 |
} |
|
1783 |
} |