You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@camel.apache.org by Claus Ibsen <cl...@gmail.com> on 2009/05/14 09:02:18 UTC
Re: svn commit: r774658 - in /camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http:
DefaultHttpBinding.java HttpHeaderFilterStrategy.java HttpProducer.java
RequestEntityConverter.java helper/GZIPHelper.java
Willem
Beware to do WARN logging in case it will log all the time
+ LOG.warn("Missing the ContentType in the request entity!");
Its quite often not to set a Content-Type using camel http to just do
a text/plain GET.
So be sure that the http client sets a Content-Type if the end user
have not provided anyone.
If the content-type is often null then consider changing the WARN to
DEBUG. And you could output the URI also so people looking in the log
can better known which HTTP call is the problem.
On Thu, May 14, 2009 at 8:46 AM, <ni...@apache.org> wrote:
> Author: ningjiang
> Date: Thu May 14 06:46:16 2009
> New Revision: 774658
>
> URL: http://svn.apache.org/viewvc?rev=774658&view=rev
> Log:
> CAMEL-1609 Better fixing the issue of Content type setting for HTTP producer
>
> Modified:
> 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/HttpHeaderFilterStrategy.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
>
> 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=774658&r1=774657&r2=774658&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 Thu May 14 06:46:16 2009
> @@ -20,6 +20,8 @@
> import java.io.IOException;
> import java.io.InputStream;
> import java.io.PrintWriter;
> +import java.util.Enumeration;
> +import java.util.Map;
>
> import javax.servlet.ServletOutputStream;
> import javax.servlet.http.HttpServletRequest;
> @@ -50,7 +52,40 @@
> public void readRequest(HttpServletRequest request, HttpMessage message) {
> // lets force a parse of the body and headers
> message.getBody();
> - message.getHeaders();
> + // populate the headers from the request
> + Map<String, Object> headers = message.getHeaders();
> +
> + //apply the headerFilterStrategy
> + Enumeration names = request.getHeaderNames();
> + while (names.hasMoreElements()) {
> + String name = (String)names.nextElement();
> + Object value = request.getHeader(name);
> + if (headerFilterStrategy != null
> + && !headerFilterStrategy.applyFilterToExternalHeaders(name, value, message.getExchange())) {
> + headers.put(name, value);
> + }
> + }
> +
> + //if the request method is Get, we also populate the http request parameters
> + if (request.getMethod().equalsIgnoreCase("GET")) {
> + names = request.getParameterNames();
> + while (names.hasMoreElements()) {
> + String name = (String)names.nextElement();
> + Object value = request.getParameter(name);
> + if (headerFilterStrategy != null
> + && !headerFilterStrategy.applyFilterToExternalHeaders(name, value, message.getExchange())) {
> + headers.put(name, value);
> + }
> + }
> + }
> +
> + // store the method and query and other info in headers
> + headers.put(HttpConstants.HTTP_METHOD, request.getMethod());
> + headers.put(HttpConstants.HTTP_QUERY, request.getQueryString());
> + headers.put(HttpConstants.HTTP_PATH, request.getPathInfo());
> + headers.put(HttpConstants.HTTP_CONTENT_TYPE, request.getContentType());
> + headers.put(HttpConstants.HTTP_CHARACTER_ENCODING, request.getCharacterEncoding());
> +
> }
>
> public void writeResponse(HttpExchange exchange, HttpServletResponse response) throws IOException {
>
> Modified: camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpHeaderFilterStrategy.java
> URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpHeaderFilterStrategy.java?rev=774658&r1=774657&r2=774658&view=diff
> ==============================================================================
> --- camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpHeaderFilterStrategy.java (original)
> +++ camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpHeaderFilterStrategy.java Thu May 14 06:46:16 2009
> @@ -28,7 +28,8 @@
> }
>
> protected void initialize() {
> - getOutFilter().add("content-length");
> + getOutFilter().add("content-length");
> + getOutFilter().add("content-type");
> getOutFilter().add(HttpConstants.HTTP_METHOD.toLowerCase());
> getOutFilter().add(HttpConstants.HTTP_QUERY);
> getOutFilter().add(HttpConstants.HTTP_RESPONSE_CODE.toLowerCase());
>
> 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=774658&r1=774657&r2=774658&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 Thu May 14 06:46:16 2009
> @@ -27,6 +27,8 @@
> import org.apache.camel.converter.stream.CachedOutputStream;
> import org.apache.camel.impl.DefaultProducer;
> import org.apache.camel.spi.HeaderFilterStrategy;
> +import org.apache.camel.util.ExchangeHelper;
> +import org.apache.camel.util.MessageHelper;
> import org.apache.camel.util.ObjectHelper;
> import org.apache.commons.httpclient.Header;
> import org.apache.commons.httpclient.HttpClient;
> @@ -220,6 +222,9 @@
> }
> if (methodToUse.isEntityEnclosing()) {
> ((EntityEnclosingMethod)method).setRequestEntity(requestEntity);
> + if (requestEntity.getContentType() == null) {
> + LOG.warn("Missing the ContentType in the request entity!");
> + }
> }
>
> return method;
> @@ -237,12 +242,12 @@
> return null;
> }
>
> - RequestEntity answer = in.getBody(RequestEntity.class);
> + RequestEntity answer = in.getBody(RequestEntity.class);
> if (answer == null) {
> try {
> String data = in.getBody(String.class);
> if (data != null) {
> - String contentType = in.getHeader("Content-Type", String.class);
> + String contentType = ExchangeHelper.getContentType(exchange);
> String charset = exchange.getProperty(Exchange.CHARSET_NAME, String.class);
> answer = new StringRequestEntity(data, contentType, charset);
> }
> @@ -250,7 +255,6 @@
> throw new RuntimeCamelException(e);
> }
> }
> -
> return answer;
> }
>
>
> 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=774658&r1=774657&r2=774658&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 Thu May 14 06:46:16 2009
> @@ -22,6 +22,7 @@
> import org.apache.camel.Converter;
> import org.apache.camel.Exchange;
> import org.apache.camel.component.http.helper.GZIPHelper;
> +import org.apache.camel.util.ExchangeHelper;
> import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
> import org.apache.commons.httpclient.methods.RequestEntity;
>
> @@ -36,7 +37,7 @@
> return new InputStreamRequestEntity(
> GZIPHelper.toGZIPInputStreamIfRequested(
> exchange.getIn().getHeader(GZIPHelper.CONTENT_ENCODING, String.class),
> - buffer.array()));
> + buffer.array()), ExchangeHelper.getContentType(exchange));
> }
>
> @Converter
> @@ -44,7 +45,7 @@
> return new InputStreamRequestEntity(
> GZIPHelper.toGZIPInputStreamIfRequested(
> exchange.getIn().getHeader(GZIPHelper.CONTENT_ENCODING, String.class),
> - array));
> + array), ExchangeHelper.getContentType(exchange));
> }
>
> @Converter
> @@ -52,16 +53,21 @@
> return new InputStreamRequestEntity(
> GZIPHelper.getGZIPWrappedInputStream(
> exchange.getIn().getHeader(GZIPHelper.CONTENT_ENCODING, String.class),
> - inStream));
> + inStream), ExchangeHelper.getContentType(exchange));
> }
>
>
> @Converter
> public RequestEntity toRequestEntity(String str, Exchange exchange) throws Exception {
> - return new InputStreamRequestEntity(
> + if (GZIPHelper.containsGzip(exchange.getIn().getHeader(GZIPHelper.CONTENT_ENCODING, String.class))) {
> + return new InputStreamRequestEntity(
> GZIPHelper.toGZIPInputStreamIfRequested(
> exchange.getIn().getHeader(GZIPHelper.CONTENT_ENCODING, String.class),
> - str.getBytes()));
> + str.getBytes()), ExchangeHelper.getContentType(exchange));
> + } else {
> + // will use the default StringRequestEntity
> + return null;
> + }
> }
>
>
>
> 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=774658&r1=774657&r2=774658&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 Thu May 14 06:46:16 2009
> @@ -124,7 +124,7 @@
> return compressed;
> }
>
> - private static boolean containsGzip(String str) {
> + public static boolean containsGzip(String str) {
> return str != null && str.toLowerCase().indexOf(GZIP) >= 0;
> }
>
>
>
>
--
Claus Ibsen
Apache Camel Committer
Open Source Integration: http://fusesource.com
Blog: http://davsclaus.blogspot.com/
Twitter: http://twitter.com/davsclaus
Apache Camel Reference Card:
http://refcardz.dzone.com/refcardz/enterprise-integration
Interview with me:
http://architects.dzone.com/articles/interview-claus-ibsen-about?mz=7893-progress
Re: svn commit: r774658 - in /camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http:
DefaultHttpBinding.java HttpHeaderFilterStrategy.java HttpProducer.java
RequestEntityConverter.java helper/GZIPHelper.java
Posted by Willem Jiang <wi...@gmail.com>.
Hi Claus,
if (methodToUse.isEntityEnclosing()) {
((EntityEnclosingMethod)method).setRequestEntity(requestEntity);
if (requestEntity.getContentType() == null) {
LOG.warn("Missing the ContentType in the request entity!");
}
}
The Get method will be excluded, since there is no entity for Get method
. The code should be fine, and I will add the URI into the warning log.
Thanks,
Willem
Claus Ibsen wrote:
> Willem
>
> Beware to do WARN logging in case it will log all the time
> + LOG.warn("Missing the ContentType in the request entity!");
>
> Its quite often not to set a Content-Type using camel http to just do
> a text/plain GET.
> So be sure that the http client sets a Content-Type if the end user
> have not provided anyone.
>
> If the content-type is often null then consider changing the WARN to
> DEBUG. And you could output the URI also so people looking in the log
> can better known which HTTP call is the problem.
>
>
>
> On Thu, May 14, 2009 at 8:46 AM, <ni...@apache.org> wrote:
>> Author: ningjiang
>> Date: Thu May 14 06:46:16 2009
>> New Revision: 774658
>>
>> URL: http://svn.apache.org/viewvc?rev=774658&view=rev
>> Log:
>> CAMEL-1609 Better fixing the issue of Content type setting for HTTP producer
>>
>> Modified:
>> 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/HttpHeaderFilterStrategy.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
>>
>> 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=774658&r1=774657&r2=774658&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 Thu May 14 06:46:16 2009
>> @@ -20,6 +20,8 @@
>> import java.io.IOException;
>> import java.io.InputStream;
>> import java.io.PrintWriter;
>> +import java.util.Enumeration;
>> +import java.util.Map;
>>
>> import javax.servlet.ServletOutputStream;
>> import javax.servlet.http.HttpServletRequest;
>> @@ -50,7 +52,40 @@
>> public void readRequest(HttpServletRequest request, HttpMessage message) {
>> // lets force a parse of the body and headers
>> message.getBody();
>> - message.getHeaders();
>> + // populate the headers from the request
>> + Map<String, Object> headers = message.getHeaders();
>> +
>> + //apply the headerFilterStrategy
>> + Enumeration names = request.getHeaderNames();
>> + while (names.hasMoreElements()) {
>> + String name = (String)names.nextElement();
>> + Object value = request.getHeader(name);
>> + if (headerFilterStrategy != null
>> + && !headerFilterStrategy.applyFilterToExternalHeaders(name, value, message.getExchange())) {
>> + headers.put(name, value);
>> + }
>> + }
>> +
>> + //if the request method is Get, we also populate the http request parameters
>> + if (request.getMethod().equalsIgnoreCase("GET")) {
>> + names = request.getParameterNames();
>> + while (names.hasMoreElements()) {
>> + String name = (String)names.nextElement();
>> + Object value = request.getParameter(name);
>> + if (headerFilterStrategy != null
>> + && !headerFilterStrategy.applyFilterToExternalHeaders(name, value, message.getExchange())) {
>> + headers.put(name, value);
>> + }
>> + }
>> + }
>> +
>> + // store the method and query and other info in headers
>> + headers.put(HttpConstants.HTTP_METHOD, request.getMethod());
>> + headers.put(HttpConstants.HTTP_QUERY, request.getQueryString());
>> + headers.put(HttpConstants.HTTP_PATH, request.getPathInfo());
>> + headers.put(HttpConstants.HTTP_CONTENT_TYPE, request.getContentType());
>> + headers.put(HttpConstants.HTTP_CHARACTER_ENCODING, request.getCharacterEncoding());
>> +
>> }
>>
>> public void writeResponse(HttpExchange exchange, HttpServletResponse response) throws IOException {
>>
>> Modified: camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpHeaderFilterStrategy.java
>> URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpHeaderFilterStrategy.java?rev=774658&r1=774657&r2=774658&view=diff
>> ==============================================================================
>> --- camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpHeaderFilterStrategy.java (original)
>> +++ camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpHeaderFilterStrategy.java Thu May 14 06:46:16 2009
>> @@ -28,7 +28,8 @@
>> }
>>
>> protected void initialize() {
>> - getOutFilter().add("content-length");
>> + getOutFilter().add("content-length");
>> + getOutFilter().add("content-type");
>> getOutFilter().add(HttpConstants.HTTP_METHOD.toLowerCase());
>> getOutFilter().add(HttpConstants.HTTP_QUERY);
>> getOutFilter().add(HttpConstants.HTTP_RESPONSE_CODE.toLowerCase());
>>
>> 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=774658&r1=774657&r2=774658&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 Thu May 14 06:46:16 2009
>> @@ -27,6 +27,8 @@
>> import org.apache.camel.converter.stream.CachedOutputStream;
>> import org.apache.camel.impl.DefaultProducer;
>> import org.apache.camel.spi.HeaderFilterStrategy;
>> +import org.apache.camel.util.ExchangeHelper;
>> +import org.apache.camel.util.MessageHelper;
>> import org.apache.camel.util.ObjectHelper;
>> import org.apache.commons.httpclient.Header;
>> import org.apache.commons.httpclient.HttpClient;
>> @@ -220,6 +222,9 @@
>> }
>> if (methodToUse.isEntityEnclosing()) {
>> ((EntityEnclosingMethod)method).setRequestEntity(requestEntity);
>> + if (requestEntity.getContentType() == null) {
>> + LOG.warn("Missing the ContentType in the request entity!");
>> + }
>> }
>>
>> return method;
>> @@ -237,12 +242,12 @@
>> return null;
>> }
>>
>> - RequestEntity answer = in.getBody(RequestEntity.class);
>> + RequestEntity answer = in.getBody(RequestEntity.class);
>> if (answer == null) {
>> try {
>> String data = in.getBody(String.class);
>> if (data != null) {
>> - String contentType = in.getHeader("Content-Type", String.class);
>> + String contentType = ExchangeHelper.getContentType(exchange);
>> String charset = exchange.getProperty(Exchange.CHARSET_NAME, String.class);
>> answer = new StringRequestEntity(data, contentType, charset);
>> }
>> @@ -250,7 +255,6 @@
>> throw new RuntimeCamelException(e);
>> }
>> }
>> -
>> return answer;
>> }
>>
>>
>> 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=774658&r1=774657&r2=774658&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 Thu May 14 06:46:16 2009
>> @@ -22,6 +22,7 @@
>> import org.apache.camel.Converter;
>> import org.apache.camel.Exchange;
>> import org.apache.camel.component.http.helper.GZIPHelper;
>> +import org.apache.camel.util.ExchangeHelper;
>> import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
>> import org.apache.commons.httpclient.methods.RequestEntity;
>>
>> @@ -36,7 +37,7 @@
>> return new InputStreamRequestEntity(
>> GZIPHelper.toGZIPInputStreamIfRequested(
>> exchange.getIn().getHeader(GZIPHelper.CONTENT_ENCODING, String.class),
>> - buffer.array()));
>> + buffer.array()), ExchangeHelper.getContentType(exchange));
>> }
>>
>> @Converter
>> @@ -44,7 +45,7 @@
>> return new InputStreamRequestEntity(
>> GZIPHelper.toGZIPInputStreamIfRequested(
>> exchange.getIn().getHeader(GZIPHelper.CONTENT_ENCODING, String.class),
>> - array));
>> + array), ExchangeHelper.getContentType(exchange));
>> }
>>
>> @Converter
>> @@ -52,16 +53,21 @@
>> return new InputStreamRequestEntity(
>> GZIPHelper.getGZIPWrappedInputStream(
>> exchange.getIn().getHeader(GZIPHelper.CONTENT_ENCODING, String.class),
>> - inStream));
>> + inStream), ExchangeHelper.getContentType(exchange));
>> }
>>
>>
>> @Converter
>> public RequestEntity toRequestEntity(String str, Exchange exchange) throws Exception {
>> - return new InputStreamRequestEntity(
>> + if (GZIPHelper.containsGzip(exchange.getIn().getHeader(GZIPHelper.CONTENT_ENCODING, String.class))) {
>> + return new InputStreamRequestEntity(
>> GZIPHelper.toGZIPInputStreamIfRequested(
>> exchange.getIn().getHeader(GZIPHelper.CONTENT_ENCODING, String.class),
>> - str.getBytes()));
>> + str.getBytes()), ExchangeHelper.getContentType(exchange));
>> + } else {
>> + // will use the default StringRequestEntity
>> + return null;
>> + }
>> }
>>
>>
>>
>> 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=774658&r1=774657&r2=774658&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 Thu May 14 06:46:16 2009
>> @@ -124,7 +124,7 @@
>> return compressed;
>> }
>>
>> - private static boolean containsGzip(String str) {
>> + public static boolean containsGzip(String str) {
>> return str != null && str.toLowerCase().indexOf(GZIP) >= 0;
>> }
>>
>>
>>
>>
>
>
>