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 2012/12/18 08:11:26 UTC

svn commit: r1423301 - /camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpEntityConverter.java

Author: ningjiang
Date: Tue Dec 18 07:11:19 2012
New Revision: 1423301

URL: http://svn.apache.org/viewvc?rev=1423301&view=rev
Log:
CAMEL-5890 Fix the same NPE issue in HttpEntityConverter

Modified:
    camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpEntityConverter.java

Modified: camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpEntityConverter.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpEntityConverter.java?rev=1423301&r1=1423300&r2=1423301&view=diff
==============================================================================
--- camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpEntityConverter.java (original)
+++ camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpEntityConverter.java Tue Dec 18 07:11:19 2012
@@ -48,7 +48,7 @@ public final class HttpEntityConverter {
 
     @Converter
     public static HttpEntity toHttpEntity(String str, Exchange exchange) throws Exception {
-        if (GZIPHelper.isGzip(exchange.getIn())) {
+        if (exchange != null && GZIPHelper.isGzip(exchange.getIn())) {
             byte[] data = exchange.getContext().getTypeConverter().convertTo(byte[].class, str);
             return asHttpEntity(data, exchange);
         } else {
@@ -58,33 +58,36 @@ public final class HttpEntityConverter {
     }
 
     private static HttpEntity asHttpEntity(InputStream in, Exchange exchange) throws IOException {
-        String contentEncoding = exchange.getIn().getHeader(Exchange.CONTENT_ENCODING, String.class);
-        String contentType = ExchangeHelper.getContentType(exchange);
-
         InputStreamEntity entity;
         if (!exchange.getProperty(Exchange.SKIP_GZIP_ENCODING, Boolean.FALSE, Boolean.class)) {
+            String contentEncoding = exchange.getIn().getHeader(Exchange.CONTENT_ENCODING, String.class);
             entity = new InputStreamEntity(GZIPHelper.compressGzip(contentEncoding, in), -1);        
         } else {
             entity = new InputStreamEntity(in, -1);
         }
-        entity.setContentEncoding(contentEncoding);
-        entity.setContentType(contentType);
+        if (exchange != null) {
+            String contentEncoding = exchange.getIn().getHeader(Exchange.CONTENT_ENCODING, String.class);
+            String contentType = ExchangeHelper.getContentType(exchange);
+            entity.setContentEncoding(contentEncoding);
+            entity.setContentType(contentType);
+        }
         return entity;
     }
 
     private static HttpEntity asHttpEntity(byte[] data, Exchange exchange) throws Exception {
-        String contentEncoding = exchange.getIn().getHeader(Exchange.CONTENT_ENCODING, String.class);
-        String contentType = ExchangeHelper.getContentType(exchange);
-
         InputStreamEntity entity;
-        if (!exchange.getProperty(Exchange.SKIP_GZIP_ENCODING, Boolean.FALSE, Boolean.class)) {
+        if (exchange != null && !exchange.getProperty(Exchange.SKIP_GZIP_ENCODING, Boolean.FALSE, Boolean.class)) {
+            String contentEncoding = exchange.getIn().getHeader(Exchange.CONTENT_ENCODING, String.class);
             entity = new InputStreamEntity(GZIPHelper.compressGzip(contentEncoding, data), -1);
         } else {
             entity = new InputStreamEntity(new ByteArrayInputStream(data), -1);
         }
-        entity.setContentEncoding(contentEncoding);
-        entity.setContentType(contentType);
-
+        if (exchange != null) {
+            String contentEncoding = exchange.getIn().getHeader(Exchange.CONTENT_ENCODING, String.class);
+            String contentType = ExchangeHelper.getContentType(exchange);
+            entity.setContentEncoding(contentEncoding);
+            entity.setContentType(contentType);
+        }
         return entity;
     }
 }
\ No newline at end of file