You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2014/03/19 10:05:17 UTC

[3/3] git commit: CAMEL-7310: camel-restlet producer must run in sync mode due NPE bug in restlet which otherwise could cause Camel hanging as the callback is never invoked from restlet.

CAMEL-7310: camel-restlet producer must run in sync mode due NPE bug in restlet which otherwise could cause Camel hanging as the callback is never invoked from restlet.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/11db66d8
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/11db66d8
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/11db66d8

Branch: refs/heads/camel-2.12.x
Commit: 11db66d8586738e55911fe80ff0343231d9c67e0
Parents: db612f5
Author: Claus Ibsen <da...@apache.org>
Authored: Wed Mar 19 10:06:40 2014 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Wed Mar 19 10:08:00 2014 +0100

----------------------------------------------------------------------
 .../component/restlet/RestletProducer.java      | 31 ++++++++++++++++++--
 .../restlet/RestletProducerTimeoutTest.java     | 13 ++++----
 2 files changed, 34 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/11db66d8/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletProducer.java
----------------------------------------------------------------------
diff --git a/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletProducer.java b/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletProducer.java
index 3e9f8ff..1d28cdb 100644
--- a/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletProducer.java
+++ b/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletProducer.java
@@ -43,13 +43,13 @@ public class RestletProducer extends DefaultAsyncProducer {
     private static final Pattern PATTERN = Pattern.compile("\\(([\\w\\.]*)\\)");
     private Client client;
     private boolean throwException;
+    private boolean forceSync;
 
     public RestletProducer(RestletEndpoint endpoint) throws Exception {
         super(endpoint);
         this.throwException = endpoint.isThrowExceptionOnFailure();
         client = new Client(endpoint.getProtocol());
         client.setContext(new Context());
-        client.setConnectTimeout(100);
         client.getContext().getParameters().add("socketTimeout", String.valueOf(endpoint.getSocketTimeout()));
         client.getContext().getParameters().add("socketConnectTimeoutMs", String.valueOf(endpoint.getSocketTimeout()));
     }
@@ -83,7 +83,30 @@ public class RestletProducer extends DefaultAsyncProducer {
             return true;
         }
 
+        // TODO: due to https://github.com/restlet/restlet-framework-java/issues/871
+        // we force sync behavior until that is fixed, then we can switch back to async support
+
+        LOG.debug("Sending request: {} for exchangeId: {}", request, exchange.getExchangeId());
+        Response response = client.handle(request);
+        LOG.debug("Received response: {} for exchangeId: {}", response, exchange.getExchangeId());
+        try {
+            if (response != null) {
+                Integer respCode = response.getStatus().getCode();
+                if (respCode > 207 && throwException) {
+                    exchange.setException(populateRestletProducerException(exchange, response, respCode));
+                } else {
+                    binding.populateExchangeFromRestletResponse(exchange, response);
+                }
+            }
+        } catch (Exception e) {
+            exchange.setException(e);
+        }
+
+        callback.done(true);
+        return true;
+
         // process the request asynchronously
+        /*
         LOG.debug("Sending request: {} for exchangeId: {}", request, exchange.getExchangeId());
         client.handle(request, new Uniform() {
             @Override
@@ -100,12 +123,14 @@ public class RestletProducer extends DefaultAsyncProducer {
                     }
                 } catch (Exception e) {
                     exchange.setException(e);
+                } finally {
+                    callback.done(false);
                 }
             }
         });
 
-        callback.done(false);
-        return false;
+        // we continue routing async
+        return false;*/
     }
 
     private static String buildUri(RestletEndpoint endpoint, Exchange exchange) throws CamelExchangeException {

http://git-wip-us.apache.org/repos/asf/camel/blob/11db66d8/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletProducerTimeoutTest.java
----------------------------------------------------------------------
diff --git a/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletProducerTimeoutTest.java b/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletProducerTimeoutTest.java
index 68e3406..c670cf4 100644
--- a/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletProducerTimeoutTest.java
+++ b/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletProducerTimeoutTest.java
@@ -24,12 +24,13 @@ import org.junit.Test;
 public class RestletProducerTimeoutTest extends RestletTestSupport {
 
     @Test
-    public void testRestletProducerGet() throws Exception {
+    public void testRestletProducerTimeout() throws Exception {
         try {
-            String out = template.requestBodyAndHeader("restlet:http://localhost:" + portNum + "/users/123/basic?socketTimeout=100", null, "id", 123, String.class);
-            assertEquals("", null, out);
+            template.requestBodyAndHeader("restlet:http://localhost:" + portNum + "/users/123/basic?socketTimeout=100", null, "id", 123, String.class);
+            fail("Should have thrown exception");
         } catch (Exception ex) {
-            System.out.println("get the exception");
+            // expected
+            ex.printStackTrace();
         }
     }
 
@@ -38,13 +39,11 @@ public class RestletProducerTimeoutTest extends RestletTestSupport {
         return new RouteBuilder() {
             @Override
             public void configure() throws Exception {
-                from("direct:start").to("restlet:http://localhost:" + portNum + "/users/123/basic?socketTimeout=100").to("log:reply");
-
                 from("restlet:http://localhost:" + portNum + "/users/{id}/basic")
                         .process(new Processor() {
                             @Override
                             public void process(Exchange exchange) throws Exception {
-                                Thread.sleep(1000);
+                                Thread.sleep(2000);
                                 exchange.getOut().setBody("Here is the response");
                             }
                         });