You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@shindig.apache.org by jo...@apache.org on 2010/07/28 03:12:51 UTC

svn commit: r979922 - in /shindig/trunk/java/gadgets/src: main/java/org/apache/shindig/gadgets/http/BasicHttpFetcher.java test/java/org/apache/shindig/gadgets/http/BasicHttpFetcherTest.java

Author: johnh
Date: Wed Jul 28 01:12:50 2010
New Revision: 979922

URL: http://svn.apache.org/viewvc?rev=979922&view=rev
Log:
Fix NPE for ZLIB inflater workaround, when the exception thrown by the underlying inflater has e.getMessage() == null.

Patch provided by Gagan Singh and Vikas Arora.


Modified:
    shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/BasicHttpFetcher.java
    shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/http/BasicHttpFetcherTest.java

Modified: shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/BasicHttpFetcher.java
URL: http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/BasicHttpFetcher.java?rev=979922&r1=979921&r2=979922&view=diff
==============================================================================
--- shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/BasicHttpFetcher.java (original)
+++ shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/BasicHttpFetcher.java Wed Jul 28 01:12:50 2010
@@ -513,17 +513,24 @@ public class BasicHttpFetcher implements
         buffer.append(tmp, 0, l);
       }
     } catch (EOFException eofe) {
-      // Ref: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4040920
-      // Due to a bug in JDK ZLIB (InflaterInputStream), unexpected EOF error can occur.
-      // In such cases, even if the input stream is finished reading, the
-      // 'Inflater.finished()' call erroneously returns 'false' and
-      // 'java.util.zip.InflaterInputStream.fill' throws the 'EOFException'.
-      // So for such case, ignore the Exception in case Exception Cause is
-      // 'Unexpected end of ZLIB input stream'.
-      // For all other cases, re-throw the (EOF) Exception.
-      if ((instream.available() == 0) &&
-           eofe.getMessage().equals("Unexpected end of ZLIB input stream")) {
-        // Ignore
+      /**
+       * Ref: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4040920
+       * Due to a bug in JDK ZLIB (InflaterInputStream), unexpected EOF error can occur.
+       * In such cases, even if the input stream is finished reading, the
+       * 'Inflater.finished()' call erroneously returns 'false' and
+       * 'java.util.zip.InflaterInputStream.fill' throws the 'EOFException'.
+       * So for such case, ignore the Exception in case Exception Cause is
+       * 'Unexpected end of ZLIB input stream'.
+       *
+       * Also, ignore this exception in case the exception has no message
+       * body as this is the case where {@link GZIPInputStream#readUByte}
+       * throws EOFException with empty message. A bug has been filed with Sun
+       * and will be mentioned here once it is accepted.
+       */
+      if (instream.available() == 0 &&
+          (eofe.getMessage() == null ||
+           eofe.getMessage().equals("Unexpected end of ZLIB input stream"))) {
+        LOG.log(Level.FINE, "EOFException: ", eofe);
       } else {
         throw eofe;
       }

Modified: shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/http/BasicHttpFetcherTest.java
URL: http://svn.apache.org/viewvc/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/http/BasicHttpFetcherTest.java?rev=979922&r1=979921&r2=979922&view=diff
==============================================================================
--- shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/http/BasicHttpFetcherTest.java (original)
+++ shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/http/BasicHttpFetcherTest.java Wed Jul 28 01:12:50 2010
@@ -17,21 +17,22 @@
  */
 package org.apache.shindig.gadgets.http;
 
-import java.io.InputStream;
-import java.io.EOFException;
-import java.io.IOException;
-
 import org.apache.http.HttpEntity;
 import org.apache.shindig.common.uri.Uri;
 import org.apache.shindig.common.uri.UriBuilder;
 import org.easymock.EasyMock;
+import org.junit.AfterClass;
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
 import org.junit.Before;
+import org.junit.BeforeClass;
 import org.junit.Test;
 
+import java.io.EOFException;
+import java.io.IOException;
+import java.io.InputStream;
+
 public class BasicHttpFetcherTest {
   private static final int ECHO_PORT = 9003;
   protected static final Uri BASE_URL = Uri.parse("http://localhost:9003/");
@@ -59,13 +60,12 @@ public class BasicHttpFetcherTest {
     fetcher = new BasicHttpFetcher(BASE_URL.getAuthority());
 
     mockInputStream = EasyMock.createMock(InputStream.class);
-    //EasyMock.expect(mockInputStream.available()).andReturn(0).anyTimes();
     EasyMock.expect(mockInputStream.available()).andReturn(0);
     mockInputStream.close();
 
     mockEntity = EasyMock.createMock(HttpEntity.class);
     EasyMock.expect(mockEntity.getContent()).andReturn(mockInputStream);
-    EasyMock.expect(mockEntity.getContentLength()).andReturn(new Long(16384)).anyTimes();
+    EasyMock.expect(mockEntity.getContentLength()).andReturn(16384L).anyTimes();
   }
 
   @Test
@@ -83,17 +83,24 @@ public class BasicHttpFetcherTest {
 
   @Test
   public void testToByteArraySafeThrowsException1() throws Exception {
+    EasyMock.reset(mockInputStream);
+    mockInputStream.close();
+
     String exceptionMessage = "IO Exception and Any Random Cause";
     IOException e = new IOException(exceptionMessage);
     EasyMock.expect(mockInputStream.read(EasyMock.isA(byte[].class))).andThrow(e).anyTimes();
 
     EasyMock.replay(mockEntity, mockInputStream);
+    boolean exceptionCaught = false;
 
     try {
       fetcher.toByteArraySafe(mockEntity);
     } catch (IOException ioe) {
       assertEquals(exceptionMessage, ioe.getMessage());
+      exceptionCaught = true;
     }
+    assertTrue(exceptionCaught);
+    EasyMock.verify(mockEntity, mockInputStream);
   }
 
   @Test
@@ -103,16 +110,23 @@ public class BasicHttpFetcherTest {
     EasyMock.expect(mockInputStream.read(EasyMock.isA(byte[].class))).andThrow(e).anyTimes();
 
     EasyMock.replay(mockEntity, mockInputStream);
+    boolean exceptionCaught = false;
 
     try {
       fetcher.toByteArraySafe(mockEntity);
     } catch (EOFException eofe) {
       assertEquals(exceptionMessage, eofe.getMessage());
+      exceptionCaught = true;
     }
+    assertTrue(exceptionCaught);
+    EasyMock.verify(mockEntity, mockInputStream);
   }
 
   @Test
   public void testToByteArraySafeThrowsException3() throws Exception {
+    EasyMock.reset(mockInputStream);
+    mockInputStream.close();
+
     // Return non-zero for 'InputStream.available()'. This should violate the other condition.
     EasyMock.expect(mockInputStream.available()).andReturn(1);
     String exceptionMessage = "Unexpected end of ZLIB input stream";
@@ -120,12 +134,16 @@ public class BasicHttpFetcherTest {
     EasyMock.expect(mockInputStream.read(EasyMock.isA(byte[].class))).andThrow(e).anyTimes();
 
     EasyMock.replay(mockEntity, mockInputStream);
+    boolean exceptionCaught = false;
 
     try {
       fetcher.toByteArraySafe(mockEntity);
     } catch (EOFException eofe) {
       assertEquals(exceptionMessage, eofe.getMessage());
+      exceptionCaught = true;
     }
+    EasyMock.verify(mockEntity, mockInputStream);
+    assertTrue(exceptionCaught);
   }
 
   @Test
@@ -141,5 +159,21 @@ public class BasicHttpFetcherTest {
     } catch (EOFException eofe) {
       fail("Exception Should have been caught");
     }
+    EasyMock.verify(mockEntity, mockInputStream);
+  }
+
+  @Test
+  public void testToByteArraySafeHandlesExceptionWithNoMessage() throws Exception {
+    EOFException e = new EOFException();
+    EasyMock.expect(mockInputStream.read(EasyMock.isA(byte[].class))).andThrow(e).anyTimes();
+
+    EasyMock.replay(mockEntity, mockInputStream);
+
+    try {
+      fetcher.toByteArraySafe(mockEntity);
+    } catch (EOFException eofe) {
+      fail("Exception Should have been caught");
+    }
+    EasyMock.verify(mockEntity, mockInputStream);
   }
 }