[jboss-cvs] JBossAS SVN: r102161 - in projects/vfs/trunk/src: test/java/org/jboss/test/vfs and 1 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Tue Mar 9 15:13:19 EST 2010


Author: johnbailey
Date: 2010-03-09 15:13:18 -0500 (Tue, 09 Mar 2010)
New Revision: 102161

Added:
   projects/vfs/trunk/src/main/java/org/jboss/vfs/protocol/AbstractURLConnection.java
Modified:
   projects/vfs/trunk/src/main/java/org/jboss/vfs/protocol/FileURLConnection.java
   projects/vfs/trunk/src/main/java/org/jboss/vfs/protocol/VirtualFileURLConnection.java
   projects/vfs/trunk/src/test/java/org/jboss/test/vfs/URLConnectionUnitTestCase.java
   projects/vfs/trunk/src/test/resources/vfs/test/test-web.xml
Log:
[JBVFS-149] - Updated VFS URLConnection impls to support content-type/content-length/last-modified headers

Added: projects/vfs/trunk/src/main/java/org/jboss/vfs/protocol/AbstractURLConnection.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/vfs/protocol/AbstractURLConnection.java	                        (rev 0)
+++ projects/vfs/trunk/src/main/java/org/jboss/vfs/protocol/AbstractURLConnection.java	2010-03-09 20:13:18 UTC (rev 102161)
@@ -0,0 +1,103 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2010, 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.vfs.protocol;
+
+import org.jboss.vfs.VFSUtils;
+
+import java.io.BufferedInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.net.URLConnection;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Locale;
+import java.util.TimeZone;
+
+/**
+ * Abstract base class for VFS URLConection impls.
+ *
+ * @author <a href=mailto:jbailey at redhat.com">John Bailey</a>
+ * @version $Revision$
+ */
+public abstract class AbstractURLConnection extends URLConnection {
+
+   private String contentType;
+
+   protected AbstractURLConnection(final URL url) {
+      super(url);
+   }
+
+   public String getHeaderField(String name) {
+      String headerField = null;
+      if(name.equals("content-type")) {
+         headerField = getContentType();
+      } else if(name.equals("content-length")) {
+         headerField = String.valueOf(getContentLength());
+      } else if(name.equals("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 {
+         headerField = super.getHeaderField(name);
+      }
+      return headerField;
+   }
+
+   public String getContentType() {
+      if(contentType != null)
+         return contentType;
+      contentType = super.getHeaderField("content-type");
+      if(contentType == null) {
+         contentType = getFileNameMap().getContentTypeFor(getName());
+         if(contentType == null) {
+            try {
+               InputStream is = getInputStream();
+               BufferedInputStream bis = new BufferedInputStream(is);
+               contentType = java.net.URLConnection.guessContentTypeFromStream(bis);
+               bis.close();
+            } catch(IOException e) { /* ignore */ }
+         }
+      }
+      return contentType;
+   }
+
+   protected static URI toURI(URL url) throws IOException {
+      try {
+         return VFSUtils.toURI(url);
+      }
+      catch(URISyntaxException e) {
+         IOException ioe = new IOException();
+         ioe.initCause(e);
+         throw ioe;
+      }
+   }
+
+   protected abstract String getName();
+}

Modified: projects/vfs/trunk/src/main/java/org/jboss/vfs/protocol/FileURLConnection.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/vfs/protocol/FileURLConnection.java	2010-03-09 20:07:17 UTC (rev 102160)
+++ projects/vfs/trunk/src/main/java/org/jboss/vfs/protocol/FileURLConnection.java	2010-03-09 20:13:18 UTC (rev 102161)
@@ -22,7 +22,6 @@
 package org.jboss.vfs.protocol;
 
 import org.jboss.vfs.VFS;
-import org.jboss.vfs.VFSUtils;
 import org.jboss.vfs.VirtualFile;
 import org.jboss.vfs.spi.RootFileSystem;
 
@@ -30,7 +29,7 @@
 import java.io.FilePermission;
 import java.io.IOException;
 import java.io.InputStream;
-import java.net.*;
+import java.net.URL;
 import java.security.Permission;
 
 /**
@@ -39,7 +38,7 @@
  * @author <a href=mailto:jbailey at redhat.com">John Bailey</a>
  * @version $Revision$
  */
-public class FileURLConnection extends URLConnection {
+public class FileURLConnection extends AbstractURLConnection {
 
    private final RootFileSystem rootFileSystem = RootFileSystem.ROOT_INSTANCE;
 
@@ -74,21 +73,16 @@
       return rootFileSystem.openInputStream(mountPoint, file);
    }
 
-    @Override
-    public void connect() throws IOException {
-    }
+   @Override
+   public Permission getPermission() throws IOException {
+      return new FilePermission(file.getPathName(), "read");
+   }
 
-    private static URI toURI(URL url) throws IOException
-   {
-      try
-      {
-         return VFSUtils.toURI(url);
-      }
-      catch (URISyntaxException e)
-      {
-         IOException ioe = new IOException();
-         ioe.initCause(e);
-         throw ioe;
-      }
+   public void connect() throws IOException {
    }
+
+   @Override
+   protected String getName() {
+      return file.getName();
+   }
 }

Modified: projects/vfs/trunk/src/main/java/org/jboss/vfs/protocol/VirtualFileURLConnection.java
===================================================================
--- projects/vfs/trunk/src/main/java/org/jboss/vfs/protocol/VirtualFileURLConnection.java	2010-03-09 20:07:17 UTC (rev 102160)
+++ projects/vfs/trunk/src/main/java/org/jboss/vfs/protocol/VirtualFileURLConnection.java	2010-03-09 20:13:18 UTC (rev 102161)
@@ -22,17 +22,13 @@
 package org.jboss.vfs.protocol;
 
 import org.jboss.vfs.VFS;
-import org.jboss.vfs.VFSUtils;
 import org.jboss.vfs.VirtualFile;
 
 import java.io.File;
 import java.io.FilePermission;
 import java.io.IOException;
 import java.io.InputStream;
-import java.net.URI;
-import java.net.URISyntaxException;
 import java.net.URL;
-import java.net.URLConnection;
 import java.security.Permission;
 
 /**
@@ -42,7 +38,7 @@
  * @author <a href="ales.justin at jboss.com">Ales Justin</a>
  * @version $Revision: 1.1 $
  */
-class VirtualFileURLConnection extends URLConnection
+class VirtualFileURLConnection extends AbstractURLConnection
 {
    private final VirtualFile file;
 
@@ -86,17 +82,8 @@
       return new FilePermission(decodedPath, "read");
    }
 
-   private static URI toURI(URL url) throws IOException
-   {
-      try
-      {
-         return VFSUtils.toURI(url);
-      }
-      catch (URISyntaxException e)
-      {
-         IOException ioe = new IOException();
-         ioe.initCause(e);
-         throw ioe;
-      }
+   @Override
+   protected String getName() {
+      return file.getName();
    }
 }

Modified: projects/vfs/trunk/src/test/java/org/jboss/test/vfs/URLConnectionUnitTestCase.java
===================================================================
--- projects/vfs/trunk/src/test/java/org/jboss/test/vfs/URLConnectionUnitTestCase.java	2010-03-09 20:07:17 UTC (rev 102160)
+++ projects/vfs/trunk/src/test/java/org/jboss/test/vfs/URLConnectionUnitTestCase.java	2010-03-09 20:13:18 UTC (rev 102161)
@@ -148,6 +148,16 @@
       assertEquals(file.getLastModified(), conn.getLastModified());
    }
 
+   public void testVfsUrlContentType() throws Exception
+   {
+      URL url = getResource("/vfs/test/test-web.xml");
+      VirtualFile xml = VFS.getChild(url);
+      URLConnection conn = xml.toURL().openConnection();
+      String contentType = conn.getContentType();
+      assertNotNull(contentType);
+      assertEquals("application/xml", contentType);
+   }
+
    public void testOutsideUrl() throws Exception
    {
       URL url = getResource("/vfs/test/outer.jar");
@@ -194,6 +204,35 @@
       }
    }
 
+   public void testFileUrlContentType() throws Exception
+   {
+      VFS.getChild("");
+      URL url = getResource("/vfs/test/test-web.xml");
+      url = new URL("file", url.getHost(), url.getFile());
+      
+      URLConnection conn = url.openConnection();
+      String contentType = conn.getContentType();
+      assertNotNull(contentType);
+      assertEquals("application/xml", contentType);
+   }
+
+   public void testHeaderFields() throws Exception
+   {
+      VFS.getChild("");
+      URL url = getResource("/vfs/test/test-web.xml");
+      url = new URL("file", url.getHost(), url.getFile());
+
+      URLConnection conn = url.openConnection();
+      int contentLength = conn.getContentLength();
+      assertTrue(contentLength > 0);
+      String contentLengthHeader = conn.getHeaderField("content-length");
+      assertEquals(String.valueOf(contentLength), contentLengthHeader);
+
+      assertTrue(conn.getLastModified() > 0);
+      String lastModifiedHeader = conn.getHeaderField("last-modified");
+      assertNotNull(lastModifiedHeader);
+   }
+
    protected static byte[] readBytes(InputStream inputStream) throws Exception
    {
       ByteArrayOutputStream baos = new ByteArrayOutputStream();

Modified: projects/vfs/trunk/src/test/resources/vfs/test/test-web.xml
===================================================================
--- projects/vfs/trunk/src/test/resources/vfs/test/test-web.xml	2010-03-09 20:07:17 UTC (rev 102160)
+++ projects/vfs/trunk/src/test/resources/vfs/test/test-web.xml	2010-03-09 20:13:18 UTC (rev 102161)
@@ -0,0 +1,3 @@
+<web>
+    
+</web>
\ No newline at end of file




More information about the jboss-cvs-commits mailing list