01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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;
18:
19: import org.apache.commons.vfs.FileObject;
20: import org.apache.commons.vfs.FileSystemException;
21: import org.apache.commons.vfs.FileSystemOptions;
22: import org.apache.commons.vfs.FileType;
23:
24: import java.io.IOException;
25: import java.net.URL;
26: import java.net.URLConnection;
27: import java.net.URLStreamHandler;
28:
29: /**
30: * A default URL stream handler that will work for most file systems.
31: *
32: * @author <a href="mailto:brian@mmmanager.org">Brian Olsen</a>
33: * @version $Revision: 480428 $ $Date: 2006-11-28 22:15:24 -0800 (Tue, 28 Nov 2006) $
34: */
35: public class DefaultURLStreamHandler extends URLStreamHandler {
36: private final VfsComponentContext context;
37: private final FileSystemOptions fileSystemOptions;
38:
39: public DefaultURLStreamHandler(final VfsComponentContext context) {
40: this (context, null);
41: }
42:
43: public DefaultURLStreamHandler(final VfsComponentContext context,
44: final FileSystemOptions fileSystemOptions) {
45: this .context = context;
46: this .fileSystemOptions = fileSystemOptions;
47: }
48:
49: protected URLConnection openConnection(final URL url)
50: throws IOException {
51: final FileObject entry = context.resolveFile(url
52: .toExternalForm(), fileSystemOptions);
53: return new DefaultURLConnection(url, entry.getContent());
54: }
55:
56: protected void parseURL(final URL u, final String spec,
57: final int start, final int limit) {
58: try {
59: FileObject old = context.resolveFile(u.toExternalForm(),
60: fileSystemOptions);
61:
62: FileObject newURL;
63: if (start > 0 && spec.charAt(start - 1) == ':') {
64: newURL = context.resolveFile(old, spec,
65: fileSystemOptions);
66: } else {
67: if (old.getType() == FileType.FILE
68: && old.getParent() != null) {
69: // for files we have to resolve relative
70: newURL = old.getParent().resolveFile(spec);
71: } else {
72: newURL = old.resolveFile(spec);
73: }
74: }
75:
76: final String url = newURL.getName().getURI();
77: final StringBuffer filePart = new StringBuffer();
78: final String protocolPart = UriParser.extractScheme(url,
79: filePart);
80:
81: setURL(u, protocolPart, "", -1, null, null, filePart
82: .toString(), null, null);
83: } catch (FileSystemException fse) {
84: // This is rethrown to MalformedURLException in URL anyway
85: throw new RuntimeException(fse.getMessage());
86: }
87: }
88:
89: protected String toExternalForm(final URL u) {
90: return u.getProtocol() + ":" + u.getFile();
91: }
92: }
|