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.vfs2.FileContent; 026import org.apache.commons.vfs2.FileSystemException; 027 028/** 029 * A default URL connection that will work for most file systems. 030 */ 031public final class DefaultURLConnection extends URLConnection { 032 private final FileContent content; 033 034 public DefaultURLConnection(final URL url, final FileContent content) { 035 super(url); 036 this.content = content; 037 } 038 039 @Override 040 public void connect() { 041 connected = true; 042 } 043 044 @Override 045 public InputStream getInputStream() throws IOException { 046 return content.getInputStream(); 047 } 048 049 @Override 050 public OutputStream getOutputStream() throws IOException { 051 return content.getOutputStream(); 052 } 053 054 @Override 055 public long getLastModified() { 056 try { 057 return content.getLastModifiedTime(); 058 } catch (final FileSystemException ignored) { 059 return -1; // TODO: report? 060 } 061 } 062 063 @Override 064 public int getContentLength() { 065 try { 066 return (int) content.getSize(); 067 } catch (final FileSystemException fse) { 068 return -1; // TODO: report? 069 } 070 } 071 072 @Override 073 public String getContentType() { 074 try { 075 return content.getContentInfo().getContentType(); 076 } catch (final FileSystemException e) { 077 throw new RuntimeException(e.getMessage()); 078 } 079 } 080 081 @Override 082 public String getContentEncoding() { 083 try { 084 return content.getContentInfo().getContentEncoding(); 085 } catch (final FileSystemException e) { 086 throw new RuntimeException(e.getMessage()); 087 } 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 RuntimeException(e); } } 096 */ 097}