View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.vfs2.provider.jar;
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.io.OutputStream;
22  import java.net.JarURLConnection;
23  import java.net.MalformedURLException;
24  import java.net.URL;
25  import java.security.cert.Certificate;
26  import java.util.jar.Attributes;
27  import java.util.jar.JarEntry;
28  import java.util.jar.JarFile;
29  import java.util.jar.Manifest;
30  
31  import org.apache.commons.vfs2.FileContent;
32  import org.apache.commons.vfs2.FileSystemException;
33  
34  /**
35   * A default URL connection that will work for most file systems.
36   */
37  public class JarURLConnectionImpl extends JarURLConnection {
38  
39      // This is because JarURLConnection SUCKS
40      private static final String HACK_URL = "jar:http://somehost/somejar.jar!/";
41  
42      private final FileContent fileContent;
43      private final URL parentURL;
44      private final JarFileObject jarFileObject;
45      private final String entryName;
46  
47      /**
48       * Constructs a new instance.
49       *
50       * @param jarFileObject The JAR file.
51       * @param fileContent THe JAR file contents.
52       * @throws MalformedURLException Should not happen.
53       * @throws FileSystemException if an error occurs accessing the JAR file.
54       */
55      public JarURLConnectionImpl(final JarFileObject jarFileObject, final FileContent fileContent) throws MalformedURLException, FileSystemException {
56          // This is because JarURLConnection SUCKS!!
57          super(new URL(HACK_URL));
58  
59          url = jarFileObject.getURL();
60          this.fileContent = fileContent;
61          parentURL = jarFileObject.getURL();
62          entryName = jarFileObject.getName().getPath();
63          this.jarFileObject = jarFileObject;
64      }
65  
66      @Override
67      public void connect() {
68          connected = true;
69      }
70  
71      @Override
72      public Attributes getAttributes() throws IOException {
73          return jarFileObject.getAttributes();
74      }
75  
76      @Override
77      public Certificate[] getCertificates() {
78          return jarFileObject.doGetCertificates();
79      }
80  
81      @Override
82      public int getContentLength() {
83          try {
84              return (int) fileContent.getSize();
85          } catch (final FileSystemException ignored) {
86              return -1;
87          }
88      }
89  
90      @Override
91      public String getEntryName() {
92          return entryName;
93      }
94  
95      @Override
96      public InputStream getInputStream() throws IOException {
97          return fileContent.getInputStream();
98      }
99  
100     @Override
101     public JarEntry getJarEntry() throws IOException {
102         throw new FileSystemException("vfs.provider.jar/jar-entry-no-access.error");
103     }
104 
105     @Override
106     public JarFile getJarFile() throws IOException {
107         throw new FileSystemException("vfs.provider.jar/jar-file-no-access.error");
108     }
109 
110     @Override
111     public URL getJarFileURL() {
112         return parentURL;
113     }
114 
115     @Override
116     public Manifest getManifest() throws IOException {
117         return jarFileObject.getManifest();
118     }
119 
120     @Override
121     public OutputStream getOutputStream() throws IOException {
122         return fileContent.getOutputStream();
123     }
124 
125 }