1 |
/* URLConnection.java -- Abstract superclass for reading from URL's |
2 |
Copyright (C) 1998, 2002 Free Software Foundation, Inc. |
3 |
|
4 |
This file is part of GNU Classpath. |
5 |
|
6 |
GNU Classpath is free software; you can redistribute it and/or modify |
7 |
it under the terms of the GNU General Public License as published by |
8 |
the Free Software Foundation; either version 2, or (at your option) |
9 |
any later version. |
10 |
|
11 |
GNU Classpath is distributed in the hope that it will be useful, but |
12 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 |
General Public License for more details. |
15 |
|
16 |
You should have received a copy of the GNU General Public License |
17 |
along with GNU Classpath; see the file COPYING. If not, write to the |
18 |
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
19 |
02111-1307 USA. |
20 |
|
21 |
Linking this library statically or dynamically with other modules is |
22 |
making a combined work based on this library. Thus, the terms and |
23 |
conditions of the GNU General Public License cover the whole |
24 |
combination. |
25 |
|
26 |
As a special exception, the copyright holders of this library give you |
27 |
permission to link this library with independent modules to produce an |
28 |
executable, regardless of the license terms of these independent |
29 |
modules, and to copy and distribute the resulting executable under |
30 |
terms of your choice, provided that you also meet, for each linked |
31 |
independent module, the terms and conditions of the license of that |
32 |
module. An independent module is a module which is not derived from |
33 |
or based on this library. If you modify this library, you may extend |
34 |
this exception to your version of the library, but you are not |
35 |
obligated to do so. If you do not wish to do so, delete this |
36 |
exception statement from your version. */ |
37 |
|
38 |
|
39 |
package java.net; |
40 |
|
41 |
import java.io.InputStream; |
42 |
import java.io.IOException; |
43 |
import java.io.OutputStream; |
44 |
import java.security.Permission; |
45 |
import java.util.Collections; |
46 |
import java.util.Date; |
47 |
import java.util.Locale; |
48 |
import java.util.Hashtable; |
49 |
import java.util.Map; |
50 |
|
51 |
/** |
52 |
* Written using on-line Java Platform 1.2 API Specification, as well |
53 |
* as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998). |
54 |
* Status: One guessContentTypeFrom... methods not implemented. |
55 |
* getContent method assumes content type from response; see comment there. |
56 |
*/ |
57 |
|
58 |
/** |
59 |
* This class models a connection that retrieves the information pointed |
60 |
* to by a URL object. This is typically a connection to a remote node |
61 |
* on the network, but could be a simple disk read. |
62 |
* <p> |
63 |
* A URLConnection object is normally created by calling the openConnection() |
64 |
* method of a URL object. This method is somewhat misnamed because it does |
65 |
* not actually open the connection. Instead, it return an unconnected |
66 |
* instance of this object. The caller then has the opportunity to set |
67 |
* various connection options prior to calling the actual connect() method. |
68 |
* <p> |
69 |
* After the connection has been opened, there are a number of methods in |
70 |
* this class that access various attributes of the data, typically |
71 |
* represented by headers sent in advance of the actual data itself. |
72 |
* <p> |
73 |
* Also of note are the getInputStream and getContent() methods which allow |
74 |
* the caller to retrieve the actual data from the connection. Note that |
75 |
* for some types of connections, writing is also allowed. The setDoOutput() |
76 |
* method must be called prior to connecing in order to enable this, then |
77 |
* the getOutputStream method called after the connection in order to |
78 |
* obtain a stream to write the output to. |
79 |
* <p> |
80 |
* The getContent() method is of particular note. This method returns an |
81 |
* Object that encapsulates the data returned. There is no way do determine |
82 |
* the type of object that will be returned in advance. This is determined |
83 |
* by the actual content handlers as described in the description of that |
84 |
* method. |
85 |
* |
86 |
* @author Aaron M. Renn <arenn@urbanophile.com> |
87 |
* @author Warren Levy <warrenl@cygnus.com> |
88 |
*/ |
89 |
public abstract class URLConnection |
90 |
{ |
91 |
/** |
92 |
* This is an object that maps filenames to MIME types. The interface |
93 |
* to do this is implemented by this class, so just create an empty |
94 |
* instance and store it here. |
95 |
*/ |
96 |
private static FileNameMap fileNameMap = new MimeTypeMapper(); |
97 |
|
98 |
/** |
99 |
* This is the ContentHandlerFactory set by the caller, if any |
100 |
*/ |
101 |
private static ContentHandlerFactory factory; |
102 |
|
103 |
/** |
104 |
* This is the default value that will be used to determine whether or |
105 |
* not user interaction should be allowed. |
106 |
*/ |
107 |
private static boolean defaultAllowUserInteraction = false; |
108 |
|
109 |
/** |
110 |
* This is the default flag indicating whether or not to use caches to |
111 |
* store the data returned from a server |
112 |
*/ |
113 |
private static boolean defaultUseCaches = true; |
114 |
|
115 |
/** |
116 |
* This is a Hashable for setting default request properties |
117 |
*/ |
118 |
private static Hashtable def_req_props = new Hashtable(); |
119 |
|
120 |
/** |
121 |
* This variable determines whether or not interaction is allowed with |
122 |
* the user. For example, to prompt for a username and password. |
123 |
*/ |
124 |
protected boolean allowUserInteraction; |
125 |
|
126 |
/** |
127 |
* Indicates whether or not a connection has been established to the |
128 |
* destination specified in the URL |
129 |
*/ |
130 |
protected boolean connected = false; |
131 |
|
132 |
/** |
133 |
* Indicates whether or not input can be read from this URL |
134 |
*/ |
135 |
protected boolean doInput = true; |
136 |
|
137 |
/** |
138 |
* Indicates whether or not output can be sent to this URL |
139 |
*/ |
140 |
protected boolean doOutput = false; |
141 |
|
142 |
/** |
143 |
* If this flag is set, the protocol is allowed to cache data whenever |
144 |
* it can (caching is not guaranteed). If it is not set, the protocol |
145 |
* must a get a fresh copy of the data. |
146 |
* <p> |
147 |
* This field is set by the setUseCaches method and returned by the |
148 |
* getUseCaches method. |
149 |
* |
150 |
* Its default value is that determined by the last invocation of |
151 |
* setDefaultUseCaches |
152 |
*/ |
153 |
protected boolean useCaches; |
154 |
|
155 |
/** |
156 |
* If this value is non-zero, then the connection will only attempt to |
157 |
* fetch the document pointed to by the URL if the document has been |
158 |
* modified more recently than the date set in this variable. That date |
159 |
* should be specified as the number of seconds since 1/1/1970 GMT. |
160 |
*/ |
161 |
protected long ifModifiedSince = 0L; |
162 |
|
163 |
/** |
164 |
* This is the URL associated with this connection |
165 |
*/ |
166 |
protected URL url; |
167 |
|
168 |
/** |
169 |
* The list of request properties for this connection |
170 |
*/ |
171 |
private final Hashtable req_props; |
172 |
|
173 |
/** |
174 |
* Creates a URL connection to a given URL. A real connection is not made. |
175 |
* Use #connect to do this. |
176 |
* |
177 |
* @param url The Object to create the URL connection to |
178 |
* |
179 |
* @see URLConnection#connect() |
180 |
*/ |
181 |
protected URLConnection(URL url) |
182 |
{ |
183 |
// Set up all our instance variables |
184 |
this.url = url; |
185 |
allowUserInteraction = defaultAllowUserInteraction; |
186 |
useCaches = defaultUseCaches; |
187 |
req_props = new Hashtable(def_req_props); |
188 |
} |
189 |
|
190 |
/** |
191 |
* Establishes the actual connection to the URL associated with this |
192 |
* connection object |
193 |
*/ |
194 |
public abstract void connect() throws IOException; |
195 |
|
196 |
/** |
197 |
* Returns the URL object associated with this connection |
198 |
* |
199 |
* @return The URL for this connection. |
200 |
*/ |
201 |
public URL getURL() |
202 |
{ |
203 |
return url; |
204 |
} |
205 |
|
206 |
/** |
207 |
* Returns the value of the content-length header field or -1 if the value |
208 |
* is not known or not present. |
209 |
* |
210 |
* @return The content-length field |
211 |
*/ |
212 |
public int getContentLength() |
213 |
{ |
214 |
return getHeaderFieldInt("content-length", -1); |
215 |
} |
216 |
|
217 |
/** |
218 |
* Returns the the content-type of the data pointed to by the URL. This |
219 |
* method first tries looking for a content-type header. If that is not |
220 |
* present, it attempts to use the file name to determine the content's |
221 |
* MIME type. If that is unsuccessful, the method returns null. The caller |
222 |
* may then still attempt to determine the MIME type by a call to |
223 |
* guessContentTypeFromStream() |
224 |
* |
225 |
* @return The content MIME type |
226 |
*/ |
227 |
public String getContentType() |
228 |
{ |
229 |
return getHeaderField("content-type"); |
230 |
} |
231 |
|
232 |
/** |
233 |
* Returns the value of the content-encoding field or null if it is not |
234 |
* known or not present. |
235 |
* |
236 |
* @return The content-encoding field |
237 |
*/ |
238 |
public String getContentEncoding() |
239 |
{ |
240 |
return getHeaderField("content-encoding"); |
241 |
} |
242 |
|
243 |
/** |
244 |
* Returns the value of the expires header or 0 if not known or present. |
245 |
* If populated, the return value is number of seconds since midnight |
246 |
* on 1/1/1970 GMT. |
247 |
* |
248 |
* @return The expiration time. |
249 |
*/ |
250 |
public long getExpiration() |
251 |
{ |
252 |
return getHeaderFieldDate("expires", 0L); |
253 |
} |
254 |
|
255 |
/** |
256 |
* Returns the date of the document pointed to by the URL as reported in |
257 |
* the date field of the header or 0 if the value is not present or not |
258 |
* known. If populated, the return value is number of seconds since |
259 |
* midnight on 1/1/1970 GMT. |
260 |
* |
261 |
* @return The document date |
262 |
*/ |
263 |
public long getDate() |
264 |
{ |
265 |
return getHeaderFieldDate("date", 0L); |
266 |
} |
267 |
|
268 |
/** |
269 |
* Returns the value of the last-modified header field or 0 if not known known |
270 |
* or not present. If populated, the return value is the number of seconds |
271 |
* since midnight on 1/1/1970. |
272 |
* |
273 |
* @return The last modified time |
274 |
*/ |
275 |
public long getLastModified() |
276 |
{ |
277 |
return getHeaderFieldDate("last-modified", 0L); |
278 |
} |
279 |
|
280 |
/** |
281 |
* Return a String representing the header value at the specified index. |
282 |
* This allows the caller to walk the list of header fields. The analogous |
283 |
* getHeaderFieldKey(int) method allows access to the corresponding key |
284 |
* for this header field |
285 |
* |
286 |
* @param index The index into the header field list to retrieve the value for |
287 |
* |
288 |
* @return The header value or null if index is past the end of the headers |
289 |
*/ |
290 |
public String getHeaderField(int index) |
291 |
{ |
292 |
// Subclasses for specific protocols override this. |
293 |
return null; |
294 |
} |
295 |
|
296 |
/** |
297 |
* Returns a String representing the value of the header field having |
298 |
* the named key. Returns null if the header field does not exist. |
299 |
* |
300 |
* @param The key of the header field |
301 |
* |
302 |
* @return The value of the header field as a String |
303 |
*/ |
304 |
public String getHeaderField(String name) |
305 |
{ |
306 |
for (int i = 0; ; i++) |
307 |
{ |
308 |
String key = getHeaderFieldKey(i); |
309 |
if (key == null) |
310 |
return(null); |
311 |
|
312 |
if (key.toLowerCase().equals(name.toLowerCase())) |
313 |
return(getHeaderField(i)); |
314 |
} |
315 |
} |
316 |
|
317 |
/** |
318 |
* Returns a map of all sent header fields |
319 |
* |
320 |
* @since 1.4 |
321 |
*/ |
322 |
public Map getHeaderFields() |
323 |
{ |
324 |
// Subclasses for specific protocols override this. |
325 |
return null; |
326 |
} |
327 |
|
328 |
/** |
329 |
* Returns the value of the named header field as an int. If the field |
330 |
* is not present or cannot be parsed as an integer, the default value |
331 |
* will be returned. |
332 |
* |
333 |
* @param name The header field key to lookup |
334 |
* @param defaultValue The defaule value if the header field is not found |
335 |
* or can't be parsed. |
336 |
* |
337 |
* @return The value of the header field or the default value if the field |
338 |
* is missing or malformed |
339 |
*/ |
340 |
public int getHeaderFieldInt(String name, int defaultValue) |
341 |
{ |
342 |
String value = getHeaderField (name); |
343 |
|
344 |
if (value == null) |
345 |
return defaultValue; |
346 |
|
347 |
try |
348 |
{ |
349 |
return Integer.parseInt (value); |
350 |
} |
351 |
catch (NumberFormatException e) |
352 |
{ |
353 |
return defaultValue; |
354 |
} |
355 |
} |
356 |
|
357 |
/** |
358 |
* Returns the value of the named header field as a date. This date will |
359 |
* be the number of seconds since midnight 1/1/1970 GMT or the default |
360 |
* value if the field is not present or cannot be converted to a date. |
361 |
* |
362 |
* @param name The name of the header field |
363 |
* @param defaultValue The default date if the header field is not found |
364 |
* or can't be converted. |
365 |
* |
366 |
* @return Returns the date value of the header filed or the default value |
367 |
* if the field is missing or malformed |
368 |
*/ |
369 |
public long getHeaderFieldDate (String name, long defaultValue) |
370 |
{ |
371 |
String str = getHeaderField (name); |
372 |
|
373 |
if (str == null) |
374 |
return defaultValue; |
375 |
|
376 |
// This needs to change since Date(String) is deprecated, but DateFormat |
377 |
// doesn't seem to be working for some reason |
378 |
//DateFormat df = DateFormat.getDateInstance(DateFormat.FULL, Locale.US); |
379 |
//df.setLenient(true); |
380 |
|
381 |
//Date d = df.parse(value, new ParsePosition(0)); |
382 |
Date d = new Date (str); |
383 |
|
384 |
if (d == null) |
385 |
return defaultValue; |
386 |
|
387 |
return (d.getTime() / 1000); |
388 |
} |
389 |
|
390 |
/** |
391 |
* Returns a String representing the header key at the specified index. |
392 |
* This allows the caller to walk the list of header fields. The analogous |
393 |
* getHeaderField(int) method allows access to the corresponding value for |
394 |
* this tag. |
395 |
* |
396 |
* @param index The index into the header field list to retrieve the key for. |
397 |
* |
398 |
* @return The header field key or null if index is past the end |
399 |
* of the headers. |
400 |
*/ |
401 |
public String getHeaderFieldKey (int index) |
402 |
{ |
403 |
// Subclasses for specific protocols override this. |
404 |
return null; |
405 |
} |
406 |
|
407 |
/** |
408 |
* This method returns the content of the document pointed to by the URL |
409 |
* as an Object. The type of object depends on the MIME type of the |
410 |
* object and particular content hander loaded. Most text type content |
411 |
* handlers will return a subclass of InputStream. Images usually return |
412 |
* a class that implements ImageProducer. There is not guarantee what |
413 |
* type of object will be returned, however. |
414 |
* <p> |
415 |
* This class first determines the MIME type of the content, then creates |
416 |
* a ContentHandler object to process the input. If the ContentHandlerFactory |
417 |
* is set, then that object is called to load a content handler, otherwise |
418 |
* a class called gnu.java.net.content.<content_type> is tried. |
419 |
* The default class will also be used if the content handler factory returns |
420 |
* a null content handler. |
421 |
* |
422 |
* @exception IOException If an error occurs |
423 |
* @exception UnknownServiceException If the protocol does not support the |
424 |
* content type |
425 |
*/ |
426 |
public Object getContent() throws IOException |
427 |
{ |
428 |
// connect(); |
429 |
String type = getContentType(); |
430 |
|
431 |
// First try the factory |
432 |
ContentHandler ch = null; |
433 |
if (factory != null) |
434 |
ch = factory.createContentHandler(type); |
435 |
|
436 |
if (ch != null) |
437 |
return(ch.getContent(this)); |
438 |
|
439 |
// Then try our default class |
440 |
try |
441 |
{ |
442 |
Class cls = Class.forName("gnu.java.net.content." + |
443 |
type.replace('/', '.')); |
444 |
|
445 |
Object obj = cls.newInstance(); |
446 |
if (!(obj instanceof ContentHandler)) |
447 |
throw new UnknownServiceException(type); |
448 |
|
449 |
ch = (ContentHandler)obj; |
450 |
return(ch.getContent(this)); |
451 |
} |
452 |
catch (ClassNotFoundException e) |
453 |
{ |
454 |
} |
455 |
catch (InstantiationException e) |
456 |
{ |
457 |
} |
458 |
catch (IllegalAccessException e) |
459 |
{ |
460 |
} |
461 |
|
462 |
throw new UnknownServiceException(type); |
463 |
} |
464 |
|
465 |
/** |
466 |
* Retrieves the content of this URLConnection |
467 |
* |
468 |
* @exception IOException If an error occurs |
469 |
* @exception UnknownServiceException If the protocol does not support the |
470 |
* content type |
471 |
*/ |
472 |
public Object getContent(Class[] classes) throws IOException |
473 |
{ |
474 |
// FIXME: implement this |
475 |
return getContent (); |
476 |
} |
477 |
|
478 |
/** |
479 |
* This method returns a <code>Permission</code> object representing the |
480 |
* permissions required to access this URL. This method returns |
481 |
* <code>java.security.AllPermission</code> by default. Subclasses should |
482 |
* override it to return a more specific permission. For example, an |
483 |
* HTTP URL should return an instance of <code>SocketPermission</code> |
484 |
* for the appropriate host and port. |
485 |
* <p> |
486 |
* Note that because of items such as HTTP redirects, the permission |
487 |
* object returned might be different before and after connecting. |
488 |
* |
489 |
* @return A Permission object |
490 |
* |
491 |
* @exception IOException If the computation of the permission requires |
492 |
* network or file I/O and an exception occurs while computing it |
493 |
*/ |
494 |
public Permission getPermission() throws IOException |
495 |
{ |
496 |
// Subclasses may override this. |
497 |
return new java.security.AllPermission(); |
498 |
} |
499 |
|
500 |
/** |
501 |
* Returns an InputStream for this connection. As this default |
502 |
* implementation returns null, subclasses should override this method |
503 |
* |
504 |
* @return An InputStream for this connection |
505 |
* |
506 |
* @exception IOException If an error occurs |
507 |
* @exception UnknownServiceException If the protocol does not support input |
508 |
*/ |
509 |
public InputStream getInputStream() throws IOException |
510 |
{ |
511 |
return(null); |
512 |
} |
513 |
|
514 |
/** |
515 |
* Returns an OutputStream for this connection. As this default |
516 |
* implementation returns null, subclasses should override this method |
517 |
* |
518 |
* @return An OutputStream for this connection |
519 |
* |
520 |
* @exception IOException If an error occurs |
521 |
* @exception UnknownServiceException If the protocol does not support output |
522 |
*/ |
523 |
public OutputStream getOutputStream() throws IOException |
524 |
{ |
525 |
return(null); |
526 |
} |
527 |
|
528 |
/** |
529 |
* The methods prints the value of this object as a String by calling the |
530 |
* toString() method of its associated URL. Overrides Object.toString() |
531 |
* |
532 |
* @return A String representation of this object |
533 |
*/ |
534 |
public String toString() |
535 |
{ |
536 |
return(url.toString()); |
537 |
} |
538 |
|
539 |
/** |
540 |
* Returns the value of a flag indicating whether or not input is going |
541 |
* to be done for this connection. This default to true unless the |
542 |
* doOutput flag is set to false, in which case this defaults to false. |
543 |
* |
544 |
* @param input <code>true</code> if input is to be done, |
545 |
* <code>false</code> otherwise |
546 |
* |
547 |
* @exception IllegalStateException If already connected |
548 |
*/ |
549 |
public void setDoInput(boolean input) |
550 |
{ |
551 |
if (connected) |
552 |
throw new IllegalStateException ("Already connected"); |
553 |
|
554 |
doInput = input; |
555 |
} |
556 |
|
557 |
/** |
558 |
* Returns the value of a flag indicating whether or not input is going |
559 |
* to be done for this connection. This default to true unless the |
560 |
* doOutput flag is set to false, in which case this defaults to false. |
561 |
* |
562 |
* @return true if input is to be done, false otherwise |
563 |
*/ |
564 |
public boolean getDoInput() |
565 |
{ |
566 |
return doInput; |
567 |
} |
568 |
|
569 |
/** |
570 |
* Returns a boolean flag indicating whether or not output will be done |
571 |
* on this connection. The default value is false, so this method can |
572 |
* be used to override the default |
573 |
* |
574 |
* @param output ture if output is to be done, false otherwise |
575 |
* |
576 |
* @exception IllegalStateException If already connected |
577 |
*/ |
578 |
public void setDoOutput(boolean output) |
579 |
{ |
580 |
if (connected) |
581 |
throw new IllegalStateException ("Already connected"); |
582 |
|
583 |
doOutput = output; |
584 |
} |
585 |
|
586 |
/** |
587 |
* Returns a boolean flag indicating whether or not output will be done |
588 |
* on this connection. This defaults to false. |
589 |
* |
590 |
* @return true if output is to be done, false otherwise |
591 |
*/ |
592 |
public boolean getDoOutput() |
593 |
{ |
594 |
return doOutput; |
595 |
} |
596 |
|
597 |
/** |
598 |
* Sets a boolean flag indicating whether or not user interaction is |
599 |
* allowed for this connection. (For example, in order to prompt for |
600 |
* username and password info. |
601 |
* |
602 |
* @param allow true if user interaction should be allowed, false otherwise. |
603 |
* |
604 |
* @exception IllegalStateException If already connected |
605 |
*/ |
606 |
public void setAllowUserInteraction(boolean allow) |
607 |
{ |
608 |
allowUserInteraction = allow; |
609 |
} |
610 |
|
611 |
/** |
612 |
* Returns a boolean flag indicating whether or not user interaction is |
613 |
* allowed for this connection. (For example, in order to prompt for |
614 |
* username and password info. |
615 |
* |
616 |
* @return true if user interaction is allowed, false otherwise |
617 |
*/ |
618 |
public boolean getAllowUserInteraction() |
619 |
{ |
620 |
return allowUserInteraction; |
621 |
} |
622 |
|
623 |
/** |
624 |
* Sets the default flag for whether or not interaction with a user |
625 |
* is allowed. This will be used for all connections unless overridden |
626 |
* |
627 |
* @param allow true to allow user interaction, false otherwise |
628 |
*/ |
629 |
public static void setDefaultAllowUserInteraction(boolean allow) |
630 |
{ |
631 |
defaultAllowUserInteraction = allow; |
632 |
} |
633 |
|
634 |
/** |
635 |
* Returns the default flag for whether or not interaction with a user |
636 |
* is allowed. This will be used for all connections unless overridden |
637 |
* |
638 |
* @return true if user interaction is allowed, false otherwise |
639 |
*/ |
640 |
public static boolean getDefaultAllowUserInteraction() |
641 |
{ |
642 |
return defaultAllowUserInteraction; |
643 |
} |
644 |
|
645 |
/** |
646 |
* Sets a boolean flag indicating whether or not caching will be used |
647 |
* (if possible) to store data downloaded via the connection. |
648 |
* |
649 |
* @param usecaches The new value |
650 |
* |
651 |
* @exception IllegalStateException If already connected |
652 |
*/ |
653 |
public void setUseCaches(boolean usecaches) |
654 |
{ |
655 |
if (connected) |
656 |
throw new IllegalStateException ("Already connected"); |
657 |
|
658 |
useCaches = usecaches; |
659 |
} |
660 |
|
661 |
/** |
662 |
* Returns a boolean flag indicating whether or not caching will be used |
663 |
* (if possible) to store data downloaded via the connection. |
664 |
* |
665 |
* @return true if caching should be used if possible, false otherwise |
666 |
*/ |
667 |
public boolean getUseCaches() |
668 |
{ |
669 |
return useCaches; |
670 |
} |
671 |
|
672 |
/** |
673 |
* Sets the ifModified since instance variable. If this value is non |
674 |
* zero and the underlying protocol supports it, the actual document will |
675 |
* not be fetched unless it has been modified since this time. The value |
676 |
* passed should be 0 if this feature is to be disabled or the time expressed |
677 |
* as the number of seconds since midnight 1/1/1970 GMT otherwise. |
678 |
* |
679 |
* @param ifmodifiedsince The new value in milliseconds |
680 |
* since January 1, 1970 GMT |
681 |
* |
682 |
* @exception IllegalStateException If already connected |
683 |
*/ |
684 |
public void setIfModifiedSince(long ifmodifiedsince) |
685 |
{ |
686 |
if (connected) |
687 |
throw new IllegalStateException ("Already connected"); |
688 |
|
689 |
ifModifiedSince = ifmodifiedsince; |
690 |
} |
691 |
|
692 |
/** |
693 |
* Returns the ifModified since instance variable. If this value is non |
694 |
* zero and the underlying protocol supports it, the actual document will |
695 |
* not be fetched unless it has been modified since this time. The value |
696 |
* returned will be 0 if this feature is disabled or the time expressed |
697 |
* as the number of seconds since midnight 1/1/1970 GMT otherwise |
698 |
* |
699 |
* @return The ifModifiedSince value |
700 |
*/ |
701 |
public long getIfModifiedSince() |
702 |
{ |
703 |
return ifModifiedSince; |
704 |
} |
705 |
|
706 |
/** |
707 |
* Returns the default value used to determine whether or not caching |
708 |
* of documents will be done when possible. |
709 |
* |
710 |
* @return true if caches will be used, false otherwise |
711 |
*/ |
712 |
public boolean getDefaultUseCaches() |
713 |
{ |
714 |
return defaultUseCaches; |
715 |
} |
716 |
|
717 |
/** |
718 |
* Sets the default value used to determine whether or not caching |
719 |
* of documents will be done when possible. |
720 |
* |
721 |
* @param use true to use caches if possible by default, false otherwise |
722 |
*/ |
723 |
public void setDefaultUseCaches(boolean defaultusecaches) |
724 |
{ |
725 |
defaultUseCaches = defaultusecaches; |
726 |
} |
727 |
|
728 |
/** |
729 |
* Sets the value of the named request property |
730 |
* |
731 |
* @param key The name of the property |
732 |
* @param value The value of the property |
733 |
* |
734 |
* @see URLConnection#getRequestProperty(String key) |
735 |
* @see URLConnection#setRequestProperty(String key, String value) |
736 |
* |
737 |
* @since 1.4 |
738 |
*/ |
739 |
public void setRequestProperty(String key, String value) |
740 |
{ |
741 |
req_props.put(key.toLowerCase(), value); |
742 |
} |
743 |
|
744 |
/** |
745 |
* Sets the value of the named request property |
746 |
* |
747 |
* @param key Key of the property to add |
748 |
* @param value Value of the Property to add |
749 |
* |
750 |
* @exception IllegalStateException If already connected |
751 |
* @exception NullPointerException If key is null |
752 |
* |
753 |
* @see URLConnection#getRequestProperty(String key) |
754 |
* @see URLConnection#setRequestProperty(String key, String value) |
755 |
* |
756 |
* @since 1.4 |
757 |
*/ |
758 |
public void addRequestProperty(String key, String value) |
759 |
{ |
760 |
if (connected) |
761 |
throw new IllegalStateException ("Already connected"); |
762 |
|
763 |
if (getRequestProperty (key) == null) |
764 |
{ |
765 |
setRequestProperty (key, value); |
766 |
} |
767 |
} |
768 |
|
769 |
/** |
770 |
* Returns the value of the named request property. |
771 |
* |
772 |
* @param key The name of the property |
773 |
* |
774 |
* @return Value of the property |
775 |
* |
776 |
* @exception IllegalStateException If already connected |
777 |
* |
778 |
* @see URLConnection#setRequestProperty(String key, String value) |
779 |
* @see URLConnection#addRequestProperty(String key, String value) |
780 |
*/ |
781 |
public String getRequestProperty(String key) |
782 |
{ |
783 |
return((String)req_props.get(key.toLowerCase())); |
784 |
} |
785 |
|
786 |
/** |
787 |
* Returns an unmodifiable Map containing the request properties. |
788 |
* |
789 |
* @return The map of properties |
790 |
* |
791 |
* @exception IllegalStateException If already connected |
792 |
* |
793 |
* @since 1.4 |
794 |
*/ |
795 |
public Map getRequestProperties() |
796 |
{ |
797 |
return Collections.unmodifiableMap(req_props); |
798 |
} |
799 |
|
800 |
/** |
801 |
* Sets the default value of a request property. This will be used |
802 |
* for all connections unless the value of the property is manually |
803 |
* overridden. |
804 |
* |
805 |
* @param key The request property name the default is being set for |
806 |
* @param value The value to set the default to |
807 |
* |
808 |
* @deprecated 1.3 The method setRequestProperty should be used instead |
809 |
* |
810 |
* @see URLConnectionr#setRequestProperty(String key, String value) |
811 |
*/ |
812 |
public static synchronized void setDefaultRequestProperty (String key, |
813 |
String value) |
814 |
{ |
815 |
def_req_props.put(key.toLowerCase(), value); |
816 |
} |
817 |
|
818 |
/** |
819 |
* Returns the default value of a request property. This will be used |
820 |
* for all connections unless the value of the property is manually |
821 |
* overridden. |
822 |
* |
823 |
* @param key The request property to return the default value of |
824 |
* |
825 |
* @return The value of the default property or null if not available |
826 |
* |
827 |
* @deprecated 1.3 The method getRequestProperty should be used instead |
828 |
* |
829 |
* @see URLConnection#getRequestProperty(String key) |
830 |
*/ |
831 |
public static String getDefaultRequestProperty(String key) |
832 |
{ |
833 |
return((String)def_req_props.get(key.toLowerCase())); |
834 |
} |
835 |
|
836 |
/** |
837 |
* Set's the ContentHandlerFactory for an application. This can be called |
838 |
* once and only once. If it is called again, then an Error is thrown. |
839 |
* Unlike for other set factory methods, this one does not do a security |
840 |
* check prior to setting the factory. |
841 |
* |
842 |
* @param factory The ContentHandlerFactory for this application |
843 |
* |
844 |
* @exception Error If the factory has already been defined |
845 |
* @exception SecurityException If a security manager exists and its |
846 |
* checkSetFactory method doesn't allow the operation |
847 |
*/ |
848 |
public static synchronized void setContentHandlerFactory |
849 |
(ContentHandlerFactory fac) |
850 |
{ |
851 |
if (factory != null) |
852 |
throw new Error("ContentHandlerFactory already set"); |
853 |
|
854 |
// Throw an exception if an extant security mgr precludes |
855 |
// setting the factory. |
856 |
SecurityManager s = System.getSecurityManager(); |
857 |
if (s != null) |
858 |
s.checkSetFactory(); |
859 |
|
860 |
factory = fac; |
861 |
} |
862 |
|
863 |
/** |
864 |
* Returns the MIME type of a file based on the name of the file. This |
865 |
* works by searching for the file's extension in a list of file extensions |
866 |
* and returning the MIME type associated with it. If no type is found, |
867 |
* then a MIME type of "application/octet-stream" will be returned. |
868 |
* |
869 |
* @param filename The filename to determine the MIME type for |
870 |
* |
871 |
* @return The MIME type String |
872 |
* |
873 |
* @specnote public since JDK 1.4 |
874 |
*/ |
875 |
public static String guessContentTypeFromName(String filename) |
876 |
{ |
877 |
return(fileNameMap.getContentTypeFor(filename.toLowerCase())); |
878 |
} |
879 |
|
880 |
/** |
881 |
* Returns the MIME type of a stream based on the first few characters |
882 |
* at the beginning of the stream. This routine can be used to determine |
883 |
* the MIME type if a server is believed to be returning an incorrect |
884 |
* MIME type. This method returns "application/octet-stream" if it |
885 |
* cannot determine the MIME type. |
886 |
* <p> |
887 |
* NOTE: Overriding MIME types sent from the server can be obnoxious |
888 |
* to user's. See Internet Exploder 4 if you don't believe me. |
889 |
* |
890 |
* @param is The InputStream to determine the MIME type from |
891 |
* |
892 |
* @return The MIME type |
893 |
* |
894 |
* @exception IOException If an error occurs |
895 |
*/ |
896 |
public static String guessContentTypeFromStream(InputStream is) |
897 |
throws IOException |
898 |
{ |
899 |
return("application/octet-stream"); |
900 |
} |
901 |
|
902 |
/** |
903 |
* This method returns the <code>FileNameMap</code> object being used |
904 |
* to decode MIME types by file extension. |
905 |
* |
906 |
* @return The <code>FileNameMap</code>. |
907 |
* |
908 |
* @since 1.2 |
909 |
*/ |
910 |
public static FileNameMap getFileNameMap() |
911 |
{ |
912 |
return fileNameMap; |
913 |
} |
914 |
|
915 |
/** |
916 |
* This method set the <code>FileNameMap</code> object being used |
917 |
* to decode MIME types by file extension. |
918 |
* |
919 |
* @param map The <code>FileNameMap</code>. |
920 |
* |
921 |
* @exception SecurityException If a security manager exists and its |
922 |
* checkSetFactory method doesn't allow the operation |
923 |
* |
924 |
* @since 1.2 |
925 |
*/ |
926 |
public static void setFileNameMap(FileNameMap map) |
927 |
{ |
928 |
// Throw an exception if an extant security mgr precludes |
929 |
// setting the factory. |
930 |
SecurityManager s = System.getSecurityManager(); |
931 |
if (s != null) |
932 |
s.checkSetFactory(); |
933 |
|
934 |
fileNameMap = map; |
935 |
} |
936 |
} // class URLConnection |
937 |
|