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                     try {
  50                         sm.checkPermission(perm);
  51                     } catch (SecurityException se) {
  52                         // fallback to checkRead/checkConnect for pre 1.2
  53                         // security managers
  54                         if ((perm instanceof java.io.FilePermission) &&
  55                                 perm.getActions().indexOf("read") != -1) {
  56                             sm.checkRead(perm.getName());
  57                         } else if ((perm instanceof
  58                                 java.net.SocketPermission) &&
  59                                 perm.getActions().indexOf("connect") != -1) {
  60                             sm.checkConnect(u.getHost(), u.getPort());
  61                         } else {
  62                             throw se;
  63                         }
  64                     }
  65                 }
  66             } catch (java.io.IOException ioe) {
  67                     sm.checkConnect(u.getHost(), u.getPort());
  68             }
  69         }
  70         this.url = u;
  71 
  72     }
  73 
  74     public URLImageSource(String href) throws MalformedURLException {
  75         this(new URL(null, href));
  76     }
  77 
  78     public URLImageSource(URL u, URLConnection uc) {
  79         this(u);
  80         conn = uc;
  81     }
  82 
  83     public URLImageSource(URLConnection uc) {
  84         this(uc.getURL(), uc);
  85     }
  86 
  87     final boolean checkSecurity(Object context, boolean quiet) {
  88         // If actualHost is not null, then the host/port parameters that
  89         // the image was actually fetched from were different than the
  90         // host/port parameters the original URL specified for at least
  91         // one of the download attempts.  The original URL security was
  92         // checked when the applet got a handle to the image, so we only
  93         // need to check for the real host/port.
  94         if (actualHost != null) {
  95             try {
  96                 SecurityManager security = System.getSecurityManager();
  97                 if (security != null) {
  98                     security.checkConnect(actualHost, actualPort, context);
  99                 }
 100             } catch (SecurityException e) {
 101                 if (!quiet) {
 102                     throw e;
 103                 }
 104                 return false;
 105             }
 106         }
 107         return true;
 108     }
 109 
 110     private synchronized URLConnection getConnection() throws IOException {
 111         URLConnection c;
 112         if (conn != null) {
 113             c = conn;
 114             conn = null;
 115         } else {
 116             c = url.openConnection();
 117         }
 118         return c;
 119     }
 120 
 121     protected ImageDecoder getDecoder() {
 122         InputStream is = null;
 123         String type = null;
 124         URLConnection c = null;
 125         try {
 126             c = getConnection();
 127             is = c.getInputStream();
 128             type = c.getContentType();
 129             URL u = c.getURL();
 130             if (u != url && (!u.getHost().equals(url.getHost()) ||
 131                              u.getPort() != url.getPort()))
 132             {
 133                 // The image is allowed to come from either the host/port
 134                 // listed in the original URL, or it can come from one other
 135                 // host/port that the URL is redirected to.  More than that
 136                 // and we give up and just throw a SecurityException.
 137                 if (actualHost != null && (!actualHost.equals(u.getHost()) ||
 138                                            actualPort != u.getPort()))
 139                 {
 140                     throw new SecurityException("image moved!");
 141                 }
 142                 actualHost = u.getHost();
 143                 actualPort = u.getPort();
 144             }
 145         } catch (IOException e) {
 146             if (is != null) {
 147                 try {
 148                     is.close();
 149                 } catch (IOException e2) {
 150                 }
 151             } else if (c instanceof HttpURLConnection) {
 152                 ((HttpURLConnection)c).disconnect();
 153             }
 154             return null;
 155         }
 156 
 157         ImageDecoder id = decoderForType(is, type);
 158         if (id == null) {
 159             id = getDecoder(is);
 160         }
 161 
 162         if (id == null) {
 163             // probably, no appropriate decoder
 164             if  (is != null) {
 165                 try {
 166                     is.close();
 167                 } catch (IOException e) {
 168                 }
 169             } else if (c instanceof HttpURLConnection) {
 170                 ((HttpURLConnection)c).disconnect();
 171             }
 172         }
 173         return id;
 174     }
 175 }