1 16 package org.apache.commons.vfs.provider; 17 18 import org.apache.commons.vfs.FileObject; 19 import org.apache.commons.vfs.FileSystemException; 20 import org.apache.commons.vfs.FileSystemOptions; 21 import org.apache.commons.vfs.FileType; 22 23 import java.io.IOException ; 24 import java.net.URL ; 25 import java.net.URLConnection ; 26 import java.net.URLStreamHandler ; 27 28 33 public class DefaultURLStreamHandler 34 extends URLStreamHandler 35 { 36 private final VfsComponentContext context; 37 private final FileSystemOptions fileSystemOptions; 38 39 public DefaultURLStreamHandler(final VfsComponentContext context) 40 { 41 this(context, null); 42 } 43 44 public DefaultURLStreamHandler(final VfsComponentContext context, final FileSystemOptions fileSystemOptions) 45 { 46 this.context = context; 47 this.fileSystemOptions = fileSystemOptions; 48 } 49 50 protected URLConnection openConnection(final URL url) 51 throws IOException 52 { 53 final FileObject entry = context.resolveFile(url.toExternalForm(), fileSystemOptions); 54 return new DefaultURLConnection(url, entry.getContent()); 55 } 56 57 protected void parseURL(final URL u, 58 final String spec, 59 final int start, 60 final int limit) 61 { 62 try 63 { 64 FileObject old = context.resolveFile(u.toExternalForm(), fileSystemOptions); 65 66 FileObject newURL; 67 if (start > 0 && spec.charAt(start - 1) == ':') 68 { 69 newURL = context.resolveFile(old, spec, fileSystemOptions); 70 } 71 else 72 { 73 if (old.getType() == FileType.FILE && old.getParent() != null) 74 { 75 newURL = old.getParent().resolveFile(spec); 77 } 78 else 79 { 80 newURL = old.resolveFile(spec); 81 } 82 } 83 84 final String url = newURL.getName().getURI(); 85 final StringBuffer filePart = new StringBuffer (); 86 final String protocolPart = UriParser.extractScheme(url, filePart); 87 88 setURL(u, protocolPart, "", -1, null, null, filePart.toString(), null, null); 89 } 90 catch (FileSystemException fse) 91 { 92 throw new RuntimeException (fse.getMessage()); 94 } 95 } 96 97 protected String toExternalForm(final URL u) 98 { 99 return u.getProtocol() + ":" + u.getFile(); 100 } 101 } 102 | Popular Tags |