You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ni...@apache.org on 2008/09/28 06:10:26 UTC

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

Author: ningjiang
Date: Sat Sep 27 21:10:25 2008
New Revision: 699769

URL: http://svn.apache.org/viewvc?rev=699769&view=rev
Log:
CAMEL-943 HttpProducer will throw exception when it gets the 3xx, 4xx, 5xx status code

Added:
    activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpOperationFailedException.java   (with props)
Modified:
    activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java
    activemq/camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpPostWithBodyTest.java

Added: activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpOperationFailedException.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpOperationFailedException.java?rev=699769&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpOperationFailedException.java (added)
+++ activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpOperationFailedException.java Sat Sep 27 21:10:25 2008
@@ -0,0 +1,60 @@
+/**
+ * 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.http;
+
+import org.apache.camel.CamelException;
+import org.apache.camel.util.ObjectHelper;
+import org.apache.commons.httpclient.StatusLine;
+
+public class HttpOperationFailedException extends CamelException {
+    private final String redirectLocation;
+    private final int statusCode;
+    private final StatusLine statusLine;
+
+
+    public HttpOperationFailedException(int statusCode, StatusLine statusLine, String location) {
+        this.statusCode = statusCode;
+        this.statusLine = statusLine;
+        redirectLocation = location;
+    }
+
+    public HttpOperationFailedException(int statusCode, StatusLine statusLine) {
+        this(statusCode, statusLine, null);
+    }
+
+    public boolean isRedirectError() {
+        return statusCode >= 300 && statusCode < 400;
+    }
+
+    public boolean hasRedirectLocation() {
+        return ObjectHelper.isNotNullAndNonEmpty(redirectLocation);
+    }
+
+    public String getRedirectLocation() {
+        return redirectLocation;
+    }
+
+    public StatusLine getStatusLine() {
+        return statusLine;
+    }
+
+    public int getStatusCode() {
+        return statusCode;
+    }
+
+
+}

Propchange: activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpOperationFailedException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpOperationFailedException.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java?rev=699769&r1=699768&r2=699769&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java (original)
+++ activemq/camel/trunk/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java Sat Sep 27 21:10:25 2008
@@ -72,31 +72,42 @@
 
         // lets store the result in the output message.
         try {
-            Message answer = null;
             int responseCode = httpClient.executeMethod(method);
-            if (responseCode == 200) {
-                answer = exchange.getOut(true);
+            if (responseCode >= 100 && responseCode < 300) {
+                Message answer = exchange.getOut(true);
+                answer.setHeaders(in.getHeaders());
+                answer.setHeader(HTTP_RESPONSE_CODE, responseCode);
+                LoadingByteArrayOutputStream bos = new LoadingByteArrayOutputStream();
+                InputStream is = method.getResponseBodyAsStream();
+                IOUtils.copy(is, bos);
+                bos.flush();
+                is.close();
+                answer.setBody(bos.createInputStream());
+                // 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)) {
+                        answer.setHeader(name, value);
+                    }
+                }
             } else {
-                answer = exchange.getFault(true);
-            }
-            answer.setHeaders(in.getHeaders());
-            answer.setHeader(HTTP_RESPONSE_CODE, responseCode);
-            LoadingByteArrayOutputStream bos = new LoadingByteArrayOutputStream();
-            InputStream is = method.getResponseBodyAsStream();
-            IOUtils.copy(is, bos);
-            bos.flush();
-            is.close();
-            answer.setBody(bos.createInputStream());
-
-            // 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)) {
-                    answer.setHeader(name, value);
+                HttpOperationFailedException exception = null;
+                if (responseCode < 400 && responseCode >= 300) {
+                    String redirectLocation;
+                    Header locationHeader = method.getResponseHeader("location");
+                    if (locationHeader != null) {
+                        redirectLocation = locationHeader.getValue();
+                        exception = new HttpOperationFailedException(responseCode, method.getStatusLine(), redirectLocation);
+                    }
+                } else {
+                    exception = new HttpOperationFailedException(responseCode, method.getStatusLine());
                 }
+
+                throw exception;
             }
+
         } finally {
             method.releaseConnection();
         }

Modified: activemq/camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpPostWithBodyTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpPostWithBodyTest.java?rev=699769&r1=699768&r2=699769&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpPostWithBodyTest.java (original)
+++ activemq/camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/HttpPostWithBodyTest.java Sat Sep 27 21:10:25 2008
@@ -27,11 +27,13 @@
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
 
+import static org.apache.camel.component.http.HttpMethods.GET;
 import static org.apache.camel.component.http.HttpMethods.HTTP_METHOD;
 import static org.apache.camel.component.http.HttpMethods.POST;
 
+
 public class HttpPostWithBodyTest extends ContextTestSupport {
-    protected String expectedText = "<html";
+    protected String expectedText = "Not Implemented";
 
     public void testHttpPostWithError() throws Exception {
 
@@ -46,21 +48,55 @@
         assertNotNull("exchange", exchange);
         assertTrue("The exchange should be failed", exchange.isFailed());
 
-        // get the fault message
-        Message fault = exchange.getFault();
-        assertNotNull("fault", fault);
+        // get the ex message
+        HttpOperationFailedException exception = (HttpOperationFailedException)exchange.getException();
+        assertNotNull("exception", exception);
+
+        int statusCode = exception.getStatusCode();
+        assertTrue("The response code should not be 200", statusCode != 200);
+
+        String reason = exception.getStatusLine().getReasonPhrase();
+
+        assertNotNull("Should have a body!", reason);
+        assertTrue("body should contain: " + expectedText, reason.contains(expectedText));
+
+    }
+
+    public void testHttpPostRecovery() throws Exception {
+
+        MockEndpoint mockResult = resolveMandatoryEndpoint("mock:result", MockEndpoint.class);
+        MockEndpoint mockRecovery = resolveMandatoryEndpoint("mock:recovery", MockEndpoint.class);
+        mockRecovery.expectedMessageCount(1);
+        mockResult.expectedMessageCount(0);
+
+        template.send("direct:reset", new Processor() {
+
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setBody("q=activemq");
+            }
+
+        });
+
+        mockRecovery.assertIsSatisfied();
+        mockResult.assertIsSatisfied();
+        List<Exchange> list = mockRecovery.getReceivedExchanges();
+        Exchange exchange = list.get(0);
+        assertNotNull("exchange", exchange);
+
+        Message in = exchange.getIn();
+        assertNotNull("in", in);
+
+        Map<String, Object> headers = in.getHeaders();
 
-        Map<String, Object> headers = fault.getHeaders();
         log.debug("Headers: " + headers);
         assertTrue("Should be more than one header but was: " + headers, headers.size() > 0);
 
-        int responseCode = fault.getHeader(HttpProducer.HTTP_RESPONSE_CODE, Integer.class);
-        assertTrue("The response code should not be 200", responseCode != 200);
+        String body = in.getBody(String.class);
 
-        String body = fault.getBody(String.class);
         log.debug("Body: " + body);
         assertNotNull("Should have a body!", body);
-        assertTrue("body should contain: " + expectedText, body.contains(expectedText));
+        System.out.println("The body is " + body);
+        assertTrue("body should contain: <html>", body.contains("<html>"));
 
     }
 
@@ -69,6 +105,9 @@
         return new RouteBuilder() {
             public void configure() {
                 from("direct:start").setHeader(HTTP_METHOD, POST).to("http://www.google.com");
+                from("direct:reset").setHeader(HTTP_METHOD, POST).
+                    errorHandler(deadLetterChannel("direct:recovery").maximumRedeliveries(1)).to("http://www.google.com").to("mock:result");
+                from("direct:recovery").setHeader(HTTP_METHOD, GET).to("http://www.google.com").to("mock:recovery");
             }
         };
     }