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 2009/04/02 16:31:51 UTC

svn commit: r761307 - in /camel/trunk/components: camel-http/src/main/java/org/apache/camel/component/http/ camel-jetty/src/test/java/org/apache/camel/component/jetty/

Author: davsclaus
Date: Thu Apr  2 14:31:51 2009
New Revision: 761307

URL: http://svn.apache.org/viewvc?rev=761307&view=rev
Log:
CAMEL-1512: Added option throwException to allow the http producer to return any response without throwing the exception, to allow end users to handle all response in the same code.

Added:
    camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettySimplifiedHandle404Test.java
      - copied, changed from r761300, camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyHandle404Test.java
Modified:
    camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java
    camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java
    camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java

Modified: camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java?rev=761307&r1=761306&r2=761307&view=diff
==============================================================================
--- camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java (original)
+++ camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpComponent.java Thu Apr  2 14:31:51 2009
@@ -65,7 +65,6 @@
      * and password option are not null.
      * 
      * @param parameters the map of parameters 
-     * 
      */
     protected void configureParameters(Map parameters) {
         // lookup http binding in registry if provided
@@ -86,7 +85,7 @@
         if (ref != null) {
             httpClientConfigurer = CamelContextHelper.mandatoryLookup(getCamelContext(), ref, HttpClientConfigurer.class);
         }
-        
+
         matchOnUriPrefix = Boolean.parseBoolean(getAndRemoveParameter(parameters, "matchOnUriPrefix", String.class));
     }
     
@@ -102,6 +101,9 @@
 
         configureParameters(parameters);
 
+        // should we use an exception for failed error codes?
+        Boolean throwException = getAndRemoveParameter(parameters, "throwException", Boolean.class);
+
         // restructure uri to be based on the parameters left as we dont want to include the Camel internal options
         URI httpUri = URISupport.createRemainingURI(new URI(uri), parameters);
         uri = httpUri.toString();
@@ -120,6 +122,9 @@
         if (httpBinding != null) {
             endpoint.setBinding(httpBinding);
         }
+        if (throwException != null) {
+            endpoint.setThrowException(throwException);
+        }
         return endpoint;
     }
 

Modified: camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java?rev=761307&r1=761306&r2=761307&view=diff
==============================================================================
--- camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java (original)
+++ camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpEndpoint.java Thu Apr  2 14:31:51 2009
@@ -49,6 +49,7 @@
     private HttpClientParams clientParams;
     private HttpClientConfigurer httpClientConfigurer;
     private HttpConnectionManager httpConnectionManager;
+    private boolean throwException = true;
 
     public HttpEndpoint() {
     }
@@ -204,4 +205,11 @@
         this.headerFilterStrategy = headerFilterStrategy;
     }
 
+    public boolean isThrowException() {
+        return throwException;
+    }
+
+    public void setThrowException(boolean throwException) {
+        this.throwException = throwException;
+    }
 }

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=761307&r1=761306&r2=761307&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 Apr  2 14:31:51 2009
@@ -44,10 +44,12 @@
 public class HttpProducer extends DefaultProducer {
     private static final transient Log LOG = LogFactory.getLog(HttpProducer.class);
     private HttpClient httpClient;
+    private boolean throwException;
 
     public HttpProducer(HttpEndpoint endpoint) {
         super(endpoint);
-        httpClient = endpoint.createHttpClient();
+        this.httpClient = endpoint.createHttpClient();
+        this.throwException = endpoint.isThrowException();
     }
 
     public void process(Exchange exchange) throws Exception {
@@ -73,43 +75,16 @@
                 LOG.debug("Http responseCode: " + responseCode);
             }
 
-            if (responseCode >= 100 && responseCode < 300) {
-                Message answer = exchange.getOut(true);
-
-                answer.setHeaders(in.getHeaders());
-                answer.setHeader(HttpConstants.HTTP_RESPONSE_CODE, responseCode);
-                answer.setBody(extractResponseBody(method, exchange));
-
-                // propagate HTTP response headers
-                Header[] headers = method.getResponseHeaders();
-                for (Header header : headers) {
-                    String name = header.getName();
-                    String value = header.getValue();
-                    if (strategy != null && !strategy.applyFilterToExternalHeaders(name, value, exchange)) {
-                        answer.setHeader(name, value);
-                    }
-                }
+            if (!throwException) {
+                // if we do not use failed exception then populate response for all response codes
+                populateResponse(exchange, method, in, strategy, responseCode);
             } else {
-                HttpOperationFailedException exception = null;
-                Header[] headers = method.getResponseHeaders();
-                InputStream is = extractResponseBody(method, exchange);
-                if (responseCode >= 300 && responseCode < 400) {
-                    String redirectLocation;
-                    Header locationHeader = method.getResponseHeader("location");
-                    if (locationHeader != null) {
-                        redirectLocation = locationHeader.getValue();
-                        exception = new HttpOperationFailedException(responseCode, method.getStatusLine(), redirectLocation, headers, is);
-                    } else {
-                        // no redirect location
-                        exception = new HttpOperationFailedException(responseCode, method.getStatusLine(), headers, is);
-                    }
+                if (responseCode >= 100 && responseCode < 300) {
+                    // only populate reponse for OK response
+                    populateResponse(exchange, method, in, strategy, responseCode);
                 } else {
-                    // internal server error (error code 500)
-                    exception = new HttpOperationFailedException(responseCode, method.getStatusLine(), headers, is);
-                }
-
-                if (exception != null) {                    
-                    throw exception;
+                    // operation failed so populate exception to throw
+                    throw populateHttpOperationFailedException(exchange, method, responseCode);
                 }
             }
 
@@ -118,6 +93,45 @@
         }
     }
 
+    protected void populateResponse(Exchange exchange, HttpMethod method, Message in, HeaderFilterStrategy strategy, int responseCode) throws IOException {
+        Message answer = exchange.getOut(true);
+
+        answer.setHeaders(in.getHeaders());
+        answer.setHeader(HttpConstants.HTTP_RESPONSE_CODE, responseCode);
+        answer.setBody(extractResponseBody(method, exchange));
+
+        // propagate HTTP response headers
+        Header[] headers = method.getResponseHeaders();
+        for (Header header : headers) {
+            String name = header.getName();
+            String value = header.getValue();
+            if (strategy != null && !strategy.applyFilterToExternalHeaders(name, value, exchange)) {
+                answer.setHeader(name, value);
+            }
+        }
+    }
+
+    protected HttpOperationFailedException populateHttpOperationFailedException(Exchange exchange, HttpMethod method, int responseCode) throws IOException {
+        HttpOperationFailedException exception;
+        Header[] headers = method.getResponseHeaders();
+        InputStream is = extractResponseBody(method, exchange);
+        if (responseCode >= 300 && responseCode < 400) {
+            String redirectLocation;
+            Header locationHeader = method.getResponseHeader("location");
+            if (locationHeader != null) {
+                redirectLocation = locationHeader.getValue();
+                exception = new HttpOperationFailedException(responseCode, method.getStatusLine(), redirectLocation, headers, is);
+            } else {
+                // no redirect location
+                exception = new HttpOperationFailedException(responseCode, method.getStatusLine(), headers, is);
+            }
+        } else {
+            // internal server error (error code 500)
+            exception = new HttpOperationFailedException(responseCode, method.getStatusLine(), headers, is);
+        }
+        return exception;
+    }
+
     /**
      * Strategy when executing the method (calling the remote server).
      *

Copied: camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettySimplifiedHandle404Test.java (from r761300, camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyHandle404Test.java)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettySimplifiedHandle404Test.java?p2=camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettySimplifiedHandle404Test.java&p1=camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyHandle404Test.java&r1=761300&r2=761307&rev=761307&view=diff
==============================================================================
--- camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyHandle404Test.java (original)
+++ camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettySimplifiedHandle404Test.java Thu Apr  2 14:31:51 2009
@@ -21,7 +21,6 @@
 import org.apache.camel.Processor;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.http.HttpConstants;
-import org.apache.camel.component.http.HttpOperationFailedException;
 import org.apache.camel.component.mock.MockEndpoint;
 import org.apache.camel.processor.aggregate.AggregationStrategy;
 
@@ -30,7 +29,7 @@
  *
  * @version $Revision$
  */
-public class JettyHandle404Test extends ContextTestSupport {
+public class JettySimplifiedHandle404Test extends ContextTestSupport {
 
     public void testSimulate404() throws Exception {
         MockEndpoint mock = getMockEndpoint("mock:result");
@@ -50,7 +49,12 @@
                 // disable error handling
                 errorHandler(noErrorHandler());
 
-                from("direct:start").enrich("direct:tohttp", new AggregationStrategy() {
+                // START SNIPPET: e1
+                // We set throwException to false to let Camel return any response from the remove HTTP server without thrown
+                // HttpOperationFailedException in case of failures.
+                // This allows us to handle all responses in the aggregation strategy where we can check the HTTP response code
+                // and decide what to do. As this is based on an unit test we assert the code is 404
+                from("direct:start").enrich("http://localhost:8123/myserver?throwException=false&user=Camel", new AggregationStrategy() {
                     public Exchange aggregate(Exchange original, Exchange resource) {
                         // get the response code
                         Integer code = resource.getOut().getHeader(HttpConstants.HTTP_RESPONSE_CODE, Integer.class);
@@ -59,24 +63,6 @@
                     }
                 }).to("mock:result");
 
-                // use this sub route as indirection to handle the HttpOperationFailedException
-                // and set the data back as data on the exchange to not cause the exception to be thrown
-                from("direct:tohttp")
-                        .tryBlock()
-                            .to("http://localhost:8123/myserver?user=Camel")
-                        .handle(HttpOperationFailedException.class)
-                            .process(new Processor() {
-                                public void process(Exchange exchange) {
-                                    // copy the caused exception values to the exchange as we want the response in the regular exchange
-                                    // instead as an exception that will get thrown and thus the route breaks
-                                    HttpOperationFailedException cause = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, HttpOperationFailedException.class);
-                                    exchange.getOut().setHeader(HttpConstants.HTTP_RESPONSE_CODE, cause.getStatusCode());
-                                    exchange.getOut().setBody(cause.getResponseBody());
-                                }
-                            })
-                        .end();
-
-
                 // this is our jetty server where we simulate the 404
                 from("jetty://http://localhost:8123/myserver")
                         .process(new Processor() {
@@ -85,7 +71,8 @@
                                 exchange.getOut().setHeader(HttpConstants.HTTP_RESPONSE_CODE, 404);
                             }
                         });
+                // END SNIPPET: e1
             }
         };
     }
-}
+}
\ No newline at end of file