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 2009/05/25 14:10:21 UTC
svn commit: r778399 - in /camel/trunk/components/camel-restlet/src:
main/java/org/apache/camel/component/restlet/
test/java/org/apache/camel/component/restlet/ test/resources/
Author: davsclaus
Date: Mon May 25 12:10:21 2009
New Revision: 778399
URL: http://svn.apache.org/viewvc?rev=778399&view=rev
Log:
CAMEL-1646: camel-restlet should be InOut by default
Added:
camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletProducerConcurrentTest.java (with props)
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
camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletEndpoint.java
camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletProducer.java
camel/trunk/components/camel-restlet/src/test/resources/log4j.properties
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=778399&r1=778398&r2=778399&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 Mon May 25 12:10:21 2009
@@ -16,7 +16,6 @@
*/
package org.apache.camel.component.restlet;
-import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Map;
@@ -49,33 +48,23 @@
private static final Log LOG = LogFactory.getLog(DefaultRestletBinding.class);
private HeaderFilterStrategy headerFilterStrategy;
- /**
- * Populate Camel message from Restlet request
- *
- * @param request message to be copied from
- * @param exchange to be populated
- * @throws Exception
- */
public void populateExchangeFromRestletRequest(Request request, Exchange exchange) throws Exception {
-
Message inMessage = exchange.getIn();
- // extract headers from restlet
+
+ // extract headers from restlet
for (Map.Entry<String, Object> entry : request.getAttributes().entrySet()) {
- if (!headerFilterStrategy.applyFilterToExternalHeaders(entry.getKey(),
- entry.getValue(), exchange)) {
-
+ if (!headerFilterStrategy.applyFilterToExternalHeaders(entry.getKey(), entry.getValue(), exchange)) {
inMessage.setHeader(entry.getKey(), entry.getValue());
if (LOG.isDebugEnabled()) {
LOG.debug("Populate exchange from Restlet request header: "
+ entry.getKey() + " value: " + entry.getValue());
}
-
}
}
// copy query string to header
String query = request.getResourceRef().getQuery();
- if (null != query) {
+ if (query != null) {
inMessage.setHeader(RestletConstants.RESTLET_QUERY_STRING, query);
}
@@ -83,37 +72,26 @@
return;
}
- // TODO: What is this form used for? Doesnt make sence in the code below as form is never used
Form form = new Form(request.getEntity());
- if (form != null) {
- for (Map.Entry<String, String> entry : form.getValuesMap().entrySet()) {
- // extract body added to the form as the key which has null value
- if (entry.getValue() == null) {
- inMessage.setBody(entry.getKey());
+ for (Map.Entry<String, String> entry : form.getValuesMap().entrySet()) {
+ // extract body added to the form as the key which has null value
+ if (entry.getValue() == null) {
+ inMessage.setBody(entry.getKey());
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Populate exchange from Restlet request body: " + entry.getValue());
+ }
+ } else {
+ if (!headerFilterStrategy.applyFilterToExternalHeaders(entry.getKey(), entry.getValue(), exchange)) {
+ inMessage.setHeader(entry.getKey(), entry.getValue());
if (LOG.isDebugEnabled()) {
- LOG.debug("Populate exchange from Restlet request body: " + entry.getValue());
- }
- } else {
- if (!headerFilterStrategy.applyFilterToExternalHeaders(entry.getKey(),
- entry.getValue(), exchange)) {
-
- inMessage.setHeader(entry.getKey(), entry.getValue());
- if (LOG.isDebugEnabled()) {
- LOG.debug("Populate exchange from Restlet request user header: "
- + entry.getKey() + " value: " + entry.getValue());
- }
+ LOG.debug("Populate exchange from Restlet request user header: "
+ + entry.getKey() + " value: " + entry.getValue());
}
}
}
}
}
- /**
- * Populate Restlet Request from Camel message
- *
- * @param request to be populated
- * @param exchange message to be copied from
- */
public void populateRestletRequestFromExchange(Request request, Exchange exchange) {
request.setReferrerRef("camel-restlet");
String body = exchange.getIn().getBody(String.class);
@@ -156,12 +134,6 @@
request.setEntity(form.getWebRepresentation());
}
- /**
- * Populate Restlet request from Camel message
- *
- * @param exchange message to be copied from
- * @param response to be populated
- */
public void populateRestletResponseFromExchange(Exchange exchange, Response response) {
Message out;
@@ -223,14 +195,7 @@
}
}
- /**
- * Populate Camel message from Restlet response
- *
- * @param exchange to be populated
- * @param response message to be copied from
- * @throws IOException
- */
- public void populateExchangeFromRestletResponse(Exchange exchange, Response response) throws IOException {
+ public void populateExchangeFromRestletResponse(Exchange exchange, Response response) throws Exception {
for (Map.Entry<String, Object> entry : response.getAttributes().entrySet()) {
if (!headerFilterStrategy.applyFilterToExternalHeaders(entry.getKey(), entry.getValue(), exchange)) {
@@ -250,7 +215,7 @@
if (exchange.getPattern().isOutCapable()) {
exchange.getOut().setBody(text);
} else {
- throw new RuntimeCamelException("Exchange is incapable of receiving response: " + exchange);
+ throw new RuntimeCamelException("Exchange is incapable of receiving response: " + exchange + " with pattern: " + exchange.getPattern());
}
}
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=778399&r1=778398&r2=778399&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 Mon May 25 12:10:21 2009
@@ -16,8 +16,6 @@
*/
package org.apache.camel.component.restlet;
-import java.io.IOException;
-
import org.apache.camel.Exchange;
import org.restlet.data.Request;
import org.restlet.data.Response;
@@ -35,18 +33,16 @@
* @param exchange message to be copied from
* @param response to be populated
*/
- void populateRestletResponseFromExchange(Exchange exchange,
- Response response);
+ void populateRestletResponseFromExchange(Exchange exchange, Response response);
/**
* Populate Camel message from Restlet request
*
* @param request message to be copied from
* @param exchange to be populated
- * @throws Exception
+ * @throws Exception is thrown if error processing
*/
- void populateExchangeFromRestletRequest(Request request,
- Exchange exchange) throws Exception;
+ void populateExchangeFromRestletRequest(Request request, Exchange exchange) throws Exception;
/**
* Populate Restlet Request from Camel message
@@ -61,9 +57,8 @@
*
* @param exchange to be populated
* @param response message to be copied from
- * @throws IOException
+ * @throws Exception is thrown if error processing
*/
- void populateExchangeFromRestletResponse(Exchange exchange,
- Response response) throws IOException;
+ void populateExchangeFromRestletResponse(Exchange exchange, Response response) 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=778399&r1=778398&r2=778399&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 Mon May 25 12:10:21 2009
@@ -177,7 +177,6 @@
private static String buildKey(RestletEndpoint endpoint) {
return endpoint.getHost() + ":" + endpoint.getPort();
}
-
-
+
}
Modified: camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletEndpoint.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletEndpoint.java?rev=778399&r1=778398&r2=778399&view=diff
==============================================================================
--- camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletEndpoint.java (original)
+++ camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletEndpoint.java Mon May 25 12:10:21 2009
@@ -22,6 +22,7 @@
import org.apache.camel.Consumer;
import org.apache.camel.Processor;
import org.apache.camel.Producer;
+import org.apache.camel.ExchangePattern;
import org.apache.camel.impl.DefaultEndpoint;
import org.apache.camel.spi.HeaderFilterStrategy;
import org.apache.camel.spi.HeaderFilterStrategyAware;
@@ -131,8 +132,7 @@
if (!bindingInitialized.getAndSet(true)
&& restletBinding instanceof HeaderFilterStrategyAware) {
- ((HeaderFilterStrategyAware)restletBinding)
- .setHeaderFilterStrategy(getHeaderFilterStrategy());
+ ((HeaderFilterStrategyAware)restletBinding).setHeaderFilterStrategy(getHeaderFilterStrategy());
}
return restletBinding;
}
@@ -145,8 +145,7 @@
public void setHeaderFilterStrategy(HeaderFilterStrategy headerFilterStrategy) {
this.headerFilterStrategy = headerFilterStrategy;
if (restletBinding instanceof HeaderFilterStrategyAware) {
- ((HeaderFilterStrategyAware)restletBinding)
- .setHeaderFilterStrategy(headerFilterStrategy);
+ ((HeaderFilterStrategyAware)restletBinding).setHeaderFilterStrategy(headerFilterStrategy);
}
}
@@ -154,8 +153,7 @@
if (headerFilterStrategy == null) {
headerFilterStrategy = new RestletHeaderFilterStrategy();
if (LOG.isDebugEnabled()) {
- LOG.debug("Create Restlet default header filter strategy "
- + headerFilterStrategy);
+ LOG.debug("Create Restlet default header filter strategy " + headerFilterStrategy);
}
}
return headerFilterStrategy;
@@ -168,4 +166,10 @@
public Map<String, String> getRestletRealm() {
return restletRealm;
}
+
+ @Override
+ public ExchangePattern getExchangePattern() {
+ // should always use in out for restlet
+ return ExchangePattern.InOut;
+ }
}
Modified: camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletProducer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletProducer.java?rev=778399&r1=778398&r2=778399&view=diff
==============================================================================
--- camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletProducer.java (original)
+++ camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletProducer.java Mon May 25 12:10:21 2009
@@ -56,9 +56,8 @@
RestletEndpoint endpoint = (RestletEndpoint)getEndpoint();
String resourceUri = buildUri(endpoint);
- Request request = new Request(endpoint.getRestletMethod(),
- resourceUri);
-
+ Request request = new Request(endpoint.getRestletMethod(), resourceUri);
+
RestletBinding binding = endpoint.getRestletBinding();
binding.populateRestletRequestFromExchange(request, exchange);
Added: camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletProducerConcurrentTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletProducerConcurrentTest.java?rev=778399&view=auto
==============================================================================
--- camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletProducerConcurrentTest.java (added)
+++ camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletProducerConcurrentTest.java Mon May 25 12:10:21 2009
@@ -0,0 +1,97 @@
+/**
+ * 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 java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+
+/**
+ * Resltet producer concurrent test
+ *
+ * @version $Revision$
+ */
+public class RestletProducerConcurrentTest extends ContextTestSupport {
+
+ public void testNoConcurrentProducers() throws Exception {
+ doSendMessages(1, 1);
+ }
+
+ public void testConcurrentProducers() throws Exception {
+ doSendMessages(10, 5);
+ }
+
+ private void doSendMessages(int files, int poolSize) throws Exception {
+ getMockEndpoint("mock:result").expectedMessageCount(files);
+
+ ExecutorService executor = Executors.newFixedThreadPool(poolSize);
+ Map<Integer, Future> responses = new ConcurrentHashMap();
+ for (int i = 0; i < files; i++) {
+ final int index = i;
+ Future out = executor.submit(new Callable<Object>() {
+ public Object call() throws Exception {
+ Map<String, Object> headers = new HashMap<String, Object>();
+ headers.put("username", "davsclaus");
+ headers.put("id", index);
+ return template.requestBodyAndHeaders("restlet:http://localhost:9080/users/{username}/{id}?restletMethod=POST", null, headers, String.class);
+ }
+ });
+ responses.put(index, out);
+ }
+
+ assertMockEndpointsSatisfied();
+ assertEquals(files, responses.size());
+
+ // get all responses
+ Set unique = new HashSet();
+ for (Future future : responses.values()) {
+ unique.add(future.get());
+ }
+
+ // should be 10 unique responses
+ assertEquals("Should be " + files + " unique responses", files, unique.size());
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("restlet:http://localhost:9080/users/{username}/{id}?restletMethod=POST")
+ .to("log:inbox")
+ .process(new Processor() {
+ public void process(Exchange exchange) throws Exception {
+ String index = exchange.getIn().getHeader("id", String.class);
+ exchange.getOut().setBody(index);
+ }
+ }).to("mock:result");
+ }
+ };
+ }
+
+}
Propchange: camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletProducerConcurrentTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletProducerConcurrentTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Modified: camel/trunk/components/camel-restlet/src/test/resources/log4j.properties
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-restlet/src/test/resources/log4j.properties?rev=778399&r1=778398&r2=778399&view=diff
==============================================================================
--- camel/trunk/components/camel-restlet/src/test/resources/log4j.properties (original)
+++ camel/trunk/components/camel-restlet/src/test/resources/log4j.properties Mon May 25 12:10:21 2009
@@ -18,9 +18,9 @@
#
# The logging properties used during tests..
#
-log4j.rootLogger=INFO, out
+log4j.rootLogger=INFO, file
-log4j.logger.org.apache.camel.component.restlet=DEBUG
+#log4j.logger.org.apache.camel.component.restlet=DEBUG
# CONSOLE appender not used by default
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
@@ -28,8 +28,8 @@
log4j.appender.stdout.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - %m%n
# File appender
-log4j.appender.out=org.apache.log4j.FileAppender
-log4j.appender.out.layout=org.apache.log4j.PatternLayout
-log4j.appender.out.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - %m%n
-log4j.appender.out.file=target/camel-restlet-test.log
-log4j.appender.out.append=false
+log4j.appender.file=org.apache.log4j.FileAppender
+log4j.appender.file.layout=org.apache.log4j.PatternLayout
+log4j.appender.file.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - %m%n
+log4j.appender.file.file=target/camel-restlet-test.log
+log4j.appender.file.append=false