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

  1. package java.net;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.OutputStream;
  6. import java.util.Date;
  7. import java.util.Hashtable;
  8.  
  9. public abstract class URLConnection {
  10.    protected URL url;
  11.    protected boolean doInput = true;
  12.    protected boolean doOutput = false;
  13.    private static boolean defaultAllowUserInteraction;
  14.    protected boolean allowUserInteraction;
  15.    private static boolean defaultUseCaches = true;
  16.    protected boolean useCaches;
  17.    protected long ifModifiedSince;
  18.    protected boolean connected;
  19.    static ContentHandlerFactory factory;
  20.    private static Hashtable handlers = new Hashtable();
  21.    private static ContentHandler UnknownContentHandlerP = new UnknownContentHandler();
  22.    private static String content_class_prefix = "sun.net.www.content.";
  23.    static Hashtable extension_map = new Hashtable();
  24.  
  25.    public abstract void connect() throws IOException;
  26.  
  27.    protected URLConnection(URL url) {
  28.       this.allowUserInteraction = defaultAllowUserInteraction;
  29.       this.useCaches = defaultUseCaches;
  30.       this.connected = false;
  31.       this.url = url;
  32.    }
  33.  
  34.    public URL getURL() {
  35.       return this.url;
  36.    }
  37.  
  38.    public int getContentLength() {
  39.       return this.getHeaderFieldInt("content-length", -1);
  40.    }
  41.  
  42.    public String getContentType() {
  43.       return this.getHeaderField("content-type");
  44.    }
  45.  
  46.    public String getContentEncoding() {
  47.       return this.getHeaderField("content-encoding");
  48.    }
  49.  
  50.    public long getExpiration() {
  51.       return this.getHeaderFieldDate("expires", 0L);
  52.    }
  53.  
  54.    public long getDate() {
  55.       return this.getHeaderFieldDate("date", 0L);
  56.    }
  57.  
  58.    public long getLastModified() {
  59.       return this.getHeaderFieldDate("last-modified", 0L);
  60.    }
  61.  
  62.    public String getHeaderField(String name) {
  63.       return null;
  64.    }
  65.  
  66.    public int getHeaderFieldInt(String name, int Default) {
  67.       try {
  68.          return Integer.parseInt(this.getHeaderField(name));
  69.       } catch (Throwable var3) {
  70.          return Default;
  71.       }
  72.    }
  73.  
  74.    public long getHeaderFieldDate(String name, long Default) {
  75.       try {
  76.          return Date.parse(this.getHeaderField(name));
  77.       } catch (Throwable var4) {
  78.          return Default;
  79.       }
  80.    }
  81.  
  82.    public String getHeaderFieldKey(int n) {
  83.       return null;
  84.    }
  85.  
  86.    public String getHeaderField(int n) {
  87.       return null;
  88.    }
  89.  
  90.    public Object getContent() throws IOException {
  91.       return this.getContentHandler().getContent(this);
  92.    }
  93.  
  94.    public InputStream getInputStream() throws IOException {
  95.       throw new UnknownServiceException("protocol doesn't support input");
  96.    }
  97.  
  98.    public OutputStream getOutputStream() throws IOException {
  99.       throw new UnknownServiceException("protocol doesn't support output");
  100.    }
  101.  
  102.    public String toString() {
  103.       return this.getClass().getName() + ":" + this.url;
  104.    }
  105.  
  106.    public void setDoInput(boolean doinput) {
  107.       if (this.connected) {
  108.          throw new IllegalAccessError("Already connected");
  109.       } else {
  110.          this.doInput = doinput;
  111.       }
  112.    }
  113.  
  114.    public boolean getDoInput() {
  115.       return this.doInput;
  116.    }
  117.  
  118.    public void setDoOutput(boolean dooutput) {
  119.       if (this.connected) {
  120.          throw new IllegalAccessError("Already connected");
  121.       } else {
  122.          this.doOutput = dooutput;
  123.       }
  124.    }
  125.  
  126.    public boolean getDoOutput() {
  127.       return this.doOutput;
  128.    }
  129.  
  130.    public void setAllowUserInteraction(boolean allowuserinteraction) {
  131.       if (this.connected) {
  132.          throw new IllegalAccessError("Already connected");
  133.       } else {
  134.          this.allowUserInteraction = allowuserinteraction;
  135.       }
  136.    }
  137.  
  138.    public boolean getAllowUserInteraction() {
  139.       return this.allowUserInteraction;
  140.    }
  141.  
  142.    public static void setDefaultAllowUserInteraction(boolean defaultallowuserinteraction) {
  143.       defaultAllowUserInteraction = defaultallowuserinteraction;
  144.    }
  145.  
  146.    public static boolean getDefaultAllowUserInteraction() {
  147.       return defaultAllowUserInteraction;
  148.    }
  149.  
  150.    public void setUseCaches(boolean usecaches) {
  151.       if (this.connected) {
  152.          throw new IllegalAccessError("Already connected");
  153.       } else {
  154.          this.useCaches = usecaches;
  155.       }
  156.    }
  157.  
  158.    public boolean getUseCaches() {
  159.       return this.useCaches;
  160.    }
  161.  
  162.    public void setIfModifiedSince(long ifmodifiedsince) {
  163.       if (this.connected) {
  164.          throw new IllegalAccessError("Already connected");
  165.       } else {
  166.          this.ifModifiedSince = ifmodifiedsince;
  167.       }
  168.    }
  169.  
  170.    public long getIfModifiedSince() {
  171.       return this.ifModifiedSince;
  172.    }
  173.  
  174.    public boolean getDefaultUseCaches() {
  175.       return defaultUseCaches;
  176.    }
  177.  
  178.    public void setDefaultUseCaches(boolean defaultusecaches) {
  179.       defaultUseCaches = defaultusecaches;
  180.    }
  181.  
  182.    public void setRequestProperty(String key, String value) {
  183.       if (this.connected) {
  184.          throw new IllegalAccessError("Already connected");
  185.       }
  186.    }
  187.  
  188.    public String getRequestProperty(String key) {
  189.       if (this.connected) {
  190.          throw new IllegalAccessError("Already connected");
  191.       } else {
  192.          return null;
  193.       }
  194.    }
  195.  
  196.    public static void setDefaultRequestProperty(String key, String value) {
  197.    }
  198.  
  199.    public static String getDefaultRequestProperty(String key) {
  200.       return null;
  201.    }
  202.  
  203.    public static synchronized void setContentHandlerFactory(ContentHandlerFactory fac) {
  204.       if (factory != null) {
  205.          throw new Error("factory already defined");
  206.       } else {
  207.          SecurityManager security = System.getSecurityManager();
  208.          if (security != null) {
  209.             security.checkSetFactory();
  210.          }
  211.  
  212.          factory = fac;
  213.       }
  214.    }
  215.  
  216.    synchronized ContentHandler getContentHandler() throws UnknownServiceException {
  217.       String contentType = this.getContentType();
  218.       ContentHandler handler = null;
  219.       if (contentType == null) {
  220.          throw new UnknownServiceException("no content-type");
  221.       } else {
  222.          try {
  223.             handler = (ContentHandler)handlers.get(contentType);
  224.             if (handler != null) {
  225.                return handler;
  226.             }
  227.          } catch (Exception var9) {
  228.          }
  229.  
  230.          if (factory != null) {
  231.             handler = factory.createContentHandler(contentType);
  232.          }
  233.  
  234.          if (handler == null) {
  235.             try {
  236.                int i = content_class_prefix.length();
  237.                int j = contentType.length();
  238.                char[] nm = new char[i + j];
  239.                content_class_prefix.getChars(0, i, nm, 0);
  240.                contentType.getChars(0, j, nm, i);
  241.  
  242.                while(true) {
  243.                   --j;
  244.                   if (j < 0) {
  245.                      String name = new String(nm);
  246.  
  247.                      try {
  248.                         handler = (ContentHandler)Class.forName(name).newInstance();
  249.                      } catch (Exception e) {
  250.                         ((Throwable)e).printStackTrace();
  251.                         handler = UnknownContentHandlerP;
  252.                      }
  253.                      break;
  254.                   }
  255.  
  256.                   char c = nm[i];
  257.                   if (c == '/') {
  258.                      nm[i] = '.';
  259.                   } else if ((c < 'A' || c > 'Z') && (c < 'a' || c > 'z') && (c < '0' || c > '9')) {
  260.                      nm[i] = '_';
  261.                   }
  262.  
  263.                   ++i;
  264.                }
  265.             } catch (Exception e) {
  266.                ((Throwable)e).printStackTrace();
  267.                handler = UnknownContentHandlerP;
  268.             }
  269.  
  270.             handlers.put(contentType, handler);
  271.          }
  272.  
  273.          return handler;
  274.       }
  275.    }
  276.  
  277.    protected static String guessContentTypeFromName(String fname) {
  278.       String ext = "";
  279.       int i = fname.lastIndexOf(35);
  280.       if (i != -1) {
  281.          fname = fname.substring(0, i - 1);
  282.       }
  283.  
  284.       i = fname.lastIndexOf(46);
  285.       i = Math.max(i, fname.lastIndexOf(47));
  286.       i = Math.max(i, fname.lastIndexOf(63));
  287.       if (i != -1 && fname.charAt(i) == '.') {
  288.          ext = fname.substring(i).toLowerCase();
  289.       }
  290.  
  291.       return (String)extension_map.get(ext);
  292.    }
  293.  
  294.    private static void setSuffix(String ext, String ct) {
  295.       extension_map.put(ext, ct);
  296.    }
  297.  
  298.    protected static String guessContentTypeFromStream(InputStream is) throws IOException {
  299.       is.mark(10);
  300.       int c1 = is.read();
  301.       int c2 = is.read();
  302.       int c3 = is.read();
  303.       int c4 = is.read();
  304.       int c5 = is.read();
  305.       int c6 = is.read();
  306.       is.reset();
  307.       if (c1 == 71 && c2 == 73 && c3 == 70 && c4 == 56) {
  308.          return "image/gif";
  309.       } else if (c1 == 35 && c2 == 100 && c3 == 101 && c4 == 102) {
  310.          return "image/x-bitmap";
  311.       } else if (c1 == 33 && c2 == 32 && c3 == 88 && c4 == 80 && c5 == 77 && c6 == 50) {
  312.          return "image/x-pixmap";
  313.       } else {
  314.          return c1 != 60 || c2 != 33 && (c6 != 62 || (c2 != 104 || (c3 != 116 || c4 != 109 || c5 != 108) && (c3 != 101 || c4 != 97 || c5 != 100)) && (c2 != 98 || c3 != 111 || c4 != 100 || c5 != 121)) ? null : "text/html";
  315.       }
  316.    }
  317.  
  318.    static {
  319.       setSuffix("", "content/unknown");
  320.       setSuffix(".uu", "application/octet-stream");
  321.       setSuffix(".saveme", "application/octet-stream");
  322.       setSuffix(".dump", "application/octet-stream");
  323.       setSuffix(".hqx", "application/octet-stream");
  324.       setSuffix(".arc", "application/octet-stream");
  325.       setSuffix(".o", "application/octet-stream");
  326.       setSuffix(".a", "application/octet-stream");
  327.       setSuffix(".bin", "application/octet-stream");
  328.       setSuffix(".exe", "application/octet-stream");
  329.       setSuffix(".z", "application/octet-stream");
  330.       setSuffix(".gz", "application/octet-stream");
  331.       setSuffix(".oda", "application/oda");
  332.       setSuffix(".pdf", "application/pdf");
  333.       setSuffix(".eps", "application/postscript");
  334.       setSuffix(".ai", "application/postscript");
  335.       setSuffix(".ps", "application/postscript");
  336.       setSuffix(".rtf", "application/rtf");
  337.       setSuffix(".dvi", "application/x-dvi");
  338.       setSuffix(".hdf", "application/x-hdf");
  339.       setSuffix(".latex", "application/x-latex");
  340.       setSuffix(".cdf", "application/x-netcdf");
  341.       setSuffix(".nc", "application/x-netcdf");
  342.       setSuffix(".tex", "application/x-tex");
  343.       setSuffix(".texinfo", "application/x-texinfo");
  344.       setSuffix(".texi", "application/x-texinfo");
  345.       setSuffix(".t", "application/x-troff");
  346.       setSuffix(".tr", "application/x-troff");
  347.       setSuffix(".roff", "application/x-troff");
  348.       setSuffix(".man", "application/x-troff-man");
  349.       setSuffix(".me", "application/x-troff-me");
  350.       setSuffix(".ms", "application/x-troff-ms");
  351.       setSuffix(".src", "application/x-wais-source");
  352.       setSuffix(".wsrc", "application/x-wais-source");
  353.       setSuffix(".zip", "application/zip");
  354.       setSuffix(".bcpio", "application/x-bcpio");
  355.       setSuffix(".cpio", "application/x-cpio");
  356.       setSuffix(".gtar", "application/x-gtar");
  357.       setSuffix(".shar", "application/x-shar");
  358.       setSuffix(".sh", "application/x-shar");
  359.       setSuffix(".sv4cpio", "application/x-sv4cpio");
  360.       setSuffix(".sv4crc", "application/x-sv4crc");
  361.       setSuffix(".tar", "application/x-tar");
  362.       setSuffix(".ustar", "application/x-ustar");
  363.       setSuffix(".snd", "audio/basic");
  364.       setSuffix(".au", "audio/basic");
  365.       setSuffix(".aifc", "audio/x-aiff");
  366.       setSuffix(".aif", "audio/x-aiff");
  367.       setSuffix(".aiff", "audio/x-aiff");
  368.       setSuffix(".wav", "audio/x-wav");
  369.       setSuffix(".gif", "image/gif");
  370.       setSuffix(".ief", "image/ief");
  371.       setSuffix(".jfif", "image/jpeg");
  372.       setSuffix(".jfif-tbnl", "image/jpeg");
  373.       setSuffix(".jpe", "image/jpeg");
  374.       setSuffix(".jpg", "image/jpeg");
  375.       setSuffix(".jpeg", "image/jpeg");
  376.       setSuffix(".tif", "image/tiff");
  377.       setSuffix(".tiff", "image/tiff");
  378.       setSuffix(".ras", "image/x-cmu-rast");
  379.       setSuffix(".pnm", "image/x-portable-anymap");
  380.       setSuffix(".pbm", "image/x-portable-bitmap");
  381.       setSuffix(".pgm", "image/x-portable-graymap");
  382.       setSuffix(".ppm", "image/x-portable-pixmap");
  383.       setSuffix(".rgb", "image/x-rgb");
  384.       setSuffix(".xbm", "image/x-xbitmap");
  385.       setSuffix(".xpm", "image/x-xpixmap");
  386.       setSuffix(".xwd", "image/x-xwindowdump");
  387.       setSuffix(".htm", "text/html");
  388.       setSuffix(".html", "text/html");
  389.       setSuffix(".text", "text/plain");
  390.       setSuffix(".c", "text/plain");
  391.       setSuffix(".cc", "text/plain");
  392.       setSuffix(".c++", "text/plain");
  393.       setSuffix(".h", "text/plain");
  394.       setSuffix(".pl", "text/plain");
  395.       setSuffix(".txt", "text/plain");
  396.       setSuffix(".java", "text/plain");
  397.       setSuffix(".rtx", "application/rtf");
  398.       setSuffix(".tsv", "text/tab-separated-values");
  399.       setSuffix(".etx", "text/x-setext");
  400.       setSuffix(".mpg", "video/mpeg");
  401.       setSuffix(".mpe", "video/mpeg");
  402.       setSuffix(".mpeg", "video/mpeg");
  403.       setSuffix(".mov", "video/quicktime");
  404.       setSuffix(".qt", "video/quicktime");
  405.       setSuffix(".avi", "application/x-troff-msvideo");
  406.       setSuffix(".movie", "video/x-sgi-movie");
  407.       setSuffix(".mv", "video/x-sgi-movie");
  408.       setSuffix(".mime", "message/rfc822");
  409.    }
  410. }
  411.