View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      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.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   * A default URL stream handler that will work for most file systems.
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                  // for files we have to resolve relative
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              // This is rethrown to MalformedURLException in URL anyway
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  }