You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by ay...@apache.org on 2012/10/24 11:32:10 UTC

svn commit: r1401597 - in /cxf/trunk: rt/transports/http-hc/src/main/java/org/apache/cxf/transport/http/asyncclient/CXFHttpAsyncRequestProducer.java systests/transports/src/test/java/org/apache/cxf/systest/http_jetty/JettyCachedOutDigestAuthTest.java

Author: ay
Date: Wed Oct 24 09:32:09 2012
New Revision: 1401597

URL: http://svn.apache.org/viewvc?rev=1401597&view=rev
Log:
[CXF-4599] Async HTTP may fail for large data due to a file caching issue

Added:
    cxf/trunk/systests/transports/src/test/java/org/apache/cxf/systest/http_jetty/JettyCachedOutDigestAuthTest.java
Modified:
    cxf/trunk/rt/transports/http-hc/src/main/java/org/apache/cxf/transport/http/asyncclient/CXFHttpAsyncRequestProducer.java

Modified: cxf/trunk/rt/transports/http-hc/src/main/java/org/apache/cxf/transport/http/asyncclient/CXFHttpAsyncRequestProducer.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/transports/http-hc/src/main/java/org/apache/cxf/transport/http/asyncclient/CXFHttpAsyncRequestProducer.java?rev=1401597&r1=1401596&r2=1401597&view=diff
==============================================================================
--- cxf/trunk/rt/transports/http-hc/src/main/java/org/apache/cxf/transport/http/asyncclient/CXFHttpAsyncRequestProducer.java (original)
+++ cxf/trunk/rt/transports/http-hc/src/main/java/org/apache/cxf/transport/http/asyncclient/CXFHttpAsyncRequestProducer.java Wed Oct 24 09:32:09 2012
@@ -21,8 +21,11 @@ package org.apache.cxf.transport.http.as
 
 import java.io.FileInputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.net.URI;
 import java.nio.ByteBuffer;
+import java.nio.channels.Channels;
+import java.nio.channels.ReadableByteChannel;
 
 import org.apache.cxf.io.CachedOutputStream;
 import org.apache.http.HttpException;
@@ -39,7 +42,8 @@ public class CXFHttpAsyncRequestProducer
     private final SharedOutputBuffer buf;
     private volatile CachedOutputStream content;
     private volatile ByteBuffer buffer;
-    private volatile FileInputStream fis;
+    private volatile InputStream fis;
+    private volatile ReadableByteChannel chan;
     
     public CXFHttpAsyncRequestProducer(final CXFHttpRequest request, final SharedOutputBuffer buf) {
         super();
@@ -72,14 +76,16 @@ public class CXFHttpAsyncRequestProducer
                 if (content.getTempFile() == null) {
                     buffer = ByteBuffer.wrap(content.getBytes());
                 } else {
-                    fis = (FileInputStream)content.getInputStream();
+                    fis = content.getInputStream();
+                    chan = (fis instanceof FileInputStream) 
+                        ? ((FileInputStream)fis).getChannel() : Channels.newChannel(fis);
                     buffer = ByteBuffer.allocate(8 * 1024);
                 }
             }
             int i = -1;
-            if (!buffer.hasRemaining() && fis != null) {
-                buffer.reset();
-                i = fis.getChannel().read(buffer);
+            buffer.rewind();
+            if (buffer.hasRemaining() && chan != null) {
+                i = chan.read(buffer);
                 buffer.flip();
             }
             enc.write(buffer);
@@ -98,6 +104,7 @@ public class CXFHttpAsyncRequestProducer
             } catch (IOException io) {
                 //ignore
             }
+            chan = null;
             fis = null;
         }
         buffer = null;
@@ -126,6 +133,7 @@ public class CXFHttpAsyncRequestProducer
             } catch (IOException io) {
                 //ignore
             }
+            chan = null;
             fis = null;
         }
         buffer = null;

Added: cxf/trunk/systests/transports/src/test/java/org/apache/cxf/systest/http_jetty/JettyCachedOutDigestAuthTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/transports/src/test/java/org/apache/cxf/systest/http_jetty/JettyCachedOutDigestAuthTest.java?rev=1401597&view=auto
==============================================================================
--- cxf/trunk/systests/transports/src/test/java/org/apache/cxf/systest/http_jetty/JettyCachedOutDigestAuthTest.java (added)
+++ cxf/trunk/systests/transports/src/test/java/org/apache/cxf/systest/http_jetty/JettyCachedOutDigestAuthTest.java Wed Oct 24 09:32:09 2012
@@ -0,0 +1,51 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.cxf.systest.http_jetty;
+
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+
+
+/**
+ * Tests thread pool config.
+ */
+
+public class JettyCachedOutDigestAuthTest extends JettyDigestAuthTest {
+    private static String oldThreshold;
+    
+    @BeforeClass
+    public static void startServers() throws Exception {
+        oldThreshold = System.getProperty("org.apache.cxf.io.CachedOutputStream.Threshold");
+        // forces the CacheOutputStream to use temporary file caching
+        System.setProperty("org.apache.cxf.io.CachedOutputStream.Threshold", "8");
+        assertTrue("server did not launch correctly", 
+                   launchServer(JettyDigestServer.class, true));
+    }
+
+    @AfterClass
+    public static void cleanup() throws Exception {
+        if (oldThreshold == null) {
+            System.clearProperty("org.apache.cxf.io.CachedOutputStream.Threshold");
+        } else {
+            System.setProperty("org.apache.cxf.io.CachedOutputStream.Threshold", oldThreshold);
+        }
+    }
+
+}