35 |
obligated to do so. If you do not wish to do so, delete this |
obligated to do so. If you do not wish to do so, delete this |
36 |
exception statement from your version. */ |
exception statement from your version. */ |
37 |
|
|
38 |
|
|
39 |
package java.net; |
package java.net; |
40 |
|
|
41 |
import java.io.IOException; |
import java.io.IOException; |
50 |
import java.util.Locale; |
import java.util.Locale; |
51 |
import java.util.Map; |
import java.util.Map; |
52 |
|
|
|
|
|
53 |
/** |
/** |
54 |
* Written using on-line Java Platform 1.2 API Specification, as well |
* Written using on-line Java Platform 1.2 API Specification, as well |
55 |
* as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998). |
* as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998). |
84 |
* by the actual content handlers as described in the description of that |
* by the actual content handlers as described in the description of that |
85 |
* method. |
* method. |
86 |
* |
* |
87 |
* @author Aaron M. Renn <arenn@urbanophile.com> |
* @author Aaron M. Renn (arenn@urbanophile.com) |
88 |
* @author Warren Levy <warrenl@cygnus.com> |
* @author Warren Levy (warrenl@cygnus.com) |
89 |
*/ |
*/ |
90 |
public abstract class URLConnection |
public abstract class URLConnection |
91 |
{ |
{ |
160 |
* This is the URL associated with this connection |
* This is the URL associated with this connection |
161 |
*/ |
*/ |
162 |
protected URL url; |
protected URL url; |
|
|
|
163 |
private static SimpleDateFormat[] dateFormats; |
private static SimpleDateFormat[] dateFormats; |
164 |
private static boolean dateformats_initialized; |
private static boolean dateformats_initialized; |
165 |
|
|
166 |
/* Cached ParsePosition, used when parsing dates. */ |
/* Cached ParsePosition, used when parsing dates. */ |
167 |
private ParsePosition position; |
private ParsePosition position; |
168 |
|
|
169 |
/** |
/** |
170 |
* Creates a URL connection to a given URL. A real connection is not made. |
* Creates a URL connection to a given URL. A real connection is not made. |
171 |
* Use #connect to do this. |
* Use #connect to do this. |
362 |
{ |
{ |
363 |
if (! dateformats_initialized) |
if (! dateformats_initialized) |
364 |
initializeDateFormats(); |
initializeDateFormats(); |
365 |
|
|
366 |
if (position == null) |
if (position == null) |
367 |
position = new ParsePosition(0); |
position = new ParsePosition(0); |
368 |
|
|
408 |
* the object and particular content hander loaded. Most text type |
* the object and particular content hander loaded. Most text type |
409 |
* content handlers will return a subclass of |
* content handlers will return a subclass of |
410 |
* <code>InputStream</code>. Images usually return a class that |
* <code>InputStream</code>. Images usually return a class that |
411 |
* implements <code>ImageProducer<code>. There is not guarantee |
* implements <code>ImageProducer</code>. There is not guarantee |
412 |
* what type of object will be returned, however. |
* what type of object will be returned, however. |
413 |
|
* |
414 |
<p> |
* <p>This class first determines the MIME type of the content, then |
|
|
|
|
* This class first determines the MIME type of the content, then |
|
415 |
* creates a ContentHandler object to process the input. If the |
* creates a ContentHandler object to process the input. If the |
416 |
* <code>ContentHandlerFactory</code> is set, then that object is |
* <code>ContentHandlerFactory</code> is set, then that object is |
417 |
* called to load a content handler, otherwise a class called |
* called to load a content handler, otherwise a class called |
421 |
* <code>getInputStream()</code>. Note that the default |
* <code>getInputStream()</code>. Note that the default |
422 |
* implementation of <code>getInputStream()</code> throws a |
* implementation of <code>getInputStream()</code> throws a |
423 |
* <code>UnknownServiceException</code> so subclasses are encouraged |
* <code>UnknownServiceException</code> so subclasses are encouraged |
424 |
* to override this method. |
* to override this method.</p> |
425 |
* |
* |
426 |
* @exception IOException If an error with the connection occurs. |
* @exception IOException If an error with the connection occurs. |
427 |
* @exception UnknownServiceException If the protocol does not support the |
* @exception UnknownServiceException If the protocol does not support the |
429 |
*/ |
*/ |
430 |
public Object getContent() throws IOException |
public Object getContent() throws IOException |
431 |
{ |
{ |
432 |
// connect(); |
if (!connected) |
433 |
String type = getContentType(); |
connect(); |
434 |
|
|
435 |
// First try the factory |
// FIXME: Doc indicates that other criteria should be applied as |
436 |
ContentHandler ch = null; |
// heuristics to determine the true content type, e.g. see |
437 |
|
// guessContentTypeFromName() and guessContentTypeFromStream methods |
438 |
if (factory != null) |
// as well as FileNameMap class & fileNameMap field & get/set methods. |
439 |
ch = factory.createContentHandler(type); |
String type = getContentType(); |
440 |
|
ContentHandler ch = getContentHandler(type); |
441 |
|
|
442 |
if (ch != null) |
if (ch != null) |
443 |
return ch.getContent(this); |
return ch.getContent(this); |
444 |
|
|
|
// Then try our default class |
|
|
try |
|
|
{ |
|
|
String typeClass = type.replace('/', '.'); |
|
|
|
|
|
// deal with "Content-Type: text/html; charset=ISO-8859-1" |
|
|
int parameterBegin = typeClass.indexOf(';'); |
|
|
if (parameterBegin >= 1) |
|
|
typeClass = typeClass.substring(0, parameterBegin); |
|
|
|
|
|
Class cls = Class.forName("gnu.java.net.content." + typeClass); |
|
|
|
|
|
Object obj = cls.newInstance(); |
|
|
|
|
|
if (obj instanceof ContentHandler) |
|
|
{ |
|
|
ch = (ContentHandler) obj; |
|
|
return ch.getContent(this); |
|
|
} |
|
|
} |
|
|
catch (ClassNotFoundException e) |
|
|
{ |
|
|
} |
|
|
catch (InstantiationException e) |
|
|
{ |
|
|
} |
|
|
catch (IllegalAccessException e) |
|
|
{ |
|
|
} |
|
|
|
|
445 |
return getInputStream(); |
return getInputStream(); |
446 |
} |
} |
447 |
|
|
908 |
public static String guessContentTypeFromStream(InputStream is) |
public static String guessContentTypeFromStream(InputStream is) |
909 |
throws IOException |
throws IOException |
910 |
{ |
{ |
911 |
return ("application/octet-stream"); |
return "application/octet-stream"; |
912 |
} |
} |
913 |
|
|
914 |
/** |
/** |
941 |
*/ |
*/ |
942 |
public static void setFileNameMap(FileNameMap map) |
public static void setFileNameMap(FileNameMap map) |
943 |
{ |
{ |
944 |
// Throw an exception if an extant security mgr precludes |
// Throw an exception if an extant security manager precludes |
945 |
// setting the factory. |
// setting the factory. |
946 |
SecurityManager s = System.getSecurityManager(); |
SecurityManager s = System.getSecurityManager(); |
947 |
if (s != null) |
if (s != null) |
950 |
fileNameMap = map; |
fileNameMap = map; |
951 |
} |
} |
952 |
|
|
953 |
|
private ContentHandler getContentHandler(String contentType) |
954 |
|
{ |
955 |
|
// No content type so just handle it as the default. |
956 |
|
if (contentType == null || contentType.equals("")) |
957 |
|
return null; |
958 |
|
|
959 |
|
ContentHandler handler = null; |
960 |
|
|
961 |
|
// If a non-default factory has been set, use it. |
962 |
|
if (factory != null) |
963 |
|
handler = factory.createContentHandler(contentType); |
964 |
|
|
965 |
|
// Then try our default class. |
966 |
|
try |
967 |
|
{ |
968 |
|
String typeClass = contentType.replace('/', '.'); |
969 |
|
|
970 |
|
// Deal with "Content-Type: text/html; charset=ISO-8859-1". |
971 |
|
int parameterBegin = typeClass.indexOf(';'); |
972 |
|
if (parameterBegin >= 1) |
973 |
|
typeClass = typeClass.substring(0, parameterBegin); |
974 |
|
|
975 |
|
Class cls = Class.forName("gnu.java.net.content." + typeClass); |
976 |
|
Object obj = cls.newInstance(); |
977 |
|
|
978 |
|
if (obj instanceof ContentHandler) |
979 |
|
{ |
980 |
|
handler = (ContentHandler) obj; |
981 |
|
return handler; |
982 |
|
} |
983 |
|
} |
984 |
|
catch (ClassNotFoundException e) |
985 |
|
{ |
986 |
|
// Ignore. |
987 |
|
} |
988 |
|
catch (InstantiationException e) |
989 |
|
{ |
990 |
|
// Ignore. |
991 |
|
} |
992 |
|
catch (IllegalAccessException e) |
993 |
|
{ |
994 |
|
// Ignore. |
995 |
|
} |
996 |
|
|
997 |
|
return handler; |
998 |
|
} |
999 |
|
|
1000 |
// We don't put these in a static initializer, because it creates problems |
// We don't put these in a static initializer, because it creates problems |
1001 |
// with initializer co-dependency: SimpleDateFormat's constructors eventually |
// with initializer co-dependency: SimpleDateFormat's constructors eventually |
1002 |
// depend on URLConnection (via the java.text.*Symbols classes). |
// depend on URLConnection (via the java.text.*Symbols classes). |