Save This Page
Home » jboss-5.0.0.CR1-src » org » jboss » net » protocol » file » [javadoc | source]
    1   /***************************************
    2    *                                     *
    3    *  JBoss: The OpenSource J2EE WebOS   *
    4    *                                     *
    5    *  Distributable under LGPL license.  *
    6    *  See terms of license at gnu.org.   *
    7    *                                     *
    8    ***************************************/
    9   
   10   package org.jboss.net.protocol.file;
   11   
   12   import java.io.File;
   13   import java.io.FileNotFoundException;
   14   import java.io.IOException;
   15   import java.io.InputStream;
   16   import java.io.OutputStream;
   17   import java.io.FileInputStream;
   18   import java.io.FileOutputStream;
   19   
   20   import java.net.URLConnection;
   21   import java.net.URL;
   22   import java.net.MalformedURLException;
   23   
   24   import java.security.Permission;
   25   import java.io.FilePermission;
   26   
   27   /**
   28    * Provides local file access via URL semantics, correctly returning
   29    * the last modified time of the underlying file.
   30    *
   31    * @version $Revision: 1.7 $
   32    * @author  <a href="mailto:jason@planet57.com">Jason Dillon</a>
   33    * @author  Scott.Stark@jboss.org
   34    */
   35   public class FileURLConnection
   36      extends URLConnection
   37   {
   38      protected File file;
   39   
   40      public FileURLConnection(final URL url)
   41         throws MalformedURLException, IOException
   42      {
   43         super(url);
   44         
   45         file = new File(url.getPath().replace('/', File.separatorChar).replace('|', ':'));
   46   
   47         doOutput = false;
   48      }
   49   
   50      /**
   51       * Returns the underlying file for this connection.
   52       */
   53      public File getFile()
   54      {
   55         return file;
   56      }
   57   
   58      /**
   59       * Checks if the underlying file for this connection exists.
   60       *
   61       * @throws FileNotFoundException
   62       */
   63      public void connect() throws IOException
   64      {
   65         if (connected)
   66            return;
   67   
   68         if (!file.exists())
   69         {
   70            throw new FileNotFoundException(file.getPath());
   71         }
   72         
   73         connected = true;
   74      }
   75   
   76      public InputStream getInputStream() throws IOException
   77      {
   78         if (!connected)
   79            connect();
   80   
   81         return new FileInputStream(file);
   82      }
   83   
   84      public OutputStream getOutputStream() throws IOException
   85      {
   86         if (!connected)
   87            connect();
   88         
   89         return new FileOutputStream(file);
   90      }
   91   
   92      /**
   93       * Provides support for returning the value for the
   94       * <tt>last-modified</tt> header.
   95       */
   96      public String getHeaderField(final String name)
   97      {
   98         String headerField = null;
   99         if (name.equalsIgnoreCase("last-modified"))
  100            headerField = String.valueOf(getLastModified());
  101         else if (name.equalsIgnoreCase("content-length"))
  102            headerField = String.valueOf(file.length());
  103         else if (name.equalsIgnoreCase("content-type"))
  104            headerField = getFileNameMap().getContentTypeFor(file.getName());
  105         else if (name.equalsIgnoreCase("date"))
  106            headerField = String.valueOf(file.lastModified());
  107         else
  108         {
  109            // This always returns null currently
  110            headerField = super.getHeaderField(name);
  111         }
  112         return headerField;
  113      }
  114   
  115      /**
  116       * Return a permission for both read & write since both
  117       * input and output streams are supported.
  118       */
  119      public Permission getPermission() throws IOException
  120      {
  121         return new FilePermission(file.getPath(), "read,write");
  122      }
  123   
  124      /**
  125       * Returns the last modified time of the underlying file.
  126       */
  127      public long getLastModified()
  128      {
  129         return file.lastModified();
  130      }
  131   }

Save This Page
Home » jboss-5.0.0.CR1-src » org » jboss » net » protocol » file » [javadoc | source]