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 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
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 }