Author: ningjiang Date: Wed May 5 14:13:07 2010 New Revision: 941301 URL: http://svn.apache.org/viewvc?rev=941301&view=rev Log: CAMEL-2694 merge the patch of GZIPHelper into Camel 1.x branch
Added: camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpGZipEncodingTest.java (with props) Modified: camel/branches/camel-1.x/components/camel-http/src/main/java/org/apache/camel/component/http/HttpConverter.java camel/branches/camel-1.x/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java camel/branches/camel-1.x/components/camel-http/src/main/java/org/apache/camel/component/http/RequestEntityConverter.java camel/branches/camel-1.x/components/camel-http/src/main/java/org/apache/camel/component/http/helper/GZIPHelper.java camel/branches/camel-1.x/components/camel-http/src/test/java/org/apache/camel/component/http/HttpGetTest.java camel/branches/camel-1.x/components/camel-http/src/test/java/org/apache/camel/component/http/HttpGetWithHeadersTest.java Modified: camel/branches/camel-1.x/components/camel-http/src/main/java/org/apache/camel/component/http/HttpConverter.java URL: http://svn.apache.org/viewvc/camel/branches/camel-1.x/components/camel-http/src/main/java/org/apache/camel/component/http/HttpConverter.java?rev=941301&r1=941300&r2=941301&view=diff ============================================================================== --- camel/branches/camel-1.x/components/camel-http/src/main/java/org/apache/camel/component/http/HttpConverter.java (original) +++ camel/branches/camel-1.x/components/camel-http/src/main/java/org/apache/camel/component/http/HttpConverter.java Wed May 5 14:13:07 2010 @@ -74,7 +74,7 @@ public final class HttpConverter { return null; } String contentEncoding = request.getHeader(GZIPHelper.CONTENT_ENCODING); - return GZIPHelper.toGZIPInputStream(contentEncoding, request.getInputStream()); + return GZIPHelper.uncompressGzip(contentEncoding, request.getInputStream()); } } Modified: camel/branches/camel-1.x/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java URL: http://svn.apache.org/viewvc/camel/branches/camel-1.x/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java?rev=941301&r1=941300&r2=941301&view=diff ============================================================================== --- camel/branches/camel-1.x/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java (original) +++ camel/branches/camel-1.x/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java Wed May 5 14:13:07 2010 @@ -170,7 +170,7 @@ public class HttpProducer extends Defaul Header header = method.getRequestHeader(GZIPHelper.CONTENT_ENCODING); String contentEncoding = header != null ? header.getValue() : null; - is = GZIPHelper.toGZIPInputStream(contentEncoding, is); + is = GZIPHelper.uncompressGzip(contentEncoding, is); return doExtractResponseBody(is, exchange); } Modified: camel/branches/camel-1.x/components/camel-http/src/main/java/org/apache/camel/component/http/RequestEntityConverter.java URL: http://svn.apache.org/viewvc/camel/branches/camel-1.x/components/camel-http/src/main/java/org/apache/camel/component/http/RequestEntityConverter.java?rev=941301&r1=941300&r2=941301&view=diff ============================================================================== --- camel/branches/camel-1.x/components/camel-http/src/main/java/org/apache/camel/component/http/RequestEntityConverter.java (original) +++ camel/branches/camel-1.x/components/camel-http/src/main/java/org/apache/camel/component/http/RequestEntityConverter.java Wed May 5 14:13:07 2010 @@ -63,15 +63,13 @@ public class RequestEntityConverter { private RequestEntity asRequestEntity(InputStream in, Exchange exchange) throws IOException { return new InputStreamRequestEntity( - GZIPHelper.toGZIPInputStream( - exchange.getIn().getHeader(GZIPHelper.CONTENT_ENCODING, String.class), + GZIPHelper.compressGzip(exchange.getIn().getHeader(GZIPHelper.CONTENT_ENCODING, String.class), in), ExchangeHelper.getContentType(exchange)); } private RequestEntity asRequestEntity(byte[] data, Exchange exchange) throws Exception { return new InputStreamRequestEntity( - GZIPHelper.toGZIPInputStream( - exchange.getIn().getHeader(GZIPHelper.CONTENT_ENCODING, String.class), + GZIPHelper.compressGzip(exchange.getIn().getHeader(GZIPHelper.CONTENT_ENCODING, String.class), data), ExchangeHelper.getContentType(exchange)); } Modified: camel/branches/camel-1.x/components/camel-http/src/main/java/org/apache/camel/component/http/helper/GZIPHelper.java URL: http://svn.apache.org/viewvc/camel/branches/camel-1.x/components/camel-http/src/main/java/org/apache/camel/component/http/helper/GZIPHelper.java?rev=941301&r1=941300&r2=941301&view=diff ============================================================================== --- camel/branches/camel-1.x/components/camel-http/src/main/java/org/apache/camel/component/http/helper/GZIPHelper.java (original) +++ camel/branches/camel-1.x/components/camel-http/src/main/java/org/apache/camel/component/http/helper/GZIPHelper.java Wed May 5 14:13:07 2010 @@ -25,6 +25,7 @@ import java.util.zip.GZIPOutputStream; import javax.servlet.http.HttpServletResponse; import org.apache.camel.Message; +import org.apache.camel.util.IOHelper; import org.apache.camel.util.ObjectHelper; /** @@ -54,15 +55,34 @@ public final class GZIPHelper { response.setHeader(CONTENT_ENCODING, GZIP); } - public static InputStream toGZIPInputStream(String contentEncoding, InputStream in) throws IOException { + public static InputStream uncompressGzip(String contentEncoding, InputStream in) throws IOException { if (isGzip(contentEncoding)) { return new GZIPInputStream(in); } else { return in; } } + + public static InputStream compressGzip(String contentEncoding, InputStream in) throws IOException { + + if (isGzip(contentEncoding)) { + ByteArrayOutputStream os = new ByteArrayOutputStream(); + GZIPOutputStream gzip = new GZIPOutputStream(os); + try { + IOHelper.copy(in, gzip); + gzip.finish(); + return new ByteArrayInputStream(os.toByteArray()); + } finally { + ObjectHelper.close(gzip, "gzip", null); + ObjectHelper.close(os, "byte array output stream", null); + } + } else { + return in; + } + + } - public static InputStream toGZIPInputStream(String contentEncoding, byte[] data) throws Exception { + public static InputStream compressGzip(String contentEncoding, byte[] data) throws IOException { if (isGzip(contentEncoding)) { ByteArrayOutputStream os = null; GZIPOutputStream gzip = null; Modified: camel/branches/camel-1.x/components/camel-http/src/test/java/org/apache/camel/component/http/HttpGetTest.java URL: http://svn.apache.org/viewvc/camel/branches/camel-1.x/components/camel-http/src/test/java/org/apache/camel/component/http/HttpGetTest.java?rev=941301&r1=941300&r2=941301&view=diff ============================================================================== --- camel/branches/camel-1.x/components/camel-http/src/test/java/org/apache/camel/component/http/HttpGetTest.java (original) +++ camel/branches/camel-1.x/components/camel-http/src/test/java/org/apache/camel/component/http/HttpGetTest.java Wed May 5 14:13:07 2010 @@ -50,7 +50,7 @@ public class HttpGetTest extends Context log.debug("Headers: " + headers); checkHeaders(headers); - String body = in.getBody(String.class); + String body = in.getBody(String.class); log.debug("Body: " + body); assertNotNull("Should have a body!", body); Modified: camel/branches/camel-1.x/components/camel-http/src/test/java/org/apache/camel/component/http/HttpGetWithHeadersTest.java URL: http://svn.apache.org/viewvc/camel/branches/camel-1.x/components/camel-http/src/test/java/org/apache/camel/component/http/HttpGetWithHeadersTest.java?rev=941301&r1=941300&r2=941301&view=diff ============================================================================== --- camel/branches/camel-1.x/components/camel-http/src/test/java/org/apache/camel/component/http/HttpGetWithHeadersTest.java (original) +++ camel/branches/camel-1.x/components/camel-http/src/test/java/org/apache/camel/component/http/HttpGetWithHeadersTest.java Wed May 5 14:13:07 2010 @@ -29,7 +29,7 @@ public class HttpGetWithHeadersTest exte from("direct:start") .setHeader("TestHeader", constant("test")) .setHeader("Content-Length", constant(0)) - .setHeader("Accept-Language", constant("pl")) + .setHeader("Accept-Language", constant("en")) .to("http://www.google.com/search") .to("mock:results"); } @@ -39,7 +39,7 @@ public class HttpGetWithHeadersTest exte @Override protected void setUp() throws Exception { // "Szukaj" is "Search" in polish language - expectedText = "Szukaj"; + expectedText = "Google"; super.setUp(); } Added: camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpGZipEncodingTest.java URL: http://svn.apache.org/viewvc/camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpGZipEncodingTest.java?rev=941301&view=auto ============================================================================== --- camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpGZipEncodingTest.java (added) +++ camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpGZipEncodingTest.java Wed May 5 14:13:07 2010 @@ -0,0 +1,59 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.jetty; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.ExpressionBuilder; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.converter.IOConverter; + + +public class HttpGZipEncodingTest extends ContextTestSupport { + + public void testHttpProducerWithGzip() throws Exception { + InputStream response = (InputStream) template.requestBodyAndHeader("http://localhost:9081/gzip", new ByteArrayInputStream("<Hello>World</Hello>".getBytes()), "Content-Encoding", "gzip"); + assertEquals("The response is wrong", "<b>Hello World</b>", IOConverter.toString(response)); + } + + + + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + public void configure() { + errorHandler(noErrorHandler()); + + 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); + exchange.getOut().setHeader("Content-Encoding", "gzip"); + exchange.getOut().setBody("<b>Hello World</b>"); + } + + }); + + } + }; + } + +} Propchange: camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpGZipEncodingTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/branches/camel-1.x/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpGZipEncodingTest.java ------------------------------------------------------------------------------ svn:keywords = Rev Date