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.vfs.provider.jar;
18  
19  import org.apache.commons.vfs.FileContent;
20  import org.apache.commons.vfs.FileSystemException;
21  
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.io.OutputStream;
25  import java.net.JarURLConnection;
26  import java.net.MalformedURLException;
27  import java.net.URL;
28  import java.security.cert.Certificate;
29  import java.util.jar.Attributes;
30  import java.util.jar.JarEntry;
31  import java.util.jar.JarFile;
32  import java.util.jar.Manifest;
33  
34  /***
35   * A default URL connection that will work for most file systems.
36   *
37   * @author <a href="mailto:brian@mmmanager.org">Brian Olsen</a>
38   * @version $Revision: 480428 $ $Date: 2006-11-29 07:15:24 +0100 (Mi, 29 Nov 2006) $
39   */
40  public class JarURLConnectionImpl
41      extends JarURLConnection
42  {
43      // This is because JarURLConnection SUCKS
44      private static final String HACK_URL = "jar:http://somehost/somejar.jar!/";
45  
46      private FileContent content;
47      private URL parentURL;
48      private JarFileObject file;
49      private String entryName;
50  
51      public JarURLConnectionImpl(JarFileObject file, FileContent content)
52          throws MalformedURLException, FileSystemException
53      {
54          //This is because JarURLConnection SUCKS!!
55          super(new URL(HACK_URL));
56  
57          this.url = file.getURL();
58          this.content = content;
59          this.parentURL = file.getURL();
60          this.entryName = file.getName().getPath();
61          this.file = file;
62      }
63  
64  
65      public URL getJarFileURL()
66      {
67          return parentURL;
68      }
69  
70  
71      public String getEntryName()
72      {
73          return entryName;
74      }
75  
76  
77      public JarFile getJarFile() throws IOException
78      {
79          throw new FileSystemException("vfs.provider.jar/jar-file-no-access.error");
80      }
81  
82  
83      public Manifest getManifest() throws IOException
84      {
85          return file.getManifest();
86      }
87  
88  
89      public JarEntry getJarEntry() throws IOException
90      {
91          throw new FileSystemException("vfs.provider.jar/jar-entry-no-access.error");
92      }
93  
94  
95      public Attributes getAttributes() throws IOException
96      {
97          return file.getAttributes();
98      }
99  
100 
101     public Certificate[] getCertificates()
102     {
103         return file.doGetCertificates();
104     }
105 
106 
107     public void connect()
108     {
109         connected = true;
110     }
111 
112     public InputStream getInputStream()
113         throws IOException
114     {
115         return content.getInputStream();
116     }
117 
118     public OutputStream getOutputStream()
119         throws IOException
120     {
121         return content.getOutputStream();
122     }
123 
124     public int getContentLength()
125     {
126         try
127         {
128             return (int) content.getSize();
129         }
130         catch (FileSystemException fse)
131         {
132         }
133 
134         return -1;
135     }
136 
137 }