| 1 | // URLConnection.java - Superclass of all communications links between
|
|---|
| 2 | // an application and a URL.
|
|---|
| 3 |
|
|---|
| 4 | /* Copyright (C) 1999, 2000 Free Software Foundation
|
|---|
| 5 |
|
|---|
| 6 | This file is part of libgcj.
|
|---|
| 7 |
|
|---|
| 8 | This software is copyrighted work licensed under the terms of the
|
|---|
| 9 | Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
|
|---|
| 10 | details. */
|
|---|
| 11 |
|
|---|
| 12 | package java.net;
|
|---|
| 13 |
|
|---|
| 14 | import java.io.*;
|
|---|
| 15 | import java.text.ParsePosition;
|
|---|
| 16 | import java.text.SimpleDateFormat;
|
|---|
| 17 | import java.util.Date;
|
|---|
| 18 | import java.util.Locale;
|
|---|
| 19 | import java.util.Hashtable;
|
|---|
| 20 | import java.util.Map;
|
|---|
| 21 | import java.util.StringTokenizer;
|
|---|
| 22 | import java.security.Permission;
|
|---|
| 23 | import java.security.AllPermission;
|
|---|
| 24 | import gnu.gcj.io.MimeTypes;
|
|---|
| 25 |
|
|---|
| 26 | /**
|
|---|
| 27 | * @author Warren Levy <[email protected]>
|
|---|
| 28 | * @date March 5, 1999.
|
|---|
| 29 | */
|
|---|
| 30 |
|
|---|
| 31 | /**
|
|---|
| 32 | * Written using on-line Java Platform 1.2 API Specification, as well
|
|---|
| 33 | * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998).
|
|---|
| 34 | * Status: One guessContentTypeFrom... methods not implemented.
|
|---|
| 35 | * getContent method assumes content type from response; see comment there.
|
|---|
| 36 | */
|
|---|
| 37 |
|
|---|
| 38 | public abstract class URLConnection
|
|---|
| 39 | {
|
|---|
| 40 | protected URL url;
|
|---|
| 41 | protected boolean doInput = true;
|
|---|
| 42 | protected boolean doOutput = false;
|
|---|
| 43 | protected boolean allowUserInteraction;
|
|---|
| 44 | protected boolean useCaches;
|
|---|
| 45 | protected long ifModifiedSince = 0L;
|
|---|
| 46 | protected boolean connected = false;
|
|---|
| 47 | private static boolean defaultAllowUserInteraction = false;
|
|---|
| 48 | private static boolean defaultUseCaches = true;
|
|---|
| 49 | private static FileNameMap fileNameMap; // Set by the URLConnection subclass.
|
|---|
| 50 | private static ContentHandlerFactory factory;
|
|---|
| 51 | private static ContentHandler contentHandler;
|
|---|
| 52 | private static Hashtable handlers = new Hashtable();
|
|---|
| 53 | private static Locale locale;
|
|---|
| 54 | private static SimpleDateFormat dateFormat1, dateFormat2, dateFormat3;
|
|---|
| 55 | private static boolean dateformats_initialized = false;
|
|---|
| 56 |
|
|---|
| 57 | /**
|
|---|
| 58 | * Creates a URL connection to a given URL. A real connection is not made.
|
|---|
| 59 | * Use #connect to do this.
|
|---|
| 60 | *
|
|---|
| 61 | * @param url The Object to create the URL connection to
|
|---|
| 62 | *
|
|---|
| 63 | * @see URLConnection:connect
|
|---|
| 64 | */
|
|---|
| 65 | protected URLConnection(URL url)
|
|---|
| 66 | {
|
|---|
| 67 | this.url = url;
|
|---|
| 68 | allowUserInteraction = defaultAllowUserInteraction;
|
|---|
| 69 | useCaches = defaultUseCaches;
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | /**
|
|---|
| 73 | * Creates a real connection to the object references by the URL given
|
|---|
| 74 | * to the constructor
|
|---|
| 75 | *
|
|---|
| 76 | * @exception IOException If an error occurs
|
|---|
| 77 | */
|
|---|
| 78 | public abstract void connect() throws IOException;
|
|---|
| 79 |
|
|---|
| 80 | /**
|
|---|
| 81 | * Returns ths URL to the object.
|
|---|
| 82 | */
|
|---|
| 83 | public URL getURL()
|
|---|
| 84 | {
|
|---|
| 85 | return url;
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | /**
|
|---|
| 89 | * Returns the value of the content-length header field
|
|---|
| 90 | */
|
|---|
| 91 | public int getContentLength()
|
|---|
| 92 | {
|
|---|
| 93 | return getHeaderFieldInt("content-length", -1);
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | /**
|
|---|
| 97 | * Returns the value of the content-type header field
|
|---|
| 98 | */
|
|---|
| 99 | public String getContentType()
|
|---|
| 100 | {
|
|---|
| 101 | return getHeaderField("content-type");
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | /**
|
|---|
| 105 | * Returns the value of the content-encoding header field
|
|---|
| 106 | */
|
|---|
| 107 | public String getContentEncoding()
|
|---|
| 108 | {
|
|---|
| 109 | return getHeaderField("content-encoding");
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | /**
|
|---|
| 113 | * Returns the value of the expires header field
|
|---|
| 114 | */
|
|---|
| 115 | public long getExpiration()
|
|---|
| 116 | {
|
|---|
| 117 | return getHeaderFieldDate("expiration", 0L);
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | /**
|
|---|
| 121 | * Returns the value of the date header field
|
|---|
| 122 | */
|
|---|
| 123 | public long getDate()
|
|---|
| 124 | {
|
|---|
| 125 | return getHeaderFieldDate("date", 0L);
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | /**
|
|---|
| 129 | * Returns the value of the last-modified header field
|
|---|
| 130 | */
|
|---|
| 131 | public long getLastModified()
|
|---|
| 132 | {
|
|---|
| 133 | return getHeaderFieldDate("last-modified", 0L);
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | /**
|
|---|
| 137 | * Returns the value of the n-th header field
|
|---|
| 138 | *
|
|---|
| 139 | * @param num The number of the header field
|
|---|
| 140 | */
|
|---|
| 141 | public String getHeaderField(int num)
|
|---|
| 142 | {
|
|---|
| 143 | // Subclasses for specific protocols override this.
|
|---|
| 144 | return null;
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | /**
|
|---|
| 148 | * Returns the value of the header filed specified by name
|
|---|
| 149 | *
|
|---|
| 150 | * @param name The name of the header field
|
|---|
| 151 | */
|
|---|
| 152 | public String getHeaderField(String name)
|
|---|
| 153 | {
|
|---|
| 154 | // Subclasses for specific protocols override this.
|
|---|
| 155 | return null;
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | /**
|
|---|
| 159 | * Returns a map of all sent header fields
|
|---|
| 160 | *
|
|---|
| 161 | * @since 1.4
|
|---|
| 162 | */
|
|---|
| 163 | public Map getHeaderFields()
|
|---|
| 164 | {
|
|---|
| 165 | // Subclasses for specific protocols override this.
|
|---|
| 166 | return null;
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | /**
|
|---|
| 170 | * Returns the value of the header filed name as int.
|
|---|
| 171 | *
|
|---|
| 172 | * @param name The name of the header field
|
|---|
| 173 | * @param val The default value
|
|---|
| 174 | *
|
|---|
| 175 | * @return Returns the value of the header filed or the default value
|
|---|
| 176 | * if the field is missing or malformed
|
|---|
| 177 | */
|
|---|
| 178 | public int getHeaderFieldInt(String name, int val)
|
|---|
| 179 | {
|
|---|
| 180 | String str = getHeaderField(name);
|
|---|
| 181 | try
|
|---|
| 182 | {
|
|---|
| 183 | if (str != null)
|
|---|
| 184 | val = Integer.parseInt(str);
|
|---|
| 185 | }
|
|---|
| 186 | catch (NumberFormatException e)
|
|---|
| 187 | {
|
|---|
| 188 | ; // Do nothing; val is the default.
|
|---|
| 189 | }
|
|---|
| 190 | return val;
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 | /**
|
|---|
| 194 | * Returns the value of a header field parsed as date. The result is then
|
|---|
| 195 | * number of milliseconds since January 1st, 1970 GMT.
|
|---|
| 196 | *
|
|---|
| 197 | * @param name The name of the header field
|
|---|
|
|---|