Line 0
Link Here
|
|
|
1 |
/* |
2 |
* Copyright 2000-2004 The Apache Software Foundation |
3 |
* |
4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
5 |
* you may not use this file except in compliance with the License. |
6 |
* You may obtain a copy of the License at |
7 |
* |
8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
9 |
* |
10 |
* Unless required by applicable law or agreed to in writing, software |
11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 |
* See the License for the specific language governing permissions and |
14 |
* limitations under the License. |
15 |
* |
16 |
*/ |
17 |
|
18 |
package org.apache.tools.ant.protocol.resource; |
19 |
|
20 |
import org.apache.tools.ant.util.LoaderUtils; |
21 |
|
22 |
import java.io.IOException; |
23 |
import java.io.InputStream; |
24 |
import java.net.URL; |
25 |
import java.net.URLConnection; |
26 |
import java.net.URLDecoder; |
27 |
import java.util.Collections; |
28 |
|
29 |
/** open an resource input stream given a URL */ |
30 |
|
31 |
public class ResourceURLConnection extends URLConnection { |
32 |
/** |
33 |
* URLConnection of the jar URL that is the actual source of this resource. |
34 |
*/ |
35 |
protected URLConnection physicalConnection = null; |
36 |
|
37 |
/** |
38 |
* <P>Construct a URL connection that is specific to the resource protocol. This constructor |
39 |
* decodes the given URL to get the resource name, performs a class loader search to locate |
40 |
* that resource, and then constructs the physical URLConnection to that resource.</P> |
41 |
* <P>The physical connection is created by first getting the physical URL by calling |
42 |
* getResource() on the context class loader (See |
43 |
* <code>LoaderUtils.getContextClassLoader()</code>). If that class loader fails, a second |
44 |
* attempt will be made using the same class loader as used to load the ResourceURLConnection |
45 |
* class. Finally, if neither of those class loaders succeed, the system class loader will |
46 |
* be attempted.</P> |
47 |
* |
48 |
* @param url the target of this connection |
49 |
* @throws IOException to report that the resource can not be located as well as all I/O |
50 |
* errors while creating the physical connection. |
51 |
*/ |
52 |
public ResourceURLConnection(URL url) throws IOException { |
53 |
super(url); |
54 |
|
55 |
String resourceName = url.getPath(); |
56 |
|
57 |
// Decode escaped characters now that the path has been identified. This will permit |
58 |
// escaping path delimiters into the actual path. |
59 |
// Note: FileUtils.fromURI() can NOT be used here as it is hardcoded to only work with |
60 |
// file: URLs. |
61 |
resourceName = URLDecoder.decode(resourceName); |
62 |
|
63 |
URL physicalUrl = null; |
64 |
findURL: |
65 |
{ |
66 |
ClassLoader[] loaders = new ClassLoader[] { |
67 |
LoaderUtils.getContextClassLoader(), |
68 |
this.getClass().getClassLoader()}; |
69 |
|
70 |
for (int i = 0; i < loaders.length; i++) { |
71 |
physicalUrl = loaders[i].getResource(resourceName); |
72 |
if (physicalUrl != null) { |
73 |
break findURL; |
74 |
} |
75 |
} |
76 |
|
77 |
for (int i = 0; i < loaders.length; i++) { |
78 |
physicalUrl = loaders[i].getSystemResource(resourceName); |
79 |
if (physicalUrl != null) { |
80 |
break findURL; |
81 |
} |
82 |
} |
83 |
|
84 |
StringBuffer msg = new StringBuffer(); |
85 |
msg |
86 |
.append("Failed to find resource ") |
87 |
.append(url) |
88 |
.append(" in "); |
89 |
|
90 |
for (int i = 0; i < loaders.length; i++) { |
91 |
if (loaders[i] instanceof org.apache.tools.ant.AntClassLoader) { |
92 |
msg.append(((org.apache.tools.ant.AntClassLoader) loaders[i]).getClasspath()); |
93 |
} else if (loaders[i] instanceof java.net.URLClassLoader) { |
94 |
URL urls[] = ((java.net.URLClassLoader) loaders[i]).getURLs(); |
95 |
|
96 |
msg.append(urls[0].toString()); |
97 |
for (int j = 1; j < urls.length; j++) { |
98 |
msg.append(", "); |
99 |
msg.append(urls[j].toString()); |
100 |
} |
101 |
} |
102 |
} |
103 |
|
104 |
throw new IOException(msg.toString()); |
105 |
} |
106 |
|
107 |
physicalConnection = physicalUrl.openConnection(); |
108 |
} |
109 |
|
110 |
/** |
111 |
* Opens a communications link to the resource referenced by the physical connection. |
112 |
*/ |
113 |
public void connect() throws IOException { |
114 |
physicalConnection.connect(); |
115 |
} |
116 |
|
117 |
/** |
118 |
* Returns the value of the <code>allowUserInteraction</code> field for |
119 |
* this object. |
120 |
* |
121 |
* @return the value of the <code>allowUserInteraction</code> field for |
122 |
* this object. |
123 |
* @see #setAllowUserInteraction(boolean) |
124 |
*/ |
125 |
public boolean getAllowUserInteraction() { |
126 |
return physicalConnection.getAllowUserInteraction(); |
127 |
} |
128 |
|
129 |
/** |
130 |
* Returns the default value of a <code>URLConnection</code>'s |
131 |
* <code>useCaches</code> flag. |
132 |
* <p> |
133 |
* Ths default is "sticky", being a part of the static state of all |
134 |
* URLConnections. This flag applies to the next, and all following |
135 |
* URLConnections that are created. |
136 |
* |
137 |
* @return the default value of a <code>URLConnection</code>'s |
138 |
* <code>useCaches</code> flag. |
139 |
* @see #setDefaultUseCaches(boolean) |
140 |
*/ |
141 |
public boolean getDefaultUseCaches() { |
142 |
return physicalConnection.getDefaultUseCaches(); |
143 |
} |
144 |
|
145 |
/** |
146 |
* Returns the value of this <code>URLConnection</code>'s |
147 |
* <code>doInput</code> flag. |
148 |
* |
149 |
* @return the value of this <code>URLConnection</code>'s |
150 |
* <code>doInput</code> flag. |
151 |
* @see #setDoInput(boolean) |
152 |
*/ |
153 |
public boolean getDoInput() { |
154 |
return physicalConnection.getDoInput(); |
155 |
} |
156 |
|
157 |
/** |
158 |
* Returns the value of this <code>URLConnection</code>'s |
159 |
* <code>doOutput</code> flag. |
160 |
* |
161 |
* @return the value of this <code>URLConnection</code>'s |
162 |
* <code>doOutput</code> flag. |
163 |
* @see #setDoOutput(boolean) |
164 |
*/ |
165 |
public boolean getDoOutput() { |
166 |
return physicalConnection.getDoOutput(); |
167 |
} |
168 |
|
169 |
/** |
170 |
* Returns the value of this <code>URLConnection</code>'s |
171 |
* <code>useCaches</code> field. |
172 |
* |
173 |
* @return the value of this <code>URLConnection</code>'s |
174 |
* <code>useCaches</code> field. |
175 |
* @see #setUseCaches(boolean) |
176 |
*/ |
177 |
public boolean getUseCaches() { |
178 |
return physicalConnection.getUseCaches(); |
179 |
} |
180 |
|
181 |
/** |
182 |
* Returns the value of the <code>content-length</code> header field. |
183 |
* |
184 |
* @return the content length of the resource that this connection's URL |
185 |
* references, or <code>-1</code> if the content length is |
186 |
* not known. |
187 |
*/ |
188 |
public int getContentLength() { |
189 |
return physicalConnection.getContentLength(); |
190 |
} |
191 |
|
192 |
/** |
193 |
* Returns the value of the named field parsed as a number. |
194 |
* <p> |
195 |
* This form of <code>getHeaderField</code> exists because some |
196 |
* connection types (e.g., <code>http-ng</code>) have pre-parsed |
197 |
* headers. Classes for that connection type can override this method |
198 |
* and short-circuit the parsing. |
199 |
* |
200 |
* @param name the name of the header field. |
201 |
* @param Default the default value. |
202 |
* @return the value of the named field, parsed as an integer. The |
203 |
* <code>Default</code> value is returned if the field is |
204 |
* missing or malformed. |
205 |
*/ |
206 |
public int getHeaderFieldInt(String name,int Default) { |
207 |
return physicalConnection.getHeaderFieldInt(name, Default); |
208 |
} |
209 |
|
210 |
/** |
211 |
* Returns an input stream that reads from this open connection. |
212 |
* |
213 |
* @return an input stream that reads from this open connection. |
214 |
* @exception IOException if an I/O error occurs while |
215 |
* creating the input stream. |
216 |
* @exception UnknownServiceException if the protocol does not support |
217 |
* input. |
218 |
*/ |
219 |
public java.io.InputStream getInputStream()throws IOException { |
220 |
return physicalConnection.getInputStream(); |
221 |
} |
222 |
|
223 |
/** |
224 |
* Returns an output stream that writes to this connection. |
225 |
* |
226 |
* @return an output stream that writes to this connection. |
227 |
* @exception IOException if an I/O error occurs while |
228 |
* creating the output stream. |
229 |
* @exception UnknownServiceException if the protocol does not support |
230 |
* output. |
231 |
*/ |
232 |
public java.io.OutputStream getOutputStream()throws IOException { |
233 |
return physicalConnection.getOutputStream(); |
234 |
} |
235 |
|
236 |
/** |
237 |
* Retrieves the contents of this URL connection. |
238 |
* |
239 |
* @return the object fetched. The <code>instanceof</code> operator |
240 |
* should be used to determine the specific kind of object |
241 |
* returned. |
242 |
* @exception IOException if an I/O error occurs while |
243 |
* getting the content. |
244 |
* @exception UnknownServiceException if the protocol does not support |
245 |
* the content type. |
246 |
* @see java.net.URLConnection#getContent() |
247 |
*/ |
248 |
public Object getContent()throws IOException { |
249 |
return physicalConnection.getContent(); |
250 |
} |
251 |
|
252 |
/** |
253 |
* Retrieves the contents of this URL connection. |
254 |
* |
255 |
* @param classes the <code>Class</code> array |
256 |
* indicating the requested types |
257 |
* @return the object fetched that is the first match of the type specified in the classes |
258 |
* array. Returns <code>null</code> if none of the requested types are supported. |
259 |
* The <code>instanceof</code> operator should be used to determine the specific |
260 |
* kind of object returned. |
261 |
* @exception IOException if an I/O error occurs while getting the content. |
262 |
* @exception UnknownServiceException if the protocol does not support the content type. |
263 |
* @see java.net.URLConnection#getContent(Class[]) |
264 |
*/ |
265 |
public Object getContent(Class[] classes) throws IOException { |
266 |
return physicalConnection.getContent(classes); |
267 |
} |
268 |
|
269 |
/** |
270 |
* Returns the value of the <code>content-encoding</code> header field. |
271 |
* |
272 |
* @return the content encoding of the resource that the URL references, |
273 |
* or <code>null</code> if not known. |
274 |
* @see java.net.URLConnection#getHeaderField(java.lang.String) |
275 |
*/ |
276 |
public String getContentEncoding() { |
277 |
return physicalConnection.getContentEncoding(); |
278 |
} |
279 |
|
280 |
/** |
281 |
* Returns the value of the <code>content-type</code> header field. |
282 |
* |
283 |
* @return the content type of the resource that the URL references, |
284 |
* or <code>null</code> if not known. |
285 |
* @see java.net.URLConnection#getHeaderField(java.lang.String) |
286 |
*/ |
287 |
public String getContentType() { |
288 |
return physicalConnection.getContentType(); |
289 |
} |
290 |
|
291 |
/** |
292 |
* <P>Returns the value for the <code>n</code><sup>th</sup> header field. |
293 |
* It returns <code>null</code> if there are fewer than |
294 |
* <code>n+1</code> fields.</P> |
295 |
* |
296 |
* <P>This method can be used in conjunction with the |
297 |
* {@link #getHeaderFieldKey(int) getHeaderFieldKey} method to iterate through all |
298 |
* the headers in the message. </P> |
299 |
* |
300 |
* @param n an index, where n>=0 |
301 |
* @return the value of the <code>n</code><sup>th</sup> header field |
302 |
* or <code>null</code> if there are fewer than <code>n+1</code> fields |
303 |
* @see java.net.URLConnection#getHeaderFieldKey(int) |
304 |
*/ |
305 |
public String getHeaderField(int n) { |
306 |
return physicalConnection.getHeaderField(n); |
307 |
} |
308 |
|
309 |
/** |
310 |
* <P>Returns the value of the named header field.</P> |
311 |
* |
312 |
* <P>If called on a connection that sets the same header multiple times |
313 |
* with possibly different values, only the last value is returned.</P> |
314 |
* |
315 |
* @param name the name of a header field. |
316 |
* @return the value of the named header field, or <code>null</code> |
317 |
* if there is no such field in the header. |
318 |
*/ |
319 |
public String getHeaderField(String name) { |
320 |
return physicalConnection.getHeaderField(name); |
321 |
} |
322 |
|
323 |
/** |
324 |
* Returns the key for the <code>n</code><sup>th</sup> header field. |
325 |
* It returns <code>null</code> if there are fewer than <code>n+1</code> fields. |
326 |
* |
327 |
* @param n an index, where n>=0 |
328 |
* @return the key for the <code>n</code><sup>th</sup> header field, |
329 |
* or <code>null</code> if there are fewer than <code>n+1</code> |
330 |
* fields. |
331 |
*/ |
332 |
public String getHeaderFieldKey(int n) { |
333 |
return physicalConnection.getHeaderFieldKey(n); |
334 |
} |
335 |
|
336 |
/** |
337 |
* Returns the value of the named general request property for this |
338 |
* connection. |
339 |
* |
340 |
* @param key the keyword by which the request is known (e.g., "accept"). |
341 |
* @return the value of the named general request property for this |
342 |
* connection. If key is null, then null is returned. |
343 |
* @throws IllegalStateException if already connected |
344 |
* @see #setRequestProperty(java.lang.String, java.lang.String) |
345 |
*/ |
346 |
public String getRequestProperty(String key) { |
347 |
return physicalConnection.getRequestProperty(key); |
348 |
} |
349 |
|
350 |
/** |
351 |
* Returns a <code>String</code> representation of this URL connection. |
352 |
* |
353 |
* @return a string representation of this <code>URLConnection</code>. |
354 |
*/ |
355 |
public String toString() { |
356 |
return this.getClass().getName() + ":" + url; |
357 |
} |
358 |
|
359 |
/** |
360 |
* Returns a permission object representing the permission |
361 |
* necessary to make the connection represented by this |
362 |
* object. This method returns null if no permission is |
363 |
* required to make the connection. |
364 |
* |
365 |
* <p>The permission returned may dependent upon the state of the |
366 |
* connection. For example, the permission before connecting may be |
367 |
* different from that after connecting. For example, an HTTP |
368 |
* sever, say foo.com, may redirect the connection to a different |
369 |
* host, say bar.com. Before connecting the permission returned by |
370 |
* the connection will represent the permission needed to connect |
371 |
* to foo.com, while the permission returned after connecting will |
372 |
* be to bar.com. |
373 |
* |
374 |
* <p>Permissions are generally used for two purposes: to protect |
375 |
* caches of objects obtained through URLConnections, and to check |
376 |
* the right of a recipient to learn about a particular URL. In |
377 |
* the first case, the permission should be obtained |
378 |
* <em>after</em> the object has been obtained. For example, in an |
379 |
* HTTP connection, this will represent the permission to connect |
380 |
* to the host from which the data was ultimately fetched. In the |
381 |
* second case, the permission should be obtained and tested |
382 |
* <em>before</em> connecting. |
383 |
* |
384 |
* @return the permission object representing the permission |
385 |
* necessary to make the connection represented by this |
386 |
* URLConnection. |
387 |
* |
388 |
* @exception IOException if the computation of the permission |
389 |
* requires network or file I/O and an exception occurs while |
390 |
* computing it. |
391 |
*/ |
392 |
public java.security.Permission getPermission()throws IOException { |
393 |
return physicalConnection.getPermission(); |
394 |
} |
395 |
|
396 |
/** |
397 |
* Returns the value of the <code>date</code> header field. |
398 |
* |
399 |
* @return the sending date of the resource that the URL references, |
400 |
* or <code>0</code> if not known. The value returned is the |
401 |
* number of milliseconds since January 1, 1970 GMT. |
402 |
* @see java.net.URLConnection#getHeaderField(java.lang.String) |
403 |
*/ |
404 |
public long getDate() { |
405 |
return physicalConnection.getDate(); |
406 |
} |
407 |
|
408 |
/** |
409 |
* Returns the value of the <code>expires</code> header field. |
410 |
* |
411 |
* @return the expiration date of the resource that this URL references, |
412 |
* or 0 if not known. The value is the number of milliseconds since |
413 |
* January 1, 1970 GMT. |
414 |
* @see java.net.URLConnection#getHeaderField(java.lang.String) |
415 |
*/ |
416 |
public long getExpiration() { |
417 |
return physicalConnection.getExpiration(); |
418 |
} |
419 |
|
420 |
/** |
421 |
* Returns the value of the named field parsed as date. |
422 |
* The result is the number of milliseconds since January 1, 1970 GMT |
423 |
* represented by the named field. |
424 |
* <p> |
425 |
* This form of <code>getHeaderField</code> exists because some |
426 |
* connection types (e.g., <code>http-ng</code>) have pre-parsed |
427 |
* headers. Classes for that connection type can override this method |
428 |
* and short-circuit the parsing. |
429 |
* |
430 |
* @param name the name of the header field. |
431 |
* @param Default a default value. |
432 |
* @return the value of the field, parsed as a date. The value of the |
433 |
* <code>Default</code> argument is returned if the field is |
434 |
* missing or malformed. |
435 |
*/ |
436 |
public long getHeaderFieldDate(String name,long Default) { |
437 |
return physicalConnection.getHeaderFieldDate(name, Default); |
438 |
} |
439 |
|
440 |
/** |
441 |
* Returns the value of this object's <code>ifModifiedSince</code> field. |
442 |
* |
443 |
* @return the value of this object's <code>ifModifiedSince</code> field. |
444 |
* @see #setIfModifiedSince(long) |
445 |
*/ |
446 |
public long getIfModifiedSince() { |
447 |
return physicalConnection.getIfModifiedSince(); |
448 |
} |
449 |
|
450 |
/** |
451 |
* Returns the value of the <code>last-modified</code> header field. |
452 |
* The result is the number of milliseconds since January 1, 1970 GMT. |
453 |
* |
454 |
* @return the date the resource referenced by this |
455 |
* <code>URLConnection</code> was last modified, or 0 if not known. |
456 |
* @see java.net.URLConnection#getHeaderField(java.lang.String) |
457 |
*/ |
458 |
public long getLastModified() { |
459 |
return physicalConnection.getLastModified(); |
460 |
} |
461 |
|
462 |
/** |
463 |
* Set the value of the <code>allowUserInteraction</code> field of |
464 |
* this <code>URLConnection</code>. |
465 |
* |
466 |
* @param allowuserinteraction the new value. |
467 |
* @throws IllegalStateException if already connected |
468 |
* @see #getAllowUserInteraction() |
469 |
*/ |
470 |
public void setAllowUserInteraction(boolean allowuserinteraction) { |
471 |
physicalConnection.setAllowUserInteraction(allowuserinteraction); |
472 |
} |
473 |
|
474 |
/** |
475 |
* Sets the default value of the <code>useCaches</code> field to the |
476 |
* specified value. |
477 |
* |
478 |
* @param defaultusecaches the new value. |
479 |
* @see #getDefaultUseCaches() |
480 |
*/ |
481 |
public void setDefaultUseCaches(boolean defaultusecaches) { |
482 |
physicalConnection.setDefaultUseCaches(defaultusecaches); |
483 |
} |
484 |
|
485 |
/** |
486 |
* Sets the value of the <code>doInput</code> field for this |
487 |
* <code>URLConnection</code> to the specified value. |
488 |
* <p> |
489 |
* A URL connection can be used for input and/or output. Set the DoInput |
490 |
* flag to true if you intend to use the URL connection for input, |
491 |
* false if not. The default is true. |
492 |
* |
493 |
* @param doinput the new value. |
494 |
* @throws IllegalStateException if already connected |
495 |
* @see java.net.URLConnection#doInput |
496 |
* @see #getDoInput() |
497 |
*/ |
498 |
public void setDoInput(boolean doinput) { |
499 |
physicalConnection.setDoInput(doinput); |
500 |
} |
501 |
|
502 |
/** |
503 |
* Sets the value of the <code>doOutput</code> field for this |
504 |
* <code>URLConnection</code> to the specified value. |
505 |
* <p> |
506 |
* A URL connection can be used for input and/or output. Set the DoOutput |
507 |
* flag to true if you intend to use the URL connection for output, |
508 |
* false if not. The default is false. |
509 |
* |
510 |
* @param dooutput the new value. |
511 |
* @throws IllegalStateException if already connected |
512 |
* @see #getDoOutput() |
513 |
*/ |
514 |
public void setDoOutput(boolean dooutput) { |
515 |
physicalConnection.setDoOutput(dooutput); |
516 |
} |
517 |
|
518 |
/** |
519 |
* Sets the value of the <code>ifModifiedSince</code> field of |
520 |
* this <code>URLConnection</code> to the specified value. |
521 |
* |
522 |
* @param ifmodifiedsince the new value. |
523 |
* @throws IllegalStateException if already connected |
524 |
* @see #getIfModifiedSince() |
525 |
*/ |
526 |
public void setIfModifiedSince(long ifmodifiedsince) { |
527 |
physicalConnection.setIfModifiedSince(ifmodifiedsince); |
528 |
} |
529 |
|
530 |
/** |
531 |
* Sets the general request property. If a property with the key already |
532 |
* exists, overwrite its value with the new value. |
533 |
* |
534 |
* @param key the keyword by which the request is known |
535 |
* (e.g., "<code>accept</code>"). |
536 |
* @param value the value associated with it. |
537 |
* @throws IllegalStateException if already connected |
538 |
* @throws NullPointerException if key is <CODE>null</CODE> |
539 |
* @see #getRequestProperty(java.lang.String) |
540 |
*/ |
541 |
public void setRequestProperty(String key, String value) { |
542 |
physicalConnection.setRequestProperty(key, value); |
543 |
} |
544 |
|
545 |
/** |
546 |
* Sets the value of the <code>useCaches</code> field of this |
547 |
* <code>URLConnection</code> to the specified value. |
548 |
* <p> |
549 |
* Some protocols do caching of documents. Occasionally, it is important |
550 |
* to be able to "tunnel through" and ignore the caches (e.g., the |
551 |
* "reload" button in a browser). If the UseCaches flag on a connection |
552 |
* is true, the connection is allowed to use whatever caches it can. |
553 |
* If false, caches are to be ignored. |
554 |
* The default value comes from DefaultUseCaches, which defaults to |
555 |
* true. |
556 |
* |
557 |
* @param usecaches a <code>boolean</code> indicating whether |
558 |
* or not to allow caching |
559 |
* @throws IllegalStateException if already connected |
560 |
* @see #getUseCaches() |
561 |
*/ |
562 |
public void setUseCaches(boolean usecaches) { |
563 |
physicalConnection.setUseCaches(usecaches); |
564 |
} |
565 |
|
566 |
/** |
567 |
* Returns an unmodifiable Map of the header fields. |
568 |
* The Map keys are Strings that represent the |
569 |
* response-header field names. Each Map value is an |
570 |
* unmodifiable List of Strings that represents |
571 |
* the corresponding field values. |
572 |
* |
573 |
* @return a Map of header fields |
574 |
* @since 1.4 (returns Collections.EMPTY_MAP for all previous versions) |
575 |
*/ |
576 |
public java.util.Map getHeaderFields() { |
577 |
try { |
578 |
java.lang.reflect.Method method = |
579 |
physicalConnection.getClass().getDeclaredMethod("getHeaderFields", new Class[]{}); |
580 |
return (java.util.Map) method.invoke(physicalConnection, new Object[]{}); |
581 |
} catch (NoSuchMethodException e) { |
582 |
} catch (java.lang.reflect.InvocationTargetException e) { |
583 |
Throwable tgt = e.getTargetException(); |
584 |
|
585 |
if (tgt instanceof IllegalStateException) { |
586 |
throw (IllegalStateException) tgt; |
587 |
} |
588 |
|
589 |
throw new RuntimeException(tgt.getClass().getName() + ": " + tgt.getMessage()); |
590 |
} catch (IllegalAccessException e) { |
591 |
throw new RuntimeException(e.getClass().getName() + ": " + e.getMessage()); |
592 |
} |
593 |
|
594 |
return Collections.EMPTY_MAP; |
595 |
} |
596 |
|
597 |
/** |
598 |
* Returns an unmodifiable Map of general request |
599 |
* properties for this connection. The Map keys |
600 |
* are Strings that represent the request-header |
601 |
* field names. Each Map value is a unmodifiable List |
602 |
* of Strings that represents the corresponding |
603 |
* field values. |
604 |
* |
605 |
* @return a Map of the general request properties for this connection. |
606 |
* @throws IllegalStateException if already connected |
607 |
* @since 1.4 (returns Collections.EMPTY_MAP for all previous versions) |
608 |
*/ |
609 |
public java.util.Map getRequestProperties() { |
610 |
try { |
611 |
java.lang.reflect.Method method = |
612 |
physicalConnection.getClass().getDeclaredMethod("getRequestProperties", new Class[]{}); |
613 |
return (java.util.Map) method.invoke(physicalConnection, new Object[]{}); |
614 |
} catch (NoSuchMethodException e) { |
615 |
} catch (java.lang.reflect.InvocationTargetException e) { |
616 |
Throwable tgt = e.getTargetException(); |
617 |
|
618 |
if (tgt instanceof IllegalStateException) { |
619 |
throw (IllegalStateException) tgt; |
620 |
} |
621 |
|
622 |
throw new RuntimeException(tgt.getClass().getName() + ": " + tgt.getMessage()); |
623 |
} catch (IllegalAccessException e) { |
624 |
throw new RuntimeException(e.getClass().getName() + ": " + e.getMessage()); |
625 |
} |
626 |
|
627 |
return Collections.EMPTY_MAP; |
628 |
} |
629 |
|
630 |
/** |
631 |
* Adds a general request property specified by a |
632 |
* key-value pair. This method will not overwrite |
633 |
* existing values associated with the same key. |
634 |
* |
635 |
* @param key the keyword by which the request is known |
636 |
* (e.g., "<code>accept</code>"). |
637 |
* @param value the value associated with it. |
638 |
* @throws IllegalStateException if already connected |
639 |
* @throws NullPointerException if key is null |
640 |
* @see #getRequestProperties(java.lang.String) |
641 |
* @since 1.4 (does nothing for all previous versions) |
642 |
*/ |
643 |
|
644 |
public void addRequestProperty(String s1, String s2) { |
645 |
try { |
646 |
java.lang.reflect.Method method = |
647 |
physicalConnection.getClass() |
648 |
.getDeclaredMethod("addRequestProperty", |
649 |
new Class[]{String.class, String.class}); |
650 |
method.invoke(physicalConnection, new Object[]{s1, s2}); |
651 |
} catch (NoSuchMethodException e) { |
652 |
} catch (java.lang.reflect.InvocationTargetException e) { |
653 |
Throwable tgt = e.getTargetException(); |
654 |
|
655 |
if (tgt instanceof IllegalStateException) { |
656 |
throw (IllegalStateException) tgt; |
657 |
} |
658 |
|
659 |
throw new RuntimeException(tgt.getClass().getName() |
660 |
+ ": " + tgt.getMessage()); |
661 |
} catch (IllegalAccessException e) { |
662 |
throw new RuntimeException(e.getClass().getName() + ": " + e.getMessage()); |
663 |
} |
664 |
} |
665 |
} |