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.io.function.Uncheck;
025import org.apache.commons.vfs2.FileObject;
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 extends URLStreamHandler {
032
033    private final VfsComponentContext context;
034    private final FileSystemOptions fileSystemOptions;
035
036    /**
037     * Constructs a new instance.
038     *
039     * @param context VFS component context.
040     */
041    public DefaultURLStreamHandler(final VfsComponentContext context) {
042        this(context, null);
043    }
044
045    /**
046     * Constructs a new instance.
047     *
048     * @param context VFS component context.
049     * @param fileSystemOptions FileSystemOptions to resolve files.
050     */
051    public DefaultURLStreamHandler(final VfsComponentContext context, final FileSystemOptions fileSystemOptions) {
052        this.context = context;
053        this.fileSystemOptions = fileSystemOptions;
054    }
055
056    @Override
057    protected URLConnection openConnection(final URL url) throws IOException {
058        final FileObject entry = context.resolveFile(url.toExternalForm(), fileSystemOptions);
059        return new DefaultURLConnection(url, entry.getContent());
060    }
061
062    @Override
063    protected void parseURL(final URL u, final String spec, final int start, final int limit) {
064        Uncheck.run(() -> {
065            final FileObject old = context.resolveFile(u.toExternalForm(), fileSystemOptions);
066
067            final FileObject newURL;
068            if (start > 0 && spec.charAt(start - 1) == ':') {
069                newURL = context.resolveFile(old, spec, fileSystemOptions);
070            } else if (old.isFile() && old.getParent() != null) {
071                // for files we have to resolve relative
072                newURL = old.getParent().resolveFile(spec);
073            } else {
074                newURL = old.resolveFile(spec);
075            }
076
077            final String url = newURL.getName().getURI();
078            final StringBuilder filePart = new StringBuilder();
079            final String protocolPart = UriParser.extractScheme(context.getFileSystemManager().getSchemes(), url, filePart);
080
081            setURL(u, protocolPart, "", -1, null, null, filePart.toString(), null, null);
082        });
083    }
084
085    @Override
086    protected String toExternalForm(final URL u) {
087        return u.getProtocol() + ":" + u.getFile();
088    }
089}