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: */
017: package org.apache.commons.vfs.provider;
018:
019: import org.apache.commons.vfs.FileContent;
020: import org.apache.commons.vfs.FileSystemException;
021:
022: import java.io.IOException;
023: import java.io.InputStream;
024: import java.io.OutputStream;
025: import java.net.URL;
026: import java.net.URLConnection;
027:
028: /**
029: * A default URL connection that will work for most file systems.
030: *
031: * @author <a href="mailto:brian@mmmanager.org">Brian Olsen</a>
032: * @version $Revision: 480428 $ $Date: 2006-11-28 22:15:24 -0800 (Tue, 28 Nov 2006) $
033: */
034: public final class DefaultURLConnection extends URLConnection {
035: private final FileContent content;
036:
037: public DefaultURLConnection(final URL url, final FileContent content) {
038: super (url);
039: this .content = content;
040: }
041:
042: public void connect() {
043: connected = true;
044: }
045:
046: public InputStream getInputStream() throws IOException {
047: return content.getInputStream();
048: }
049:
050: public OutputStream getOutputStream() throws IOException {
051: return content.getOutputStream();
052: }
053:
054: public long getLastModified() {
055: try {
056: return content.getLastModifiedTime();
057: } catch (FileSystemException fse) {
058: }
059:
060: return -1;
061: }
062:
063: public int getContentLength() {
064: try {
065: return (int) content.getSize();
066: } catch (FileSystemException fse) {
067: }
068:
069: return -1;
070: }
071:
072: public String getContentType() {
073: try {
074: return content.getContentInfo().getContentType();
075: } catch (FileSystemException e) {
076: throw new RuntimeException(e.getMessage());
077: }
078: }
079:
080: public String getContentEncoding() {
081: try {
082: return content.getContentInfo().getContentEncoding();
083: } catch (FileSystemException e) {
084: throw new RuntimeException(e.getMessage());
085: }
086: }
087:
088: /*
089: public String getHeaderField(String name)
090: {
091: try
092: {
093: if (content.getFile().getFileSystem().hasCapability(Capability.ATTRIBUTES))
094: {
095: String value = (String) content.getAttribute(name);
096: if (value != null)
097: {
098: return value;
099: }
100: }
101:
102: return null;
103: }
104: catch (FileSystemException e)
105: {
106: throw new RuntimeException(e);
107: }
108: }
109: */
110: }
|