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