You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ni...@apache.org on 2009/05/14 10:32:37 UTC

svn commit: r774683 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/ camel-core/src/main/java/org/apache/camel/util/ components/camel-http/src/main/java/org/apache/camel/component/http/ components/camel-jetty/src/test/java/org/apache/camel...

Author: ningjiang
Date: Thu May 14 08:32:36 2009
New Revision: 774683

URL: http://svn.apache.org/viewvc?rev=774683&view=rev
Log:
CAMEL-1594 using Exchange.CAMEL_CONTENT_TYPE in camel-http and camel-jetty components

Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExchangeHelper.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/util/MessageHelper.java
    camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/DefaultHttpBinding.java
    camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java
    camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyContentTypeTest.java
    camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyImageFileTest.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java?rev=774683&r1=774682&r2=774683&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java Thu May 14 08:32:36 2009
@@ -31,6 +31,7 @@
 public interface Exchange {
 
     String ASYNC_WAIT = "CamelAsyncWait";
+    String CAMEL_CONTENT_TYPE = "CamelConentType";
 
     String BEAN_METHOD_NAME = "CamelBeanMethodName";
     String BEAN_HOLDER = "CamelBeanHolder";

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExchangeHelper.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExchangeHelper.java?rev=774683&r1=774682&r2=774683&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExchangeHelper.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExchangeHelper.java Thu May 14 08:32:36 2009
@@ -336,7 +336,7 @@
      * Returns the MIME content type on the input message or null if one is not defined
      */
     public static String getContentType(Exchange exchange) {
-        return exchange.getIn().getHeader("Content-Type", String.class);
+        return MessageHelper.getContentType(exchange.getIn());        
     }
 
     /**

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/util/MessageHelper.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/MessageHelper.java?rev=774683&r1=774682&r2=774683&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/util/MessageHelper.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/util/MessageHelper.java Thu May 14 08:32:36 2009
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.util;
 
+import org.apache.camel.Exchange;
 import org.apache.camel.Message;
 import org.apache.camel.StreamCache;
 
@@ -97,4 +98,16 @@
             ((StreamCache) message.getBody()).reset();
         }
     }
+    
+    /**
+     * Returns the MIME content type on the message or null if one is not defined
+     */
+    public static String getContentType(Message message) {        
+        String contentType = message.getHeader(Exchange.CAMEL_CONTENT_TYPE, String.class);
+        if (contentType == null) {
+            // fallback with the Content-Type
+            contentType = message.getHeader("Content-Type", String.class);
+        }
+        return contentType;
+    }
 }

Modified: camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/DefaultHttpBinding.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/DefaultHttpBinding.java?rev=774683&r1=774682&r2=774683&view=diff
==============================================================================
--- camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/DefaultHttpBinding.java (original)
+++ camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/DefaultHttpBinding.java Thu May 14 08:32:36 2009
@@ -31,6 +31,7 @@
 import org.apache.camel.Message;
 import org.apache.camel.component.http.helper.GZIPHelper;
 import org.apache.camel.spi.HeaderFilterStrategy;
+import org.apache.camel.util.MessageHelper;
 
 /**
  * Binding between {@link HttpMessage} and {@link HttpServletResponse}.
@@ -60,6 +61,10 @@
         while (names.hasMoreElements()) {
             String name = (String)names.nextElement();
             Object value = request.getHeader(name);
+            // mapping the content-type 
+            if (name.toLowerCase().equals("content-type")) {
+                name = Exchange.CAMEL_CONTENT_TYPE;
+            }
             if (headerFilterStrategy != null
                 && !headerFilterStrategy.applyFilterToExternalHeaders(name, value, message.getExchange())) {
                 headers.put(name, value);
@@ -134,8 +139,8 @@
             response.setStatus(code);
         }
         // set the content type in the response.
-        if (message.getHeader("Content-Type") != null) {            
-            String contentType = message.getHeader("Content-Type", String.class);            
+        String contentType = MessageHelper.getContentType(message);
+        if (MessageHelper.getContentType(message) != null) {
             response.setContentType(contentType);
         }
 

Modified: camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java?rev=774683&r1=774682&r2=774683&view=diff
==============================================================================
--- camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java (original)
+++ camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java Thu May 14 08:32:36 2009
@@ -98,7 +98,7 @@
     protected void populateResponse(Exchange exchange, HttpMethod method, Message in, HeaderFilterStrategy strategy, int responseCode) throws IOException {
         Message answer = exchange.getOut();
 
-        answer.setHeaders(in.getHeaders());
+        //answer.setHeaders(in.getHeaders());
         answer.setHeader(HttpConstants.HTTP_RESPONSE_CODE, responseCode);
         answer.setBody(extractResponseBody(method, exchange));
 
@@ -107,6 +107,9 @@
         for (Header header : headers) {
             String name = header.getName();
             String value = header.getValue();
+            if (name.toLowerCase().equals("content-type")) {
+                name = Exchange.CAMEL_CONTENT_TYPE;
+            }
             if (strategy != null && !strategy.applyFilterToExternalHeaders(name, value, exchange)) {
                 answer.setHeader(name, value);
             }

Modified: camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyContentTypeTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyContentTypeTest.java?rev=774683&r1=774682&r2=774683&view=diff
==============================================================================
--- camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyContentTypeTest.java (original)
+++ camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyContentTypeTest.java Thu May 14 08:32:36 2009
@@ -22,6 +22,8 @@
 import org.apache.camel.Processor;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.http.helper.GZIPHelper;
+import org.apache.camel.util.ExchangeHelper;
+import org.apache.camel.util.MessageHelper;
 
 /**
  * Unit test for content-type
@@ -39,8 +41,9 @@
         template.send(endpoint, exchange);
 
         String body = exchange.getOut().getBody(String.class);
+        System.out.print("The out message header is " + exchange.getOut().getHeaders());
         assertEquals("<order>OK</order>", body);
-        assertOutMessageHeader(exchange, "Content-Type", "text/xml");
+        assertEquals("Get a wrong content-type ", MessageHelper.getContentType(exchange.getOut()), "text/xml");
     }
 
     public void testSameContentType() throws Exception {
@@ -60,7 +63,7 @@
 
         String body = exchange.getOut().getBody(String.class);
         assertEquals("FAIL", body);
-        assertOutMessageHeader(exchange, "Content-Type", "text/plain");
+        assertEquals("Get a wrong content-type ", MessageHelper.getContentType(exchange.getOut()), "text/plain");
     }
 
     @Override
@@ -76,12 +79,12 @@
         public void process(Exchange exchange) throws Exception {
             if (exchange.getIn().getHeader("user") != null 
                 && exchange.getIn().getBody(String.class).equals("<order>123</order>")
-                && exchange.getIn().getHeader("Content-Type").equals("text/xml")) {
+                && "text/xml".equals(ExchangeHelper.getContentType(exchange))) {
                 exchange.getOut().setBody("<order>OK</order>");
                 exchange.getOut().setHeader("Content-Type", "text/xml");
             } else {
                 exchange.getOut().setBody("FAIL");
-                exchange.getOut().setHeader("Content-Type", "text/plain");
+                exchange.getOut().setHeader(Exchange.CAMEL_CONTENT_TYPE, "text/plain");
             }
         }
     }

Modified: camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyImageFileTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyImageFileTest.java?rev=774683&r1=774682&r2=774683&view=diff
==============================================================================
--- camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyImageFileTest.java (original)
+++ camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyImageFileTest.java Thu May 14 08:32:36 2009
@@ -24,6 +24,7 @@
 import org.apache.camel.Processor;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.http.helper.GZIPHelper;
+import org.apache.camel.util.MessageHelper;
 
 /**
  * Unit test for exposing a http server that returns images
@@ -39,7 +40,7 @@
         template.send(endpoint, exchange);
 
         assertNotNull(exchange.getOut().getBody());
-        assertOutMessageHeader(exchange, "Content-Type", "image/jpeg");
+        assertEquals("Get a wrong content-type ", MessageHelper.getContentType(exchange.getOut()), "image/jpeg");
     }
 
     public void testImageContentType() throws Exception {