Author: davsclaus
Date: Fri Jun 8 07:16:50 2012
New Revision: 1347901
URL: http://svn.apache.org/viewvc?rev=1347901&view=rev
Log:
CAMEL-5108: Restlet component can configure Restlet server parameters. Thanks
to Michael Shorter for the patch.
Added:
camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletAddRouteTest.java
camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletRestartRouteTest.java
Modified:
camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/DefaultRestletBinding.java
camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletBinding.java
camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletComponent.java
Modified:
camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/DefaultRestletBinding.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/DefaultRestletBinding.java?rev=1347901&r1=1347900&r2=1347901&view=diff
==============================================================================
---
camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/DefaultRestletBinding.java
(original)
+++
camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/DefaultRestletBinding.java
Fri Jun 8 07:16:50 2012
@@ -20,12 +20,14 @@ import java.io.File;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
+import java.util.Calendar;
+import java.util.Date;
import java.util.Map;
-
import javax.xml.transform.dom.DOMSource;
import org.apache.camel.Exchange;
import org.apache.camel.Message;
+import org.apache.camel.NoTypeConversionAvailableException;
import org.apache.camel.StringSource;
import org.apache.camel.WrappedFile;
import org.apache.camel.component.file.GenericFile;
@@ -42,6 +44,7 @@ import org.restlet.data.MediaType;
import org.restlet.data.Method;
import org.restlet.data.Preference;
import org.restlet.data.Status;
+import org.restlet.engine.http.header.HeaderConstants;
import org.restlet.representation.FileRepresentation;
import org.restlet.representation.InputRepresentation;
import org.slf4j.Logger;
@@ -155,7 +158,7 @@ public class DefaultRestletBinding imple
} else {
request.setEntity(body, mediaType);
}
-
+
MediaType acceptedMediaType =
exchange.getIn().getHeader(Exchange.ACCEPT_CONTENT_TYPE, MediaType.class);
if (acceptedMediaType != null) {
request.getClientInfo().getAcceptedMediaTypes().add(new
Preference<MediaType>(acceptedMediaType));
@@ -163,7 +166,7 @@ public class DefaultRestletBinding imple
}
- public void populateRestletResponseFromExchange(Exchange exchange,
Response response) {
+ public void populateRestletResponseFromExchange(Exchange exchange,
Response response) throws Exception {
Message out;
if (exchange.isFailed()) {
// 500 for internal server error which can be overridden by
response code in header
@@ -201,13 +204,6 @@ public class DefaultRestletBinding imple
response.setStatus(Status.valueOf(responseCode));
}
- for (Map.Entry<String, Object> entry : out.getHeaders().entrySet()) {
- if
(!headerFilterStrategy.applyFilterToCamelHeaders(entry.getKey(),
entry.getValue(), exchange)) {
- response.getAttributes().put(entry.getKey(), entry.getValue());
- LOG.debug("Populate Restlet response from exchange header: {}
value: {}", entry.getKey(), entry.getValue());
- }
- }
-
// set response body according to the message body
Object body = out.getBody();
if (body instanceof WrappedFile) {
@@ -237,6 +233,14 @@ public class DefaultRestletBinding imple
CharacterSet cs =
CharacterSet.valueOf(exchange.getProperty(Exchange.CHARSET_NAME, String.class));
response.getEntity().setCharacterSet(cs);
}
+
+ // set headers at the end, as the entity must be set first
+ for (Map.Entry<String, Object> entry : out.getHeaders().entrySet()) {
+ if
(!headerFilterStrategy.applyFilterToCamelHeaders(entry.getKey(),
entry.getValue(), exchange)) {
+ setResponseHeader(exchange, response, entry.getKey(),
entry.getValue());
+ LOG.debug("Populate Restlet HTTP header in response from
exchange header: {} value: {}", entry.getKey(), entry.getValue());
+ }
+ }
}
public void populateExchangeFromRestletResponse(Exchange exchange,
Response response) throws Exception {
@@ -272,6 +276,62 @@ public class DefaultRestletBinding imple
MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), false);
}
+ @SuppressWarnings("unchecked")
+ protected void setResponseHeader(Exchange exchange, org.restlet.Message
message, String header, Object value) throws NoTypeConversionAvailableException
{
+ // put the header first
+ message.getAttributes().put(header, value);
+
+ // special for certain headers
+ if (message.getEntity() != null) {
+ if (header.equalsIgnoreCase(HeaderConstants.HEADER_EXPIRES)) {
+ if (value instanceof Calendar) {
+ message.getEntity().setExpirationDate(((Calendar)
value).getTime());
+ } else if (value instanceof Date) {
+ message.getEntity().setExpirationDate((Date) value);
+ } else {
+ Date date =
exchange.getContext().getTypeConverter().mandatoryConvertTo(Date.class, value);
+ message.getEntity().setExpirationDate(date);
+ }
+ }
+
+ if (header.equalsIgnoreCase(HeaderConstants.HEADER_LAST_MODIFIED))
{
+ if (value instanceof Calendar) {
+ message.getEntity().setModificationDate(((Calendar)
value).getTime());
+ } else if (value instanceof Date) {
+ message.getEntity().setModificationDate((Date) value);
+ } else {
+ Date date =
exchange.getContext().getTypeConverter().mandatoryConvertTo(Date.class, value);
+ message.getEntity().setModificationDate(date);
+ }
+ }
+
+ if
(header.equalsIgnoreCase(HeaderConstants.HEADER_CONTENT_LENGTH)) {
+ if (value instanceof Long) {
+ message.getEntity().setSize((Long) value);
+ } else if (value instanceof Integer) {
+ message.getEntity().setSize((Integer) value);
+ } else {
+ Long num =
exchange.getContext().getTypeConverter().mandatoryConvertTo(Long.class, value);
+ message.getEntity().setSize(num);
+ }
+ }
+
+ if (header.equalsIgnoreCase(HeaderConstants.HEADER_CONTENT_TYPE)) {
+ if (value instanceof MediaType) {
+ message.getEntity().setMediaType((MediaType) value);
+ } else {
+ String type = value.toString();
+ MediaType media = MediaType.valueOf(type);
+ if (media != null) {
+ message.getEntity().setMediaType(media);
+ } else {
+ LOG.debug("Value {} cannot be converted as a
MediaType. The value will be ignored.", value);
+ }
+ }
+ }
+ }
+ }
+
public HeaderFilterStrategy getHeaderFilterStrategy() {
return headerFilterStrategy;
}
Modified:
camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletBinding.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletBinding.java?rev=1347901&r1=1347900&r2=1347901&view=diff
==============================================================================
---
camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletBinding.java
(original)
+++
camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletBinding.java
Fri Jun 8 07:16:50 2012
@@ -32,16 +32,18 @@ public interface RestletBinding {
*
* @param exchange message to be copied from
* @param response to be populated
+ * @throws Exception is thrown if error processing
*/
- void populateRestletResponseFromExchange(Exchange exchange, Response
response);
+ void populateRestletResponseFromExchange(Exchange exchange, Response
response) throws Exception;
/**
* Populate Camel message from Restlet request
*
*
* @param request message to be copied from
- * @param response
- *@param exchange to be populated @throws Exception is thrown if error
processing
+ * @param response the response
+ * @param exchange to be populated @throws Exception is thrown if error
processing
+ * @throws Exception is thrown if error processing
*/
void populateExchangeFromRestletRequest(Request request, Response
response, Exchange exchange) throws Exception;
Modified:
camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletComponent.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletComponent.java?rev=1347901&r1=1347900&r2=1347901&view=diff
==============================================================================
---
camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletComponent.java
(original)
+++
camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletComponent.java
Fri Jun 8 07:16:50 2012
@@ -31,9 +31,11 @@ import org.restlet.Restlet;
import org.restlet.Server;
import org.restlet.data.ChallengeScheme;
import org.restlet.data.Method;
+import org.restlet.data.Parameter;
import org.restlet.data.Protocol;
import org.restlet.security.ChallengeAuthenticator;
import org.restlet.security.MapVerifier;
+import org.restlet.util.Series;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -49,6 +51,20 @@ public class RestletComponent extends He
private final Map<String, MethodBasedRouter> routers = new HashMap<String,
MethodBasedRouter>();
private final Component component;
+ // options that can be set on the restlet server
+ private Boolean controllerDaemon;
+ private Integer controllerSleepTimeMs;
+ private Integer inboundBufferSize;
+ private Integer minThreads;
+ private Integer maxThreads;
+ private Integer maxConnectionsPerHost;
+ private Integer maxTotalConnections;
+ private Integer outboundBufferSize;
+ private Boolean persistingConnections;
+ private Boolean pipeliningConnections;
+ private Integer threadMaxIdleTimeMs;
+ private Boolean useForwardedForHeader;
+
public RestletComponent() {
this.component = new Component();
}
@@ -179,8 +195,50 @@ public class RestletComponent extends He
synchronized (servers) {
server = servers.get(key);
if (server == null) {
- server =
component.getServers().add(Protocol.valueOf(endpoint.getProtocol()),
- endpoint.getPort());
+ server =
component.getServers().add(Protocol.valueOf(endpoint.getProtocol()),
endpoint.getPort());
+
+ // Add any Restlet server parameters that were included
+ Series<Parameter> params = server.getContext().getParameters();
+
+ if (getControllerDaemon() != null) {
+ params.add("controllerDaemon",
getControllerDaemon().toString());
+ }
+ if (getControllerSleepTimeMs() != null) {
+ params.add("controllerSleepTimeMs",
getControllerSleepTimeMs().toString());
+ }
+ if (getInboundBufferSize() != null) {
+ params.add("inboundBufferSize",
getInboundBufferSize().toString());
+ }
+ if (getMinThreads() != null) {
+ params.add("minThreads", getMinThreads().toString());
+ }
+ if (getMaxThreads() != null) {
+ params.add("maxThreads", getMaxThreads().toString());
+ }
+ if (getMaxConnectionsPerHost() != null) {
+ params.add("maxConnectionsPerHost",
getMaxConnectionsPerHost().toString());
+ }
+ if (getMaxTotalConnections() != null) {
+ params.add("maxTotalConnections",
getMaxTotalConnections().toString());
+ }
+ if (getOutboundBufferSize() != null) {
+ params.add("outboundBufferSize",
getOutboundBufferSize().toString());
+ }
+ if (getPersistingConnections() != null) {
+ params.add("persistingConnections",
getPersistingConnections().toString());
+ }
+ if (getPipeliningConnections() != null) {
+ params.add("pipeliningConnections",
getPipeliningConnections().toString());
+ }
+ if (getThreadMaxIdleTimeMs() != null) {
+ params.add("threadMaxIdleTimeMs",
getThreadMaxIdleTimeMs().toString());
+ }
+ if (getUseForwardedForHeader() != null) {
+ params.add("useForwardedForHeader",
getUseForwardedForHeader().toString());
+ }
+ LOG.debug("Setting parameters: {} to server: {}", params,
server);
+ server.getContext().setParameters(params);
+
servers.put(key, server);
LOG.debug("Added server: {}", key);
server.start();
@@ -239,4 +297,100 @@ public class RestletComponent extends He
private static String decodePattern(String pattern) {
return pattern == null ? null : pattern.replaceAll("\\(",
"{").replaceAll("\\)", "}");
}
+
+ public Boolean getControllerDaemon() {
+ return controllerDaemon;
+ }
+
+ public void setControllerDaemon(Boolean controllerDaemon) {
+ this.controllerDaemon = controllerDaemon;
+ }
+
+ public Integer getControllerSleepTimeMs() {
+ return controllerSleepTimeMs;
+ }
+
+ public void setControllerSleepTimeMs(Integer controllerSleepTimeMs) {
+ this.controllerSleepTimeMs = controllerSleepTimeMs;
+ }
+
+ public Integer getInboundBufferSize() {
+ return inboundBufferSize;
+ }
+
+ public void setInboundBufferSize(Integer inboundBufferSize) {
+ this.inboundBufferSize = inboundBufferSize;
+ }
+
+ public Integer getMaxConnectionsPerHost() {
+ return maxConnectionsPerHost;
+ }
+
+ public void setMaxConnectionsPerHost(Integer maxConnectionsPerHost) {
+ this.maxConnectionsPerHost = maxConnectionsPerHost;
+ }
+
+ public Integer getMaxThreads() {
+ return maxThreads;
+ }
+
+ public void setMaxThreads(Integer maxThreads) {
+ this.maxThreads = maxThreads;
+ }
+
+ public Integer getMaxTotalConnections() {
+ return maxTotalConnections;
+ }
+
+ public void setMaxTotalConnections(Integer maxTotalConnections) {
+ this.maxTotalConnections = maxTotalConnections;
+ }
+
+ public Integer getMinThreads() {
+ return minThreads;
+ }
+
+ public void setMinThreads(Integer minThreads) {
+ this.minThreads = minThreads;
+ }
+
+ public Integer getOutboundBufferSize() {
+ return outboundBufferSize;
+ }
+
+ public void setOutboundBufferSize(Integer outboundBufferSize) {
+ this.outboundBufferSize = outboundBufferSize;
+ }
+
+ public Boolean getPersistingConnections() {
+ return persistingConnections;
+ }
+
+ public void setPersistingConnections(Boolean persistingConnections) {
+ this.persistingConnections = persistingConnections;
+ }
+
+ public Boolean getPipeliningConnections() {
+ return pipeliningConnections;
+ }
+
+ public void setPipeliningConnections(Boolean pipeliningConnections) {
+ this.pipeliningConnections = pipeliningConnections;
+ }
+
+ public Integer getThreadMaxIdleTimeMs() {
+ return threadMaxIdleTimeMs;
+ }
+
+ public void setThreadMaxIdleTimeMs(Integer threadMaxIdleTimeMs) {
+ this.threadMaxIdleTimeMs = threadMaxIdleTimeMs;
+ }
+
+ public Boolean getUseForwardedForHeader() {
+ return useForwardedForHeader;
+ }
+
+ public void setUseForwardedForHeader(Boolean useForwardedForHeader) {
+ this.useForwardedForHeader = useForwardedForHeader;
+ }
}
Added:
camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletAddRouteTest.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletAddRouteTest.java?rev=1347901&view=auto
==============================================================================
---
camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletAddRouteTest.java
(added)
+++
camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletAddRouteTest.java
Fri Jun 8 07:16:50 2012
@@ -0,0 +1,71 @@
+/**
+ * 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.restlet;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.Test;
+
+/**
+ * @version
+ */
+public class RestletAddRouteTest extends RestletTestSupport {
+
+ @Test
+ public void testResetProducer() throws Exception {
+ String out = template.requestBodyAndHeader("direct:start", null, "id",
123, String.class);
+ assertEquals("123;Donald Duck", out);
+
+ // add a 2nd route, with a different path
+ context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("restlet:http://localhost:" + portNum +
"/users/{id}/verbose").routeId("bar2")
+ .process(new Processor() {
+ public void process(Exchange exchange) throws
Exception {
+ String id = exchange.getIn().getHeader("id",
String.class);
+ exchange.getOut().setBody(id + ";Donald
Duck;Duckville");
+ }
+ });
+
+ }
+ });
+
+ out = template.requestBodyAndHeader("restlet:http://localhost:" +
portNum + "/users/{id}/verbose", null, "id", 456, String.class);
+ assertEquals("456;Donald Duck;Duckville", out);
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("direct:start").routeId("foo")
+ .to("restlet:http://localhost:" + portNum +
"/users/{id}/basic").to("log:reply");
+
+ from("restlet:http://localhost:" + portNum +
"/users/{id}/basic").routeId("bar")
+ .process(new Processor() {
+ public void process(Exchange exchange) throws
Exception {
+ String id = exchange.getIn().getHeader("id",
String.class);
+ exchange.getOut().setBody(id + ";Donald Duck");
+ }
+ });
+ }
+ };
+ }
+}
Added:
camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletRestartRouteTest.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletRestartRouteTest.java?rev=1347901&view=auto
==============================================================================
---
camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletRestartRouteTest.java
(added)
+++
camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletRestartRouteTest.java
Fri Jun 8 07:16:50 2012
@@ -0,0 +1,73 @@
+/**
+ * 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.restlet;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.Test;
+
+/**
+ * @version
+ */
+public class RestletRestartRouteTest extends RestletTestSupport {
+
+ @Test
+ public void testResetProducer() throws Exception {
+ String out = template.requestBodyAndHeader("direct:start", null, "id",
123, String.class);
+ assertEquals("123;Donald Duck", out);
+
+ // restart foo
+ context.stopRoute("foo");
+ context.startRoute("foo");
+
+ out = template.requestBodyAndHeader("direct:start", null, "id", 456,
String.class);
+ assertEquals("456;Donald Duck", out);
+ }
+
+ @Test
+ public void testResetConsumer() throws Exception {
+ String out = template.requestBodyAndHeader("direct:start", null, "id",
123, String.class);
+ assertEquals("123;Donald Duck", out);
+
+ // restart bar
+ context.stopRoute("bar");
+ context.startRoute("bar");
+
+ out = template.requestBodyAndHeader("direct:start", null, "id", 456,
String.class);
+ assertEquals("456;Donald Duck", out);
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("direct:start").routeId("foo")
+ .to("restlet:http://localhost:" + portNum +
"/users/{id}/basic").to("log:reply");
+
+ from("restlet:http://localhost:" + portNum +
"/users/{id}/basic").routeId("bar")
+ .process(new Processor() {
+ public void process(Exchange exchange) throws
Exception {
+ String id = exchange.getIn().getHeader("id",
String.class);
+ exchange.getOut().setBody(id + ";Donald Duck");
+ }
+ });
+ }
+ };
+ }
+}