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.io.InputStream; 021import java.io.OutputStream; 022import java.net.URL; 023import java.net.URLConnection; 024 025import org.apache.commons.io.function.Uncheck; 026import org.apache.commons.vfs2.FileContent; 027import org.apache.commons.vfs2.FileSystemException; 028 029/** 030 * A default URL connection that will work for most file systems. 031 */ 032public final class DefaultURLConnection extends URLConnection { 033 034 private final FileContent fileContent; 035 036 /** 037 * Constructs a new instance. 038 * 039 * @param url The URL to connect. 040 * @param fileContent The URL fileContent. 041 */ 042 public DefaultURLConnection(final URL url, final FileContent fileContent) { 043 super(url); 044 this.fileContent = fileContent; 045 } 046 047 @Override 048 public void connect() { 049 connected = true; 050 } 051 052 @Override 053 public String getContentEncoding() { 054 return Uncheck.get(() -> fileContent.getContentInfo().getContentEncoding()); 055 } 056 057 @Override 058 public int getContentLength() { 059 try { 060 return (int) fileContent.getSize(); 061 } catch (final FileSystemException fse) { 062 return -1; // TODO: report? 063 } 064 } 065 066 @Override 067 public String getContentType() { 068 return Uncheck.get(() -> fileContent.getContentInfo().getContentType()); 069 } 070 071 @Override 072 public InputStream getInputStream() throws IOException { 073 return fileContent.getInputStream(); 074 } 075 076 @Override 077 public long getLastModified() { 078 try { 079 return fileContent.getLastModifiedTime(); 080 } catch (final FileSystemException ignored) { 081 return -1; // TODO: report? 082 } 083 } 084 085 @Override 086 public OutputStream getOutputStream() throws IOException { 087 return fileContent.getOutputStream(); 088 } 089 090 /* 091 * public String getHeaderField(String name) { try { if 092 * (content.getFile().getFileSystem().hasCapability(Capability.ATTRIBUTES)) { String value = (String) 093 * content.getAttribute(name); if (value != null) { return value; } } 094 * 095 * return null; } catch (FileSystemException e) { throw new UncheckedIOException(e); } } 096 */ 097}