Path: blob/master/src/java.base/share/classes/java/net/JarURLConnection.java
41152 views
/*1* Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.net;2627import java.io.IOException;28import java.util.jar.JarFile;29import java.util.jar.JarEntry;30import java.util.jar.Attributes;31import java.util.jar.Manifest;32import java.security.Permission;33import sun.net.www.ParseUtil;3435/**36* A URL Connection to a Java ARchive (JAR) file or an entry in a JAR37* file.38*39* <p>The syntax of a JAR URL is:40*41* <pre>42* jar:<url>!/{entry}43* </pre>44*45* <p>for example:46*47* <p>{@code jar:http://www.foo.com/bar/baz.jar!/COM/foo/Quux.class}48*49* <p>Jar URLs should be used to refer to a JAR file or entries in50* a JAR file. The example above is a JAR URL which refers to a JAR51* entry. If the entry name is omitted, the URL refers to the whole52* JAR file:53*54* {@code jar:http://www.foo.com/bar/baz.jar!/}55*56* <p>Users should cast the generic URLConnection to a57* JarURLConnection when they know that the URL they created is a JAR58* URL, and they need JAR-specific functionality. For example:59*60* <pre>61* URL url = new URL("jar:file:/home/duke/duke.jar!/");62* JarURLConnection jarConnection = (JarURLConnection)url.openConnection();63* Manifest manifest = jarConnection.getManifest();64* </pre>65*66* <p>JarURLConnection instances can only be used to read from JAR files.67* It is not possible to get a {@link java.io.OutputStream} to modify or write68* to the underlying JAR file using this class.69* <p>Examples:70*71* <dl>72*73* <dt>A Jar entry74* <dd>{@code jar:http://www.foo.com/bar/baz.jar!/COM/foo/Quux.class}75*76* <dt>A Jar file77* <dd>{@code jar:http://www.foo.com/bar/baz.jar!/}78*79* <dt>A Jar directory80* <dd>{@code jar:http://www.foo.com/bar/baz.jar!/COM/foo/}81*82* </dl>83*84* <p>{@code !/} is referred to as the <em>separator</em>.85*86* <p>When constructing a JAR url via {@code new URL(context, spec)},87* the following rules apply:88*89* <ul>90*91* <li>if there is no context URL and the specification passed to the92* URL constructor doesn't contain a separator, the URL is considered93* to refer to a JarFile.94*95* <li>if there is a context URL, the context URL is assumed to refer96* to a JAR file or a Jar directory.97*98* <li>if the specification begins with a '/', the Jar directory is99* ignored, and the spec is considered to be at the root of the Jar100* file.101*102* <p>Examples:103*104* <dl>105*106* <dt>context: <b>jar:http://www.foo.com/bar/jar.jar!/</b>,107* spec:<b>baz/entry.txt</b>108*109* <dd>url:<b>jar:http://www.foo.com/bar/jar.jar!/baz/entry.txt</b>110*111* <dt>context: <b>jar:http://www.foo.com/bar/jar.jar!/baz</b>,112* spec:<b>entry.txt</b>113*114* <dd>url:<b>jar:http://www.foo.com/bar/jar.jar!/baz/entry.txt</b>115*116* <dt>context: <b>jar:http://www.foo.com/bar/jar.jar!/baz</b>,117* spec:<b>/entry.txt</b>118*119* <dd>url:<b>jar:http://www.foo.com/bar/jar.jar!/entry.txt</b>120*121* </dl>122*123* </ul>124*125* @see java.net.URL126* @see java.net.URLConnection127*128* @see java.util.jar.JarFile129* @see java.util.jar.JarInputStream130* @see java.util.jar.Manifest131* @see java.util.zip.ZipEntry132*133* @author Benjamin Renaud134* @since 1.2135*/136public abstract class JarURLConnection extends URLConnection {137138private URL jarFileURL;139private String entryName;140141/**142* The connection to the JAR file URL, if the connection has been143* initiated. This should be set by connect.144*/145protected URLConnection jarFileURLConnection;146147/**148* Creates the new JarURLConnection to the specified URL.149* @param url the URL150* @throws MalformedURLException if no legal protocol151* could be found in a specification string or the152* string could not be parsed.153*/154155protected JarURLConnection(URL url) throws MalformedURLException {156super(url);157parseSpecs(url);158}159160/* get the specs for a given url out of the cache, and compute and161* cache them if they're not there.162*/163private void parseSpecs(URL url) throws MalformedURLException {164String spec = url.getFile();165166int separator = spec.indexOf("!/");167/*168* REMIND: we don't handle nested JAR URLs169*/170if (separator == -1) {171throw new MalformedURLException("no !/ found in url spec:" + spec);172}173174jarFileURL = new URL(spec.substring(0, separator++));175/*176* The url argument may have had a runtime fragment appended, so177* we need to add a runtime fragment to the jarFileURL to enable178* runtime versioning when the underlying jar file is opened.179*/180if ("runtime".equals(url.getRef())) {181jarFileURL = new URL(jarFileURL, "#runtime");182}183entryName = null;184185/* if ! is the last letter of the innerURL, entryName is null */186if (++separator != spec.length()) {187entryName = spec.substring(separator, spec.length());188entryName = ParseUtil.decode (entryName);189}190}191192/**193* Returns the URL for the Jar file for this connection.194*195* @return the URL for the Jar file for this connection.196*/197public URL getJarFileURL() {198return jarFileURL;199}200201/**202* Return the entry name for this connection. This method203* returns null if the JAR file URL corresponding to this204* connection points to a JAR file and not a JAR file entry.205*206* @return the entry name for this connection, if any.207*/208public String getEntryName() {209return entryName;210}211212/**213* Return the JAR file for this connection.214*215* @return the JAR file for this connection. If the connection is216* a connection to an entry of a JAR file, the JAR file object is217* returned218*219* @throws IOException if an IOException occurs while trying to220* connect to the JAR file for this connection.221*222* @see #connect223*/224public abstract JarFile getJarFile() throws IOException;225226/**227* Returns the Manifest for this connection, or null if none.228*229* @return the manifest object corresponding to the JAR file object230* for this connection.231*232* @throws IOException if getting the JAR file for this233* connection causes an IOException to be thrown.234*235* @see #getJarFile236*/237public Manifest getManifest() throws IOException {238return getJarFile().getManifest();239}240241/**242* Return the JAR entry object for this connection, if any. This243* method returns null if the JAR file URL corresponding to this244* connection points to a JAR file and not a JAR file entry.245*246* @return the JAR entry object for this connection, or null if247* the JAR URL for this connection points to a JAR file.248*249* @throws IOException if getting the JAR file for this250* connection causes an IOException to be thrown.251*252* @see #getJarFile253* @see #getJarEntry254*/255public JarEntry getJarEntry() throws IOException {256return entryName == null ? null : getJarFile().getJarEntry(entryName);257}258259/**260* Return the Attributes object for this connection if the URL261* for it points to a JAR file entry, null otherwise.262*263* @return the Attributes object for this connection if the URL264* for it points to a JAR file entry, null otherwise.265*266* @throws IOException if getting the JAR entry causes an267* IOException to be thrown.268*269* @see #getJarEntry270*/271public Attributes getAttributes() throws IOException {272JarEntry e = getJarEntry();273return e != null ? e.getAttributes() : null;274}275276/**277* Returns the main Attributes for the JAR file for this278* connection.279*280* @return the main Attributes for the JAR file for this281* connection.282*283* @throws IOException if getting the manifest causes an284* IOException to be thrown.285*286* @see #getJarFile287* @see #getManifest288*/289public Attributes getMainAttributes() throws IOException {290Manifest man = getManifest();291return man != null ? man.getMainAttributes() : null;292}293294/**295* Returns the Certificate objects for this connection if the URL296* for it points to a JAR file entry, null otherwise. This method297* can only be called once298* the connection has been completely verified by reading299* from the input stream until the end of the stream has been300* reached. Otherwise, this method will return {@code null}301*302* @return the Certificate object for this connection if the URL303* for it points to a JAR file entry, null otherwise.304*305* @throws IOException if getting the JAR entry causes an306* IOException to be thrown.307*308* @see #getJarEntry309*/310public java.security.cert.Certificate[] getCertificates()311throws IOException312{313JarEntry e = getJarEntry();314return e != null ? e.getCertificates() : null;315}316}317318319