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 content;
43      private final URL parentURL;
44      private final JarFileObject file;
45      private final String entryName;
46  
47      public JarURLConnectionImpl(final JarFileObject file, final FileContent content)
48              throws MalformedURLException, FileSystemException {
49          // This is because JarURLConnection SUCKS!!
50          super(new URL(HACK_URL));
51  
52          this.url = file.getURL();
53          this.content = content;
54          this.parentURL = file.getURL();
55          this.entryName = file.getName().getPath();
56          this.file = file;
57      }
58  
59      @Override
60      public URL getJarFileURL() {
61          return parentURL;
62      }
63  
64      @Override
65      public String getEntryName() {
66          return entryName;
67      }
68  
69      @Override
70      public JarFile getJarFile() throws IOException {
71          throw new FileSystemException("vfs.provider.jar/jar-file-no-access.error");
72      }
73  
74      @Override
75      public Manifest getManifest() throws IOException {
76          return file.getManifest();
77      }
78  
79      @Override
80      public JarEntry getJarEntry() throws IOException {
81          throw new FileSystemException("vfs.provider.jar/jar-entry-no-access.error");
82      }
83  
84      @Override
85      public Attributes getAttributes() throws IOException {
86          return file.getAttributes();
87      }
88  
89      @Override
90      public Certificate[] getCertificates() {
91          return file.doGetCertificates();
92      }
93  
94      @Override
95      public void connect() {
96          connected = true;
97      }
98  
99      @Override
100     public InputStream getInputStream() throws IOException {
101         return content.getInputStream();
102     }
103 
104     @Override
105     public OutputStream getOutputStream() throws IOException {
106         return content.getOutputStream();
107     }
108 
109     @Override
110     public int getContentLength() {
111         try {
112             return (int) content.getSize();
113         } catch (final FileSystemException ignored) {
114             return -1;
115         }
116     }
117 
118 }