Author: ningjiang Date: Thu Jul 22 08:02:51 2010 New Revision: 966531 URL: http://svn.apache.org/viewvc?rev=966531&view=rev Log: CAMEL-2985 HttpProducer should check the response header with the content encoding instead of the request one
Modified: camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/RequestEntityConverter.java camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpEntityConverter.java camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpProducer.java camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpGZipEncodingTest.java camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyContentTypeTest.java 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=966531&r1=966530&r2=966531&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 Jul 22 08:02:51 2010 @@ -203,7 +203,7 @@ public class HttpProducer extends Defaul return null; } - Header header = method.getRequestHeader(Exchange.CONTENT_ENCODING); + Header header = method.getResponseHeader(Exchange.CONTENT_ENCODING); String contentEncoding = header != null ? header.getValue() : null; if (!exchange.getProperty(Exchange.SKIP_GZIP_ENCODING, Boolean.FALSE, Boolean.class)) { Modified: camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/RequestEntityConverter.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/RequestEntityConverter.java?rev=966531&r1=966530&r2=966531&view=diff ============================================================================== --- camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/RequestEntityConverter.java (original) +++ camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/RequestEntityConverter.java Thu Jul 22 08:02:51 2010 @@ -55,8 +55,8 @@ public class RequestEntityConverter { } private RequestEntity asRequestEntity(InputStream in, Exchange exchange) throws IOException { - if (exchange == null - || !exchange.getProperty(Exchange.SKIP_GZIP_ENCODING, Boolean.FALSE, Boolean.class)) { + if (exchange != null + && !exchange.getProperty(Exchange.SKIP_GZIP_ENCODING, Boolean.FALSE, Boolean.class)) { return new InputStreamRequestEntity(GZIPHelper.compressGzip(exchange.getIn() .getHeader(Exchange.CONTENT_ENCODING, String.class), in), ExchangeHelper .getContentType(exchange)); @@ -66,8 +66,8 @@ public class RequestEntityConverter { } private RequestEntity asRequestEntity(byte[] data, Exchange exchange) throws Exception { - if (exchange == null - || !exchange.getProperty(Exchange.SKIP_GZIP_ENCODING, Boolean.FALSE, Boolean.class)) { + if (exchange != null + && !exchange.getProperty(Exchange.SKIP_GZIP_ENCODING, Boolean.FALSE, Boolean.class)) { return new InputStreamRequestEntity(GZIPHelper.compressGzip(exchange.getIn() .getHeader(Exchange.CONTENT_ENCODING, String.class), data), ExchangeHelper .getContentType(exchange)); 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=966531&r1=966530&r2=966531&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 Thu Jul 22 08:02:51 2010 @@ -59,8 +59,8 @@ public class HttpEntityConverter { String contentType = ExchangeHelper.getContentType(exchange); InputStreamEntity entity = null; - if (exchange == null - || !exchange.getProperty(Exchange.SKIP_GZIP_ENCODING, Boolean.FALSE, Boolean.class)) { + if (exchange != null + && !exchange.getProperty(Exchange.SKIP_GZIP_ENCODING, Boolean.FALSE, Boolean.class)) { entity = new InputStreamEntity(GZIPHelper.compressGzip(contentEncoding, in), -1); } else { entity = new InputStreamEntity(in, -1); @@ -75,8 +75,8 @@ public class HttpEntityConverter { String contentType = ExchangeHelper.getContentType(exchange); InputStreamEntity entity = null; - if (exchange == null - || !exchange.getProperty(Exchange.SKIP_GZIP_ENCODING, Boolean.FALSE, Boolean.class)) { + if (exchange != null + && !exchange.getProperty(Exchange.SKIP_GZIP_ENCODING, Boolean.FALSE, Boolean.class)) { entity = new InputStreamEntity(GZIPHelper.compressGzip(contentEncoding, data), -1); } else { entity = new InputStreamEntity(new ByteArrayInputStream(data), -1); Modified: camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpProducer.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpProducer.java?rev=966531&r1=966530&r2=966531&view=diff ============================================================================== --- camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpProducer.java (original) +++ camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpProducer.java Thu Jul 22 08:02:51 2010 @@ -205,7 +205,7 @@ public class HttpProducer extends Defaul return null; } - Header header = httpRequest.getFirstHeader(Exchange.CONTENT_ENCODING); + Header header = httpResponse.getFirstHeader(Exchange.CONTENT_ENCODING); String contentEncoding = header != null ? header.getValue() : null; if (!exchange.getProperty(Exchange.SKIP_GZIP_ENCODING, Boolean.FALSE, Boolean.class)) { Modified: camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpGZipEncodingTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpGZipEncodingTest.java?rev=966531&r1=966530&r2=966531&view=diff ============================================================================== --- camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpGZipEncodingTest.java (original) +++ camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpGZipEncodingTest.java Thu Jul 22 08:02:51 2010 @@ -19,6 +19,8 @@ package org.apache.camel.component.jetty import java.io.ByteArrayInputStream; import java.io.InputStream; +import javax.servlet.http.HttpServletRequest; + import org.apache.camel.Exchange; import org.apache.camel.Processor; import org.apache.camel.builder.ExpressionBuilder; @@ -47,6 +49,12 @@ public class HttpGZipEncodingTest extend assertEquals("The response is wrong", "<b>Hello World</b>", response); } + @Test + public void testGzipGet() throws Exception { + String response = template.requestBodyAndHeader("http://localhost:9081/gzip", null, "Accept-Encoding", "gzip", String.class); + assertEquals("The response is wrong", "<b>Hello World for gzip</b>", response); + } + protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { public void configure() { @@ -60,10 +68,22 @@ public class HttpGZipEncodingTest extend from("jetty:http://localhost:9081/gzip").process(new Processor() { public void process(Exchange exchange) throws Exception { - String request = exchange.getIn().getBody(String.class); - assertEquals("Get a wrong request string", "<Hello>World</Hello>", request); + // check the request method + HttpServletRequest request = exchange.getIn().getHeader(Exchange.HTTP_SERVLET_REQUEST, HttpServletRequest.class); + if ("POST".equals(request.getMethod())) { + String requestBody = exchange.getIn().getBody(String.class); + assertEquals("Get a wrong request string", "<Hello>World</Hello>", requestBody); + } exchange.getOut().setHeader(Exchange.CONTENT_ENCODING, "gzip"); - exchange.getOut().setBody("<b>Hello World</b>"); + // check the Accept Encoding header + String header = exchange.getIn().getHeader("Accept-Encoding", String.class); + if (header != null && header.indexOf("gzip") > -1) { + exchange.getOut().setBody("<b>Hello World for gzip</b>"); + } else { + exchange.getOut().setBody("<b>Hello World</b>"); + } + + } }); 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=966531&r1=966530&r2=966531&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 Jul 22 08:02:51 2010 @@ -38,7 +38,7 @@ public class JettyContentTypeTest extend exchange.getIn().setHeader("SOAPAction", "test"); exchange.getIn().setHeader("Content-Type", "text/xml"); if (usingGZip) { - exchange.getIn().setHeader("Content-Encoding", "gzip"); + exchange.getIn().setHeader(Exchange.CONTENT_ENCODING, "gzip"); } template.send(endpoint, exchange); @@ -84,6 +84,10 @@ public class JettyContentTypeTest extend String user = exchange.getIn().getHeader("User", String.class); String contentType = ExchangeHelper.getContentType(exchange); String body = exchange.getIn().getBody(String.class); + String encoding = exchange.getIn().getHeader(Exchange.CONTENT_ENCODING, String.class); + if (encoding != null) { + exchange.getOut().setHeader(Exchange.CONTENT_ENCODING, encoding); + } if ("Claus".equals(user) && contentType.startsWith("text/xml") && body.equals("<order>123</order>")) { assertEquals("test", exchange.getIn().getHeader("SOAPAction", String.class)); exchange.getOut().setBody("<order>OK</order>");