1 /*
   2  * Copyright (c) 1995, 2006, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.awt.image;
  27 
  28 import java.io.InputStream;
  29 import java.io.IOException;
  30 import java.net.HttpURLConnection;
  31 import java.net.URL;
  32 import java.net.URLConnection;
  33 import java.net.MalformedURLException;
  34 import sun.net.util.URLUtil;
  35 
  36 public class URLImageSource extends InputStreamImageSource {
  37     URL url;
  38     URLConnection conn;
  39     String actualHost;
  40     int actualPort;
  41 
  42     public URLImageSource(URL u) {
  43         SecurityManager sm = System.getSecurityManager();
  44         if (sm != null) {
  45             try {
  46                 java.security.Permission perm =
  47                       URLUtil.getConnectPermission(u);
  48                 if (perm != null) {
  49                     sm.checkPermission(perm);
  50                 }
  51             } catch (java.io.IOException ioe) {
  52                 sm.checkConnect(u.getHost(), u.getPort());
  53             }
  54         }
  55         this.url = u;
  56 
  57     }
  58 
  59     public URLImageSource(String href) throws MalformedURLException {
  60         this(new URL(null, href));
  61     }
  62 
  63     public URLImageSource(URL u, URLConnection uc) {
  64         this(u);
  65         conn = uc;
  66     }
  67 
  68     public URLImageSource(URLConnection uc) {
  69         this(uc.getURL(), uc);
  70     }
  71 
  72     final boolean checkSecurity(Object context, boolean quiet) {
  73         // If actualHost is not null, then the host/port parameters that
  74         // the image was actually fetched from were different than the
  75         // host/port parameters the original URL specified for at least
  76         // one of the download attempts.  The original URL security was
  77         // checked when the applet got a handle to the image, so we only
  78         // need to check for the real host/port.
  79         if (actualHost != null) {
  80             try {
  81                 SecurityManager security = System.getSecurityManager();
  82                 if (security != null) {
  83                     security.checkConnect(actualHost, actualPort, context);
  84                 }
  85             } catch (SecurityException e) {
  86                 if (!quiet) {
  87                     throw e;
  88                 }
  89                 return false;
  90             }
  91         }
  92         return true;
  93     }
  94 
  95     private synchronized URLConnection getConnection() throws IOException {
  96         URLConnection c;
  97         if (conn != null) {
  98             c = conn;
  99             conn = null;
 100         } else {
 101             c = url.openConnection();
 102         }
 103         return c;
 104     }
 105 
 106     protected ImageDecoder getDecoder() {
 107         InputStream is = null;
 108         String type = null;
 109         URLConnection c = null;
 110         try {
 111             c = getConnection();
 112             is = c.getInputStream();
 113             type = c.getContentType();
 114             URL u = c.getURL();
 115             if (u != url && (!u.getHost().equals(url.getHost()) ||
 116                              u.getPort() != url.getPort()))
 117             {
 118                 // The image is allowed to come from either the host/port
 119                 // listed in the original URL, or it can come from one other
 120                 // host/port that the URL is redirected to.  More than that
 121                 // and we give up and just throw a SecurityException.
 122                 if (actualHost != null && (!actualHost.equals(u.getHost()) ||
 123                                            actualPort != u.getPort()))
 124                 {
 125                     throw new SecurityException("image moved!");
 126                 }
 127                 actualHost = u.getHost();
 128                 actualPort = u.getPort();
 129             }
 130         } catch (IOException e) {
 131             if (is != null) {
 132                 try {
 133                     is.close();
 134                 } catch (IOException e2) {
 135                 }
 136             } else if (c instanceof HttpURLConnection) {
 137                 ((HttpURLConnection)c).disconnect();
 138             }
 139             return null;
 140         }
 141 
 142         ImageDecoder id = decoderForType(is, type);
 143         if (id == null) {
 144             id = getDecoder(is);
 145         }
 146 
 147         if (id == null) {
 148             // probably, no appropriate decoder
 149             if  (is != null) {
 150                 try {
 151                     is.close();
 152                 } catch (IOException e) {
 153                 }
 154             } else if (c instanceof HttpURLConnection) {
 155                 ((HttpURLConnection)c).disconnect();
 156             }
 157         }
 158         return id;
 159     }
 160 }