harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From zhouke...@apache.org
Subject svn commit: r935873 - in /harmony/enhanced/java/trunk/classlib/modules/luni/src: main/java/org/apache/harmony/luni/internal/net/www/protocol/file/ test/api/common/org/apache/harmony/luni/tests/internal/net/www/protocol/file/
Date Tue, 20 Apr 2010 11:27:38 GMT
Author: zhoukevin
Date: Tue Apr 20 11:27:38 2010
New Revision: 935873

URL: http://svn.apache.org/viewvc?rev=935873&view=rev
Log:
Apply patch for HARMONY-6504: [classlib][luni]Harmony returns null for header related function
when file URL is handled. This patch implements the header related functions [getHeaderField(int)/getHeaderFieldKey(int)/getHeaderField(String)/getLastModified()]
to prevent Harmony to return null when file URL is handled.
 

Modified:
    harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/file/FileURLConnection.java
    harmony/enhanced/java/trunk/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/internal/net/www/protocol/file/FileURLConnectionTest.java

Modified: harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/file/FileURLConnection.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/file/FileURLConnection.java?rev=935873&r1=935872&r2=935873&view=diff
==============================================================================
--- harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/file/FileURLConnection.java
(original)
+++ harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/file/FileURLConnection.java
Tue Apr 20 11:27:38 2010
@@ -28,6 +28,9 @@ import java.io.InputStream;
 import java.io.PrintStream;
 import java.net.URL;
 import java.net.URLConnection;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.LinkedHashMap;
 
 import org.apache.harmony.luni.internal.net.www.MimeTable;
 import org.apache.harmony.luni.util.Util;
@@ -48,8 +51,12 @@ public class FileURLConnection extends U
 
     private boolean isDir;
 
+    private long lastModified = 0;
+
     private FilePermission permission;
 
+    private final LinkedHashMap<String, String> header;
+
     /**
      * Creates an instance of <code>FileURLConnection</code> for establishing
      * a connection to the file pointed by this <code>URL<code>
@@ -63,6 +70,7 @@ public class FileURLConnection extends U
             fileName = ""; //$NON-NLS-1$
         }
         fileName = Util.decode(fileName, false);
+        header = new LinkedHashMap<String, String>();
     }
 
     /**
@@ -76,15 +84,90 @@ public class FileURLConnection extends U
     @Override
     public void connect() throws IOException {
         File f = new File(fileName);
-        if (f.isDirectory()) {
-            isDir = true;
+        if (isDir = f.isDirectory()) {
             is = getDirectoryListing(f);
-            // use -1 for the contentLength
         } else {
             is = new BufferedInputStream(new FileInputStream(f));
             length = is.available();
+            lastModified = f.lastModified();
         }
         connected = true;
+        // updaetHeader must be after "connected = true", for updateHeader
+        // invokes getContentType which back invokes connect causing
+        // StackOverflow
+        updateHeader();
+    }
+
+    /**
+     * Function updates the header fields of a file.
+     */
+    private void updateHeader() {
+        String contentType = getContentType();
+        if (contentType != null) {
+            header.put("content-type", contentType); //$NON-NLS-1$
+        }
+        if (length >= 0) {
+            header.put("content-length", Integer.toString(length)); //$NON-NLS-1$
+        }
+        if (lastModified != 0) {
+            SimpleDateFormat dateFormat = new SimpleDateFormat(
+                    "EEE, dd MMM yyyy HH:mm:ss 'GMT'"); //$NON-NLS-1$
+            header.put("last-modified", dateFormat.format(new Date( //$NON-NLS-1$
+                    lastModified)));
+        }
+    }
+
+    @Override
+    public String getHeaderField(String key) {
+        try {
+            if (!connected) {
+                connect();
+            }
+        } catch (IOException e) {
+            // Ignored
+        }
+        return header.get(key);
+    }
+
+    @Override
+    public String getHeaderField(int index) {
+        try {
+            if (!connected) {
+                connect();
+            }
+        } catch (IOException e) {
+            // Ignored
+        }
+        if (index > -1 && index < header.size()) {
+            return header.values().toArray(new String[0])[index];
+        }
+        return null;
+    }
+
+    @Override
+    public String getHeaderFieldKey(int index) {
+        try {
+            if (!connected) {
+                connect();
+            }
+        } catch (IOException e) {
+            // Ignored
+        }
+        if (index > -1 && index < header.size()) {
+            return header.keySet().toArray(new String[0])[index];
+        }
+        return null;
+    }
+
+    public long getLastModified() {
+        try {
+            if (!connected) {
+                connect();
+            }
+        } catch (IOException e) {
+            // default is -1
+        }
+        return lastModified;
     }
 
     /**

Modified: harmony/enhanced/java/trunk/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/internal/net/www/protocol/file/FileURLConnectionTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/trunk/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/internal/net/www/protocol/file/FileURLConnectionTest.java?rev=935873&r1=935872&r2=935873&view=diff
==============================================================================
--- harmony/enhanced/java/trunk/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/internal/net/www/protocol/file/FileURLConnectionTest.java
(original)
+++ harmony/enhanced/java/trunk/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/internal/net/www/protocol/file/FileURLConnectionTest.java
Tue Apr 20 11:27:38 2010
@@ -18,6 +18,7 @@ package org.apache.harmony.luni.tests.in
 
 import java.io.IOException;
 import java.net.URL;
+import java.net.URLConnection;
 
 import junit.framework.TestCase;
 
@@ -63,4 +64,36 @@ public class FileURLConnectionTest exten
         assertNotNull(conn.getInputStream());
         assertEquals("file",conn.getURL().getProtocol());
     }
+
+    public void testHeaderFunctions() throws IOException {
+        String resourceName = "org/apache/harmony/luni/tests/";  //folder name
+        URL url = ClassLoader.getSystemClassLoader().getResource(resourceName);
+        FileURLConnection conn = new FileURLConnection(url);
+        assertNotNull(conn.getInputStream());
+        assertEquals(conn.getContentType(),  conn.getHeaderField("content-type")) ;
+        
+        resourceName = "org/apache/harmony/luni/tests/" +  "test.rtf";;  //folder name
+        url = ClassLoader.getSystemClassLoader().getResource(resourceName);
+        conn = new FileURLConnection(url);
+        assertNotNull(conn.getInputStream());
+        assertEquals(conn.getContentType(),  conn.getHeaderField("content-type")) ;
+        assertEquals(Integer.toString(conn.getContentLength()),  conn.getHeaderField("content-length"))
;
+        assertEquals(conn.getHeaderField(0), conn.getHeaderField("content-type"));
+        assertEquals(conn.getHeaderField(1), conn.getHeaderField("content-length"));
+        assertEquals(conn.getHeaderField(2), conn.getHeaderField("last-modified"));
+        assertEquals("last-modified", conn.getHeaderFieldKey(2));
+        assertEquals("content-length", conn.getHeaderFieldKey(1));
+        assertEquals("content-type", conn.getHeaderFieldKey(0));
+    }
+
+    public void testHeader_BoundaryCheck() throws IOException {
+        String resourceName = "org/apache/harmony/luni/tests/";
+        URL url = ClassLoader.getSystemClassLoader().getResource(resourceName);
+        URLConnection urlConnection = url.openConnection();
+        assertNull(urlConnection.getHeaderField(Integer.MIN_VALUE));
+        assertNull(urlConnection.getHeaderField(Integer.MAX_VALUE));
+        assertNull(urlConnection.getHeaderFieldKey(Integer.MIN_VALUE));
+        assertNull(urlConnection.getHeaderFieldKey(Integer.MAX_VALUE));
+        assertNull(urlConnection.getHeaderField(null));
+    }
 }



Mime
View raw message