[jboss-svn-commits] JBoss Common SVN: r2859 - in common-core/trunk/src: main/java/org/jboss/net/protocol/file and 1 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Tue Jun 10 13:36:21 EDT 2008


Author: dimitris at jboss.org
Date: 2008-06-10 13:36:21 -0400 (Tue, 10 Jun 2008)
New Revision: 2859

Added:
   common-core/trunk/src/main/java/org/jboss/net/protocol/file/FileURLConnection.java
   common-core/trunk/src/main/java/org/jboss/net/protocol/file/Handler.java
Modified:
   common-core/trunk/src/main/java/org/jboss/net/protocol/URLStreamHandlerFactory.java
   common-core/trunk/src/test/java/org/jboss/test/util/test/protocol/FileURLConnectionTestCase.java
Log:
JBCOMMON-56, put back org.jboss.protocol.file.FileURLConnection and add missing functionality

Modified: common-core/trunk/src/main/java/org/jboss/net/protocol/URLStreamHandlerFactory.java
===================================================================
--- common-core/trunk/src/main/java/org/jboss/net/protocol/URLStreamHandlerFactory.java	2008-06-08 16:26:19 UTC (rev 2858)
+++ common-core/trunk/src/main/java/org/jboss/net/protocol/URLStreamHandlerFactory.java	2008-06-10 17:36:21 UTC (rev 2859)
@@ -108,6 +108,7 @@
 
    /** A list of JBoss specific protocols for preloading. */
    public static final String PROTOCOLS[] = {
+      "file",
       "resource"
    };
 

Copied: common-core/trunk/src/main/java/org/jboss/net/protocol/file/FileURLConnection.java (from rev 2851, common-core/trunk/src/main/java/org/jboss/net/protocol/file/FileURLConnection.java)
===================================================================
--- common-core/trunk/src/main/java/org/jboss/net/protocol/file/FileURLConnection.java	                        (rev 0)
+++ common-core/trunk/src/main/java/org/jboss/net/protocol/file/FileURLConnection.java	2008-06-10 17:36:21 UTC (rev 2859)
@@ -0,0 +1,211 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.net.protocol.file;
+
+import java.io.BufferedInputStream;
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.FilePermission;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLConnection;
+import java.net.URLDecoder;
+import java.security.Permission;
+import java.text.SimpleDateFormat;
+import java.util.Arrays;
+import java.util.Date;
+import java.util.Locale;
+import java.util.TimeZone;
+
+/**
+ * Provides local file access via URL semantics, correctly returning
+ * the last modified time of the underlying file.
+ *
+ * @author  <a href="mailto:jason at planet57.com">Jason Dillon</a>
+ * @author  <a href="mailto:scott.stark at jboss.org">Scott Stark</a>
+ * @author  <a href="mailto:dimitris at jboss.org">Dimitris Andreadis</a>
+ * @version $Revision$
+ */
+public class FileURLConnection extends URLConnection
+{
+   static boolean decodeFilePaths = true;
+   static
+   {
+      String flag = System.getProperty("org.jboss.net.protocol.file.decodeFilePaths");
+      if (flag != null)
+         decodeFilePaths = Boolean.valueOf(flag).booleanValue();
+   }
+   
+   /** The underlying file */
+   protected File file;
+
+   public FileURLConnection(final URL url) throws MalformedURLException, IOException
+   {
+      super(url);
+      String path = url.getPath();
+      if (decodeFilePaths)
+         path = URLDecoder.decode(path, "UTF-8");
+      
+      // Convert the url '/' to the os file separator
+      file = new File(path.replace('/', File.separatorChar).replace('|', ':'));
+
+      doOutput = false;
+   }
+
+   /**
+    * Returns the underlying file for this connection.
+    * @return the file
+    */
+   public File getFile()
+   {
+      return file;
+   }
+
+   /**
+    * Checks if the underlying file for this connection exists.
+    *
+    * @throws FileNotFoundException
+    */
+   public void connect() throws IOException
+   {
+      if (connected)
+         return;
+
+      if (!file.exists())
+      {
+         throw new FileNotFoundException(file.getPath());
+      }
+      connected = true;
+   }
+
+   public InputStream getInputStream() throws IOException
+   {
+      if (!connected)
+         connect();
+
+      if (file.isDirectory())
+      {
+         String[] files = file.list();
+         Arrays.sort(files);
+         StringBuilder sb = new StringBuilder();
+         for (int i = 0; i < files.length; i++)
+         {
+            sb.append(files[i]).append("\n");
+         }
+         return new ByteArrayInputStream(sb.toString().getBytes());
+      }
+      else
+      {
+         return new FileInputStream(file);
+      }
+   }
+
+   public OutputStream getOutputStream() throws IOException
+   {
+      if (!connected)
+         connect();
+      
+      SecurityManager sm = System.getSecurityManager();
+      if (sm != null)
+      {
+         // Check for write access
+         FilePermission p = new FilePermission(file.getPath(), "write");
+         sm.checkPermission(p);
+      }
+      return new FileOutputStream(file);
+   }
+
+   /**
+    * Provides support for returning the value for the
+    * <tt>last-modified</tt> header.
+    */
+   public String getHeaderField(final String name)
+   {
+      String headerField = null;
+      if (name.equalsIgnoreCase("last-modified"))
+      {
+         long lastModified = getLastModified();
+         if (lastModified != 0)
+         {
+            // return the last modified date formatted according to RFC 1123
+            Date modifiedDate = new Date(lastModified);
+            SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US);
+            sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
+            headerField = sdf.format(modifiedDate);
+         }
+      }
+      else if (name.equalsIgnoreCase("content-length"))
+      {
+         headerField = String.valueOf(file.length());
+      }
+      else if (name.equalsIgnoreCase("content-type"))
+      {
+         headerField = getFileNameMap().getContentTypeFor(file.getName());
+         if (headerField == null)
+         {
+            try
+            {
+               InputStream is = getInputStream();
+               BufferedInputStream bis = new BufferedInputStream(is);
+               headerField = URLConnection.guessContentTypeFromStream(bis);
+               bis.close();
+            }
+            catch(IOException e)
+            {
+               // ignore
+            }
+         }
+      }
+      else if (name.equalsIgnoreCase("date"))
+      {
+         headerField = String.valueOf(getLastModified());
+      }
+      else
+      {
+         // This always returns null currently
+         headerField = super.getHeaderField(name);
+      }
+      return headerField;
+   }
+
+   /** 
+    * Return a permission for reading of the file
+    */
+   public Permission getPermission() throws IOException
+   {
+      return new FilePermission(file.getPath(), "read");
+   }
+
+   /**
+    * Returns the last modified time of the underlying file.
+    */
+   public long getLastModified()
+   {
+      return file.lastModified();
+   }
+}

Copied: common-core/trunk/src/main/java/org/jboss/net/protocol/file/Handler.java (from rev 2851, common-core/trunk/src/main/java/org/jboss/net/protocol/file/Handler.java)
===================================================================
--- common-core/trunk/src/main/java/org/jboss/net/protocol/file/Handler.java	                        (rev 0)
+++ common-core/trunk/src/main/java/org/jboss/net/protocol/file/Handler.java	2008-06-10 17:36:21 UTC (rev 2859)
@@ -0,0 +1,47 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.net.protocol.file;
+
+import java.io.IOException;
+import java.net.URL;
+import java.net.URLConnection;
+import java.net.URLStreamHandler;
+
+/**
+ * A protocol handler for the 'file' protocol.
+ *
+ * @version <tt>$Revision$</tt>
+ * @author  <a href="mailto:jason at planet57.com">Jason Dillon</a>
+ * @author  <a href="mailto:dimitris at jboss.org">Dimitris Andreadis</a>
+ */
+public class Handler extends URLStreamHandler
+{
+   public URLConnection openConnection(final URL url) throws IOException
+   {
+      return new FileURLConnection(url);
+   }
+
+   protected void parseURL(final URL url, final String s, final int i, final int j)
+   {
+      super.parseURL(url, s.replace(java.io.File.separatorChar, '/'), i, j);
+   }
+}

Modified: common-core/trunk/src/test/java/org/jboss/test/util/test/protocol/FileURLConnectionTestCase.java
===================================================================
--- common-core/trunk/src/test/java/org/jboss/test/util/test/protocol/FileURLConnectionTestCase.java	2008-06-08 16:26:19 UTC (rev 2858)
+++ common-core/trunk/src/test/java/org/jboss/test/util/test/protocol/FileURLConnectionTestCase.java	2008-06-10 17:36:21 UTC (rev 2859)
@@ -21,13 +21,18 @@
  */
 package org.jboss.test.util.test.protocol;
 
+import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileOutputStream;
+import java.io.InputStreamReader;
 import java.net.URL;
 import java.net.URLConnection;
 
 import junit.framework.TestCase;
 
+import org.jboss.net.protocol.file.FileURLConnection;
+import org.jboss.util.file.Files;
+
 /**
  * Tests of the expected jdk file: url connection protocol handler behaviors.
  * 
@@ -37,31 +42,42 @@
  */
 public class FileURLConnectionTestCase extends TestCase
 {
-   public void testLastModified()
-      throws Exception
+   public void testLastModified() throws Exception
    {
-      File tmp = File.createTempFile("testLastModified", "test");
-      tmp.deleteOnExit();
+      File tmp = File.createTempFile("testLastModified", "");
       long lastModified = tmp.lastModified();
-      System.out.print(tmp.getAbsolutePath()+", lastModified:"+lastModified);
+      System.out.println("Created file: " + tmp.getAbsolutePath() + ", lastModified:" + lastModified);
+      
+      // This will return JDK's provided FileURLConnection
       URL tmpURL = tmp.toURL();
       URLConnection tmpConn = tmpURL.openConnection();
+      System.out.println("Got URLConnection of type: " + tmpConn.getClass().getName());
       assertEquals(lastModified, tmpConn.getLastModified());
-      long lastModifiedHdr = tmpConn.getHeaderFieldDate("Last-Modified", 0);
-      System.out.println(", Last-Modified: "+lastModifiedHdr);
-      System.out.println("Got URLConnection of type: " + tmpConn.getClass().getName());
+      
+      long lastModifiedHdr = tmpConn.getHeaderFieldDate("last-modified", 0);
+      System.out.println("last-modified header: "+lastModifiedHdr);
       // the last-modified header is expected to strip the milliseconds to
       // comply with the (dd MMM yyyy HH:mm:ss) format, so the following assertions
       // is invalid on windows that provide millisecond accuracy to File.lastModified()
       // see, http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4504473
       // assertEquals(lastModified, lastModifiedHdr);
+      
+      // Test that JBoss FileURLConnection matches JDK's FileURLConnection results
+       tmpConn = new FileURLConnection(tmpURL);
+       tmpConn.connect();
+       System.out.println("Got URLConnection of type: " + tmpConn.getClass().getName());
+       assertEquals(lastModified, tmpConn.getLastModified());       
+      
+       lastModifiedHdr = tmpConn.getHeaderFieldDate("last-modified", 0);
+       System.out.println("last-modified header: "+lastModifiedHdr);
+
+       // cleanup
+       tmp.delete();
    }
 
-   public void testLength()
-      throws Exception
+   public void testLength() throws Exception
    {
-      File tmp = File.createTempFile("testLength", "test");
-      tmp.deleteOnExit();
+      File tmp = File.createTempFile("testLength", "");
       FileOutputStream fos = new FileOutputStream(tmp);
       fos.write("testLength".getBytes());
       fos.close();
@@ -73,5 +89,53 @@
       assertEquals(tmp.length(), length);
       int lengthHdr = tmpConn.getHeaderFieldInt("Content-Length", 0);
       assertEquals(length, lengthHdr);
+
+      // cleanup
+      tmp.delete();
    }
+   
+   public void testDirectoryList() throws Exception
+   {
+      // create a directory structure
+      //    testDirectoryList####/
+      //    +-test.txt
+      //    \-test.dir/
+      File rootDir = File.createTempFile("testDirectoryList", "");
+      rootDir.delete();
+      rootDir.mkdir();
+      System.out.println(rootDir);
+      File tmpFile = new File(rootDir, "test.txt");
+      tmpFile.createNewFile();
+      System.out.println(tmpFile);
+      FileOutputStream fos = new FileOutputStream(tmpFile);
+      fos.write("this is a test file".getBytes());
+      fos.close();
+      File tmpDir = new File(rootDir, "test.dir");
+      tmpDir.mkdir();
+      System.out.println(tmpDir);
+      
+      URL rootURL = rootDir.toURL();
+      URLConnection rootConn = rootURL.openConnection();
+      System.out.println("Got URLConnection of type: " + rootConn.getClass().getName());      
+      BufferedReader in = new BufferedReader(new InputStreamReader(rootConn.getInputStream()));
+      // verify we can read the sorted file list
+      assertEquals("directory entry #1", "test.dir", in.readLine());
+      assertEquals("directory entry #2", "test.txt", in.readLine());
+      assertEquals("directory entry #3", null, in.readLine());
+      in.close();
+      
+      // Test that JBoss FileURLConnection matches JDK's FileURLConnection results
+      rootConn = new FileURLConnection(rootURL);
+      rootConn.connect();
+      System.out.println("Got URLConnection of type: " + rootConn.getClass().getName());
+      in = new BufferedReader(new InputStreamReader(rootConn.getInputStream()));
+      // verify we can read the sorted file list
+      assertEquals("directory entry #1", "test.dir", in.readLine());
+      assertEquals("directory entry #2", "test.txt", in.readLine());
+      assertEquals("directory entry #3", null, in.readLine());
+      in.close();      
+      
+      // cleanup
+      Files.delete(rootDir);
+   }
 }




More information about the jboss-svn-commits mailing list