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:29:35 UTC

svn commit: r1053064 - in /camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http: CompositeHttpConfigurer.java DefaultHttpBinding.java HttpBinding.java HttpClientConfigurer.java HttpPollingConsumer.java helper/HttpHelper.java

Author: davsclaus
Date: Mon Dec 27 13:29:35 2010
New Revision: 1053064

URL: http://svn.apache.org/viewvc?rev=1053064&view=rev
Log:
CAMEL-3364: Polished

Modified:
    camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/CompositeHttpConfigurer.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/HttpBinding.java
    camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpClientConfigurer.java
    camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpPollingConsumer.java
    camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/helper/HttpHelper.java

Modified: camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/CompositeHttpConfigurer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/CompositeHttpConfigurer.java?rev=1053064&r1=1053063&r2=1053064&view=diff
==============================================================================
--- camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/CompositeHttpConfigurer.java (original)
+++ camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/CompositeHttpConfigurer.java Mon Dec 27 13:29:35 2010
@@ -24,7 +24,7 @@ import org.apache.commons.httpclient.Htt
 
 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-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=1053064&r1=1053063&r2=1053064&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 Mon Dec 27 13:29:35 2010
@@ -19,7 +19,6 @@ package org.apache.camel.component.http;
 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.http.helper.CamelFileDataSource;
 import org.apache.camel.component.http.helper.GZIPHelper;
-import org.apache.camel.converter.stream.CachedOutputStream;
+import org.apache.camel.component.http.helper.HttpHelper;
 import org.apache.camel.spi.HeaderFilterStrategy;
 import org.apache.camel.util.IOHelper;
 import org.apache.camel.util.MessageHelper;
@@ -95,7 +95,11 @@ 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);
+        }
         
         Object body = message.getBody();
         // reset the stream cache if the body is the instance of StreamCache
@@ -114,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();
@@ -135,21 +139,16 @@ 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);
             }
         }
-        
     }
     
     protected void populateAttachments(HttpServletRequest request, HttpMessage message) {
@@ -199,11 +198,7 @@ public class DefaultHttpBinding implemen
 
         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");
@@ -261,16 +256,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
@@ -313,7 +300,7 @@ public class DefaultHttpBinding implemen
             os.write(data);
             os.flush();
         } finally {
-            os.close();
+            IOHelper.close(os);
         }
     }
 
@@ -325,26 +312,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-http/src/main/java/org/apache/camel/component/http/HttpBinding.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpBinding.java?rev=1053064&r1=1053063&r2=1053064&view=diff
==============================================================================
--- camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpBinding.java (original)
+++ camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpBinding.java Mon Dec 27 13:29:35 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-http/src/main/java/org/apache/camel/component/http/HttpClientConfigurer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpClientConfigurer.java?rev=1053064&r1=1053063&r2=1053064&view=diff
==============================================================================
--- camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpClientConfigurer.java (original)
+++ camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpClientConfigurer.java Mon Dec 27 13:29:35 2010
@@ -19,7 +19,7 @@ package org.apache.camel.component.http;
 import org.apache.commons.httpclient.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-http/src/main/java/org/apache/camel/component/http/HttpPollingConsumer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpPollingConsumer.java?rev=1053064&r1=1053063&r2=1053064&view=diff
==============================================================================
--- camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpPollingConsumer.java (original)
+++ camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpPollingConsumer.java Mon Dec 27 13:29:35 2010
@@ -17,15 +17,13 @@
 package org.apache.camel.component.http;
 
 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.http.helper.LoadingByteArrayOutputStream;
+import org.apache.camel.component.http.helper.HttpHelper;
 import org.apache.camel.impl.PollingConsumerSupport;
 import org.apache.camel.spi.HeaderFilterStrategy;
-import org.apache.camel.util.IOHelper;
 import org.apache.commons.httpclient.Header;
 import org.apache.commons.httpclient.HttpClient;
 import org.apache.commons.httpclient.HttpMethod;
@@ -43,7 +41,7 @@ public class HttpPollingConsumer extends
     public HttpPollingConsumer(HttpEndpoint endpoint) {
         super(endpoint);
         this.endpoint = endpoint;
-        httpClient = endpoint.createHttpClient();
+        this.httpClient = endpoint.createHttpClient();
     }
 
     public Exchange receive() {
@@ -68,31 +66,31 @@ public class HttpPollingConsumer extends
         }
 
         try {
+            // execute request
             int responseCode = httpClient.executeMethod(method);
+
+            Object body = HttpHelper.readResponseBodyFromInputStream(method.getResponseBodyAsStream(), exchange);
+
             // lets store the result in the output message.
-            LoadingByteArrayOutputStream bos = new LoadingByteArrayOutputStream();
-            InputStream is = method.getResponseBodyAsStream();
-            try {
-                IOHelper.copy(is, bos);
-                bos.flush();
-            } finally {
-                IOHelper.close(is, "input stream", null);
-            }
-            Message message = exchange.getIn();
-            message.setBody(bos.createInputStream());
+            Message message = exchange.getOut();
+            message.setBody(body);
 
             // lets set the headers
             Header[] headers = method.getResponseHeaders();
             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-http/src/main/java/org/apache/camel/component/http/helper/HttpHelper.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/helper/HttpHelper.java?rev=1053064&r1=1053063&r2=1053064&view=diff
==============================================================================
--- camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/helper/HttpHelper.java (original)
+++ camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/helper/HttpHelper.java Mon Dec 27 13:29:35 2010
@@ -16,15 +16,25 @@
  */
 package org.apache.camel.component.http.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.http.HttpConstants;
+import org.apache.camel.component.http.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();
+        }
+    }
+
 }