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.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-29 07:15:24 +0100 (Mi, 29 Nov 2006) $
    34   */
    35  public class DefaultURLStreamHandler
    36      extends URLStreamHandler
    37  {
    38      private final VfsComponentContext context;
    39      private final FileSystemOptions fileSystemOptions;
    40
    41      public DefaultURLStreamHandler(final VfsComponentContext context)
    42      {
    43          this(context, null);
    44      }
    45
    46      public DefaultURLStreamHandler(final VfsComponentContext context, final FileSystemOptions fileSystemOptions)
    47      {
    48          this.context = context;
    49          this.fileSystemOptions = fileSystemOptions;
    50      }
    51
    52      protected URLConnection openConnection(final URL url)
    53          throws IOException
    54      {
    55          final FileObject entry = context.resolveFile(url.toExternalForm(), fileSystemOptions);
    56          return new DefaultURLConnection(url, entry.getContent());
    57      }
    58
    59      protected void parseURL(final URL u,
    60                              final String spec,
    61                              final int start,
    62                              final int limit)
    63      {
    64          try
    65          {
    66              FileObject old = context.resolveFile(u.toExternalForm(), fileSystemOptions);
    67
    68              FileObject newURL;
    69              if (start > 0 && spec.charAt(start - 1) == ':')
    70              {
    71                  newURL = context.resolveFile(old, spec, fileSystemOptions);
    72              }
    73              else
    74              {
    75                  if (old.getType() == FileType.FILE && old.getParent() != null)
    76                  {
    77                      // for files we have to resolve relative
    78                      newURL = old.getParent().resolveFile(spec);
    79                  }
    80                  else
    81                  {
    82                      newURL = old.resolveFile(spec);
    83                  }
    84              }
    85
    86              final String url = newURL.getName().getURI();
    87              final StringBuffer filePart = new StringBuffer();
    88              final String protocolPart = UriParser.extractScheme(url, filePart);
    89
    90              setURL(u, protocolPart, "", -1, null, null, filePart.toString(), null, null);
    91          }
    92          catch (FileSystemException fse)
    93          {
    94              // This is rethrown to MalformedURLException in URL anyway
    95              throw new RuntimeException(fse.getMessage());
    96          }
    97      }
    98
    99      protected String toExternalForm(final URL u)
    100     {
    101         return u.getProtocol() + ":" + u.getFile();
    102     }
    103 }