Author: ningjiang
Date: Thu Jun 17 09:59:42 2010
New Revision: 955530

URL: http://svn.apache.org/viewvc?rev=955530&view=rev
Log:
CAMEL-2785 merged the chucked option of camel-http  into camel 1.x branch

Modified:
    
camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/Exchange.java
    
camel/branches/camel-1.x/components/camel-http/src/main/java/org/apache/camel/component/http/DefaultHttpBinding.java
    
camel/branches/camel-1.x/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java
    
camel/branches/camel-1.x/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelContinuationServlet.java
    
camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpClientRouteTest.java

Modified: 
camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/Exchange.java
URL: 
http://svn.apache.org/viewvc/camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/Exchange.java?rev=955530&r1=955529&r2=955530&view=diff
==============================================================================
--- 
camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/Exchange.java
 (original)
+++ 
camel/branches/camel-1.x/camel-core/src/main/java/org/apache/camel/Exchange.java
 Thu Jun 17 09:59:42 2010
@@ -38,6 +38,10 @@ public interface Exchange {
     String AGGREGATED_COUNT = "org.apache.camel.Exchange.AggregatedCount";
 
     String EXCEPTION_HANDLED_PROPERTY = "CamelExceptionHandled";
+    
+    String HTTP_CHUNKED  = "CamelHttpChunked";
+    
+    String TRANSFER_ENCODING     = "Transfer-Encoding";
 
     /**
      * Returns the {...@link ExchangePattern} (MEP) of this exchange.

Modified: 
camel/branches/camel-1.x/components/camel-http/src/main/java/org/apache/camel/component/http/DefaultHttpBinding.java
URL: 
http://svn.apache.org/viewvc/camel/branches/camel-1.x/components/camel-http/src/main/java/org/apache/camel/component/http/DefaultHttpBinding.java?rev=955530&r1=955529&r2=955530&view=diff
==============================================================================
--- 
camel/branches/camel-1.x/components/camel-http/src/main/java/org/apache/camel/component/http/DefaultHttpBinding.java
 (original)
+++ 
camel/branches/camel-1.x/components/camel-http/src/main/java/org/apache/camel/component/http/DefaultHttpBinding.java
 Thu Jun 17 09:59:42 2010
@@ -25,6 +25,7 @@ import javax.servlet.ServletOutputStream
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
+import org.apache.camel.Exchange;
 import org.apache.camel.InvalidTypeException;
 import org.apache.camel.Message;
 import org.apache.camel.component.http.helper.GZIPHelper;
@@ -114,7 +115,20 @@ public class DefaultHttpBinding implemen
         if (request.getHeader(GZIPHelper.CONTENT_ENCODING) != null) {
             String contentEncoding = 
request.getHeader(GZIPHelper.CONTENT_ENCODING, String.class);
             response.setHeader(GZIPHelper.CONTENT_ENCODING, contentEncoding);
-        }        
+        }
+        if (checkChunked(response)) {
+            response.setHeader(Exchange.TRANSFER_ENCODING, "chunked");
+        }
+    }
+    
+    protected boolean checkChunked(Message message) {
+        // set the default value to be true
+        boolean answer = true;
+        if (message.getHeader(Exchange.HTTP_CHUNKED) != null) {
+            
+            answer = message.getHeader(Exchange.HTTP_CHUNKED, boolean.class);
+        }
+        return answer;
     }
 
     public void doWriteExceptionResponse(Throwable exception, 
HttpServletResponse response) throws IOException {
@@ -163,7 +177,10 @@ public class DefaultHttpBinding implemen
     }
 
     protected void doWriteDirectResponse(Message message, HttpServletResponse 
response) throws IOException {
-        InputStream is = message.getBody(InputStream.class);
+        InputStream is = null;
+        if (checkChunked(message)) {
+            is = message.getBody(InputStream.class);
+        }
         if (is != null) {
             ServletOutputStream os = response.getOutputStream();
             try {

Modified: 
camel/branches/camel-1.x/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java
URL: 
http://svn.apache.org/viewvc/camel/branches/camel-1.x/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java?rev=955530&r1=955529&r2=955530&view=diff
==============================================================================
--- 
camel/branches/camel-1.x/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java
 (original)
+++ 
camel/branches/camel-1.x/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java
 Thu Jun 17 09:59:42 2010
@@ -48,6 +48,7 @@ public class HttpEndpoint extends Defaul
     private HttpClientParams clientParams;
     private HttpClientConfigurer httpClientConfigurer;
     private HttpConnectionManager httpConnectionManager;
+    private boolean chunked = true;
 
     public HttpEndpoint(String endPointURI, HttpComponent component, URI 
httpURI, HttpConnectionManager httpConnectionManager) throws URISyntaxException 
{
         this(endPointURI, component, httpURI, new HttpClientParams(), 
httpConnectionManager, null);
@@ -153,6 +154,14 @@ public class HttpEndpoint extends Defaul
     public void setHttpClientConfigurer(HttpClientConfigurer 
httpClientConfigurer) {
         this.httpClientConfigurer = httpClientConfigurer;
     }
+    
+    public boolean isChunked() {
+        return this.chunked;
+    }
+    
+    public void setChunked(boolean chunked) {
+        this.chunked = chunked;
+    }
 
     public HttpBinding getBinding() {
         if (binding == null) {

Modified: 
camel/branches/camel-1.x/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelContinuationServlet.java
URL: 
http://svn.apache.org/viewvc/camel/branches/camel-1.x/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelContinuationServlet.java?rev=955530&r1=955529&r2=955530&view=diff
==============================================================================
--- 
camel/branches/camel-1.x/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelContinuationServlet.java
 (original)
+++ 
camel/branches/camel-1.x/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelContinuationServlet.java
 Thu Jun 17 09:59:42 2010
@@ -23,6 +23,7 @@ import javax.servlet.http.HttpServletReq
 import javax.servlet.http.HttpServletResponse;
 
 import org.apache.camel.AsyncCallback;
+import org.apache.camel.Exchange;
 import org.apache.camel.component.http.CamelServlet;
 import org.apache.camel.component.http.HttpConsumer;
 import org.apache.camel.component.http.HttpExchange;
@@ -51,6 +52,9 @@ public class CamelContinuationServlet ex
 
                 // Have the camel process the HTTP exchange.
                 final HttpExchange exchange = new 
HttpExchange(consumer.getEndpoint(), request, response);
+                if (!consumer.getEndpoint().isChunked()) {
+                    exchange.getIn().setHeader(Exchange.HTTP_CHUNKED, false);
+                }
                 boolean sync = consumer.getAsyncProcessor().process(exchange, 
new AsyncCallback() {
                     public void done(boolean sync) {
                         if (sync) {

Modified: 
camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpClientRouteTest.java
URL: 
http://svn.apache.org/viewvc/camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpClientRouteTest.java?rev=955530&r1=955529&r2=955530&view=diff
==============================================================================
--- 
camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpClientRouteTest.java
 (original)
+++ 
camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpClientRouteTest.java
 Thu Jun 17 09:59:42 2010
@@ -39,8 +39,17 @@ import org.apache.commons.httpclient.Chu
 import org.apache.commons.httpclient.ChunkedOutputStream;
 
 public class HttpClientRouteTest extends ContextTestSupport {
+    
+    public void testHttpRouteWithMessageHeader() throws Exception {
+        invokeHttpClient("direct:start");
+    }
+    
+    
+    public void testHttpRouteWithOption() throws Exception {
+        invokeHttpClient("direct:start2");
+    }
 
-    public void testHttpClient() throws Exception {
+    private void invokeHttpClient(String uri) throws Exception {
         System.getProperties().put("HTTPClient.dontChunkRequests", "yes");
 
         MockEndpoint mockEndpoint = getMockEndpoint("mock:a");
@@ -61,6 +70,9 @@ public class HttpClientRouteTest extends
         log.info("Headers: " + headers);
 
         assertTrue("Should be more than one header but was: " + headers, 
headers.size() > 0);
+        // should get the Content-Length
+        assertNotNull("Should get the content-lenghth ", 
headers.get("Content-Length"));
+        System.getProperties().remove("HTTPClient.dontChunkRequests");
     }
 
     protected RouteBuilder createRouteBuilder() throws Exception {
@@ -77,6 +89,8 @@ public class HttpClientRouteTest extends
                 
                 
from("direct:start").to("http://localhost:9080/hello";).process(clientProc).intercept(new
 StreamCachingInterceptor()).convertBodyTo(String.class).to("mock:a");
                
+                
from("direct:start2").to("http://localhost:9081/hello";).to("mock:a");
+                
                 Processor proc = new Processor() {
                     public void process(Exchange exchange) throws Exception {
                         ByteArrayInputStream bis = new 
ByteArrayInputStream("<b>Hello World</b>".getBytes());                        
@@ -84,7 +98,9 @@ public class HttpClientRouteTest extends
                         exchange.getOut(true).setBody(bis);
                     }
                 };
-                from("jetty:http://localhost:9080/hello";).process(proc);
+                
from("jetty:http://localhost:9080/hello";).process(proc).setHeader(Exchange.HTTP_CHUNKED).constant(false);
+                
+                
from("jetty:http://localhost:9081/hello?chunked=false";).process(proc);
                 
                 noStreamCaching();
             }


Reply via email to