0N/A/*
2362N/A * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.net;
0N/A
0N/Aimport java.io.IOException;
0N/Aimport java.util.jar.JarFile;
0N/Aimport java.util.jar.JarEntry;
0N/Aimport java.util.jar.Attributes;
0N/Aimport java.util.jar.Manifest;
0N/Aimport java.security.Permission;
0N/Aimport sun.net.www.ParseUtil;
0N/A
0N/A/**
0N/A * A URL Connection to a Java ARchive (JAR) file or an entry in a JAR
0N/A * file.
0N/A *
0N/A * <p>The syntax of a JAR URL is:
0N/A *
0N/A * <pre>
0N/A * jar:&lt;url&gt;!/{entry}
0N/A * </pre>
0N/A *
0N/A * <p>for example:
0N/A *
0N/A * <p><code>
0N/A * jar:http://www.foo.com/bar/baz.jar!/COM/foo/Quux.class<br>
0N/A * </code>
0N/A *
0N/A * <p>Jar URLs should be used to refer to a JAR file or entries in
0N/A * a JAR file. The example above is a JAR URL which refers to a JAR
0N/A * entry. If the entry name is omitted, the URL refers to the whole
0N/A * JAR file:
0N/A *
0N/A * <code>
0N/A * jar:http://www.foo.com/bar/baz.jar!/
0N/A * </code>
0N/A *
0N/A * <p>Users should cast the generic URLConnection to a
0N/A * JarURLConnection when they know that the URL they created is a JAR
0N/A * URL, and they need JAR-specific functionality. For example:
0N/A *
0N/A * <pre>
0N/A * URL url = new URL("jar:file:/home/duke/duke.jar!/");
0N/A * JarURLConnection jarConnection = (JarURLConnection)url.openConnection();
0N/A * Manifest manifest = jarConnection.getManifest();
0N/A * </pre>
0N/A *
0N/A * <p>JarURLConnection instances can only be used to read from JAR files.
0N/A * It is not possible to get a {@link java.io.OutputStream} to modify or write
0N/A * to the underlying JAR file using this class.
0N/A * <p>Examples:
0N/A *
0N/A * <dl>
0N/A *
0N/A * <dt>A Jar entry
0N/A * <dd><code>jar:http://www.foo.com/bar/baz.jar!/COM/foo/Quux.class</code>
0N/A *
0N/A * <dt>A Jar file
0N/A * <dd><code>jar:http://www.foo.com/bar/baz.jar!/</code>
0N/A *
0N/A * <dt>A Jar directory
0N/A * <dd><code>jar:http://www.foo.com/bar/baz.jar!/COM/foo/</code>
0N/A *
0N/A * </dl>
0N/A *
0N/A * <p><code>!/</code> is refered to as the <em>separator</em>.
0N/A *
0N/A * <p>When constructing a JAR url via <code>new URL(context, spec)</code>,
0N/A * the following rules apply:
0N/A *
0N/A * <ul>
0N/A *
0N/A * <li>if there is no context URL and the specification passed to the
0N/A * URL constructor doesn't contain a separator, the URL is considered
0N/A * to refer to a JarFile.
0N/A *
0N/A * <li>if there is a context URL, the context URL is assumed to refer
0N/A * to a JAR file or a Jar directory.
0N/A *
0N/A * <li>if the specification begins with a '/', the Jar directory is
0N/A * ignored, and the spec is considered to be at the root of the Jar
0N/A * file.
0N/A *
0N/A * <p>Examples:
0N/A *
0N/A * <dl>
0N/A *
0N/A * <dt>context: <b>jar:http://www.foo.com/bar/jar.jar!/</b>,
0N/A * spec:<b>baz/entry.txt</b>
0N/A *
0N/A * <dd>url:<b>jar:http://www.foo.com/bar/jar.jar!/baz/entry.txt</b>
0N/A *
0N/A * <dt>context: <b>jar:http://www.foo.com/bar/jar.jar!/baz</b>,
0N/A * spec:<b>entry.txt</b>
0N/A *
0N/A * <dd>url:<b>jar:http://www.foo.com/bar/jar.jar!/baz/entry.txt</b>
0N/A *
0N/A * <dt>context: <b>jar:http://www.foo.com/bar/jar.jar!/baz</b>,
0N/A * spec:<b>/entry.txt</b>
0N/A *
0N/A * <dd>url:<b>jar:http://www.foo.com/bar/jar.jar!/entry.txt</b>
0N/A *
0N/A * </dl>
0N/A *
0N/A * </ul>
0N/A *
0N/A * @see java.net.URL
0N/A * @see java.net.URLConnection
0N/A *
0N/A * @see java.util.jar.JarFile
0N/A * @see java.util.jar.JarInputStream
0N/A * @see java.util.jar.Manifest
0N/A * @see java.util.zip.ZipEntry
0N/A *
0N/A * @author Benjamin Renaud
0N/A * @since 1.2
0N/A */
0N/Apublic abstract class JarURLConnection extends URLConnection {
0N/A
0N/A private URL jarFileURL;
0N/A private String entryName;
0N/A
0N/A /**
0N/A * The connection to the JAR file URL, if the connection has been
0N/A * initiated. This should be set by connect.
0N/A */
0N/A protected URLConnection jarFileURLConnection;
0N/A
0N/A /**
0N/A * Creates the new JarURLConnection to the specified URL.
0N/A * @param url the URL
0N/A * @throws MalformedURLException if no legal protocol
0N/A * could be found in a specification string or the
0N/A * string could not be parsed.
0N/A */
0N/A
0N/A protected JarURLConnection(URL url) throws MalformedURLException {
0N/A super(url);
0N/A parseSpecs(url);
0N/A }
0N/A
0N/A /* get the specs for a given url out of the cache, and compute and
0N/A * cache them if they're not there.
0N/A */
0N/A private void parseSpecs(URL url) throws MalformedURLException {
0N/A String spec = url.getFile();
0N/A
0N/A int separator = spec.indexOf("!/");
0N/A /*
0N/A * REMIND: we don't handle nested JAR URLs
0N/A */
0N/A if (separator == -1) {
0N/A throw new MalformedURLException("no !/ found in url spec:" + spec);
0N/A }
0N/A
0N/A jarFileURL = new URL(spec.substring(0, separator++));
0N/A entryName = null;
0N/A
0N/A /* if ! is the last letter of the innerURL, entryName is null */
0N/A if (++separator != spec.length()) {
0N/A entryName = spec.substring(separator, spec.length());
0N/A entryName = ParseUtil.decode (entryName);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the URL for the Jar file for this connection.
0N/A *
0N/A * @return the URL for the Jar file for this connection.
0N/A */
0N/A public URL getJarFileURL() {
0N/A return jarFileURL;
0N/A }
0N/A
0N/A /**
0N/A * Return the entry name for this connection. This method
0N/A * returns null if the JAR file URL corresponding to this
0N/A * connection points to a JAR file and not a JAR file entry.
0N/A *
0N/A * @return the entry name for this connection, if any.
0N/A */
0N/A public String getEntryName() {
0N/A return entryName;
0N/A }
0N/A
0N/A /**
0N/A * Return the JAR file for this connection.
0N/A *
0N/A * @return the JAR file for this connection. If the connection is
0N/A * a connection to an entry of a JAR file, the JAR file object is
0N/A * returned
0N/A *
0N/A * @exception IOException if an IOException occurs while trying to
0N/A * connect to the JAR file for this connection.
0N/A *
0N/A * @see #connect
0N/A */
0N/A public abstract JarFile getJarFile() throws IOException;
0N/A
0N/A /**
0N/A * Returns the Manifest for this connection, or null if none.
0N/A *
0N/A * @return the manifest object corresponding to the JAR file object
0N/A * for this connection.
0N/A *
0N/A * @exception IOException if getting the JAR file for this
0N/A * connection causes an IOException to be trown.
0N/A *
0N/A * @see #getJarFile
0N/A */
0N/A public Manifest getManifest() throws IOException {
0N/A return getJarFile().getManifest();
0N/A }
0N/A
0N/A /**
0N/A * Return the JAR entry object for this connection, if any. This
0N/A * method returns null if the JAR file URL corresponding to this
0N/A * connection points to a JAR file and not a JAR file entry.
0N/A *
0N/A * @return the JAR entry object for this connection, or null if
0N/A * the JAR URL for this connection points to a JAR file.
0N/A *
0N/A * @exception IOException if getting the JAR file for this
0N/A * connection causes an IOException to be trown.
0N/A *
0N/A * @see #getJarFile
0N/A * @see #getJarEntry
0N/A */
0N/A public JarEntry getJarEntry() throws IOException {
0N/A return getJarFile().getJarEntry(entryName);
0N/A }
0N/A
0N/A /**
0N/A * Return the Attributes object for this connection if the URL
0N/A * for it points to a JAR file entry, null otherwise.
0N/A *
0N/A * @return the Attributes object for this connection if the URL
0N/A * for it points to a JAR file entry, null otherwise.
0N/A *
0N/A * @exception IOException if getting the JAR entry causes an
0N/A * IOException to be thrown.
0N/A *
0N/A * @see #getJarEntry
0N/A */
0N/A public Attributes getAttributes() throws IOException {
0N/A JarEntry e = getJarEntry();
0N/A return e != null ? e.getAttributes() : null;
0N/A }
0N/A
0N/A /**
0N/A * Returns the main Attributes for the JAR file for this
0N/A * connection.
0N/A *
0N/A * @return the main Attributes for the JAR file for this
0N/A * connection.
0N/A *
0N/A * @exception IOException if getting the manifest causes an
0N/A * IOException to be thrown.
0N/A *
0N/A * @see #getJarFile
0N/A * @see #getManifest
0N/A */
0N/A public Attributes getMainAttributes() throws IOException {
0N/A Manifest man = getManifest();
0N/A return man != null ? man.getMainAttributes() : null;
0N/A }
0N/A
0N/A /**
0N/A * Return the Certificate object for this connection if the URL
0N/A * for it points to a JAR file entry, null otherwise. This method
0N/A * can only be called once
0N/A * the connection has been completely verified by reading
0N/A * from the input stream until the end of the stream has been
0N/A * reached. Otherwise, this method will return <code>null</code>
0N/A *
0N/A * @return the Certificate object for this connection if the URL
0N/A * for it points to a JAR file entry, null otherwise.
0N/A *
0N/A * @exception IOException if getting the JAR entry causes an
0N/A * IOException to be thrown.
0N/A *
0N/A * @see #getJarEntry
0N/A */
0N/A public java.security.cert.Certificate[] getCertificates()
0N/A throws IOException
0N/A {
0N/A JarEntry e = getJarEntry();
0N/A return e != null ? e.getCertificates() : null;
0N/A }
0N/A}