aboutsummaryrefslogtreecommitdiff
path: root/libjava/java/net/URLConnection.java
blob: 12e8a8b4cc66f1750474584f80c427be0d976fbc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
// URLConnection.java - Superclass of all communications links between
//			an application and a URL.

/* Copyright (C) 1999  Cygnus Solutions

   This file is part of libgcj.

This software is copyrighted work licensed under the terms of the
Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
details.  */

package java.net;

import java.io.*;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Hashtable;
import java.util.StringTokenizer;

/**
 * @author Warren Levy <warrenl@cygnus.com>
 * @date March 5, 1999.
 */

/**
 * Written using on-line Java Platform 1.2 API Specification, as well
 * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998).
 * Status:  Two guessContentTypeFrom... methods not implemented.
 *	getContent method assumes content type from response; see comment there.
 */

public abstract class URLConnection
{
  protected URL url;
  protected boolean doInput = true;
  protected boolean doOutput = false;
  protected boolean allowUserInteraction;
  protected boolean useCaches;
  protected long ifModifiedSince = 0L;
  protected boolean connected = false;
  private static boolean defaultAllowUserInteraction = false;
  private static boolean defaultUseCaches = true;
  private static FileNameMap fileNameMap;  // Set by the URLConnection subclass.
  private static ContentHandlerFactory factory;
  private static ContentHandler contentHandler;
  private static Hashtable handlers = new Hashtable();
  private static Locale locale = new Locale("En", "Us", "Unix");
  private static SimpleDateFormat dateFormat1 =
	  new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss 'GMT'", locale);
  private static SimpleDateFormat dateFormat2 =
	  new SimpleDateFormat("EEEE, dd-MMM-yy hh:mm:ss 'GMT'", locale);
  private static SimpleDateFormat dateFormat3 =
	  new SimpleDateFormat("EEE MMM d hh:mm:ss yyyy", locale);

  protected URLConnection(URL url)
  {
    this.url = url;
    allowUserInteraction = defaultAllowUserInteraction;
    useCaches = defaultUseCaches;
  }

  public abstract void connect() throws IOException;

  public URL getURL()
  {
    return url;
  }

  public int getContentLength()
  {
    return getHeaderFieldInt("content-length", -1);
  }

  public String getContentType()
  {
    return getHeaderField("content-type");
  }

  public String getContentEncoding()
  {
    return getHeaderField("content-encoding");
  }

  public long getExpiration()
  {
    return getHeaderFieldDate("expiration", 0L);
  }

  public long getDate()
  {
    return getHeaderFieldDate("date", 0L);
  }

  public long getLastModified()
  {
    return getHeaderFieldDate("last-modified", 0L);
  }

  public String getHeaderField(int n)
  {
    // Subclasses for specific protocols override this.
    return null;
  }

  public String getHeaderField(String name)
  {
    // Subclasses for specific protocols override this.
    return null;
  }

  public int getHeaderFieldInt(String name, int val)
  {
    String str = getHeaderField(name);
    try
      {
	if (str != null)
	  val = Integer.parseInt(str);
      }
    catch (NumberFormatException e)
      {
	; // Do nothing; val is the default.
      }
    return val;
  }

  public long getHeaderFieldDate(String name, long val)
  {
    String str = getHeaderField(name);
    if (str != null)
      {
        Date date;
	if ((date = dateFormat1.parse(str, new ParsePosition(0))) != null)
	  val = date.getTime();
	else if ((date = dateFormat2.parse(str, new ParsePosition(0))) != null)
	  val = date.getTime();
	else if ((date = dateFormat3.parse(str, new ParsePosition(0))) != null)
	  val = date.getTime();
      }
    return val;
  }

  public String getHeaderFieldKey(int n)
  {
    // Subclasses for specific protocols override this.
    return null;
  }

  public Object getContent() throws IOException
  {
    // FIXME: Doc indicates that other criteria should be applied as
    // heuristics to determine the true content type, e.g. see 
    // guessContentTypeFromName() and guessContentTypeFromStream methods
    // as well as FileNameMap class & fileNameMap field & get/set methods.
    String cType = getContentType();
    contentHandler = setContentHandler(cType);
    if (contentHandler == null)
      return getInputStream();

    return contentHandler.getContent(this);
  }

// TODO12:  public Permission getPermission() throws IOException
//   {
//     // Subclasses may override this.
//     return java.security.AllPermission;
//   }

  public InputStream getInputStream() throws IOException
  {
    // Subclasses for specific protocols override this.
    throw new UnknownServiceException("Protocol " + url.getProtocol() +
			" does not support input.");
  }

  public OutputStream getOutputStream() throws IOException
  {
    // Subclasses for specific protocols override this.
    throw new UnknownServiceException("Protocol " + url.getProtocol() +
			" does not support output.");
  }

  public String toString()
  {
    return this.getClass().getName() + ":" + url.toString();
  }

  public void setDoInput(boolean doinput)
  {
    if (connected)
      throw new IllegalAccessError("Already connected");

    doInput = doinput;
  }

  public boolean getDoInput()
  {
    return doInput;
  }

  public void setDoOutput(boolean dooutput)
  {
    if (connected)
      throw new IllegalAccessError("Already connected");

    doOutput = dooutput;
    if (doOutput)
      doInput = false;
  }

  public boolean getDoOutput()
  {
    return doOutput;
  }

  public void setAllowUserInteraction(boolean allowuserinteraction)
  {
    if (connected)
      throw new IllegalAccessError("Already connected");

    allowUserInteraction = allowuserinteraction;
  }

  public boolean getAllowUserInteraction()
  {
    return allowUserInteraction;
  }

  public static void
    setDefaultAllowUserInteraction(boolean defaultallowuserinteraction)
  {
    defaultAllowUserInteraction = defaultallowuserinteraction;
  }

  public static boolean getDefaultAllowUserInteraction()
  {
    return defaultAllowUserInteraction;
  }

  public void setUseCaches(boolean usecaches)
  {
    if (connected)
      throw new IllegalAccessError("Already connected");

    useCaches = usecaches;
  }

  public boolean getUseCaches()
  {
    return useCaches;
  }

  public void setIfModifiedSince(long ifmodifiedsince)
  {
    if (connected)
      throw new IllegalAccessError("Already connected");

    ifModifiedSince = ifmodifiedsince;
  }

  public long getIfModifiedSince()
  {
    return ifModifiedSince;
  }

  public boolean getDefaultUseCaches()
  {
    return defaultUseCaches;
  }

  public void setDefaultUseCaches(boolean defaultusecaches)
  {
    defaultUseCaches = defaultusecaches;
  }

  public void setRequestProperty(String key, String value)
  {
    // Do nothing unless overridden by subclasses that support setting
    // header fields in the request.
  }

  public String getRequestProperty(String key)
  {
    // Overridden by subclasses that support reading header fields from the
    // request.
    return null;
  }

  public static void setDefaultRequestProperty(String key, String value)
  {
    // Do nothing unless overridden by subclasses that support setting
    // default request properties.
  }

  public static String getDefaultRequestProperty(String key)
  {
    // Overridden by subclasses that support default request properties.
    return null;
  }

  public static void setContentHandlerFactory(ContentHandlerFactory fac)
  {
    if (factory != null)
      throw new Error("ContentHandlerFactory already set");

    // Throw an exception if an extant security mgr precludes
    // setting the factory.
    SecurityManager s = System.getSecurityManager();
    if (s != null)
      s.checkSetFactory();
    factory = fac;
  }

// TODO:  protected static String guessContentTypeFromName(String fname)
//   {
//   }

// TODO:  public static String guessContentTypeFromStream(InputStream is)
//          throws IOException
//   {
//   }

// TODO12:  protected void parseURL(URL u, String spec, int start, int limit)

  // JDK1.2
  public static FileNameMap getFileNameMap()
  {
    return fileNameMap;
  }

  // JDK1.2
  public static void setFileNameMap(FileNameMap map)
  {
    // Throw an exception if an extant security mgr precludes
    // setting the factory.
    SecurityManager s = System.getSecurityManager();
    if (s != null)
      s.checkSetFactory();

    fileNameMap = map;
  }

  private ContentHandler setContentHandler(String contentType)
  {
    ContentHandler handler;

    // No content type so just handle it as the default.
    if (contentType == null || contentType == "")
      return null;

    // See if a handler has been cached for this content type.
    // For efficiency, if a content type has been searched for but not
    // found, it will be in the hash table but as the contentType String
    // instead of a ContentHandler.
    if ((handler = (ContentHandler) handlers.get(contentType)) != null)
      if (handler instanceof ContentHandler)
	return handler;
      else
	return null;

    // If a non-default factory has been set, use it to find the content type.
    if (factory != null)
      handler = factory.createContentHandler(contentType);

    // Non-default factory may have returned null or a factory wasn't set.
    // Use the default search algorithm to find a handler for this content type.
    if (handler == null)
      {
	// Get the list of packages to check and append our default handler
	// to it, along with the JDK specified default as a last resort.
	// Except in very unusual environments the JDK specified one shouldn't
	// ever be needed (or available).
	String propVal = System.getProperty("java.content.handler.pkgs");
	propVal = (propVal == null) ? "" : (propVal + "|");
	propVal = propVal + "gnu.gcj.content|sun.net.www.content";

	// Replace the '/' character in the content type with '.' and
	// all other non-alphabetic, non-numeric characters with '_'.
	StringTokenizer pkgPrefix = new StringTokenizer(propVal, "|");
	char[] cArray = contentType.toCharArray();
	for (int i = 0; i < cArray.length; i++)
	  {
	    if (cArray[i] == '/')
	      cArray[i] = '.';
	    else if (! ((cArray[i] >= 'A' && cArray[i] <= 'Z') || 
			(cArray[i] >= 'a' && cArray[i] <= 'z') ||
			(cArray[i] >= '0' && cArray[i] <= '9')))
	      cArray[i] = '_';
	  }
	String contentClass = new String(cArray);

	// See if a class of this content type exists in any of the packages.
	do
	  {
	    String facName = pkgPrefix.nextToken() + "." + contentClass;
	    try
	      {
		handler =
		  (ContentHandler) Class.forName(facName).newInstance();
	      }
	    catch (Exception e)
	      {
		// Can't instantiate; handler still null, go on to next element.
	      }
	  } while ((handler == null ||
		    ! (handler instanceof ContentHandler)) &&
		   pkgPrefix.hasMoreTokens());
      }

    // Update the hashtable with the new content handler.
    if (handler != null && handler instanceof ContentHandler)
      {
	handlers.put(contentType, handler);
	return handler;
      }

    // For efficiency on subsequent searches, put a dummy entry in the hash
    // table for content types that don't have a non-default ContentHandler.
    handlers.put(contentType, contentType);
    return null;
  }
}