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 2010/05/05 15:11:26 UTC
svn commit: r941275 - in /camel/trunk:
camel-core/src/main/java/org/apache/camel/
components/camel-http/src/main/java/org/apache/camel/component/http/
components/camel-http/src/main/java/org/apache/camel/component/http/helper/
components/camel-http/src...
Author: ningjiang
Date: Wed May 5 13:11:26 2010
New Revision: 941275
URL: http://svn.apache.org/viewvc?rev=941275&view=rev
Log:
CAMEL-2686 CAMEL-2694 fix some issues about the gzip of camel-http
Added:
camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpGZipEncodingTest.java (with props)
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java
camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/CamelServlet.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/HttpConverter.java
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-http/src/main/java/org/apache/camel/component/http/helper/GZIPHelper.java
camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/helper/GZIPHelperTest.java
camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpConverterTest.java
camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/test/MyTypeConverter.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=941275&r1=941274&r2=941275&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 Wed May 5 13:11:26 2010
@@ -104,6 +104,8 @@ public interface Exchange {
String REDELIVERY_EXHAUSTED = "CamelRedeliveryExhausted";
String ROLLBACK_ONLY = "CamelRollbackOnly";
String ROLLBACK_ONLY_LAST = "CamelRollbackOnlyLast";
+
+ String SKIP_GZIP_ENCODING = "CamelSkipGzipEncoding";
String SOAP_ACTION = "CamelSoapAction";
String SPLIT_INDEX = "CamelSplitIndex";
Modified: camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/CamelServlet.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/CamelServlet.java?rev=941275&r1=941274&r2=941275&view=diff
==============================================================================
--- camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/CamelServlet.java (original)
+++ camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/CamelServlet.java Wed May 5 13:11:26 2010
@@ -24,6 +24,7 @@ import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
+import org.apache.camel.Exchange;
import org.apache.camel.ExchangePattern;
import org.apache.camel.impl.DefaultExchange;
@@ -55,6 +56,9 @@ public class CamelServlet extends HttpSe
// Have the camel process the HTTP exchange.
DefaultExchange exchange = new DefaultExchange(consumer.getEndpoint(), ExchangePattern.InOut);
+ if (((HttpEndpoint)consumer.getEndpoint()).isBridgeEndpoint()) {
+ exchange.setProperty(Exchange.SKIP_GZIP_ENCODING, Boolean.TRUE);
+ }
exchange.setIn(new HttpMessage(exchange, request, response));
consumer.getProcessor().process(exchange);
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=941275&r1=941274&r2=941275&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 Wed May 5 13:11:26 2010
@@ -309,7 +309,7 @@ public class DefaultHttpBinding implemen
return request.getReader();
} else {
// otherwise use input stream and we need to cache it first
- InputStream is = HttpConverter.toInputStream(request);
+ InputStream is = HttpConverter.toInputStream(request, httpMessage.getExchange());
if (is == null) {
return is;
}
Modified: camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpConverter.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpConverter.java?rev=941275&r1=941274&r2=941275&view=diff
==============================================================================
--- camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpConverter.java (original)
+++ camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpConverter.java Wed May 5 13:11:26 2010
@@ -69,8 +69,8 @@ public final class HttpConverter {
}
@Converter
- public static InputStream toInputStream(HttpMessage message) throws Exception {
- return toInputStream(toServletRequest(message));
+ public static InputStream toInputStream(HttpMessage message, Exchange exchange) throws Exception {
+ return toInputStream(toServletRequest(message), exchange);
}
@Converter
@@ -83,12 +83,16 @@ public final class HttpConverter {
}
@Converter
- public static InputStream toInputStream(HttpServletRequest request) throws IOException {
+ public static InputStream toInputStream(HttpServletRequest request, Exchange exchange) throws IOException {
if (request == null) {
return null;
}
- String contentEncoding = request.getHeader(Exchange.CONTENT_ENCODING);
- return GZIPHelper.toGZIPInputStream(contentEncoding, request.getInputStream());
+ if (exchange == null || !exchange.getProperty(Exchange.SKIP_GZIP_ENCODING, Boolean.FALSE, Boolean.class)) {
+ String contentEncoding = request.getHeader(Exchange.CONTENT_ENCODING);
+ return GZIPHelper.uncompressGzip(contentEncoding, request.getInputStream());
+ } else {
+ return request.getInputStream();
+ }
}
}
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=941275&r1=941274&r2=941275&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 Wed May 5 13:11:26 2010
@@ -57,6 +57,9 @@ public class HttpProducer extends Defaul
}
public void process(Exchange exchange) throws Exception {
+ if (((HttpEndpoint)getEndpoint()).isBridgeEndpoint()) {
+ exchange.setProperty(Exchange.SKIP_GZIP_ENCODING, Boolean.TRUE);
+ }
HttpMethod method = createMethod(exchange);
Message in = exchange.getIn();
HeaderFilterStrategy strategy = getEndpoint().getHeaderFilterStrategy();
@@ -198,7 +201,10 @@ public class HttpProducer extends Defaul
Header header = method.getRequestHeader(Exchange.CONTENT_ENCODING);
String contentEncoding = header != null ? header.getValue() : null;
- is = GZIPHelper.toGZIPInputStream(contentEncoding, is);
+ if (!exchange.getProperty(Exchange.SKIP_GZIP_ENCODING, Boolean.FALSE, Boolean.class)) {
+ is = GZIPHelper.uncompressGzip(contentEncoding, is);
+ }
+
// Honor the character encoding
header = method.getResponseHeader("content-type");
if (header != null) {
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=941275&r1=941274&r2=941275&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 Wed May 5 13:11:26 2010
@@ -16,6 +16,7 @@
*/
package org.apache.camel.component.http;
+import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
@@ -54,17 +55,25 @@ public class RequestEntityConverter {
}
private RequestEntity asRequestEntity(InputStream in, Exchange exchange) throws IOException {
- return new InputStreamRequestEntity(
- GZIPHelper.toGZIPInputStream(
- exchange.getIn().getHeader(Exchange.CONTENT_ENCODING, String.class),
- in), ExchangeHelper.getContentType(exchange));
+ 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));
+ } else {
+ return new InputStreamRequestEntity(in);
+ }
}
private RequestEntity asRequestEntity(byte[] data, Exchange exchange) throws Exception {
- return new InputStreamRequestEntity(
- GZIPHelper.toGZIPInputStream(
- exchange.getIn().getHeader(Exchange.CONTENT_ENCODING, String.class),
- data), ExchangeHelper.getContentType(exchange));
+ 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));
+ } else {
+ return new InputStreamRequestEntity(new ByteArrayInputStream(data));
+ }
}
}
Modified: camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/helper/GZIPHelper.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/helper/GZIPHelper.java?rev=941275&r1=941274&r2=941275&view=diff
==============================================================================
--- camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/helper/GZIPHelper.java (original)
+++ camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/helper/GZIPHelper.java Wed May 5 13:11:26 2010
@@ -25,6 +25,7 @@ import java.util.zip.GZIPOutputStream;
import org.apache.camel.Exchange;
import org.apache.camel.Message;
+import org.apache.camel.util.IOHelper;
import org.apache.camel.util.ObjectHelper;
/**
@@ -35,15 +36,34 @@ public final class GZIPHelper {
private GZIPHelper() {
}
- 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 IOException {
+ public static InputStream compressGzip(String contentEncoding, byte[] data) throws IOException {
if (isGzip(contentEncoding)) {
ByteArrayOutputStream os = null;
GZIPOutputStream gzip = null;
@@ -75,8 +95,16 @@ public final class GZIPHelper {
}
}
- public static boolean isGzip(Message message) {
- return isGzip(message.getHeader(Exchange.CONTENT_ENCODING, String.class));
+ public static boolean isGzip(Message message) {
+ return isGzip(message.getHeader(Exchange.CONTENT_ENCODING, String.class), message.getExchange());
+ }
+
+ public static boolean isGzip(String header , Exchange exchange) {
+ if (exchange == null || !exchange.getProperty(Exchange.SKIP_GZIP_ENCODING, Boolean.FALSE, Boolean.class)) {
+ return isGzip(header);
+ } else {
+ return false;
+ }
}
public static boolean isGzip(String header) {
Modified: camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/helper/GZIPHelperTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/helper/GZIPHelperTest.java?rev=941275&r1=941274&r2=941275&view=diff
==============================================================================
--- camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/helper/GZIPHelperTest.java (original)
+++ camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/helper/GZIPHelperTest.java Wed May 5 13:11:26 2010
@@ -23,20 +23,18 @@ import java.io.InputStream;
import org.apache.camel.Message;
import org.apache.camel.converter.IOConverter;
import org.apache.camel.impl.DefaultMessage;
+import org.junit.Assert;
import org.junit.Test;
-import static org.junit.Assert.assertArrayEquals;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
-public class GZIPHelperTest {
+public class GZIPHelperTest extends Assert {
private static byte[] sampleBytes = new byte[]{1, 2, 3, 1, 2, 3};
+ private static String sampleString = "<Hello>World</Hello>";
@Test
public void toGZIPInputStreamShouldReturnTheSameInputStream() throws IOException {
- InputStream inputStream = GZIPHelper.toGZIPInputStream("text", new ByteArrayInputStream(sampleBytes));
+ InputStream inputStream = GZIPHelper.uncompressGzip("text", new ByteArrayInputStream(sampleBytes));
byte[] bytes = new byte[6];
inputStream.read(bytes);
@@ -46,11 +44,21 @@ public class GZIPHelperTest {
@Test
public void toGZIPInputStreamShouldReturnAByteArrayInputStream() throws IOException {
- InputStream inputStream = GZIPHelper.toGZIPInputStream("text", sampleBytes);
+ InputStream inputStream = GZIPHelper.compressGzip("text", sampleBytes);
byte[] bytes = IOConverter.toBytes(inputStream);
assertArrayEquals(sampleBytes, bytes);
}
+
+ @Test
+ public void testCompressAndUnCompressData() throws IOException {
+ InputStream inputStream = GZIPHelper.compressGzip("gzip", new ByteArrayInputStream(sampleString.getBytes()));
+ assertNotNull("The inputStream should not be null.", inputStream);
+ inputStream = GZIPHelper.uncompressGzip("gzip", inputStream);
+ String result = IOConverter.toString(inputStream, null);
+ assertEquals("The result is wrong.", sampleString, result);
+
+ }
@Test
public void testIsGzipMessage() {
Modified: camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpConverterTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpConverterTest.java?rev=941275&r1=941274&r2=941275&view=diff
==============================================================================
--- camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpConverterTest.java (original)
+++ camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpConverterTest.java Wed May 5 13:11:26 2010
@@ -121,13 +121,13 @@ public class HttpConverterTest extends C
@Test
public void testNulls() throws Exception {
HttpMessage msg = null;
- assertNull(HttpConverter.toInputStream(msg));
+ assertNull(HttpConverter.toInputStream(msg, null));
assertNull(HttpConverter.toServletInputStream(msg));
assertNull(HttpConverter.toServletRequest(msg));
assertNull(HttpConverter.toServletResponse(msg));
HttpServletRequest req = null;
- assertNull(HttpConverter.toInputStream(req));
+ assertNull(HttpConverter.toInputStream(req, null));
}
}
Added: 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=941275&view=auto
==============================================================================
--- camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpGZipEncodingTest.java (added)
+++ camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpGZipEncodingTest.java Wed May 5 13:11:26 2010
@@ -0,0 +1,78 @@
+/**
+ * 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.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.ExpressionBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+public class HttpGZipEncodingTest extends CamelTestSupport {
+
+ @Test
+ public void testHttpProducerWithGzip() throws Exception {
+ String response = template.requestBodyAndHeader("http://localhost:9081/gzip", new ByteArrayInputStream("<Hello>World</Hello>".getBytes()), Exchange.CONTENT_ENCODING, "gzip", String.class);
+ assertEquals("The response is wrong", "<b>Hello World</b>", response);
+ }
+
+ @Test
+ public void testGzipProxy() throws Exception {
+ String response =
+ template.requestBodyAndHeader("http://localhost:9084/route", new ByteArrayInputStream("<Hello>World</Hello>".getBytes()), Exchange.CONTENT_ENCODING, "gzip", String.class);
+ assertEquals("The response is wrong", "<b>Hello World</b>", response);
+ }
+
+ @Test
+ public void testGzipProducerWithGzipData() throws Exception {
+ String response = template.requestBodyAndHeader("direct:gzip", new ByteArrayInputStream("<Hello>World</Hello>".getBytes()), Exchange.CONTENT_ENCODING, "gzip", String.class);
+ assertEquals("The response is wrong", "<b>Hello World</b>", response);
+ }
+
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ public void configure() {
+ errorHandler(noErrorHandler());
+
+ from("direct:gzip")
+ .marshal().gzip()
+ .setProperty(Exchange.SKIP_GZIP_ENCODING, ExpressionBuilder.constantExpression(Boolean.TRUE))
+ .to("http://localhost:9081/gzip").unmarshal().gzip();
+
+ 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(Exchange.CONTENT_ENCODING, "gzip");
+ exchange.getOut().setBody("<b>Hello World</b>");
+ }
+
+ });
+
+ from("jetty:http://localhost:9084/route?bridgeEndpoint=true").to("http://localhost:9081/gzip?bridgeEndpoint=true");
+
+
+ }
+ };
+ }
+
+}
Propchange: camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpGZipEncodingTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpGZipEncodingTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Modified: camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/test/MyTypeConverter.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/test/MyTypeConverter.java?rev=941275&r1=941274&r2=941275&view=diff
==============================================================================
--- camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/test/MyTypeConverter.java (original)
+++ camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/test/MyTypeConverter.java Wed May 5 13:11:26 2010
@@ -57,7 +57,7 @@ public final class MyTypeConverter {
// use a fallback type converter so we can convert the embedded body if the value is GenericFile
if (GenericFile.class.isAssignableFrom(value.getClass())) {
GenericFile file = (GenericFile) value;
- Class from = file.getBody().getClass();
+ Class<?> from = file.getBody().getClass();
// maybe from is already the type we want
if (from.isAssignableFrom(type)) {