camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r801145 - in /camel/trunk/components: camel-http/src/main/java/org/apache/camel/component/http/ camel-jetty/src/test/java/org/apache/camel/component/jetty/
Date Wed, 05 Aug 2009 11:56:36 GMT
Author: davsclaus
Date: Wed Aug  5 11:56:35 2009
New Revision: 801145

URL: http://svn.apache.org/viewvc?rev=801145&view=rev
Log:
CAMEL-1879: added http servlet response as well to HttpMessage so end users can get hold of
it when needed.

Added:
    camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpRequestResponseTest.java
      - copied, changed from r801074, camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyRouteTest.java
Modified:
    camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/CamelServlet.java
    camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpConverter.java
    camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java
    camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpMessage.java

Modified: camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/CamelServlet.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/CamelServlet.java?rev=801145&r1=801144&r2=801145&view=diff
==============================================================================
--- camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/CamelServlet.java
(original)
+++ camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/CamelServlet.java
Wed Aug  5 11:56:35 2009
@@ -54,7 +54,7 @@
 
             // Have the camel process the HTTP exchange.
             DefaultExchange exchange = new DefaultExchange(consumer.getEndpoint(), ExchangePattern.InOut);
-            exchange.setIn(new HttpMessage(exchange, request));
+            exchange.setIn(new HttpMessage(exchange, request, response));
             consumer.getProcessor().process(exchange);
 
             // HC: The getBinding() is interesting because it illustrates the

Modified: camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpConverter.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpConverter.java?rev=801145&r1=801144&r2=801145&view=diff
==============================================================================
--- camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpConverter.java
(original)
+++ camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpConverter.java
Wed Aug  5 11:56:35 2009
@@ -21,6 +21,7 @@
 import java.io.InputStream;
 import javax.servlet.ServletInputStream;
 import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
 
 import org.apache.camel.Converter;
 import org.apache.camel.Exchange;
@@ -47,6 +48,14 @@
     }
 
     @Converter
+    public static HttpServletResponse toServletResponse(HttpMessage message) {
+        if (message == null) {
+            return null;
+        }
+        return message.getResponse();
+    }
+
+    @Converter
     public static ServletInputStream toServletInputStream(HttpMessage message) throws IOException
{
         HttpServletRequest request = toServletRequest(message);
         if (request != null) {

Modified: camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java?rev=801145&r1=801144&r2=801145&view=diff
==============================================================================
--- camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java
(original)
+++ camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java
Wed Aug  5 11:56:35 2009
@@ -84,7 +84,7 @@
 
     public Exchange createExchange(HttpServletRequest request, HttpServletResponse response)
{
         DefaultExchange exchange = new DefaultExchange(this, ExchangePattern.InOut);
-        exchange.setIn(new HttpMessage(exchange, request));
+        exchange.setIn(new HttpMessage(exchange, request, response));
         return exchange;
     }
 

Modified: camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpMessage.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpMessage.java?rev=801145&r1=801144&r2=801145&view=diff
==============================================================================
--- camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpMessage.java
(original)
+++ camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpMessage.java
Wed Aug  5 11:56:35 2009
@@ -19,6 +19,7 @@
 import java.io.IOException;
 
 import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
 
 import org.apache.camel.Exchange;
 import org.apache.camel.RuntimeCamelException;
@@ -30,10 +31,12 @@
 public class HttpMessage extends DefaultMessage {
 
     private HttpServletRequest request;
+    private HttpServletResponse response;
 
-    public HttpMessage(Exchange exchange, HttpServletRequest request) {
+    public HttpMessage(Exchange exchange, HttpServletRequest request, HttpServletResponse
response) {
         setExchange(exchange);
         this.request = request;
+        this.response = response;
 
         // use binding to read the request allowing end users to use their
         // implementation of the binding
@@ -44,6 +47,10 @@
         return request;
     }
 
+    public HttpServletResponse getResponse() {
+        return response;
+    }
+
     @Override
     protected Object createBody() {
         try {

Copied: camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpRequestResponseTest.java
(from r801074, camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyRouteTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpRequestResponseTest.java?p2=camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpRequestResponseTest.java&p1=camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyRouteTest.java&r1=801074&r2=801145&rev=801145&view=diff
==============================================================================
--- camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyRouteTest.java
(original)
+++ camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpRequestResponseTest.java
Wed Aug  5 11:56:35 2009
@@ -17,24 +17,26 @@
 package org.apache.camel.component.jetty;
 
 import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
 
 import org.apache.camel.Exchange;
 import org.apache.camel.Processor;
+import org.apache.camel.component.http.HttpMessage;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.test.junit4.CamelTestSupport;
 import org.junit.Test;
 
 /**
- * Unit test for wiki demonstration.
+ * Unit test for request response in message
  */
-public class JettyRouteTest extends CamelTestSupport {
+public class HttpRequestResponseTest extends CamelTestSupport {
 
     @Test
-    public void testSendToJetty() throws Exception {
+    public void testHttpServletRequestResponse() throws Exception {
         Object response = template.requestBody("http://localhost:9080/myapp/myservice", "bookid=123");
         // convert the response to a String
         String body = context.getTypeConverter().convertTo(String.class, response);
-        assertEquals("<html><body>Book 123 is Camel in Action</body></html>",
body);
+        assertEquals("Written by servlet response<html><body>Book 123 is Camel
in Action</body></html>", body);
     }
 
     @Override
@@ -48,7 +50,6 @@
         };
     }
 
-    // START SNIPPET: e2
     public class MyBookService implements Processor {
         public void process(Exchange exchange) throws Exception {
             // just get the body as a string
@@ -58,6 +59,18 @@
             HttpServletRequest req = exchange.getIn().getBody(HttpServletRequest.class);
             assertNotNull(req);
 
+            // we have access to the HttpServletResponse here and we can grab it if we need
it
+            HttpServletResponse res = exchange.getIn().getBody(HttpServletResponse.class);
+            assertNotNull(res);
+
+            // and they should also be on HttpMessage
+            HttpMessage msg = (HttpMessage) exchange.getIn();
+            assertNotNull(msg.getRequest());
+            assertNotNull(msg.getResponse());
+
+            // and we can use servlet response to write to output stream also
+            res.getOutputStream().print("Written by servlet response");
+
             // for unit testing
             assertEquals("bookid=123", body);
 
@@ -65,6 +78,5 @@
             exchange.getOut().setBody("<html><body>Book 123 is Camel in Action</body></html>");
         }
     }
-    // END SNIPPET: e2
 
-}
+}
\ No newline at end of file



Mime
View raw message