001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package java.net;
019:
020: import java.io.IOException;
021: import java.security.cert.Certificate;
022: import java.util.jar.Attributes;
023: import java.util.jar.JarEntry;
024: import java.util.jar.JarFile;
025: import java.util.jar.Manifest;
026:
027: /**
028: * This class establishes a connection to a URL using the jar protocol. Jar URLs
029: * are specified as follows: <center><code>jar:<url>!/{entry}</code></center>
030: * where "!/" is called a seperator.
031: */
032: public abstract class JarURLConnection extends URLConnection {
033:
034: // the location of the separator
035: protected URLConnection jarFileURLConnection;
036:
037: private String entryName;
038:
039: private URL fileURL;
040:
041: // the file component of the URL
042: private String file;
043:
044: /**
045: * Constructs an instance of <code>JarURLConnection</code>.
046: *
047: * @param url
048: * java.net.URL the URL that contains the location to connect to
049: */
050: protected JarURLConnection(URL url) throws MalformedURLException {
051: super (url);
052: file = url.getFile();
053: int sepIdx;
054: if ((sepIdx = file.indexOf("!/")) < 0) { //$NON-NLS-1$
055: throw new MalformedURLException();
056: }
057: fileURL = new URL(url.getFile().substring(0, sepIdx)); //$NON-NLS-1$
058: sepIdx += 2;
059: if (file.length() == sepIdx) {
060: return;
061: }
062: entryName = file.substring(sepIdx, file.length());
063: if (null != url.getRef()) {
064: entryName += "#" + url.getRef(); //$NON-NLS-1$
065: }
066: }
067:
068: /**
069: * Answers the attributes of the JarEntry referenced by this
070: * <code>JarURLConnection</code>.
071: *
072: * @return java.util.jar.Attributes the attributes of the the JarEntry
073: * @exception java.io.IOException
074: * thrown if an IO exception occurs while retrieving the
075: * JarEntry
076: */
077: public Attributes getAttributes() throws java.io.IOException {
078: JarEntry jEntry = getJarEntry();
079: return (jEntry == null) ? null : jEntry.getAttributes();
080: }
081:
082: /**
083: * Answers the Certificates of the JarEntry referenced by this
084: * <code>URLConnection</code>. This method will return null until the
085: * InputStream has been completely verified
086: *
087: * @return Certificate[] the Certificates of the JarEntry.
088: * @exception java.io.IOException
089: * thrown if there is an IO exception occurs while getting
090: * the JarEntry.
091: */
092: public Certificate[] getCertificates() throws java.io.IOException {
093: JarEntry jEntry = getJarEntry();
094: if (jEntry == null) {
095: return null;
096: }
097:
098: return jEntry.getCertificates();
099: }
100:
101: /**
102: * Answers the JarEntry name of the entry referenced by this
103: * <code>URLConnection</code>.
104: *
105: * @return java.lang.String the JarEntry name
106: */
107: public String getEntryName() {
108: return entryName;
109: }
110:
111: /**
112: * Answers the JarEntry of the entry referenced by this
113: * <code>URLConnection</code>.
114: *
115: * @return java.util.jar.JarEntry the JarEntry referenced
116: */
117: public JarEntry getJarEntry() throws IOException {
118: if (!connected) {
119: connect();
120: }
121: if (entryName == null) {
122: return null;
123: }
124: // The entry must exist since the connect succeeded
125: return getJarFile().getJarEntry(entryName);
126: }
127:
128: /**
129: * Answers the Manifest associated with the Jar URL
130: *
131: * @return java.util.jar.Manifest The JarFile's Manifest
132: */
133: public Manifest getManifest() throws java.io.IOException {
134: return (Manifest) getJarFile().getManifest().clone();
135: }
136:
137: /**
138: * Answers the the JarFile referenced by this <code>URLConnection</code>.
139: *
140: * @return java.util.jar.JarFile the JarFile
141: * @exception java.io.IOException
142: * thrown if an IO exception occurs while retrieving the Jar
143: * file
144: */
145: public abstract JarFile getJarFile() throws java.io.IOException;
146:
147: /**
148: * Answers the URL of the JarFile referenced by this
149: * <code>URLConnection</code>.
150: *
151: * @return java.net.URL the URL of the JarFile.
152: */
153: public URL getJarFileURL() {
154: return fileURL;
155: }
156:
157: /**
158: * Answers the main Attributes of the JarFile referenced by this
159: * <code>URLConnection</code>.
160: *
161: * @return java.util.jar.Attributes the Attributes of the the JarFile
162: * @exception java.io.IOException
163: * thrown if an IO exception occurs while retrieving the
164: * JarFile
165: */
166: public Attributes getMainAttributes() throws java.io.IOException {
167: Manifest m = getJarFile().getManifest();
168: return (m == null) ? null : m.getMainAttributes();
169: }
170: }
|