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.io.function.Uncheck;
25  import org.apache.commons.vfs2.FileObject;
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  
33      private final VfsComponentContext context;
34      private final FileSystemOptions fileSystemOptions;
35  
36      /**
37       * Constructs a new instance.
38       *
39       * @param context VFS component context.
40       */
41      public DefaultURLStreamHandler(final VfsComponentContext context) {
42          this(context, null);
43      }
44  
45      /**
46       * Constructs a new instance.
47       *
48       * @param context VFS component context.
49       * @param fileSystemOptions FileSystemOptions to resolve files.
50       */
51      public DefaultURLStreamHandler(final VfsComponentContext context, final FileSystemOptions fileSystemOptions) {
52          this.context = context;
53          this.fileSystemOptions = fileSystemOptions;
54      }
55  
56      @Override
57      protected URLConnection openConnection(final URL url) throws IOException {
58          final FileObject entry = context.resolveFile(url.toExternalForm(), fileSystemOptions);
59          return new DefaultURLConnection(url, entry.getContent());
60      }
61  
62      @Override
63      protected void parseURL(final URL u, final String spec, final int start, final int limit) {
64          Uncheck.run(() -> {
65              final FileObject old = context.resolveFile(u.toExternalForm(), fileSystemOptions);
66  
67              final FileObject newURL;
68              if (start > 0 && spec.charAt(start - 1) == ':') {
69                  newURL = context.resolveFile(old, spec, fileSystemOptions);
70              } else if (old.isFile() && old.getParent() != null) {
71                  // for files we have to resolve relative
72                  newURL = old.getParent().resolveFile(spec);
73              } else {
74                  newURL = old.resolveFile(spec);
75              }
76  
77              final String url = newURL.getName().getURI();
78              final StringBuilder filePart = new StringBuilder();
79              final String protocolPart = UriParser.extractScheme(context.getFileSystemManager().getSchemes(), url, filePart);
80  
81              setURL(u, protocolPart, "", -1, null, null, filePart.toString(), null, null);
82          });
83      }
84  
85      @Override
86      protected String toExternalForm(final URL u) {
87          return u.getProtocol() + ":" + u.getFile();
88      }
89  }