home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 January / PCO0198.ISO / 1&1 / java.z / java_301 / netscape / net / URLConnection.class (.txt) next >
Encoding:
Java Class File  |  1996-10-20  |  4.7 KB  |  214 lines

  1. package netscape.net;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.OutputStream;
  6. import java.net.URL;
  7. import java.net.UnknownHostException;
  8. import java.net.UnknownServiceException;
  9. import java.util.Enumeration;
  10. import java.util.Hashtable;
  11. import netscape.applet.AppletSecurity;
  12.  
  13. public class URLConnection extends java.net.URLConnection {
  14.    static final String EOL = "\r\n";
  15.    int pStreamData;
  16.    URLInputStream currentInputStream;
  17.    URLOutputStream currentOutputStream;
  18.    String postHeaders;
  19.    Hashtable properties;
  20.    static Hashtable defaultProperties;
  21.  
  22.    protected URLConnection(URL url) {
  23.       super(url);
  24.       String hostAddress = null;
  25.  
  26.       try {
  27.          hostAddress = url.getHostAddress();
  28.       } catch (UnknownHostException var3) {
  29.       } catch (SecurityException var4) {
  30.       }
  31.  
  32.       this.pCreate(url.toExternalForm(), hostAddress);
  33.    }
  34.  
  35.    private native void pCreate(String var1, String var2);
  36.  
  37.    protected native void finalize();
  38.  
  39.    public void connect() throws IOException {
  40.       if (!super.connected) {
  41.          SecurityManager security = System.getSecurityManager();
  42.          if (security != null && security instanceof AppletSecurity) {
  43.             ((AppletSecurity)security).checkURLConnect(super.url);
  44.          }
  45.  
  46.          super.connected = true;
  47.          StringBuffer propString = new StringBuffer();
  48.          if (this.postHeaders != null) {
  49.             propString.append(this.postHeaders);
  50.          }
  51.  
  52.          boolean contentTypeSet = false;
  53.          Hashtable props = this.properties;
  54.          if (props == null) {
  55.             props = defaultProperties;
  56.          }
  57.  
  58.          if (props != null) {
  59.             Enumeration keys = props.keys();
  60.  
  61.             while(keys.hasMoreElements()) {
  62.                String key = (String)keys.nextElement();
  63.                if (!key.equalsIgnoreCase("Content-length")) {
  64.                   if (key.equalsIgnoreCase("Content-type")) {
  65.                      contentTypeSet = true;
  66.                   }
  67.  
  68.                   String value = (String)props.get(key);
  69.                   propString.append(key);
  70.                   propString.append(":");
  71.                   propString.append(value);
  72.                   propString.append("\r\n");
  73.                }
  74.             }
  75.          }
  76.  
  77.          if (!contentTypeSet) {
  78.             propString.append("Content-type: multipart/form-data");
  79.             propString.append("\r\n");
  80.          }
  81.  
  82.          this.postHeaders = propString.toString();
  83.          this.properties = null;
  84.          if (this.currentOutputStream != null) {
  85.             this.currentOutputStream.close();
  86.          }
  87.  
  88.          URLInputStream in = (URLInputStream)this.getInputStream();
  89.          in.open();
  90.       }
  91.    }
  92.  
  93.    public int getContentLength() {
  94.       try {
  95.          this.getInputStream();
  96.       } catch (Exception var1) {
  97.          return -1;
  98.       }
  99.  
  100.       return this.getContentLength0();
  101.    }
  102.  
  103.    public native int getContentLength0();
  104.  
  105.    public String getContentType() {
  106.       try {
  107.          this.getInputStream();
  108.       } catch (Exception var1) {
  109.          return null;
  110.       }
  111.  
  112.       return this.getContentType0();
  113.    }
  114.  
  115.    public native String getContentType0();
  116.  
  117.    public String getHeaderField(String name) {
  118.       try {
  119.          this.getInputStream();
  120.       } catch (Exception var2) {
  121.          return null;
  122.       }
  123.  
  124.       return this.getHeaderField0(name);
  125.    }
  126.  
  127.    public native String getHeaderField0(String var1);
  128.  
  129.    public String getHeaderFieldKey(int n) {
  130.       try {
  131.          this.getInputStream();
  132.       } catch (Exception var2) {
  133.          return null;
  134.       }
  135.  
  136.       return this.getHeaderFieldKey0(n);
  137.    }
  138.  
  139.    public native String getHeaderFieldKey0(int var1);
  140.  
  141.    public InputStream getInputStream() throws IOException {
  142.       if (!super.connected) {
  143.          this.connect();
  144.       }
  145.  
  146.       if (!super.doInput) {
  147.          throw new UnknownServiceException("protocol doesn't support input");
  148.       } else {
  149.          if (this.currentInputStream == null) {
  150.             this.currentInputStream = new URLInputStream(this);
  151.          }
  152.  
  153.          return this.currentInputStream;
  154.       }
  155.    }
  156.  
  157.    public OutputStream getOutputStream() throws IOException {
  158.       if (super.connected) {
  159.          throw new IllegalAccessError("Already connected");
  160.       } else if (!super.doOutput) {
  161.          throw new UnknownServiceException("protocol doesn't support output");
  162.       } else {
  163.          if (this.currentOutputStream == null) {
  164.             this.currentOutputStream = new URLOutputStream(this);
  165.             this.currentOutputStream.open();
  166.          }
  167.  
  168.          return this.currentOutputStream;
  169.       }
  170.    }
  171.  
  172.    public void setRequestProperty(String key, String value) {
  173.       if (super.connected) {
  174.          throw new IllegalAccessError("Already connected");
  175.       } else {
  176.          if (this.properties == null) {
  177.             this.properties = new Hashtable();
  178.          }
  179.  
  180.          if (value != null) {
  181.             this.properties.put(key, value);
  182.          } else {
  183.             this.properties.remove(key);
  184.          }
  185.       }
  186.    }
  187.  
  188.    public String getRequestProperty(String key) {
  189.       if (super.connected) {
  190.          throw new IllegalAccessError("Already connected");
  191.       } else {
  192.          return this.properties == null ? null : (String)this.properties.get(key);
  193.       }
  194.    }
  195.  
  196.    public static void setDefaultRequestProperty(String key, String value) {
  197.       if (defaultProperties == null) {
  198.          defaultProperties = new Hashtable();
  199.       }
  200.  
  201.       if (value != null) {
  202.          defaultProperties.put(key, value);
  203.       } else {
  204.          defaultProperties.remove(key);
  205.       }
  206.    }
  207.  
  208.    public static String getDefaultRequestProperty(String key) {
  209.       return defaultProperties == null ? null : (String)defaultProperties.get(key);
  210.    }
  211.  
  212.    native void close() throws IOException;
  213. }
  214.