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 extends URLStreamHandler { 032 private final VfsComponentContext context; 033 private final FileSystemOptions fileSystemOptions; 034 035 public DefaultURLStreamHandler(final VfsComponentContext context) { 036 this(context, null); 037 } 038 039 public DefaultURLStreamHandler(final VfsComponentContext context, final FileSystemOptions fileSystemOptions) { 040 this.context = context; 041 this.fileSystemOptions = fileSystemOptions; 042 } 043 044 @Override 045 protected URLConnection openConnection(final URL url) throws IOException { 046 final FileObject entry = context.resolveFile(url.toExternalForm(), fileSystemOptions); 047 return new DefaultURLConnection(url, entry.getContent()); 048 } 049 050 @Override 051 protected void parseURL(final URL u, final String spec, final int start, final int limit) { 052 try { 053 final FileObject old = context.resolveFile(u.toExternalForm(), fileSystemOptions); 054 055 FileObject newURL; 056 if (start > 0 && spec.charAt(start - 1) == ':') { 057 newURL = context.resolveFile(old, spec, fileSystemOptions); 058 } else { 059 if (old.isFile() && old.getParent() != null) { 060 // for files we have to resolve relative 061 newURL = old.getParent().resolveFile(spec); 062 } else { 063 newURL = old.resolveFile(spec); 064 } 065 } 066 067 final String url = newURL.getName().getURI(); 068 final StringBuilder filePart = new StringBuilder(); 069 final String protocolPart = UriParser.extractScheme(url, filePart); 070 071 setURL(u, protocolPart, "", -1, null, null, filePart.toString(), null, null); 072 } catch (final FileSystemException fse) { 073 // This is rethrown to MalformedURLException in URL anyway 074 throw new RuntimeException(fse.getMessage()); 075 } 076 } 077 078 @Override 079 protected String toExternalForm(final URL u) { 080 return u.getProtocol() + ":" + u.getFile(); 081 } 082}