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/04/28 04:35:40 UTC
svn commit: r938752 - in /camel/trunk/components/camel-http4/src:
main/java/org/apache/camel/component/http4/
test/java/org/apache/camel/component/http4/
Author: ningjiang
Date: Wed Apr 28 02:35:40 2010
New Revision: 938752
URL: http://svn.apache.org/viewvc?rev=938752&view=rev
Log:
CAMEL-2676 CAMEL-2679 Merges the change for camel-http to camel-http4
Modified:
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/HttpProducer.java
camel/trunk/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpConcurrentTest.java
camel/trunk/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpNoConnectionRedeliveryTest.java
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=938752&r1=938751&r2=938752&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 Apr 28 02:35:40 2010
@@ -20,6 +20,8 @@ import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
+import java.io.UnsupportedEncodingException;
+import java.net.URLDecoder;
import java.util.Enumeration;
import java.util.Map;
@@ -34,7 +36,9 @@ import org.apache.camel.Endpoint;
import org.apache.camel.Exchange;
import org.apache.camel.InvalidPayloadException;
import org.apache.camel.Message;
+import org.apache.camel.StreamCache;
import org.apache.camel.component.http4.helper.GZIPHelper;
+import org.apache.camel.converter.stream.CachedOutputStream;
import org.apache.camel.spi.HeaderFilterStrategy;
import org.apache.camel.util.IOHelper;
import org.apache.camel.util.MessageHelper;
@@ -78,8 +82,16 @@ public class DefaultHttpBinding implemen
headers.put(name, value);
}
}
+
+ if (request.getCharacterEncoding() != null) {
+ headers.put(Exchange.HTTP_CHARACTER_ENCODING, request.getCharacterEncoding());
+ message.getExchange().setProperty(Exchange.CHARSET_NAME, request.getCharacterEncoding());
+ }
popluateRequestParameters(request, message);
+ // reset the stream cache
+ StreamCache cache = message.getBody(StreamCache.class);
+ cache.reset();
// store the method and query and other info in headers
headers.put(Exchange.HTTP_METHOD, request.getMethod());
@@ -88,7 +100,6 @@ public class DefaultHttpBinding implemen
headers.put(Exchange.HTTP_URI, request.getRequestURI());
headers.put(Exchange.HTTP_PATH, request.getPathInfo());
headers.put(Exchange.CONTENT_TYPE, request.getContentType());
- headers.put(Exchange.HTTP_CHARACTER_ENCODING, request.getCharacterEncoding());
popluateAttachments(request, message);
}
@@ -105,6 +116,27 @@ public class DefaultHttpBinding implemen
headers.put(name, value);
}
}
+ if (request.getMethod().equals("POST") && request.getContentType() != null && request.getContentType().equals("application/x-www-form-urlencoded")) {
+ String charset = request.getCharacterEncoding();
+ if (charset == null) {
+ charset = "UTF-8";
+ }
+ // Push POST form params into the headers to retain compatibility with DefaultHttpBinding
+ String body = message.getBody(String.class);
+ try {
+ for (String param : body.split("&")) {
+ String[] pair = param.split("=", 2);
+ String name = URLDecoder.decode(pair[0], charset);
+ String value = URLDecoder.decode(pair[1], charset);
+ if (headerFilterStrategy != null
+ && !headerFilterStrategy.applyFilterToExternalHeaders(name, value, message.getExchange())) {
+ headers.put(name, value);
+ }
+ }
+ } catch (UnsupportedEncodingException e) {
+ throw new RuntimeException(e);
+ }
+ }
}
protected void popluateAttachments(HttpServletRequest request, HttpMessage message) {
@@ -271,8 +303,15 @@ public class DefaultHttpBinding implemen
if (isUseReaderForPayload()) {
return request.getReader();
} else {
- // otherwise use input stream
- return HttpConverter.toInputStream(request);
+ // otherwise use input stream and we need to cache it first
+ InputStream is = HttpConverter.toInputStream(request);
+ try {
+ CachedOutputStream cos = new CachedOutputStream(httpMessage.getExchange());
+ IOHelper.copy(is, cos);
+ return cos.getStreamCache();
+ } finally {
+ is.close();
+ }
}
}
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=938752&r1=938751&r2=938752&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 Apr 28 02:35:40 2010
@@ -202,6 +202,17 @@ public class HttpProducer extends Defaul
String contentEncoding = header != null ? header.getValue() : null;
is = GZIPHelper.toGZIPInputStream(contentEncoding, is);
+ // Honor the character encoding
+ header = httpRequest.getFirstHeader("content-type");
+ if (header != null) {
+ String contentType = header.getValue();
+ // find the charset and set it to the Exchange
+ int index = contentType.indexOf("charset=");
+ if (index > 0) {
+ String charset = contentType.substring(index + 8);
+ exchange.setProperty(Exchange.CHARSET_NAME, charset);
+ }
+ }
return doExtractResponseBody(is, exchange);
}
Modified: camel/trunk/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpConcurrentTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpConcurrentTest.java?rev=938752&r1=938751&r2=938752&view=diff
==============================================================================
--- camel/trunk/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpConcurrentTest.java (original)
+++ camel/trunk/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpConcurrentTest.java Wed Apr 28 02:35:40 2010
@@ -71,7 +71,7 @@ public class HttpConcurrentTest extends
private void doSendMessages(int files, int poolSize) throws Exception {
ExecutorService executor = Executors.newFixedThreadPool(poolSize);
- Map<Integer, Future> responses = new ConcurrentHashMap();
+ Map<Integer, Future> responses = new ConcurrentHashMap<Integer, Future>();
for (int i = 0; i < files; i++) {
final int index = i;
Future out = executor.submit(new Callable<Object>() {
@@ -85,7 +85,7 @@ public class HttpConcurrentTest extends
assertEquals(files, responses.size());
// get all responses
- Set unique = new HashSet();
+ Set<Object> unique = new HashSet<Object>();
for (Future future : responses.values()) {
unique.add(future.get());
}
Modified: camel/trunk/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpNoConnectionRedeliveryTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpNoConnectionRedeliveryTest.java?rev=938752&r1=938751&r2=938752&view=diff
==============================================================================
--- camel/trunk/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpNoConnectionRedeliveryTest.java (original)
+++ camel/trunk/components/camel-http4/src/test/java/org/apache/camel/component/http4/HttpNoConnectionRedeliveryTest.java Wed Apr 28 02:35:40 2010
@@ -66,7 +66,7 @@ public class HttpNoConnectionRedeliveryT
.onException(ConnectException.class)
.maximumRedeliveries(4)
.backOffMultiplier(2)
- .redeliverDelay(100)
+ .redeliveryDelay(100)
.maximumRedeliveryDelay(5000)
.useExponentialBackOff()
.end()