View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  package org.apache.commons.vfs.provider;
    18
    19  import org.apache.commons.vfs.FileContent;
    20  import org.apache.commons.vfs.FileSystemException;
    21
    22  import java.io.IOException;
    23  import java.io.InputStream;
    24  import java.io.OutputStream;
    25  import java.net.URL;
    26  import java.net.URLConnection;
    27
    28  /***
    29   * A default URL connection that will work for most file systems.
    30   *
    31   * @author <a href="mailto:brian@mmmanager.org">Brian Olsen</a>
    32   * @version $Revision: 480428 $ $Date: 2006-11-29 07:15:24 +0100 (Mi, 29 Nov 2006) $
    33   */
    34  public final class DefaultURLConnection
    35      extends URLConnection
    36  {
    37      private final FileContent content;
    38
    39      public DefaultURLConnection(final URL url,
    40                                  final FileContent content)
    41      {
    42          super(url);
    43          this.content = content;
    44      }
    45
    46      public void connect()
    47      {
    48          connected = true;
    49      }
    50
    51      public InputStream getInputStream()
    52          throws IOException
    53      {
    54          return content.getInputStream();
    55      }
    56
    57      public OutputStream getOutputStream()
    58          throws IOException
    59      {
    60          return content.getOutputStream();
    61      }
    62
    63
    64      public long getLastModified()
    65  	{
    66          try
    67          {
    68              return content.getLastModifiedTime();
    69          }
    70          catch (FileSystemException fse)
    71          {
    72          }
    73
    74          return -1;
    75  	}
    76
    77  	public int getContentLength()
    78      {
    79          try
    80          {
    81              return (int) content.getSize();
    82          }
    83          catch (FileSystemException fse)
    84          {
    85          }
    86
    87          return -1;
    88      }
    89
    90      public String getContentType()
    91      {
    92          try
    93          {
    94              return content.getContentInfo().getContentType();
    95          }
    96          catch (FileSystemException e)
    97          {
    98              throw new RuntimeException(e.getMessage());
    99          }
    100     }
    101
    102     public String getContentEncoding()
    103     {
    104         try
    105         {
    106             return content.getContentInfo().getContentEncoding();
    107         }
    108         catch (FileSystemException e)
    109         {
    110             throw new RuntimeException(e.getMessage());
    111         }
    112     }
    113
    114     /*
    115     public String getHeaderField(String name)
    116     {
    117         try
    118         {
    119             if (content.getFile().getFileSystem().hasCapability(Capability.ATTRIBUTES))
    120             {
    121                 String value = (String) content.getAttribute(name);
    122                 if (value != null)
    123                 {
    124                     return value;
    125                 }
    126             }
    127
    128             return null;
    129         }
    130         catch (FileSystemException e)
    131         {
    132             throw new RuntimeException(e);
    133         }
    134     }
    135     */
    136 }