001 /*
002 * Copyright 1997-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 java.net;
027
028 import java.io.IOException;
029 import java.util.jar.JarFile;
030 import java.util.jar.JarEntry;
031 import java.util.jar.Attributes;
032 import java.util.jar.Manifest;
033 import java.security.Permission;
034 import sun.net.www.ParseUtil;
035
036 /**
037 * A URL Connection to a Java ARchive (JAR) file or an entry in a JAR
038 * file.
039 *
040 * <p>The syntax of a JAR URL is:
041 *
042 * <pre>
043 * jar:<url>!/{entry}
044 * </pre>
045 *
046 * <p>for example:
047 *
048 * <p><code>
049 * jar:http://www.foo.com/bar/baz.jar!/COM/foo/Quux.class<br>
050 * </code>
051 *
052 * <p>Jar URLs should be used to refer to a JAR file or entries in
053 * a JAR file. The example above is a JAR URL which refers to a JAR
054 * entry. If the entry name is omitted, the URL refers to the whole
055 * JAR file:
056 *
057 * <code>
058 * jar:http://www.foo.com/bar/baz.jar!/
059 * </code>
060 *
061 * <p>Users should cast the generic URLConnection to a
062 * JarURLConnection when they know that the URL they created is a JAR
063 * URL, and they need JAR-specific functionality. For example:
064 *
065 * <pre>
066 * URL url = new URL("jar:file:/home/duke/duke.jar!/");
067 * JarURLConnection jarConnection = (JarURLConnection)url.openConnection();
068 * Manifest manifest = jarConnection.getManifest();
069 * </pre>
070 *
071 * <p>JarURLConnection instances can only be used to read from JAR files.
072 * It is not possible to get a {@link java.io.OutputStream} to modify or write
073 * to the underlying JAR file using this class.
074 * <p>Examples:
075 *
076 * <dl>
077 *
078 * <dt>A Jar entry
079 * <dd><code>jar:http://www.foo.com/bar/baz.jar!/COM/foo/Quux.class</code>
080 *
081 * <dt>A Jar file
082 * <dd><code>jar:http://www.foo.com/bar/baz.jar!/</code>
083 *
084 * <dt>A Jar directory
085 * <dd><code>jar:http://www.foo.com/bar/baz.jar!/COM/foo/</code>
086 *
087 * </dl>
088 *
089 * <p><code>!/</code> is refered to as the <em>separator</em>.
090 *
091 * <p>When constructing a JAR url via <code>new URL(context, spec)</code>,
092 * the following rules apply:
093 *
094 * <ul>
095 *
096 * <li>if there is no context URL and the specification passed to the
097 * URL constructor doesn't contain a separator, the URL is considered
098 * to refer to a JarFile.
099 *
100 * <li>if there is a context URL, the context URL is assumed to refer
101 * to a JAR file or a Jar directory.
102 *
103 * <li>if the specification begins with a '/', the Jar directory is
104 * ignored, and the spec is considered to be at the root of the Jar
105 * file.
106 *
107 * <p>Examples:
108 *
109 * <dl>
110 *
111 * <dt>context: <b>jar:http://www.foo.com/bar/jar.jar!/</b>,
112 * spec:<b>baz/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!/baz/entry.txt</b>
120 *
121 * <dt>context: <b>jar:http://www.foo.com/bar/jar.jar!/baz</b>,
122 * spec:<b>/entry.txt</b>
123 *
124 * <dd>url:<b>jar:http://www.foo.com/bar/jar.jar!/entry.txt</b>
125 *
126 * </dl>
127 *
128 * </ul>
129 *
130 * @see java.net.URL
131 * @see java.net.URLConnection
132 *
133 * @see java.util.jar.JarFile
134 * @see java.util.jar.JarInputStream
135 * @see java.util.jar.Manifest
136 * @see java.util.zip.ZipEntry
137 *
138 * @author Benjamin Renaud
139 * @since 1.2
140 */
141 public abstract class JarURLConnection extends URLConnection {
142
143 private URL jarFileURL;
144 private String entryName;
145
146 /**
147 * The connection to the JAR file URL, if the connection has been
148 * initiated. This should be set by connect.
149 */
150 protected URLConnection jarFileURLConnection;
151
152 /**
153 * Creates the new JarURLConnection to the specified URL.
154 * @param url the URL
155 * @throws MalformedURLException if no legal protocol
156 * could be found in a specification string or the
157 * string could not be parsed.
158 */
159
160 protected JarURLConnection(URL url) throws MalformedURLException {
161 super (url);
162 parseSpecs(url);
163 }
164
165 /* get the specs for a given url out of the cache, and compute and
166 * cache them if they're not there.
167 */
168 private void parseSpecs(URL url) throws MalformedURLException {
169 String spec = url.getFile();
170
171 int separator = spec.indexOf("!/");
172 /*
173 * REMIND: we don't handle nested JAR URLs
174 */
175 if (separator == -1) {
176 throw new MalformedURLException("no !/ found in url spec:"
177 + spec);
178 }
179
180 jarFileURL = new URL(spec.substring(0, separator++));
181 entryName = null;
182
183 /* if ! is the last letter of the innerURL, entryName is null */
184 if (++separator != spec.length()) {
185 entryName = spec.substring(separator, spec.length());
186 entryName = ParseUtil.decode(entryName);
187 }
188 }
189
190 /**
191 * Returns the URL for the Jar file for this connection.
192 *
193 * @return the URL for the Jar file for this connection.
194 */
195 public URL getJarFileURL() {
196 return jarFileURL;
197 }
198
199 /**
200 * Return the entry name for this connection. This method
201 * returns null if the JAR file URL corresponding to this
202 * connection points to a JAR file and not a JAR file entry.
203 *
204 * @return the entry name for this connection, if any.
205 */
206 public String getEntryName() {
207 return entryName;
208 }
209
210 /**
211 * Return the JAR file for this connection.
212 *
213 * @return the JAR file for this connection. If the connection is
214 * a connection to an entry of a JAR file, the JAR file object is
215 * returned
216 *
217 * @exception IOException if an IOException occurs while trying to
218 * connect to the JAR file for this connection.
219 *
220 * @see #connect
221 */
222 public abstract JarFile getJarFile() throws IOException;
223
224 /**
225 * Returns the Manifest for this connection, or null if none.
226 *
227 * @return the manifest object corresponding to the JAR file object
228 * for this connection.
229 *
230 * @exception IOException if getting the JAR file for this
231 * connection causes an IOException to be trown.
232 *
233 * @see #getJarFile
234 */
235 public Manifest getManifest() throws IOException {
236 return getJarFile().getManifest();
237 }
238
239 /**
240 * Return the JAR entry object for this connection, if any. This
241 * method returns null if the JAR file URL corresponding to this
242 * connection points to a JAR file and not a JAR file entry.
243 *
244 * @return the JAR entry object for this connection, or null if
245 * the JAR URL for this connection points to a JAR file.
246 *
247 * @exception IOException if getting the JAR file for this
248 * connection causes an IOException to be trown.
249 *
250 * @see #getJarFile
251 * @see #getJarEntry
252 */
253 public JarEntry getJarEntry() throws IOException {
254 return getJarFile().getJarEntry(entryName);
255 }
256
257 /**
258 * Return the Attributes object for this connection if the URL
259 * for it points to a JAR file entry, null otherwise.
260 *
261 * @return the Attributes object for this connection if the URL
262 * for it points to a JAR file entry, null otherwise.
263 *
264 * @exception IOException if getting the JAR entry causes an
265 * IOException to be thrown.
266 *
267 * @see #getJarEntry
268 */
269 public Attributes getAttributes() throws IOException {
270 JarEntry e = getJarEntry();
271 return e != null ? e.getAttributes() : null;
272 }
273
274 /**
275 * Returns the main Attributes for the JAR file for this
276 * connection.
277 *
278 * @return the main Attributes for the JAR file for this
279 * connection.
280 *
281 * @exception IOException if getting the manifest causes an
282 * IOException to be thrown.
283 *
284 * @see #getJarFile
285 * @see #getManifest
286 */
287 public Attributes getMainAttributes() throws IOException {
288 Manifest man = getManifest();
289 return man != null ? man.getMainAttributes() : null;
290 }
291
292 /**
293 * Return the Certificate object for this connection if the URL
294 * for it points to a JAR file entry, null otherwise. This method
295 * can only be called once
296 * the connection has been completely verified by reading
297 * from the input stream until the end of the stream has been
298 * reached. Otherwise, this method will return <code>null</code>
299 *
300 * @return the Certificate object for this connection if the URL
301 * for it points to a JAR file entry, null otherwise.
302 *
303 * @exception IOException if getting the JAR entry causes an
304 * IOException to be thrown.
305 *
306 * @see #getJarEntry
307 */
308 public java.security.cert.Certificate[] getCertificates()
309 throws IOException {
310 JarEntry e = getJarEntry();
311 return e != null ? e.getCertificates() : null;
312 }
313 }
|