home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 25 / CDROM25.iso / Share / prog / VJ11 / VJTRIAL.EXE / IE30Java.exe / classd.exe / sun / awt / image / URLImageSource.java < prev    next >
Encoding:
Java Source  |  1997-01-27  |  3.7 KB  |  129 lines

  1. /*
  2.  * @(#)URLImageSource.java    1.12 95/12/14 Jim Graham
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package sun.awt.image;
  21.  
  22. import java.io.InputStream;
  23. import java.io.IOException;
  24. import java.net.URL;
  25. import java.net.URLConnection;
  26. import java.net.MalformedURLException;
  27.  
  28. public class URLImageSource extends InputStreamImageSource {
  29.     URL url;
  30.     URLConnection conn;
  31.     String actualHost;
  32.     int actualPort;
  33.  
  34.     public URLImageSource(URL u) {
  35.     SecurityManager security = System.getSecurityManager();
  36.     if (security != null) {
  37.         security.checkConnect(u.getHost(), u.getPort());
  38.     }
  39.     url = u;
  40.     }
  41.  
  42.     public URLImageSource(String href) throws MalformedURLException {
  43.     this(new URL(null, href));
  44.     }
  45.  
  46.     public URLImageSource(URL u, URLConnection uc) {
  47.     this(u);
  48.     conn = uc;
  49.     }
  50.  
  51.     public URLImageSource(URLConnection uc) {
  52.     this(uc.getURL(), uc);
  53.     }
  54.  
  55.     final boolean checkSecurity(Object context, boolean quiet) {
  56.     // If actualHost is not null, then the host/port parameters that
  57.     // the image was actually fetched from were different than the
  58.     // host/port parameters the original URL specified for at least
  59.     // one of the download attempts.  The original URL security was
  60.     // checked when the applet got a handle to the image, so we only
  61.     // need to check for the real host/port.
  62.     if (actualHost != null) {
  63.         try {
  64.                 SecurityManager security = System.getSecurityManager();
  65.                 if (security != null) {
  66.                     security.checkConnect(actualHost, actualPort, context);
  67.                 }
  68.         } catch (SecurityException e) {
  69.         if (!quiet) {
  70.             throw e;
  71.         }
  72.         return false;
  73.         }
  74.     }
  75.     return true;
  76.     }
  77.  
  78.     private synchronized URLConnection getConnection() throws IOException {
  79.     URLConnection c;
  80.     if (conn != null) {
  81.         c = conn;
  82.         conn = null;
  83.     } else {
  84.         c = url.openConnection();
  85.     }
  86.     return c;
  87.     }
  88.  
  89.     protected ImageDecoder getDecoder() {
  90.     InputStream is = null;
  91.     String type = null;
  92.     try {
  93.         URLConnection c = getConnection();
  94.         is = c.getInputStream();
  95.         type = c.getContentType();
  96.         URL u = c.getURL();
  97.         if (u != url && (u.getHost() != url.getHost() ||
  98.                  u.getPort() != url.getPort()))
  99.         {
  100.         // The image is allowed to come from either the host/port
  101.         // listed in the original URL, or it can come from one other
  102.         // host/port that the URL is redirected to.  More than that
  103.         // and we give up and just throw a SecurityException.
  104.         if (actualHost != null && (actualHost != u.getHost() ||
  105.                        actualPort != u.getPort()))
  106.         {
  107.             throw new SecurityException("image moved!");
  108.         }
  109.         actualHost = u.getHost();
  110.         actualPort = u.getPort();
  111.         }
  112.     } catch (IOException e) {
  113.         if (is != null) {
  114.         try {
  115.             is.close();
  116.         } catch (IOException e2) {
  117.         }
  118.         }
  119.         return null;
  120.     }
  121.  
  122.     ImageDecoder id = decoderForType(is, type);
  123.     if (id == null) {
  124.         id = getDecoder(is);
  125.     }
  126.     return id;
  127.     }
  128. }
  129.