1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
36
37 public class JarURLConnectionImpl extends JarURLConnection {
38
39
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
49
50
51
52
53
54
55 public JarURLConnectionImpl(final JarFileObject jarFileObject, final FileContent fileContent) throws MalformedURLException, FileSystemException {
56
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 }