You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ac...@apache.org on 2016/08/01 09:39:53 UTC

[1/2] camel git commit: Camel-Undertow replaced deprecated methods

Repository: camel
Updated Branches:
  refs/heads/master aa4bdf651 -> c1c471af8


Camel-Undertow replaced deprecated methods


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/c1c471af
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/c1c471af
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/c1c471af

Branch: refs/heads/master
Commit: c1c471af807f669da1658fcaa825e7606ac2a044
Parents: a16958b
Author: Andrea Cosentino <an...@gmail.com>
Authored: Mon Aug 1 11:39:06 2016 +0200
Committer: Andrea Cosentino <an...@gmail.com>
Committed: Mon Aug 1 11:39:28 2016 +0200

----------------------------------------------------------------------
 .../camel/component/undertow/handlers/HttpCamelHandler.java      | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/c1c471af/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/handlers/HttpCamelHandler.java
----------------------------------------------------------------------
diff --git a/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/handlers/HttpCamelHandler.java b/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/handlers/HttpCamelHandler.java
index 352dd26..d9778c5 100644
--- a/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/handlers/HttpCamelHandler.java
+++ b/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/handlers/HttpCamelHandler.java
@@ -67,7 +67,7 @@ public class HttpCamelHandler implements HttpHandler {
                 allowedMethods = "GET,HEAD,POST,PUT,DELETE,TRACE,OPTIONS,CONNECT,PATCH";
             }
             //return list of allowed methods in response headers
-            httpExchange.setResponseCode(StatusCodes.OK);
+            httpExchange.setStatusCode(StatusCodes.OK);
             httpExchange.getResponseHeaders().put(ExchangeHeaders.CONTENT_TYPE, MimeMappings.DEFAULT_MIME_MAPPINGS.get("txt"));
             httpExchange.getResponseHeaders().put(ExchangeHeaders.CONTENT_LENGTH, 0);
             httpExchange.getResponseHeaders().put(Headers.ALLOW, allowedMethods);
@@ -78,7 +78,7 @@ public class HttpCamelHandler implements HttpHandler {
         //reject if the method is not allowed
         if (consumer.getEndpoint().getHttpMethodRestrict() != null
             && !consumer.getEndpoint().getHttpMethodRestrict().contains(requestMethod.toString())) {
-            httpExchange.setResponseCode(StatusCodes.METHOD_NOT_ALLOWED);
+            httpExchange.setStatusCode(StatusCodes.METHOD_NOT_ALLOWED);
             httpExchange.getResponseHeaders().put(ExchangeHeaders.CONTENT_TYPE, MimeMappings.DEFAULT_MIME_MAPPINGS.get("txt"));
             httpExchange.getResponseHeaders().put(ExchangeHeaders.CONTENT_LENGTH, 0);
             httpExchange.getResponseSender().close();


[2/2] camel git commit: CAMEL-10212: Camel-Undertow: transferException and throwExceptionOnFailure options are never used in the component

Posted by ac...@apache.org.
CAMEL-10212: Camel-Undertow: transferException and throwExceptionOnFailure options are never used in the component


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/a16958b8
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/a16958b8
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/a16958b8

Branch: refs/heads/master
Commit: a16958b83a099abf13e939c4a98366468355848a
Parents: aa4bdf6
Author: Andrea Cosentino <an...@gmail.com>
Authored: Mon Aug 1 11:35:11 2016 +0200
Committer: Andrea Cosentino <an...@gmail.com>
Committed: Mon Aug 1 11:39:28 2016 +0200

----------------------------------------------------------------------
 .../camel-undertow/src/main/docs/undertow.adoc  |  6 ++-
 .../undertow/DefaultUndertowHttpBinding.java    | 55 +++++++++++++++-----
 .../component/undertow/UndertowEndpoint.java    |  9 ++--
 .../component/undertow/UndertowHttpBinding.java |  5 +-
 .../component/undertow/UndertowProducer.java    |  5 +-
 .../undertow/handlers/HttpCamelHandler.java     |  3 +-
 6 files changed, 61 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/a16958b8/components/camel-undertow/src/main/docs/undertow.adoc
----------------------------------------------------------------------
diff --git a/components/camel-undertow/src/main/docs/undertow.adoc b/components/camel-undertow/src/main/docs/undertow.adoc
index b82a5c4..7fd77d7 100644
--- a/components/camel-undertow/src/main/docs/undertow.adoc
+++ b/components/camel-undertow/src/main/docs/undertow.adoc
@@ -61,6 +61,7 @@ The Undertow component supports 2 options which are listed below.
 
 
 
+
 // endpoint options: START
 The Undertow component supports 17 endpoint options which are listed below:
 
@@ -69,7 +70,6 @@ The Undertow component supports 17 endpoint options which are listed below:
 |=======================================================================
 | Name | Group | Default | Java Type | Description
 | httpURI | common |  | URI | *Required* The url of the HTTP endpoint to use.
-| transferException | common |  | Boolean | Option to disable throwing the HttpOperationFailedException in case of failed responses from the remote server. This allows you to get all responses regardless of the HTTP status code.
 | bridgeErrorHandler | consumer | false | boolean | Allows for bridging the consumer to the Camel routing Error Handler which mean any exceptions occurred while the consumer is trying to pickup incoming messages or the likes will now be processed as a message and handled by the routing Error Handler. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions that will be logged at WARN/ERROR level and ignored.
 | httpMethodRestrict | consumer |  | String | Used to only allow consuming if the HttpMethod matches such as GET/POST/PUT etc. Multiple methods can be specified separated by comma.
 | matchOnUriPrefix | consumer | true | Boolean | Whether or not the consumer should try to find a target consumer by matching the URI prefix if no exact match is found.
@@ -79,7 +79,8 @@ The Undertow component supports 17 endpoint options which are listed below:
 | options | producer |  | Map | Sets additional channel options. The options that can be used are defined in org.xnio.Options. To configure from endpoint uri then prefix each option with option. such as option.close-abort=true&option.send-buffer=8192
 | reuseAddresses | producer | true | Boolean | Setting to facilitate socket multiplexing
 | tcpNoDelay | producer | true | Boolean | Setting to improve TCP protocol performance
-| throwExceptionOnFailure | producer |  | Boolean | If the option is true HttpProducer will ignore the Exchange.HTTP_URI header and use the endpoint's URI for request. You may also set the option throwExceptionOnFailure to be false to let the producer send all the fault response back.
+| throwExceptionOnFailure | producer | true | Boolean | If the option is true HttpProducer will ignore the Exchange.HTTP_URI header and use the endpoint's URI for request. You may also set the option throwExceptionOnFailure to be false to let the producer send all the fault response back.
+| transferException | producer | false | Boolean | Option to disable throwing the HttpOperationFailedException in case of failed responses from the remote server. This allows you to get all responses regardless of the HTTP status code.
 | exchangePattern | advanced | InOnly | ExchangePattern | Sets the default exchange pattern when creating an exchange
 | headerFilterStrategy | advanced |  | HeaderFilterStrategy | To use a custom HeaderFilterStrategy to filter header to and from Camel message.
 | synchronous | advanced | false | boolean | Sets whether synchronous processing should be strictly used or Camel is allowed to use asynchronous processing (if supported).
@@ -90,6 +91,7 @@ The Undertow component supports 17 endpoint options which are listed below:
 // endpoint options: END
 
 
+
 [[Undertow-MessageHeaders]]
 Message Headers
 ^^^^^^^^^^^^^^^

http://git-wip-us.apache.org/repos/asf/camel/blob/a16958b8/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/DefaultUndertowHttpBinding.java
----------------------------------------------------------------------
diff --git a/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/DefaultUndertowHttpBinding.java b/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/DefaultUndertowHttpBinding.java
index 4fbd762..f8a90ea 100644
--- a/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/DefaultUndertowHttpBinding.java
+++ b/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/DefaultUndertowHttpBinding.java
@@ -18,6 +18,7 @@ package org.apache.camel.component.undertow;
 
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
+import java.io.ObjectOutputStream;
 import java.io.PrintWriter;
 import java.io.StringWriter;
 import java.nio.ByteBuffer;
@@ -34,7 +35,6 @@ import io.undertow.server.HttpServerExchange;
 import io.undertow.util.Headers;
 import io.undertow.util.HttpString;
 import io.undertow.util.Methods;
-import io.undertow.util.MimeMappings;
 
 import org.apache.camel.Exchange;
 import org.apache.camel.Message;
@@ -42,6 +42,7 @@ import org.apache.camel.TypeConverter;
 import org.apache.camel.impl.DefaultMessage;
 import org.apache.camel.spi.HeaderFilterStrategy;
 import org.apache.camel.util.ExchangeHelper;
+import org.apache.camel.util.IOHelper;
 import org.apache.camel.util.MessageHelper;
 import org.apache.camel.util.ObjectHelper;
 import org.slf4j.Logger;
@@ -59,13 +60,16 @@ public class DefaultUndertowHttpBinding implements UndertowHttpBinding {
 
     //use default filter strategy from Camel HTTP
     private HeaderFilterStrategy headerFilterStrategy;
+    private Boolean transferException;
 
     public DefaultUndertowHttpBinding() {
         this.headerFilterStrategy = new UndertowHeaderFilterStrategy();
+        this.transferException = Boolean.FALSE;
     }
 
-    public DefaultUndertowHttpBinding(HeaderFilterStrategy headerFilterStrategy) {
+    public DefaultUndertowHttpBinding(HeaderFilterStrategy headerFilterStrategy, Boolean transferException) {
         this.headerFilterStrategy = headerFilterStrategy;
+        this.transferException = transferException;
     }
 
     public HeaderFilterStrategy getHeaderFilterStrategy() {
@@ -76,7 +80,15 @@ public class DefaultUndertowHttpBinding implements UndertowHttpBinding {
         this.headerFilterStrategy = headerFilterStrategy;
     }
 
-    @Override
+    public Boolean isTransferException() {
+		return transferException;
+	}
+
+	public void setTransferException(Boolean transferException) {
+		this.transferException = transferException;
+	}
+
+	@Override
     public Message toCamelMessage(HttpServerExchange httpExchange, Exchange exchange) throws Exception {
         Message result = new DefaultMessage();
 
@@ -237,7 +249,7 @@ public class DefaultUndertowHttpBinding implements UndertowHttpBinding {
     }
 
     @Override
-    public Object toHttpResponse(HttpServerExchange httpExchange, Message message) {
+    public Object toHttpResponse(HttpServerExchange httpExchange, Message message) throws IOException {
         boolean failed = message.getExchange().isFailed();
         int defaultCode = failed ? 500 : 200;
 
@@ -263,19 +275,36 @@ public class DefaultUndertowHttpBinding implements UndertowHttpBinding {
             }
         }
 
+        Object body = message.getBody();
         Exception exception = message.getExchange().getException();
 
         if (exception != null) {
-            httpExchange.getResponseHeaders().put(Headers.CONTENT_TYPE, MimeMappings.DEFAULT_MIME_MAPPINGS.get("txt"));
-
-            StringWriter sw = new StringWriter();
-            PrintWriter pw = new PrintWriter(sw);
-            exception.printStackTrace(pw);
-
-            String exceptionMessage = sw.toString();
+            if (isTransferException()) {
+                // we failed due an exception, and transfer it as java serialized object
+                ByteArrayOutputStream bos = new ByteArrayOutputStream();
+                ObjectOutputStream oos = new ObjectOutputStream(bos);
+                oos.writeObject(exception.getCause());
+                oos.flush();
+                IOHelper.close(oos, bos);
+
+                // the body should be the serialized java object of the exception
+                body = ByteBuffer.wrap(bos.toByteArray());
+                // force content type to be serialized java object
+                message.setHeader(Exchange.CONTENT_TYPE, "application/x-java-serialized-object");
+            } else {
+                // we failed due an exception so print it as plain text
+                StringWriter sw = new StringWriter();
+                PrintWriter pw = new PrintWriter(sw);
+                exception.getCause().printStackTrace(pw);
+
+                // the body should then be the stacktrace
+                body = ByteBuffer.wrap(sw.toString().getBytes());
+                // force content type to be text/plain as that is what the stacktrace is
+                message.setHeader(Exchange.CONTENT_TYPE, "text/plain");
+            }
 
+            // and mark the exception as failure handled, as we handled it by returning it as the response
             ExchangeHelper.setFailureHandled(message.getExchange());
-            return exceptionMessage;
         }
 
         // set the content type in the response.
@@ -286,7 +315,7 @@ public class DefaultUndertowHttpBinding implements UndertowHttpBinding {
             LOG.trace("Content-Type: {}", contentType);
         }
 
-        return message.getBody();
+        return body;
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/camel/blob/a16958b8/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowEndpoint.java
----------------------------------------------------------------------
diff --git a/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowEndpoint.java b/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowEndpoint.java
index b808b67..9a36978 100644
--- a/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowEndpoint.java
+++ b/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowEndpoint.java
@@ -68,10 +68,10 @@ public class UndertowEndpoint extends DefaultEndpoint implements AsyncEndpoint,
     private String httpMethodRestrict;
     @UriParam(label = "consumer", defaultValue = "true")
     private Boolean matchOnUriPrefix = true;
-    @UriParam(label = "producer")
-    private Boolean throwExceptionOnFailure;
-    @UriParam
-    private Boolean transferException;
+    @UriParam(label = "producer", defaultValue = "true")
+    private Boolean throwExceptionOnFailure = Boolean.TRUE;
+    @UriParam(label = "producer", defaultValue = "false")
+    private Boolean transferException = Boolean.FALSE;
     @UriParam(label = "producer", defaultValue = "true")
     private Boolean keepAlive = Boolean.TRUE;
     @UriParam(label = "producer", defaultValue = "true")
@@ -221,6 +221,7 @@ public class UndertowEndpoint extends DefaultEndpoint implements AsyncEndpoint,
             // create a new binding and use the options from this endpoint
             undertowHttpBinding = new DefaultUndertowHttpBinding();
             undertowHttpBinding.setHeaderFilterStrategy(getHeaderFilterStrategy());
+            undertowHttpBinding.setTransferException(getTransferException());
         }
         return undertowHttpBinding;
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/a16958b8/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowHttpBinding.java
----------------------------------------------------------------------
diff --git a/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowHttpBinding.java b/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowHttpBinding.java
index 736ea0b..e3cf49a 100644
--- a/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowHttpBinding.java
+++ b/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowHttpBinding.java
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.component.undertow;
 
+import java.io.IOException;
 import java.util.Map;
 
 import io.undertow.client.ClientExchange;
@@ -39,10 +40,12 @@ public interface UndertowHttpBinding {
 
     void populateCamelHeaders(ClientResponse response, Map<String, Object> headerMap, Exchange exchange) throws Exception;
 
-    Object toHttpResponse(HttpServerExchange httpExchange, Message message);
+    Object toHttpResponse(HttpServerExchange httpExchange, Message message) throws IOException;
 
     Object toHttpRequest(ClientRequest clientRequest, Message message);
 
     void setHeaderFilterStrategy(HeaderFilterStrategy headerFilterStrategy);
+    
+    void setTransferException(Boolean transferException);
 
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/a16958b8/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowProducer.java
----------------------------------------------------------------------
diff --git a/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowProducer.java b/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowProducer.java
index 5b6a087..df6d63e 100644
--- a/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowProducer.java
+++ b/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowProducer.java
@@ -210,7 +210,10 @@ public class UndertowProducer extends DefaultAsyncProducer {
         @Override
         public void failed(IOException e) {
             LOG.trace("failed: {}", e);
-            camelExchange.setException(e);
+            System.out.println("Failed!");
+            if (getEndpoint().getThrowExceptionOnFailure()) {
+                camelExchange.setException(e);
+            }
             IOHelper.close(connection);
             // make sure to call callback
             callback.done(false);

http://git-wip-us.apache.org/repos/asf/camel/blob/a16958b8/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/handlers/HttpCamelHandler.java
----------------------------------------------------------------------
diff --git a/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/handlers/HttpCamelHandler.java b/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/handlers/HttpCamelHandler.java
index ca0cc07..352dd26 100644
--- a/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/handlers/HttpCamelHandler.java
+++ b/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/handlers/HttpCamelHandler.java
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.component.undertow.handlers;
 
+import java.io.IOException;
 import java.nio.ByteBuffer;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
@@ -118,7 +119,7 @@ public class HttpCamelHandler implements HttpHandler {
         httpExchange.getResponseSender().close();
     }
 
-    private Object getResponseBody(HttpServerExchange httpExchange, Exchange camelExchange, UndertowConsumer consumer) {
+    private Object getResponseBody(HttpServerExchange httpExchange, Exchange camelExchange, UndertowConsumer consumer) throws IOException {
         Object result;
         if (camelExchange.hasOut()) {
             result = consumer.getEndpoint().getUndertowHttpBinding().toHttpResponse(httpExchange, camelExchange.getOut());