001: /*
002: * Copyright 1995-2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.awt.image;
027:
028: import java.io.InputStream;
029: import java.io.IOException;
030: import java.net.HttpURLConnection;
031: import java.net.URL;
032: import java.net.URLConnection;
033: import java.net.MalformedURLException;
034:
035: public class URLImageSource extends InputStreamImageSource {
036: URL url;
037: URLConnection conn;
038: String actualHost;
039: int actualPort;
040:
041: public URLImageSource(URL u) {
042: SecurityManager sm = System.getSecurityManager();
043: if (sm != null) {
044: try {
045: java.security.Permission perm = u.openConnection()
046: .getPermission();
047: if (perm != null) {
048: try {
049: sm.checkPermission(perm);
050: } catch (SecurityException se) {
051: // fallback to checkRead/checkConnect for pre 1.2
052: // security managers
053: if ((perm instanceof java.io.FilePermission)
054: && perm.getActions().indexOf("read") != -1) {
055: sm.checkRead(perm.getName());
056: } else if ((perm instanceof java.net.SocketPermission)
057: && perm.getActions().indexOf("connect") != -1) {
058: sm.checkConnect(u.getHost(), u.getPort());
059: } else {
060: throw se;
061: }
062: }
063: }
064: } catch (java.io.IOException ioe) {
065: sm.checkConnect(u.getHost(), u.getPort());
066: }
067: }
068: this .url = u;
069:
070: }
071:
072: public URLImageSource(String href) throws MalformedURLException {
073: this (new URL(null, href));
074: }
075:
076: public URLImageSource(URL u, URLConnection uc) {
077: this (u);
078: conn = uc;
079: }
080:
081: public URLImageSource(URLConnection uc) {
082: this (uc.getURL(), uc);
083: }
084:
085: final boolean checkSecurity(Object context, boolean quiet) {
086: // If actualHost is not null, then the host/port parameters that
087: // the image was actually fetched from were different than the
088: // host/port parameters the original URL specified for at least
089: // one of the download attempts. The original URL security was
090: // checked when the applet got a handle to the image, so we only
091: // need to check for the real host/port.
092: if (actualHost != null) {
093: try {
094: SecurityManager security = System.getSecurityManager();
095: if (security != null) {
096: security.checkConnect(actualHost, actualPort,
097: context);
098: }
099: } 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()
110: 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
131: && (!u.getHost().equals(url.getHost()) || u
132: .getPort() != url.getPort())) {
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
138: && (!actualHost.equals(u.getHost()) || actualPort != u
139: .getPort())) {
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: }
|