Author: janstey
Date: Thu Jun 14 17:23:12 2012
New Revision: 1350347
URL: http://svn.apache.org/viewvc?rev=1350347&view=rev
Log:
Merged revisions 1347901 via svnmerge from
https://svn.apache.org/repos/asf/camel/trunk
........
r1347901 | davsclaus | 2012-06-08 04:46:50 -0230 (Fri, 08 Jun 2012) | 1 line
CAMEL-5108: Restlet component can configure Restlet server parameters. Thanks
to Michael Shorter for the patch.
........
Added:
camel/branches/camel-2.9.x/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletAddRouteTest.java
- copied unchanged from r1347901,
camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletAddRouteTest.java
camel/branches/camel-2.9.x/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletRestartRouteTest.java
- copied unchanged from r1347901,
camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletRestartRouteTest.java
Modified:
camel/branches/camel-2.9.x/ (props changed)
camel/branches/camel-2.9.x/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/DefaultRestletBinding.java
camel/branches/camel-2.9.x/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletBinding.java
camel/branches/camel-2.9.x/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletComponent.java
Propchange: camel/branches/camel-2.9.x/
------------------------------------------------------------------------------
Merged /camel/trunk:r1347901
Propchange: camel/branches/camel-2.9.x/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.
Modified:
camel/branches/camel-2.9.x/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/DefaultRestletBinding.java
URL:
http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/DefaultRestletBinding.java?rev=1350347&r1=1350346&r2=1350347&view=diff
==============================================================================
---
camel/branches/camel-2.9.x/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/DefaultRestletBinding.java
(original)
+++
camel/branches/camel-2.9.x/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/DefaultRestletBinding.java
Thu Jun 14 17:23:12 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/branches/camel-2.9.x/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletBinding.java
URL:
http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletBinding.java?rev=1350347&r1=1350346&r2=1350347&view=diff
==============================================================================
---
camel/branches/camel-2.9.x/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletBinding.java
(original)
+++
camel/branches/camel-2.9.x/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletBinding.java
Thu Jun 14 17:23:12 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/branches/camel-2.9.x/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletComponent.java
URL:
http://svn.apache.org/viewvc/camel/branches/camel-2.9.x/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletComponent.java?rev=1350347&r1=1350346&r2=1350347&view=diff
==============================================================================
---
camel/branches/camel-2.9.x/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletComponent.java
(original)
+++
camel/branches/camel-2.9.x/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletComponent.java
Thu Jun 14 17:23:12 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;
+ }
}