Author: davsclaus
Date: Wed Jun 1 15:53:30 2011
New Revision: 1130198
URL: http://svn.apache.org/viewvc?rev=1130198&view=rev
Log:
CAMEL-4036: Exposed the Restlet API in camel-restlet and allow end users to use
those API to generate response (they are now in full control).
Added:
camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletRequestAndResponseAPITest.java
- copied, changed from r1130173,
camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletProducerGet2Test.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/RestletConstants.java
camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletConsumer.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=1130198&r1=1130197&r2=1130198&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
Wed Jun 1 15:53:30 2011
@@ -50,9 +50,12 @@ public class DefaultRestletBinding imple
private static final Logger LOG =
LoggerFactory.getLogger(DefaultRestletBinding.class);
private HeaderFilterStrategy headerFilterStrategy;
- public void populateExchangeFromRestletRequest(Request request, Exchange
exchange) throws Exception {
+ public void populateExchangeFromRestletRequest(Request request, Response
response, Exchange exchange) throws Exception {
Message inMessage = exchange.getIn();
+ inMessage.setHeader(RestletConstants.RESTLET_REQUEST, request);
+ inMessage.setHeader(RestletConstants.RESTLET_RESPONSE, response);
+
// extract headers from restlet
for (Map.Entry<String, Object> entry :
request.getAttributes().entrySet()) {
if
(!headerFilterStrategy.applyFilterToExternalHeaders(entry.getKey(),
entry.getValue(), exchange)) {
@@ -128,9 +131,7 @@ public class DefaultRestletBinding imple
if (login != null && password != null) {
ChallengeResponse authentication = new
ChallengeResponse(ChallengeScheme.HTTP_BASIC, login, password);
request.setChallengeResponse(authentication);
- if (LOG.isDebugEnabled()) {
- LOG.debug("Basic HTTP Authentication has been applied");
- }
+ LOG.debug("Basic HTTP Authentication has been applied");
}
for (Map.Entry<String, Object> entry :
exchange.getIn().getHeaders().entrySet()) {
@@ -226,18 +227,19 @@ public class DefaultRestletBinding imple
if (body == null) {
// empty response
response.setEntity("", MediaType.TEXT_PLAIN);
+ } else if (body instanceof Response) {
+ // its already a restlet response, so dont do anything
+ LOG.debug("Using existing Restlet Response from exchange body:
{}", body);
} else if (body instanceof InputStream) {
response.setEntity(new
InputRepresentation(out.getBody(InputStream.class), mediaType));
} else if (body instanceof File) {
response.setEntity(new FileRepresentation(out.getBody(File.class),
mediaType));
- } else if (body != null) {
+ } else {
// fallback and use string
String text = out.getBody(String.class);
response.setEntity(text, mediaType);
}
- if (LOG.isDebugEnabled()) {
- LOG.debug("Populate Restlet response from exchange body: " + body);
- }
+ LOG.debug("Populate Restlet response from exchange body: {}", body);
if (exchange.getProperty(Exchange.CHARSET_NAME) != null) {
CharacterSet cs =
CharacterSet.valueOf(exchange.getProperty(Exchange.CHARSET_NAME, String.class));
@@ -260,6 +262,9 @@ public class DefaultRestletBinding imple
int responseCode = response.getStatus().getCode();
exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, responseCode);
+ // set restlet response as header so end user have access to it if
needed
+ exchange.getOut().setHeader(RestletConstants.RESTLET_RESPONSE,
response);
+
if (response.getEntity() != null) {
// get content type
MediaType mediaType = response.getEntity().getMediaType();
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=1130198&r1=1130197&r2=1130198&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
Wed Jun 1 15:53:30 2011
@@ -38,11 +38,12 @@ public interface RestletBinding {
/**
* Populate Camel message from Restlet request
*
+ *
* @param request message to be copied from
- * @param exchange to be populated
- * @throws Exception is thrown if error processing
+ * @param response
+ *@param exchange to be populated @throws Exception is thrown if error
processing
*/
- void populateExchangeFromRestletRequest(Request request, Exchange
exchange) throws Exception;
+ void populateExchangeFromRestletRequest(Request request, Response
response, Exchange exchange) throws Exception;
/**
* Populate Restlet Request from Camel message
Modified:
camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletConstants.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletConstants.java?rev=1130198&r1=1130197&r2=1130198&view=diff
==============================================================================
---
camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletConstants.java
(original)
+++
camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletConstants.java
Wed Jun 1 15:53:30 2011
@@ -25,6 +25,8 @@ public final class RestletConstants {
public static final String RESTLET_LOGIN = "CamelRestletLogin";
public static final String RESTLET_PASSWORD = "CamelRestletPassword";
+ public static final String RESTLET_REQUEST = "CamelRestletRequest";
+ public static final String RESTLET_RESPONSE = "CamelRestletResponse";
private RestletConstants() {
}
Modified:
camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletConsumer.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletConsumer.java?rev=1130198&r1=1130197&r2=1130198&view=diff
==============================================================================
---
camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletConsumer.java
(original)
+++
camel/trunk/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletConsumer.java
Wed Jun 1 15:53:30 2011
@@ -49,26 +49,38 @@ public class RestletConsumer extends Def
try {
Exchange exchange = getEndpoint().createExchange();
- RestletBinding binding =
((RestletEndpoint)getEndpoint()).getRestletBinding();
- binding.populateExchangeFromRestletRequest(request,
exchange);
- getProcessor().process(exchange);
+
+ RestletBinding binding = getEndpoint().getRestletBinding();
+ binding.populateExchangeFromRestletRequest(request,
response, exchange);
+
+ try {
+ getProcessor().process(exchange);
+ } catch (Exception e) {
+ exchange.setException(e);
+ }
binding.populateRestletResponseFromExchange(exchange,
response);
+
} catch (Exception e) {
- throw new RuntimeCamelException(e);
+ throw new RuntimeCamelException("Cannot process request",
e);
}
}
};
}
@Override
+ public RestletEndpoint getEndpoint() {
+ return (RestletEndpoint) super.getEndpoint();
+ }
+
+ @Override
protected void doStart() throws Exception {
super.doStart();
- ((RestletEndpoint)getEndpoint()).connect(this);
+ getEndpoint().connect(this);
}
@Override
public void doStop() throws Exception {
- ((RestletEndpoint)getEndpoint()).disconnect(this);
+ getEndpoint().disconnect(this);
super.doStop();
}
Copied:
camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletRequestAndResponseAPITest.java
(from r1130173,
camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletProducerGet2Test.java)
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletRequestAndResponseAPITest.java?p2=camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletRequestAndResponseAPITest.java&p1=camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletProducerGet2Test.java&r1=1130173&r2=1130198&rev=1130198&view=diff
==============================================================================
---
camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletProducerGet2Test.java
(original)
+++
camel/trunk/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletRequestAndResponseAPITest.java
Wed Jun 1 15:53:30 2011
@@ -24,20 +24,50 @@ import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;
+import org.restlet.Request;
+import org.restlet.Response;
+import org.restlet.data.MediaType;
+import org.restlet.data.Status;
/**
+ * Testing that end users can use the {@link Response} API from Restlet
directly to have fine grained
+ * control of the response they want to use.
+ *
* @version
*/
-public class RestletProducerGet2Test extends CamelTestSupport {
+public class RestletRequestAndResponseAPITest extends CamelTestSupport {
@Test
- public void testRestletProducerGet2() throws Exception {
+ public void testRestletProducer() throws Exception {
Map<String, Object> headers = new HashMap<String, Object>();
headers.put("id", 123);
headers.put("beverage.beer", "Carlsberg");
- String out = template.requestBodyAndHeaders("direct:start", null,
headers, String.class);
- assertEquals("123;Donald Duck;Carlsberg", out);
+ Object out = template.requestBodyAndHeaders("direct:start", null,
headers);
+ assertEquals("<response>Beer is Good</response>", out);
+ }
+
+ @Test
+ public void testRestletProducer2() throws Exception {
+ final Map<String, Object> headers = new HashMap<String, Object>();
+ headers.put("id", 123);
+ headers.put("beverage.beer", "Carlsberg");
+
+ Exchange out = template.request("direct:start", new Processor() {
+ @Override
+ public void process(Exchange exchange) throws Exception {
+ exchange.getIn().setHeaders(headers);
+ }
+ });
+ assertNotNull(out);
+ assertEquals("text/xml",
out.getOut().getHeader(Exchange.CONTENT_TYPE));
+ assertEquals(200, out.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE));
+ assertEquals("<response>Beer is Good</response>",
out.getOut().getBody());
+
+ // the restlet response should be accessible if neeeded
+ Response response =
out.getOut().getHeader(RestletConstants.RESTLET_RESPONSE, Response.class);
+ assertNotNull(response);
+ assertEquals(200, response.getStatus().getCode());
}
@Override
@@ -47,14 +77,23 @@ public class RestletProducerGet2Test ext
public void configure() throws Exception {
from("direct:start").to("restlet:http://localhost:9080/users/{id}/like/{beverage.beer}");
+ // START SNIPPET: e1
from("restlet:http://localhost:9080/users/{id}/like/{beer}")
.process(new Processor() {
public void process(Exchange exchange) throws
Exception {
- String id = exchange.getIn().getHeader("id",
String.class);
- String beer = exchange.getIn().getHeader("beer",
String.class);
- exchange.getOut().setBody(id + ";Donald Duck;" +
beer);
+ // the Restlet request should be available if
neeeded
+ Request request =
exchange.getIn().getHeader(RestletConstants.RESTLET_REQUEST, Request.class);
+ assertNotNull("Restlet Request", request);
+
+ // use Restlet API to create the response
+ Response response =
exchange.getIn().getHeader(RestletConstants.RESTLET_RESPONSE, Response.class);
+ assertNotNull("Restlet Response", response);
+ response.setStatus(Status.SUCCESS_OK);
+ response.setEntity("<response>Beer is
Good</response>", MediaType.TEXT_XML);
+ exchange.getOut().setBody(response);
}
});
+ // END SNIPPET: e1
}
};
}