You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2010/12/27 14:43:57 UTC
svn commit: r1053065 - in
/camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4:
CompositeHttpConfigurer.java DefaultHttpBinding.java HttpBinding.java
HttpClientConfigurer.java HttpPollingConsumer.java helper/HttpHelper.java
Author: davsclaus
Date: Mon Dec 27 13:43:56 2010
New Revision: 1053065
URL: http://svn.apache.org/viewvc?rev=1053065&view=rev
Log:
CAMEL-3466: Polished
Modified:
camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/CompositeHttpConfigurer.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/HttpBinding.java
camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpClientConfigurer.java
camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpPollingConsumer.java
camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/helper/HttpHelper.java
Modified: camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/CompositeHttpConfigurer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/CompositeHttpConfigurer.java?rev=1053065&r1=1053064&r2=1053065&view=diff
==============================================================================
--- camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/CompositeHttpConfigurer.java (original)
+++ camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/CompositeHttpConfigurer.java Mon Dec 27 13:43:56 2010
@@ -23,7 +23,7 @@ import org.apache.http.client.HttpClient
public class CompositeHttpConfigurer implements HttpClientConfigurer {
- private List<HttpClientConfigurer> configurers = new ArrayList<HttpClientConfigurer>();
+ private final List<HttpClientConfigurer> configurers = new ArrayList<HttpClientConfigurer>();
public void addConfigurer(HttpClientConfigurer configurer) {
if (configurer != null) {
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=1053065&r1=1053064&r2=1053065&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 Mon Dec 27 13:43:56 2010
@@ -19,7 +19,6 @@ package org.apache.camel.component.http4
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
-import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
@@ -34,10 +33,11 @@ import org.apache.camel.Endpoint;
import org.apache.camel.Exchange;
import org.apache.camel.InvalidPayloadException;
import org.apache.camel.Message;
+import org.apache.camel.RuntimeCamelException;
import org.apache.camel.StreamCache;
import org.apache.camel.component.http4.helper.CamelFileDataSource;
import org.apache.camel.component.http4.helper.GZIPHelper;
-import org.apache.camel.converter.stream.CachedOutputStream;
+import org.apache.camel.component.http4.helper.HttpHelper;
import org.apache.camel.spi.HeaderFilterStrategy;
import org.apache.camel.util.IOHelper;
import org.apache.camel.util.MessageHelper;
@@ -95,7 +95,12 @@ public class DefaultHttpBinding implemen
message.getExchange().setProperty(Exchange.CHARSET_NAME, request.getCharacterEncoding());
}
- populateRequestParameters(request, message);
+ try {
+ populateRequestParameters(request, message);
+ } catch (UnsupportedEncodingException e) {
+ throw new RuntimeCamelException("Cannot read request parameters due " + e.getMessage(), e);
+ }
+
// reset the stream cache
StreamCache cache = message.getBody(StreamCache.class);
if (cache != null) {
@@ -113,7 +118,7 @@ public class DefaultHttpBinding implemen
populateAttachments(request, message);
}
- protected void populateRequestParameters(HttpServletRequest request, HttpMessage message) {
+ protected void populateRequestParameters(HttpServletRequest request, HttpMessage message) throws UnsupportedEncodingException {
//we populate the http request parameters without checking the request method
Map<String, Object> headers = message.getHeaders();
Enumeration names = request.getParameterNames();
@@ -133,18 +138,14 @@ public class DefaultHttpBinding implemen
}
// 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);
- }
+ 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);
}
}
}
@@ -195,11 +196,7 @@ public class DefaultHttpBinding implemen
response.setStatus(500);
if (endpoint != null && endpoint.isTransferException()) {
// transfer the exception as a serialized java object
- response.setContentType(HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT);
- ObjectOutputStream oos = new ObjectOutputStream(response.getOutputStream());
- oos.writeObject(exception);
- oos.flush();
- IOHelper.close(oos);
+ HttpHelper.writeObjectToServletResponse(response, exception);
} else {
// write stacktrace as plain text
response.setContentType("text/plain");
@@ -255,16 +252,8 @@ public class DefaultHttpBinding implemen
// copy directly from input stream to output stream
IOHelper.copy(is, os);
} finally {
- try {
- os.close();
- } catch (Exception e) {
- // ignore, maybe client have disconnected or timed out
- }
- try {
- is.close();
- } catch (Exception e) {
- // ignore, maybe client have disconnected or timed out
- }
+ IOHelper.close(os);
+ IOHelper.close(is);
}
} else {
// not convertable as a stream so try as a String
@@ -307,7 +296,7 @@ public class DefaultHttpBinding implemen
os.write(data);
os.flush();
} finally {
- os.close();
+ IOHelper.close(os);
}
}
@@ -319,25 +308,11 @@ public class DefaultHttpBinding implemen
return null;
}
if (isUseReaderForPayload()) {
+ // use reader to read the response body
return request.getReader();
} else {
- // otherwise use input stream and we need to cache it first
- InputStream is = HttpConverter.toInputStream(request, httpMessage.getExchange());
- if (is == null) {
- return is;
- }
- // convert the input stream to StreamCache if the stream cache is not disabled
- if (httpMessage.getExchange().getProperty(Exchange.DISABLE_HTTP_STREAM_CACHE, Boolean.FALSE, Boolean.class)) {
- return is;
- } else {
- try {
- CachedOutputStream cos = new CachedOutputStream(httpMessage.getExchange());
- IOHelper.copy(is, cos);
- return cos.getStreamCache();
- } finally {
- is.close();
- }
- }
+ // reade the response body from servlet request
+ return HttpHelper.readResponseBodyFromServletRequest(request, httpMessage.getExchange());
}
}
Modified: camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpBinding.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpBinding.java?rev=1053065&r1=1053064&r2=1053065&view=diff
==============================================================================
--- camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpBinding.java (original)
+++ camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpBinding.java Mon Dec 27 13:43:56 2010
@@ -25,10 +25,10 @@ import org.apache.camel.Message;
import org.apache.camel.spi.HeaderFilterStrategy;
/**
- * A plugable strategy for configuring the http binding so reading request and writing response
+ * A pluggable strategy for configuring the http binding so reading request and writing response
* can be customized using the Java Servlet API.
* <p/>
- * This is also used by the camel-jetty by the JettyHttpConsumer.
+ * This is also used by the <tt>camel-jetty</tt> component in the <tt>JettyHttpConsumer</tt> class.
*
* @version $Revision$
*/
Modified: camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpClientConfigurer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpClientConfigurer.java?rev=1053065&r1=1053064&r2=1053065&view=diff
==============================================================================
--- camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpClientConfigurer.java (original)
+++ camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpClientConfigurer.java Mon Dec 27 13:43:56 2010
@@ -19,7 +19,7 @@ package org.apache.camel.component.http4
import org.apache.http.client.HttpClient;
/**
- * A plugable strategy for configuring the HttpClient used by this component
+ * A pluggable strategy for configuring the HttpClient used by this component
*
* @version $Revision$
*/
Modified: camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpPollingConsumer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpPollingConsumer.java?rev=1053065&r1=1053064&r2=1053065&view=diff
==============================================================================
--- camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpPollingConsumer.java (original)
+++ camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpPollingConsumer.java Mon Dec 27 13:43:56 2010
@@ -17,15 +17,13 @@
package org.apache.camel.component.http4;
import java.io.IOException;
-import java.io.InputStream;
import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.RuntimeCamelException;
-import org.apache.camel.component.http4.helper.LoadingByteArrayOutputStream;
+import org.apache.camel.component.http4.helper.HttpHelper;
import org.apache.camel.impl.PollingConsumerSupport;
import org.apache.camel.spi.HeaderFilterStrategy;
-import org.apache.camel.util.IOHelper;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
@@ -72,34 +70,32 @@ public class HttpPollingConsumer extends
HttpEntity responeEntity = null;
try {
+ // execute request
HttpResponse response = httpClient.execute(method);
int responseCode = response.getStatusLine().getStatusCode();
responeEntity = response.getEntity();
- // lets store the result in the output message.
- LoadingByteArrayOutputStream bos = new LoadingByteArrayOutputStream();
- InputStream is = responeEntity.getContent();
+ Object body = HttpHelper.readResponseBodyFromInputStream(responeEntity.getContent(), exchange);
- try {
- IOHelper.copy(is, bos);
- bos.flush();
- } finally {
- IOHelper.close(is, "input stream", null);
- }
- Message message = exchange.getIn();
- message.setBody(bos.createInputStream());
+ // lets store the result in the output message.
+ Message message = exchange.getOut();
+ message.setBody(body);
// lets set the headers
Header[] headers = response.getAllHeaders();
HeaderFilterStrategy strategy = endpoint.getHeaderFilterStrategy();
for (Header header : headers) {
String name = header.getName();
+ // mapping the content-type
+ if (name.toLowerCase().equals("content-type")) {
+ name = Exchange.CONTENT_TYPE;
+ }
String value = header.getValue();
if (strategy != null && !strategy.applyFilterToExternalHeaders(name, value, exchange)) {
message.setHeader(name, value);
}
}
-
message.setHeader(Exchange.HTTP_RESPONSE_CODE, responseCode);
+
return exchange;
} catch (IOException e) {
throw new RuntimeCamelException(e);
Modified: camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/helper/HttpHelper.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/helper/HttpHelper.java?rev=1053065&r1=1053064&r2=1053065&view=diff
==============================================================================
--- camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/helper/HttpHelper.java (original)
+++ camel/trunk/components/camel-http4/src/main/java/org/apache/camel/component/http4/helper/HttpHelper.java Mon Dec 27 13:43:56 2010
@@ -16,15 +16,25 @@
*/
package org.apache.camel.component.http4.helper;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.ObjectOutputStream;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletRequest;
+
import org.apache.camel.Exchange;
+import org.apache.camel.component.http4.HttpConstants;
+import org.apache.camel.component.http4.HttpConverter;
import org.apache.camel.converter.IOConverter;
+import org.apache.camel.converter.stream.CachedOutputStream;
+import org.apache.camel.util.IOHelper;
public final class HttpHelper {
-
+
private HttpHelper() {
// Helper class
}
-
+
public static void setCharsetFromContentType(String contentType, Exchange exchange) {
if (contentType != null) {
// find the charset and set it to the Exchange
@@ -36,4 +46,57 @@ public final class HttpHelper {
}
}
+ /**
+ * Writes the given object as response body to the servlet response
+ * <p/>
+ * The content type will be set to {@link HttpConstants#CONTENT_TYPE_JAVA_SERIALIZED_OBJECT}
+ *
+ * @param response servlet response
+ * @param target object to write
+ * @throws IOException is thrown if error writing
+ */
+ public static void writeObjectToServletResponse(ServletResponse response, Object target) throws IOException {
+ response.setContentType(HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT);
+ ObjectOutputStream oos = new ObjectOutputStream(response.getOutputStream());
+ oos.writeObject(target);
+ oos.flush();
+ IOHelper.close(oos);
+ }
+
+ /**
+ * Reads the response body from the given http servlet request.
+ *
+ * @param request http servlet request
+ * @param exchange the exchange
+ * @return the response body, can be <tt>null</tt> if no body
+ * @throws IOException is thrown if error reading response body
+ */
+ public static Object readResponseBodyFromServletRequest(HttpServletRequest request, Exchange exchange) throws IOException {
+ InputStream is = HttpConverter.toInputStream(request, exchange);
+ return readResponseBodyFromInputStream(is, exchange);
+ }
+
+ /**
+ * Reads the response body from the given input stream.
+ *
+ * @param is the input stream
+ * @param exchange the exchange
+ * @return the response body, can be <tt>null</tt> if no body
+ * @throws IOException is thrown if error reading response body
+ */
+ public static Object readResponseBodyFromInputStream(InputStream is, Exchange exchange) throws IOException {
+ if (is == null) {
+ return null;
+ }
+
+ // convert the input stream to StreamCache if the stream cache is not disabled
+ if (exchange.getProperty(Exchange.DISABLE_HTTP_STREAM_CACHE, Boolean.FALSE, Boolean.class)) {
+ return is;
+ } else {
+ CachedOutputStream cos = new CachedOutputStream(exchange);
+ IOHelper.copyAndCloseInput(is, cos);
+ return cos.getStreamCache();
+ }
+ }
+
}