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