You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ni...@apache.org on 2012/09/17 15:32:20 UTC

svn commit: r1386607 - in /camel/trunk/components/camel-jetty/src: main/java/org/apache/camel/component/jetty/ test/java/org/apache/camel/component/jetty/jettyproducer/

Author: ningjiang
Date: Mon Sep 17 13:32:19 2012
New Revision: 1386607

URL: http://svn.apache.org/viewvc?rev=1386607&view=rev
Log:
CAMEL-5621 fix the JettyHttpProducer close input stream issue when the exception is throw during sending the request

Added:
    camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/jettyproducer/JettyHttpProducerInvalidDestinationTest.java
    camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/jettyproducer/MyInputStream.java
Modified:
    camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyContentExchange.java
    camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/jettyproducer/JettyHttpProducerTimeoutTest.java

Modified: camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyContentExchange.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyContentExchange.java?rev=1386607&r1=1386606&r2=1386607&view=diff
==============================================================================
--- camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyContentExchange.java (original)
+++ camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyContentExchange.java Mon Sep 17 13:32:19 2012
@@ -94,12 +94,8 @@ public class JettyContentExchange extend
     @Override
     protected void onRequestComplete() throws IOException {
         LOG.trace("onRequestComplete");
-
-        // close the input stream when its not needed anymore
-        InputStream is = getRequestContentSource();
-        if (is != null) {
-            IOHelper.close(is, "RequestContentSource", LOG);
-        }
+        
+        closeRequestContentSource();
     }
 
     @Override
@@ -120,6 +116,8 @@ public class JettyContentExchange extend
         try {
             super.onExpire();
         } finally {
+            // need to close the request input stream
+            closeRequestContentSource();
             doTaskCompleted();
         }
     }
@@ -131,6 +129,8 @@ public class JettyContentExchange extend
         try {
             super.onException(ex);
         } finally {
+            // need to close the request input stream
+            closeRequestContentSource();
             doTaskCompleted(ex);
         }
     }
@@ -142,6 +142,8 @@ public class JettyContentExchange extend
         try {
             super.onConnectionFailed(ex);
         } finally {
+            // need to close the request input stream
+            closeRequestContentSource();
             doTaskCompleted(ex);
         }
     }
@@ -159,6 +161,14 @@ public class JettyContentExchange extend
         String params = getRequestFields().getStringField(HttpHeaders.CONTENT_ENCODING);
         return getScheme() + "//" + getAddress().toString() + getRequestURI() + (params != null ? "?" + params : "");
     }
+    
+    protected void closeRequestContentSource() {
+        // close the input stream when its not needed anymore
+        InputStream is = getRequestContentSource();
+        if (is != null) {
+            IOHelper.close(is, "RequestContentSource", LOG);
+        }
+    }
 
     protected void doTaskCompleted() {
         // make sure to lower the latch

Added: camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/jettyproducer/JettyHttpProducerInvalidDestinationTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/jettyproducer/JettyHttpProducerInvalidDestinationTest.java?rev=1386607&view=auto
==============================================================================
--- camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/jettyproducer/JettyHttpProducerInvalidDestinationTest.java (added)
+++ camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/jettyproducer/JettyHttpProducerInvalidDestinationTest.java Mon Sep 17 13:32:19 2012
@@ -0,0 +1,36 @@
+/**
+ * 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.camel.component.jetty.jettyproducer;
+
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+public class JettyHttpProducerInvalidDestinationTest extends CamelTestSupport {
+    
+    @Test
+    public void testInvalidDestination() throws Exception {
+        final MyInputStream is = new MyInputStream("Content".getBytes());
+        try {
+            template.requestBody("jetty:http://localhost:50000/invalidDestination", is);
+            fail("Should have thrown exception");
+        } catch (Exception ex) {
+            // need to check if the input stream is closed
+            assertTrue("The input stream should be closed", is.isClosed());
+        }
+    }
+
+}

Modified: camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/jettyproducer/JettyHttpProducerTimeoutTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/jettyproducer/JettyHttpProducerTimeoutTest.java?rev=1386607&r1=1386606&r2=1386607&view=diff
==============================================================================
--- camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/jettyproducer/JettyHttpProducerTimeoutTest.java (original)
+++ camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/jettyproducer/JettyHttpProducerTimeoutTest.java Mon Sep 17 13:32:19 2012
@@ -16,8 +16,11 @@
  */
 package org.apache.camel.component.jetty.jettyproducer;
 
+import java.io.ByteArrayInputStream;
+
 import org.apache.camel.Exchange;
 import org.apache.camel.ExchangeTimedOutException;
+import org.apache.camel.Processor;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.jetty.BaseJettyTest;
 import org.junit.Test;
@@ -38,12 +41,19 @@ public class JettyHttpProducerTimeoutTes
 
         // give Jetty time to startup properly
         Thread.sleep(1000);
+        final MyInputStream is = new MyInputStream("Content".getBytes());
 
-        Exchange reply = template.request(url, null);
+        Exchange reply = template.request(url, new Processor() {
+            @Override
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setBody(is);
+            }
+        });
         Exception e = reply.getException();
         assertNotNull("Should have thrown an exception", e);
         ExchangeTimedOutException cause = assertIsInstanceOf(ExchangeTimedOutException.class, e);
         assertEquals(2000, cause.getTimeout());
+        assertTrue("The input stream should be closed", is.isClosed());
     }
 
     @Override
@@ -55,4 +65,6 @@ public class JettyHttpProducerTimeoutTes
             }
         };
     }
+    
+    
 }
\ No newline at end of file

Added: camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/jettyproducer/MyInputStream.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/jettyproducer/MyInputStream.java?rev=1386607&view=auto
==============================================================================
--- camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/jettyproducer/MyInputStream.java (added)
+++ camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/jettyproducer/MyInputStream.java Mon Sep 17 13:32:19 2012
@@ -0,0 +1,36 @@
+/**
+ * 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.camel.component.jetty.jettyproducer;
+
+import java.io.ByteArrayInputStream;
+
+public class MyInputStream extends ByteArrayInputStream {
+    private boolean closed;
+
+    public MyInputStream(byte[] buf) {
+        super(buf);
+        // TODO Auto-generated constructor stub
+    }
+    
+    public void close() {
+        closed = true;
+    }
+    
+    public boolean isClosed() {
+        return closed;
+    }
+}