1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs2.provider;
18
19 import java.io.IOException;
20 import java.net.URL;
21 import java.net.URLConnection;
22 import java.net.URLStreamHandler;
23
24 import org.apache.commons.vfs2.FileObject;
25 import org.apache.commons.vfs2.FileSystemException;
26 import org.apache.commons.vfs2.FileSystemOptions;
27
28
29
30
31 public class DefaultURLStreamHandler extends URLStreamHandler {
32 private final VfsComponentContext context;
33 private final FileSystemOptions fileSystemOptions;
34
35 public DefaultURLStreamHandler(final VfsComponentContext context) {
36 this(context, null);
37 }
38
39 public DefaultURLStreamHandler(final VfsComponentContext context, final FileSystemOptions fileSystemOptions) {
40 this.context = context;
41 this.fileSystemOptions = fileSystemOptions;
42 }
43
44 @Override
45 protected URLConnection openConnection(final URL url) throws IOException {
46 final FileObject entry = context.resolveFile(url.toExternalForm(), fileSystemOptions);
47 return new DefaultURLConnection(url, entry.getContent());
48 }
49
50 @Override
51 protected void parseURL(final URL u, final String spec, final int start, final int limit) {
52 try {
53 final FileObject old = context.resolveFile(u.toExternalForm(), fileSystemOptions);
54
55 final FileObject newURL;
56 if (start > 0 && spec.charAt(start - 1) == ':') {
57 newURL = context.resolveFile(old, spec, fileSystemOptions);
58 } else if (old.isFile() && old.getParent() != null) {
59
60 newURL = old.getParent().resolveFile(spec);
61 } else {
62 newURL = old.resolveFile(spec);
63 }
64
65 final String url = newURL.getName().getURI();
66 final StringBuilder filePart = new StringBuilder();
67 final String protocolPart = UriParser.extractScheme(context.getFileSystemManager().getSchemes(), url, filePart);
68
69 setURL(u, protocolPart, "", -1, null, null, filePart.toString(), null, null);
70 } catch (final FileSystemException fse) {
71
72 throw new RuntimeException(fse.getMessage());
73 }
74 }
75
76 @Override
77 protected String toExternalForm(final URL u) {
78 return u.getProtocol() + ":" + u.getFile();
79 }
80 }