1. /*
  2. * @(#)JarURLConnection.java 1.18 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.net;
  8. import java.io.IOException;
  9. import java.util.jar.JarFile;
  10. import java.util.jar.JarEntry;
  11. import java.util.jar.Attributes;
  12. import java.util.jar.Manifest;
  13. import java.security.Permission;
  14. /**
  15. * A URL Connection to a Java ARchive (JAR) file or an entry in a JAR
  16. * file.
  17. *
  18. * <p>The syntax of a JAR URL is:
  19. *
  20. * <pre>
  21. * jar:<url>!/{entry}
  22. * </pre>
  23. *
  24. * <p>for example:
  25. *
  26. * <p><code>
  27. * jar:http://www.foo.com/bar/baz.jar!/COM/foo/Quux.class<br>
  28. * </code>
  29. *
  30. * <p>Jar URLs should be used to either refer to single JAR entries or
  31. * as base URLs, to refer to JAR files as codebases, or relative
  32. * URLs. The example above is a full JAR URL, which refers to a JAR
  33. * entry. If the entry name is omitted, the URL refers to the whole
  34. * JAR file:
  35. *
  36. * <code>
  37. * jar:http://www.foo.com/bar/baz.jar!/
  38. * </code>
  39. *
  40. * <p>Users should cast the generic URLConnection to a
  41. * JarURLConnection when they know that the URL they created is a JAR
  42. * URL, and they need JAR-specific functionality. For example:
  43. *
  44. * <code>
  45. * URL url = new URL("jar:file:/home/duke/duke.jar!/");
  46. * JarURLConnection jarConnection = (JarURLConnection)url.openConnection();
  47. * Manifest manifest = jarConnection.getManifest();
  48. * </code>
  49. *
  50. * <p>Examples:
  51. *
  52. * <dl>
  53. *
  54. * <dt>A Jar entry
  55. * <dd><code>jar:http://www.foo.com/bar/baz.jar!/COM/foo/Quux.class</code>
  56. *
  57. * <dt>A Jar file
  58. * <dd><code>jar:http://www.foo.com/bar/baz.jar!/</code>
  59. *
  60. * <dt>A Jar directory
  61. * <dd><code>jar:http://www.foo.com/bar/baz.jar!/COM/foo/</code>
  62. *
  63. * </dl>
  64. *
  65. * <p><code>!/</code> is refered to as the <em>separator</em>.
  66. *
  67. * <p>When constructing a JAR url, the following rules apply:
  68. *
  69. * <ul>
  70. *
  71. * <li>if there is no context URL and the specification passed to the
  72. * URL constructor doesn't contains a separator, the URL is considered
  73. * to refer to a JarFile.
  74. *
  75. * <li>if there is a context URL, the context URL is assumed to refer
  76. * to a JAR file or a Jar directory.
  77. *
  78. * <li>if the specification begins with a '/', the Jar directory is
  79. * ignored, and the spec is considered to be at the root of the Jar
  80. * file.
  81. *
  82. * <p>Examples:
  83. *
  84. * <dt>context: <b>jar:http://www.foo.com/bar/jar.jar!/</b>,
  85. * spec:<b>baz/entry.txt<b>
  86. *
  87. * <dd>url:<b>jar:http://www.foo.com/bar/baz/jar.jar!/baz/entry.txt<b>
  88. *
  89. * <dt>context: <b>jar:http://www.foo.com/bar/jar.jar!/baz</b>,
  90. * spec:<b>entry.txt<b>
  91. *
  92. * <dd>url:<b>jar:http://www.foo.com/bar/baz/jar.jar!/baz/entry.txt<b>
  93. *
  94. * <dt>context: <b>jar:http://www.foo.com/bar/jar.jar!/baz</b>,
  95. * spec:<b>/entry.txt<b>
  96. *
  97. * <dd>url:<b>jar:http://www.foo.com/bar/baz/jar.jar!/entry.txt<b>
  98. *
  99. * </dl>
  100. *
  101. * </ul>
  102. *
  103. * @see java.net.URL
  104. * @see java.net.URLConnection
  105. *
  106. * @see java.util.jar.JarFile
  107. * @see java.util.jar.JarInputStream
  108. * @see java.util.jar.Manifest
  109. * @see java.util.zip.ZipEntry
  110. *
  111. * @author Benjamin Renaud
  112. * @since JDK1.2
  113. */
  114. public abstract class JarURLConnection extends URLConnection {
  115. private URL jarFileURL;
  116. private String entryName;
  117. /**
  118. * The connection to the JAR file URL, if the connection has been
  119. * initiated. This should be set by connect.
  120. */
  121. protected URLConnection jarFileURLConnection;
  122. protected JarURLConnection(URL url) throws MalformedURLException {
  123. super(url);
  124. parseSpecs(url);
  125. }
  126. /* get the specs for a given url out of the cache, and compute and
  127. * cache them if they're not there.
  128. */
  129. private void parseSpecs(URL url) throws MalformedURLException {
  130. String spec = url.getFile();
  131. int separator = spec.indexOf('!');
  132. /*
  133. * REMIND: we don't handle nested JAR URLs
  134. */
  135. if (separator == -1) {
  136. throw new MalformedURLException("no ! found in url spec:" + spec);
  137. }
  138. jarFileURL = new URL(spec.substring(0, separator++));
  139. entryName = null;
  140. /* if ! is the last letter of the innerURL, entryName is null */
  141. if (++separator != spec.length()) {
  142. entryName = spec.substring(separator, spec.length());
  143. }
  144. }
  145. /**
  146. * Returns the URL for the Jar file for this connection.
  147. *
  148. * @return the URL for the Jar file for this connection.
  149. */
  150. public URL getJarFileURL() {
  151. return jarFileURL;
  152. }
  153. /**
  154. * Return the entry name for this connection. This method
  155. * returns null if the JAR file URL corresponding to this
  156. * connection points to a JAR file and not a JAR file entry.
  157. *
  158. * @return the entry name for this connection, if any.
  159. */
  160. public String getEntryName() {
  161. return entryName;
  162. }
  163. /**
  164. * Return the JAR file for this connection. The returned object is
  165. * not modifiable, and will throw UnsupportedOperationException
  166. * if the caller attempts to modify it.
  167. *
  168. * @return the JAR file for this connection. If the connection is
  169. * a connection to an entry of a JAR file, the JAR file object is
  170. * returned
  171. *
  172. * @exception IOException if an IOException occurs while trying to
  173. * connect to the JAR file for this connection.
  174. *
  175. * @see #connect
  176. */
  177. public abstract JarFile getJarFile() throws IOException;
  178. /**
  179. * Returns the Manifest for this connection, or null if none. The
  180. * returned object is not modifiable, and will throw
  181. * UnsupportedOperationException if the caller attempts to modify
  182. * it.
  183. *
  184. * @return the manifest object corresponding to the JAR file object
  185. * for this connection.
  186. *
  187. * @exception IOException if getting the JAR file for this
  188. * connection causes an IOException to be trown.
  189. *
  190. * @see #getJarFile
  191. */
  192. public Manifest getManifest() throws IOException {
  193. return getJarFile().getManifest();
  194. }
  195. /**
  196. * Return the JAR entry object for this connection, if any. This
  197. * method returns null if the JAR file URL corresponding to this
  198. * connection points to a JAR file and not a JAR file entry. The
  199. * returned object is not modifiable, and will throw
  200. * UnsupportedOperationException if the caller attempts to modify
  201. * it.
  202. *
  203. * @return the JAR entry object for this connection, or null if
  204. * the JAR URL for this connection points to a JAR file.
  205. *
  206. * @exception IOException if getting the JAR file for this
  207. * connection causes an IOException to be trown.
  208. *
  209. * @see #getJarFile
  210. * @see #getJarEntry
  211. */
  212. public JarEntry getJarEntry() throws IOException {
  213. return getJarFile().getJarEntry(entryName);
  214. }
  215. /**
  216. * Return the Attributes object for this connection if the URL
  217. * for it points to a JAR file entry, null otherwise.
  218. *
  219. * @return the Attributes object for this connection if the URL
  220. * for it points to a JAR file entry, null otherwise.
  221. *
  222. * @exception IOException if getting the JAR entry causes an
  223. * IOException to be thrown.
  224. *
  225. * @see #getJarEntry
  226. */
  227. public Attributes getAttributes() throws IOException {
  228. JarEntry e = getJarEntry();
  229. return e != null ? e.getAttributes() : null;
  230. }
  231. /**
  232. * Returns the main Attributes for the JAR file for this
  233. * connection.
  234. *
  235. * @return the main Attributes for the JAR file for this
  236. * connection.
  237. *
  238. * @exception IOException if getting the manifest causes an
  239. * IOException to be thrown.
  240. *
  241. * @see #getJarFile
  242. * @see #getManifest
  243. */
  244. public Attributes getMainAttributes() throws IOException {
  245. Manifest man = getManifest();
  246. return man != null ? man.getMainAttributes() : null;
  247. }
  248. /**
  249. /**
  250. * Return the Certificate object for this connection if the URL
  251. * for it points to a JAR file entry, null otherwise. This method
  252. * can only be called once
  253. * the connection has been completely verified by reading
  254. * from the input stream until the end of the stream has been
  255. * reached. Otherwise, this method will return <code>null</code>
  256. *
  257. * @return the Certificate object for this connection if the URL
  258. * for it points to a JAR file entry, null otherwise.
  259. *
  260. * @exception IOException if getting the JAR entry causes an
  261. * IOException to be thrown.
  262. *
  263. * @see #getJarEntry
  264. */
  265. public java.security.cert.Certificate[] getCertificates()
  266. throws IOException
  267. {
  268. JarEntry e = getJarEntry();
  269. return e != null ? e.getCertificates() : null;
  270. }
  271. }