From a860339ec34a717ae7013b04a37e5a28714518d5 Mon Sep 17 00:00:00 2001 From: Benjamin Graf Date: Sun, 5 Jan 2014 16:37:55 +0100 Subject: [PATCH] CAMEL-7014: Add socketTimeout parameter --- .../component/restlet/RestletEndpoint.java | 10 ++++ .../component/restlet/RestletProducer.java | 3 +- .../restlet/RestletProducerTimeoutTest.java | 49 +++++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletProducerTimeoutTest.java diff --git a/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletEndpoint.java b/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletEndpoint.java index f27787b6b05b0..2caccd159d0a3 100644 --- a/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletEndpoint.java +++ b/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletEndpoint.java @@ -39,6 +39,7 @@ public class RestletEndpoint extends DefaultEndpoint implements HeaderFilterStra private static final int DEFAULT_PORT = 80; private static final String DEFAULT_PROTOCOL = "http"; private static final String DEFAULT_HOST = "localhost"; + private static final int DEFAULT_SOCKETTIMEOUT = 30000; private Method restletMethod = Method.GET; @@ -49,6 +50,7 @@ public class RestletEndpoint extends DefaultEndpoint implements HeaderFilterStra private String protocol = DEFAULT_PROTOCOL; private String host = DEFAULT_HOST; private int port = DEFAULT_PORT; + private int socketTimeout = DEFAULT_SOCKETTIMEOUT; private String uriPattern; // Optional and for consumer only. This allows a single route to service multiple URI patterns. @@ -124,6 +126,14 @@ public void setPort(int port) { this.port = port; } + public int getSocketTimeout() { + return socketTimeout; + } + + public void setSocketTimeout(int socketTimeout) { + this.socketTimeout = socketTimeout; + } + public String getUriPattern() { return uriPattern; } 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 b8d84c492b715..2cad21f36bb81 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 @@ -49,6 +49,7 @@ public RestletProducer(RestletEndpoint endpoint) throws Exception { this.throwException = endpoint.isThrowExceptionOnFailure(); client = new Client(endpoint.getProtocol()); client.setContext(new Context()); + client.getContext().getParameters().add("socketTimeout", String.valueOf(endpoint.getSocketTimeout())); } @Override @@ -98,10 +99,10 @@ public void handle(Request request, Response response) { } catch (Exception e) { exchange.setException(e); } - callback.done(false); } }); + callback.done(false); return false; } 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 new file mode 100644 index 0000000000000..b713b36bcbc11 --- /dev/null +++ b/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletProducerTimeoutTest.java @@ -0,0 +1,49 @@ +/* + * Copyright 2014 The Apache Software Foundation. + * + * Licensed 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.restlet; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import static org.apache.camel.component.restlet.RestletTestSupport.portNum; +import org.junit.Test; + +public class RestletProducerTimeoutTest extends RestletTestSupport { + + @Test + public void testRestletProducerGet() throws Exception { + String out = template.requestBodyAndHeader("direct:start", null, "id", 123, String.class); + assertEquals(null, out); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start").to("restlet:http://localhost:" + portNum + "/users/123/basic?timeout=500").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); + } + }); + } + }; + } +}