001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.vfs2.provider;
018
019import java.io.IOException;
020import java.net.URL;
021import java.net.URLConnection;
022import java.net.URLStreamHandler;
023
024import org.apache.commons.vfs2.FileObject;
025import org.apache.commons.vfs2.FileSystemException;
026import org.apache.commons.vfs2.FileSystemOptions;
027
028/**
029 * A default URL stream handler that will work for most file systems.
030 */
031public class DefaultURLStreamHandler
032    extends URLStreamHandler
033{
034    private final VfsComponentContext context;
035    private final FileSystemOptions fileSystemOptions;
036
037    public DefaultURLStreamHandler(final VfsComponentContext context)
038    {
039        this(context, null);
040    }
041
042    public DefaultURLStreamHandler(final VfsComponentContext context, final FileSystemOptions fileSystemOptions)
043    {
044        this.context = context;
045        this.fileSystemOptions = fileSystemOptions;
046    }
047
048    @Override
049    protected URLConnection openConnection(final URL url)
050        throws IOException
051    {
052        final FileObject entry = context.resolveFile(url.toExternalForm(), fileSystemOptions);
053        return new DefaultURLConnection(url, entry.getContent());
054    }
055
056    @Override
057    protected void parseURL(final URL u,
058                            final String spec,
059                            final int start,
060                            final int limit)
061    {
062        try
063        {
064            final FileObject old = context.resolveFile(u.toExternalForm(), fileSystemOptions);
065
066            FileObject newURL;
067            if (start > 0 && spec.charAt(start - 1) == ':')
068            {
069                newURL = context.resolveFile(old, spec, fileSystemOptions);
070            }
071            else
072            {
073                if (old.isFile() && old.getParent() != null)
074                {
075                    // for files we have to resolve relative
076                    newURL = old.getParent().resolveFile(spec);
077                }
078                else
079                {
080                    newURL = old.resolveFile(spec);
081                }
082            }
083
084            final String url = newURL.getName().getURI();
085            final StringBuilder filePart = new StringBuilder();
086            final String protocolPart = UriParser.extractScheme(url, filePart);
087
088            setURL(u, protocolPart, "", -1, null, null, filePart.toString(), null, null);
089        }
090        catch (final FileSystemException fse)
091        {
092            // This is rethrown to MalformedURLException in URL anyway
093            throw new RuntimeException(fse.getMessage());
094        }
095    }
096
097    @Override
098    protected String toExternalForm(final URL u)
099    {
100        return u.getProtocol() + ":" + u.getFile();
101    }
102}