home *** CD-ROM | disk | FTP | other *** search
/ Print Shop Ensemble 3 / the-print-shop-ensemble-iii.iso / worldnet / disk2 / java.z / MOZ2_01.ZIP / netscape / net / URLConnection.class (.txt) next >
Encoding:
Java Class File  |  1996-03-08  |  4.0 KB  |  163 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.UnknownServiceException;
  8. import java.util.Enumeration;
  9. import java.util.Hashtable;
  10. import netscape.applet.AppletSecurity;
  11.  
  12. public class URLConnection extends java.net.URLConnection {
  13.    static final String EOL = "\r\n";
  14.    int pStreamData;
  15.    URLInputStream currentInputStream;
  16.    URLOutputStream currentOutputStream;
  17.    String postHeaders;
  18.    Hashtable properties;
  19.    static Hashtable defaultProperties;
  20.  
  21.    protected URLConnection(URL url) {
  22.       super(url);
  23.       this.pCreate(url.toExternalForm(), url.getHostAddress());
  24.    }
  25.  
  26.    private native void pCreate(String var1, String var2);
  27.  
  28.    protected native void finalize();
  29.  
  30.    public void connect() throws IOException {
  31.       if (!super.connected) {
  32.          SecurityManager security = System.getSecurityManager();
  33.          if (security != null && security instanceof AppletSecurity) {
  34.             ((AppletSecurity)security).checkURLConnect(super.url);
  35.          }
  36.  
  37.          super.connected = true;
  38.          StringBuffer propString = new StringBuffer();
  39.          if (this.postHeaders != null) {
  40.             propString.append(this.postHeaders);
  41.          }
  42.  
  43.          boolean contentTypeSet = false;
  44.          Hashtable props = this.properties;
  45.          if (props == null) {
  46.             props = defaultProperties;
  47.          }
  48.  
  49.          if (props != null) {
  50.             Enumeration keys = props.keys();
  51.  
  52.             while(keys.hasMoreElements()) {
  53.                String key = (String)keys.nextElement();
  54.                if (!key.equalsIgnoreCase("Content-length")) {
  55.                   if (key.equalsIgnoreCase("Content-type")) {
  56.                      contentTypeSet = true;
  57.                   }
  58.  
  59.                   String value = (String)props.get(key);
  60.                   propString.append(key);
  61.                   propString.append(":");
  62.                   propString.append(value);
  63.                   propString.append("\r\n");
  64.                }
  65.             }
  66.          }
  67.  
  68.          if (!contentTypeSet) {
  69.             propString.append("Content-type: multipart/form-data");
  70.             propString.append("\r\n");
  71.          }
  72.  
  73.          this.postHeaders = propString.toString();
  74.          this.properties = null;
  75.          if (this.currentOutputStream != null) {
  76.             this.currentOutputStream.close();
  77.          }
  78.  
  79.          URLInputStream in = (URLInputStream)this.getInputStream();
  80.          in.open();
  81.       }
  82.    }
  83.  
  84.    public native int getContentLength();
  85.  
  86.    public native String getContentType();
  87.  
  88.    public native String getHeaderField(String var1);
  89.  
  90.    public InputStream getInputStream() throws IOException {
  91.       if (!super.connected) {
  92.          this.connect();
  93.       }
  94.  
  95.       if (!super.doInput) {
  96.          throw new UnknownServiceException("protocol doesn't support input");
  97.       } else {
  98.          if (this.currentInputStream == null) {
  99.             this.currentInputStream = new URLInputStream(this);
  100.          }
  101.  
  102.          return this.currentInputStream;
  103.       }
  104.    }
  105.  
  106.    public OutputStream getOutputStream() throws IOException {
  107.       if (super.connected) {
  108.          throw new IllegalAccessError("Already connected");
  109.       } else if (!super.doOutput) {
  110.          throw new UnknownServiceException("protocol doesn't support output");
  111.       } else {
  112.          if (this.currentOutputStream == null) {
  113.             this.currentOutputStream = new URLOutputStream(this);
  114.             this.currentOutputStream.open();
  115.          }
  116.  
  117.          return this.currentOutputStream;
  118.       }
  119.    }
  120.  
  121.    public void setRequestProperty(String key, String value) {
  122.       if (super.connected) {
  123.          throw new IllegalAccessError("Already connected");
  124.       } else {
  125.          if (this.properties == null) {
  126.             this.properties = new Hashtable();
  127.          }
  128.  
  129.          if (value != null) {
  130.             this.properties.put(key, value);
  131.          } else {
  132.             this.properties.remove(key);
  133.          }
  134.       }
  135.    }
  136.  
  137.    public String getRequestProperty(String key) {
  138.       if (super.connected) {
  139.          throw new IllegalAccessError("Already connected");
  140.       } else {
  141.          return this.properties == null ? null : (String)this.properties.get(key);
  142.       }
  143.    }
  144.  
  145.    public static void setDefaultRequestProperty(String key, String value) {
  146.       if (defaultProperties == null) {
  147.          defaultProperties = new Hashtable();
  148.       }
  149.  
  150.       if (value != null) {
  151.          defaultProperties.put(key, value);
  152.       } else {
  153.          defaultProperties.remove(key);
  154.       }
  155.    }
  156.  
  157.    public static String getDefaultRequestProperty(String key) {
  158.       return defaultProperties == null ? null : (String)defaultProperties.get(key);
  159.    }
  160.  
  161.    native void close() throws IOException;
  162. }
  163.