home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 25 / CDROM25.iso / Share / prog / VJ11 / VJTRIAL.EXE / IE30Java.exe / classd.exe / sun / net / www / URLConnection.java < prev    next >
Encoding:
Java Source  |  1997-01-27  |  5.8 KB  |  197 lines

  1. /*
  2.  * @(#)URLConnection.java    1.16 95/12/06
  3.  * 
  4.  * Copyright (c) 1995 Sun Microsystems, Inc.  All Rights reserved Permission to
  5.  * use, copy, modify, and distribute this software and its documentation for
  6.  * NON-COMMERCIAL purposes and without fee is hereby granted provided that
  7.  * this copyright notice appears in all copies. Please refer to the file
  8.  * copyright.html for further important copyright and licensing information.
  9.  * 
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  11.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  12.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
  13.  * OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
  14.  * LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR
  15.  * ITS DERIVATIVES.
  16.  */
  17.  
  18. package sun.net.www;
  19.  
  20. import java.net.URL;
  21. import java.net.ContentHandler;
  22. import java.util.*;
  23. import java.io.InputStream;
  24. import java.io.OutputStream;
  25. import java.io.BufferedInputStream;
  26. import java.net.UnknownServiceException;
  27.  
  28. /**
  29.  * A class to represent an active connection to an object
  30.  * represented by a URL.
  31.  * @author  James Gosling
  32.  */
  33.  
  34. abstract public class URLConnection extends java.net.URLConnection {
  35.  
  36.     /** The URL that it is connected to */
  37.  
  38.     private String contentType;
  39.     private int contentLength = -1;
  40.  
  41.     private MessageHeader properties;
  42.  
  43.  
  44.     /** Create a URLConnection object.  These should not be created directly:
  45.     instead they should be created by protocol handers in response to
  46.     URL.openConnection.
  47.     @param    u    The URL that this connects to.
  48.      */
  49.     public URLConnection (URL u) {
  50.     super(u);
  51.     }
  52.  
  53.     /** Call this routine to get the property list for this object.
  54.      * Properties (like content-type) that have explicit getXX() methods
  55.      * associated with them should be accessed using those methods.  */
  56.     public MessageHeader getProperties() {
  57.     return properties;
  58.     }
  59.  
  60.     /** Call this routine to set the property list for this object. */
  61.     public void setProperties(MessageHeader properties) {
  62.     this.properties = properties;
  63.     }
  64.  
  65.     public String getHeaderField(String name) {
  66.     try {
  67.         getInputStream();
  68.     } catch (Exception e) {
  69.         return null;
  70.     }
  71.     MessageHeader props = properties;
  72.     return props == null ? null : props.findValue(name);
  73.     }
  74.  
  75.     /**
  76.      * Return the key for the nth header field. Returns null if
  77.      * there are fewer than n fields.  This can be used to iterate
  78.      * through all the headers in the message.
  79.      */
  80.     public String GetHeaderFieldKey(int n) {
  81.     try {
  82.         getInputStream();
  83.     } catch (Exception e) {
  84.         return null;
  85.     }
  86.     MessageHeader props = properties;
  87.     return props == null ? null : props.getKey(n);
  88.     }
  89.  
  90.     /**
  91.      * Return the value for the nth header field. Returns null if
  92.      * there are fewer than n fields.  This can be used in conjunction
  93.      * with GetHeaderFieldKey to iterate through all the headers in the message.
  94.      */
  95.     public String GetHeaderField(int n) {
  96.     try {
  97.         getInputStream();
  98.     } catch (Exception e) {
  99.         return null;
  100.     }
  101.     MessageHeader props = properties;
  102.     return props == null ? null : props.getValue(n);
  103.     }
  104.  
  105.     /** Call this routine to get the content-type associated with this
  106.      * object.
  107.      */
  108.     public String getContentType() {
  109.     if (contentType == null)
  110.         contentType = getHeaderField("content-type");
  111.     if (contentType == null) {
  112.         String ct = null;
  113.         try {
  114.         ct = guessContentTypeFromStream(getInputStream());
  115.         } catch(java.io.IOException e) {
  116.         }
  117.         String ce = properties.findValue("content-encoding");
  118.         if (ct == null) {
  119.         ct = properties.findValue("content-type");
  120.  
  121.         if (ct == null)
  122.             if (url.getFile().endsWith("/"))
  123.             ct = "text/html";
  124.             else
  125.             ct = guessContentTypeFromName(url.getFile());
  126.         }
  127.  
  128.         /*
  129.          * If the Mime header had a Content-encoding field and its value
  130.          * was not one of the values that essentially indicate no
  131.          * encoding, we force the content type to be unknown. This will
  132.          * cause a save dialog to be presented to the user.  It is not
  133.          * ideal but is better than what we were previously doing, namely
  134.          * bringing up an image tool for compressed tar files.
  135.          */
  136.  
  137.         if (ct == null || ce != null &&
  138.             !(ce.equalsIgnoreCase("7bit")
  139.               || ce.equalsIgnoreCase("8bit")
  140.               || ce.equalsIgnoreCase("binary")))
  141.         ct = "content/unknown";
  142.         contentType = ct;
  143.     }
  144.     return contentType;
  145.     }
  146.  
  147.     /**
  148.      * Set the content type of this URL to a specific value.
  149.      * @param    type    The content type to use.  One of the
  150.      *            content_* static variables in this
  151.      *            class should be used.
  152.      *            eg. setType(URL.content_html);
  153.      */
  154.     public void setContentType(String type) {
  155.     contentType = type;
  156.     }
  157.  
  158.     /** Call this routine to get the content-length associated with this
  159.      * object.
  160.      */
  161.     public int getContentLength() {
  162.     int l = contentLength;
  163.     if (l < 0) {
  164.         try {
  165.                 getInputStream();
  166.         l = Integer.parseInt(properties.findValue("content-length"));
  167.         contentLength = l;
  168.         } catch(Exception e) {
  169.         }
  170.     }
  171.     return l;
  172.     }
  173.  
  174.     /** Call this routine to set the content-length associated with this
  175.      * object.
  176.      */
  177.     protected void setContentLength(int length) {
  178.     contentLength = length;
  179.     }
  180.  
  181.     /**
  182.      * Returns true if the data associated with this URL can be cached.
  183.      */
  184.     public boolean canCache() {
  185.     return url.getFile().indexOf('?') < 0    /* && url.postData == null
  186.             REMIND */ ;
  187.     }
  188.  
  189.     /**
  190.      * Call this to close the connection and flush any remaining data.
  191.      * Overriders must remember to call super.close()
  192.      */
  193.     public void close() {
  194.     url = null;
  195.     }
  196. }
  197.