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