Author: ningjiang
Date: Wed May 5 13:37:37 2010
New Revision: 941282
URL: http://svn.apache.org/viewvc?rev=941282&view=rev
Log:
CAMEL-2686 merged the change of camel-http to camel-http4
Modified:
camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/CamelServlet.java
camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/DefaultHttpBinding.java
camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpConverter.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-http4/src/main/java/org/apache/camel/component/http4/helper/GZIPHelper.java
camel/trunk/components/camel-http4/src/test/java/org/apache/camel/component/http4/helper/GZIPHelperTest.java
Modified:
camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/CamelServlet.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/CamelServlet.java?rev=941282&r1=941281&r2=941282&view=diff
==============================================================================
---
camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/CamelServlet.java
(original)
+++
camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/CamelServlet.java
Wed May 5 13:37:37 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;
@@ -49,6 +50,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-http4/src/main/java/org/apache/camel/component/http4/DefaultHttpBinding.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/DefaultHttpBinding.java?rev=941282&r1=941281&r2=941282&view=diff
==============================================================================
---
camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/DefaultHttpBinding.java
(original)
+++
camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/DefaultHttpBinding.java
Wed May 5 13:37:37 2010
@@ -306,7 +306,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());
try {
CachedOutputStream cos = new
CachedOutputStream(httpMessage.getExchange());
IOHelper.copy(is, cos);
Modified:
camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpConverter.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpConverter.java?rev=941282&r1=941281&r2=941282&view=diff
==============================================================================
---
camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpConverter.java
(original)
+++
camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpConverter.java
Wed May 5 13:37:37 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,17 @@ 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-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=941282&r1=941281&r2=941282&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
Wed May 5 13:37:37 2010
@@ -16,6 +16,7 @@
*/
package org.apache.camel.component.http4;
+import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
@@ -57,11 +58,15 @@ public class HttpEntityConverter {
String contentEncoding =
exchange.getIn().getHeader(Exchange.CONTENT_ENCODING, String.class);
String contentType = ExchangeHelper.getContentType(exchange);
- InputStreamEntity entity = new InputStreamEntity(
- GZIPHelper.toGZIPInputStream(contentEncoding, in), -1);
+ InputStreamEntity entity = null;
+ 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);
+ }
entity.setContentEncoding(contentEncoding);
entity.setContentType(contentType);
-
return entity;
}
@@ -69,8 +74,13 @@ public class HttpEntityConverter {
String contentEncoding =
exchange.getIn().getHeader(Exchange.CONTENT_ENCODING, String.class);
String contentType = ExchangeHelper.getContentType(exchange);
- InputStreamEntity entity = new InputStreamEntity(
- GZIPHelper.toGZIPInputStream(contentEncoding, data), -1);
+ InputStreamEntity entity = null;
+ 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);
+ }
entity.setContentEncoding(contentEncoding);
entity.setContentType(contentType);
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=941282&r1=941281&r2=941282&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
Wed May 5 13:37:37 2010
@@ -62,6 +62,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);
+ }
HttpRequestBase httpRequest = createMethod(exchange);
Message in = exchange.getIn();
HeaderFilterStrategy strategy =
getEndpoint().getHeaderFilterStrategy();
@@ -201,7 +204,9 @@ public class HttpProducer extends Defaul
Header header = httpRequest.getFirstHeader(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 = httpRequest.getFirstHeader("content-type");
if (header != null) {
Modified:
camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/helper/GZIPHelper.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/helper/GZIPHelper.java?rev=941282&r1=941281&r2=941282&view=diff
==============================================================================
---
camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/helper/GZIPHelper.java
(original)
+++
camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/helper/GZIPHelper.java
Wed May 5 13:37:37 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 {
- public static InputStream toGZIPInputStream(String contentEncoding, byte[]
data) 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 compressGzip(String contentEncoding, byte[]
data) throws IOException {
if (isGzip(contentEncoding)) {
ByteArrayOutputStream os = null;
GZIPOutputStream gzip = null;
@@ -75,12 +95,19 @@ 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) {
return header != null && header.toLowerCase().contains("gzip");
}
-
}
Modified:
camel/trunk/components/camel-http4/src/test/java/org/apache/camel/component/http4/helper/GZIPHelperTest.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-http4/src/test/java/org/apache/camel/component/http4/helper/GZIPHelperTest.java?rev=941282&r1=941281&r2=941282&view=diff
==============================================================================
---
camel/trunk/components/camel-http4/src/test/java/org/apache/camel/component/http4/helper/GZIPHelperTest.java
(original)
+++
camel/trunk/components/camel-http4/src/test/java/org/apache/camel/component/http4/helper/GZIPHelperTest.java
Wed May 5 13:37:37 2010
@@ -23,6 +23,7 @@ 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;
@@ -30,13 +31,14 @@ import static org.junit.Assert.assertEqu
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 +48,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() {