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