camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ningji...@apache.org
Subject svn commit: r918453 - in /camel/trunk/components: camel-http/src/main/java/org/apache/camel/component/http/ camel-jetty/src/test/java/org/apache/camel/component/jetty/
Date Wed, 03 Mar 2010 12:53:48 GMT
Author: ningjiang
Date: Wed Mar  3 12:53:47 2010
New Revision: 918453

URL: http://svn.apache.org/viewvc?rev=918453&view=rev
Log:
CAMEL-2519 support to set the response code from the message header Exchange.HTTP_RESPONSE_CODE
when returning an exception

Added:
    camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyHandleExceptionTest.java
  (with props)
Modified:
    camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/DefaultHttpBinding.java
    camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyHttpBindingRefTest.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=918453&r1=918452&r2=918453&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
Wed Mar  3 12:53:47 2010
@@ -101,17 +101,23 @@
     public void writeResponse(Exchange exchange, HttpServletResponse response) throws IOException
{
         if (exchange.isFailed()) {
             if (exchange.getException() != null) {
-                doWriteExceptionResponse(exchange.getException(), response);
+                // need to check the response code header
+                int responseCode = 500;
+                if (exchange.hasOut()) {
+                    responseCode = exchange.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE,
500, int.class);
+                } else { // get the header from in message
+                    responseCode = exchange.getIn().getHeader(Exchange.HTTP_RESPONSE_CODE,
500, int.class);
+                }
+                doWriteExceptionResponse(exchange.getException(), response, responseCode);
             } else {
                 // it must be a fault, no need to check for the fault flag on the message
                 doWriteFaultResponse(exchange.getOut(), response, exchange);
             }
         } else {
-            // just copy the protocol relates header
-            copyProtocolHeaders(exchange.getIn(), exchange.getOut());
-            Message out = exchange.getOut();
-            if (out != null) {
-                doWriteResponse(out, response, exchange);
+            if (exchange.hasOut()) {
+                // just copy the protocol relates header
+                copyProtocolHeaders(exchange.getIn(), exchange.getOut());
+                doWriteResponse(exchange.getOut(), response, exchange);
             }
         }
     }
@@ -122,9 +128,18 @@
             response.setHeader(Exchange.CONTENT_ENCODING, contentEncoding);
         }
     }
-
+    
+    /**
+     * Please use public void doWriteExceptionResponse(Throwable exception, HttpServletResponse
response, int responseCode) throws IOException
+     */
+    @Deprecated
     public void doWriteExceptionResponse(Throwable exception, HttpServletResponse response)
throws IOException {
-        response.setStatus(500); // 500 for internal server error
+        // set the reponse code to be 500
+        doWriteExceptionResponse(exception, response, 500);
+    }
+
+    public void doWriteExceptionResponse(Throwable exception, HttpServletResponse response,
int responseCode) throws IOException {
+        response.setStatus(responseCode); 
         response.setContentType("text/plain");
 
         // append the stacktrace as response

Added: camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyHandleExceptionTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyHandleExceptionTest.java?rev=918453&view=auto
==============================================================================
--- camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyHandleExceptionTest.java
(added)
+++ camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyHandleExceptionTest.java
Wed Mar  3 12:53:47 2010
@@ -0,0 +1,107 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.jetty;
+
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.ExchangePattern;
+import org.apache.camel.ExchangeTimedOutException;
+import org.apache.camel.Processor;
+import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.ValidationException;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.http.HttpOperationFailedException;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.processor.aggregate.AggregationStrategy;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+/**
+ * Based on end user on forum how to get the 404 error code in his enrich aggregator
+ *
+ * @version $Revision$
+ */
+public class JettyHandleExceptionTest extends CamelTestSupport {
+
+    public String getProducerUrl() {
+        return "http://localhost:8123/myserver?user=Camel";
+    }
+
+    @Test
+    public void testValidationException() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, HttpServletResponse.SC_BAD_REQUEST);
+        
+        template.sendBody("direct:start", "ValidationException");
+        String response = mock.getReceivedExchanges().get(0).getIn().getBody(String.class);
+        assertMockEndpointsSatisfied();
+        assertTrue("Should get the ValidationExcpetion", response.startsWith("org.apache.camel.ValidationException"));
+    }
+    
+    @Test
+    public void testExchangeTimedOutException() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, HttpServletResponse.SC_GATEWAY_TIMEOUT);
+        
+        template.sendBody("direct:start", "ExchangeTimedOutException");
+        String response = mock.getReceivedExchanges().get(0).getIn().getBody(String.class);
+        assertMockEndpointsSatisfied();
+        assertTrue("Should get the ExchangeTimedOutException", response.startsWith("org.apache.camel.ExchangeTimedOutException"));
+    }
+    
+    @Test
+    public void testOtherException() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
+        
+        template.sendBody("direct:start", "OtherException");
+        String response = mock.getReceivedExchanges().get(0).getIn().getBody(String.class);
+        assertMockEndpointsSatisfied();
+        assertTrue("Should get the RuntimeCamelException", response.startsWith("org.apache.camel.RuntimeCamelException"));
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                onException(ValidationException.class).setHeader(Exchange.HTTP_RESPONSE_CODE,
constant(HttpServletResponse.SC_BAD_REQUEST));
+                onException(ExchangeTimedOutException.class).setOutHeader(Exchange.HTTP_RESPONSE_CODE,
constant(HttpServletResponse.SC_GATEWAY_TIMEOUT));
+
+                from("direct:start").to("jetty://http://localhost:8123/myserver?throwExceptionOnFailure=false").to("mock:result");
+                // this is our jetty server where we simulate the 404
+                from("jetty://http://localhost:8123/myserver")
+                        
+                            .process(new Processor() {
+                                public void process(Exchange exchange) throws Exception {
+                                    String request = exchange.getIn().getBody(String.class);
+                                    if (request.equals("ValidationException")) {
+                                        throw new ValidationException(exchange, request);
+                                    } 
+                                    if (request.equals("ExchangeTimedOutException")) {
+                                        throw new ExchangeTimedOutException(exchange, 200);
+                                    }
+                                    throw new RuntimeCamelException("Runtime exception");
+                                }
+                            });
+                         
+            }
+        };
+    }
+    
+}

Propchange: camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyHandleExceptionTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyHandleExceptionTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyHttpBindingRefTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyHttpBindingRefTest.java?rev=918453&r1=918452&r2=918453&view=diff
==============================================================================
--- camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyHttpBindingRefTest.java
(original)
+++ camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyHttpBindingRefTest.java
Wed Mar  3 12:53:47 2010
@@ -73,8 +73,11 @@
     // START SNIPPET: e1
     public class MyHttpBinding extends DefaultHttpBinding {
 
+        // From camel 2.3.0, we added a new parameter responseCode into doWriteExceptionResponse
method
+        // If the camel version is below camel 2.3.0, please still use below method
+        // public void doWriteExceptionResponse(Throwable exception, HttpServletResponse
response) throws IOException
         @Override
-        public void doWriteExceptionResponse(Throwable exception, HttpServletResponse response)
throws IOException {
+        public void doWriteExceptionResponse(Throwable exception, HttpServletResponse response,
int responseCode) throws IOException {
             // we override the doWriteExceptionResponse as we only want to alter the binding
how exceptions is
             // written back to the client. 
 



Mime
View raw message