camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dk...@apache.org
Subject camel git commit: Try to push as many Jetty 8/9 things down into the JettyContentExchange and CamelHttpClient classes as possible to start trying to get it to support both.
Date Fri, 21 Nov 2014 19:33:06 GMT
Repository: camel
Updated Branches:
  refs/heads/master 1b79de058 -> 02e03637d


Try to push as many Jetty 8/9 things down into the JettyContentExchange and CamelHttpClient
classes as possible to start trying to get it to support both.


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

Branch: refs/heads/master
Commit: 02e03637dee65cd286ce3baf813a0694b9ed9904
Parents: 1b79de0
Author: Daniel Kulp <dkulp@apache.org>
Authored: Fri Nov 21 14:22:12 2014 -0500
Committer: Daniel Kulp <dkulp@apache.org>
Committed: Fri Nov 21 14:31:38 2014 -0500

----------------------------------------------------------------------
 .../jetty/DefaultJettyHttpBinding.java          | 18 +++----
 .../component/jetty/JettyContentExchange.java   | 56 ++++++++++++++++++++
 .../component/jetty/JettyHttpProducer.java      | 36 +++++++------
 3 files changed, 86 insertions(+), 24 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/02e03637/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/DefaultJettyHttpBinding.java
----------------------------------------------------------------------
diff --git a/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/DefaultJettyHttpBinding.java
b/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/DefaultJettyHttpBinding.java
index a69c82c..9c7ac03 100644
--- a/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/DefaultJettyHttpBinding.java
+++ b/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/DefaultJettyHttpBinding.java
@@ -18,6 +18,7 @@ package org.apache.camel.component.jetty;
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.util.Collection;
 import java.util.Enumeration;
 import java.util.Map;
 
@@ -96,13 +97,11 @@ public class DefaultJettyHttpBinding implements JettyHttpBinding {
 
         // must use response fields to get the http headers as
         // httpExchange.getHeaders() does not work well with multi valued headers
-        Enumeration<String> names = httpExchange.getResponseFields().getFieldNames();
-        while (names.hasMoreElements()) {
-            String name = names.nextElement();
-            Enumeration<String> values = httpExchange.getResponseFields().getValues(name);
-            while (values.hasMoreElements()) {
-                String value = values.nextElement();
-
+        Map<String, Collection<String>> headers = httpExchange.getResponseHeaders();
+        for (Map.Entry<String, Collection<String>> ent : headers.entrySet())
{
+            String name = ent.getKey();
+            Collection<String> values = ent.getValue();
+            for (String value : values) {
                 if (name.toLowerCase().equals("content-type")) {
                     name = Exchange.CONTENT_TYPE;
                     exchange.setProperty(Exchange.CHARSET_NAME, IOHelper.getCharsetNameFromContentType(value));
@@ -141,8 +140,9 @@ public class DefaultJettyHttpBinding implements JettyHttpBinding {
         }
 
         if (responseCode >= 300 && responseCode < 400) {
-            String locationHeader = httpExchange.getResponseFields().getStringField("location");
-            if (locationHeader != null) {
+            Collection<String> loc = httpExchange.getResponseHeaders().get("location");
+            if (loc != null && !loc.isEmpty()) {
+                String locationHeader = loc.iterator().next();
                 answer = new HttpOperationFailedException(uri, responseCode, null, locationHeader,
headers, copy);
             } else {
                 // no redirect location

http://git-wip-us.apache.org/repos/asf/camel/blob/02e03637/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyContentExchange.java
----------------------------------------------------------------------
diff --git a/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyContentExchange.java
b/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyContentExchange.java
index 32081eb..f8f6053 100644
--- a/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyContentExchange.java
+++ b/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyContentExchange.java
@@ -18,8 +18,12 @@ package org.apache.camel.component.jetty;
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.UnsupportedEncodingException;
+import java.util.Collection;
+import java.util.HashMap;
 import java.util.LinkedHashMap;
 import java.util.Map;
+import java.util.TreeMap;
 import java.util.concurrent.CountDownLatch;
 
 import org.apache.camel.AsyncCallback;
@@ -32,6 +36,7 @@ import org.apache.camel.util.StringHelper;
 import org.eclipse.jetty.client.ContentExchange;
 import org.eclipse.jetty.client.HttpClient;
 import org.eclipse.jetty.client.HttpExchange;
+import org.eclipse.jetty.http.HttpFields;
 import org.eclipse.jetty.http.HttpHeaders;
 import org.eclipse.jetty.io.Buffer;
 import org.eclipse.jetty.io.ByteArrayBuffer;
@@ -232,4 +237,55 @@ public class JettyContentExchange extends ContentExchange {
         }
     }
 
+    public void setRequestContentType(String contentType) {
+        super.setRequestContentType(contentType);
+    }
+
+    public int getResponseStatus() {
+        return super.getResponseStatus();
+    }
+
+    public void setMethod(String method) {
+        super.setMethod(method);
+    }
+    
+    public void setURL(String url) {
+        super.setURL(url);
+    }
+
+    public void setRequestContent(byte[] byteArray) {
+        super.setRequestContent(new org.eclipse.jetty.io.ByteArrayBuffer(byteArray));   
    
+    }
+    public void setRequestContent(String data, String charset) throws UnsupportedEncodingException
{
+        if (charset == null) {
+            super.setRequestContent(new org.eclipse.jetty.io.ByteArrayBuffer(data));
+        } else {
+            super.setRequestContent(new org.eclipse.jetty.io.ByteArrayBuffer(data, charset));
+        }
+    }
+    public void setRequestContent(InputStream ins) {
+        super.setRequestContentSource(ins);        
+    }
+
+    public void addRequestHeader(String key, String s) {
+        super.addRequestHeader(key, s);
+    }
+
+    public void send(HttpClient client) throws IOException {
+        client.send(this);
+    }
+
+    public byte[] getResponseContentBytes() {
+        return super.getResponseContentBytes();
+    }
+    
+    public Map<String, Collection<String>> getResponseHeaders() {
+        final HttpFields f = super.getResponseFields();
+        Map<String, Collection<String>> ret = new TreeMap<String, Collection<String>>(String.CASE_INSENSITIVE_ORDER);
+        for (String n : f.getFieldNamesCollection()) {
+            ret.put(n,  f.getValuesCollection(n));
+        }
+        return ret;
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/02e03637/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpProducer.java
----------------------------------------------------------------------
diff --git a/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpProducer.java
b/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpProducer.java
index 45fcc53..eca9ee6 100644
--- a/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpProducer.java
+++ b/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpProducer.java
@@ -42,7 +42,6 @@ import org.apache.camel.util.IOHelper;
 import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.URISupport;
 import org.eclipse.jetty.client.HttpClient;
-import org.eclipse.jetty.io.ByteArrayBuffer;
 import org.eclipse.jetty.util.component.LifeCycle;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -142,7 +141,7 @@ public class JettyHttpProducer extends DefaultProducer implements AsyncProcessor
                 ByteArrayOutputStream bos = new ByteArrayOutputStream();
                 try {
                     HttpHelper.writeObjectToStream(bos, obj);
-                    httpExchange.setRequestContent(new ByteArrayBuffer(bos.toByteArray()));
+                    httpExchange.setRequestContent(bos.toByteArray());
                 } finally {
                     IOHelper.close(bos, "body", LOG);
                 }
@@ -155,15 +154,11 @@ public class JettyHttpProducer extends DefaultProducer implements AsyncProcessor
                     // do not fallback to use the default charset as it can influence the
request
                     // (for example application/x-www-form-urlencoded forms being sent)
                     String charset = IOHelper.getCharsetName(exchange, false);
-                    if (charset != null) {
-                        httpExchange.setRequestContent(new ByteArrayBuffer(data, charset));
-                    } else {
-                        httpExchange.setRequestContent(new ByteArrayBuffer(data));
-                    }
+                    httpExchange.setRequestContent(data, charset);
                 } else {
                     // then fallback to input stream
                     InputStream is = exchange.getContext().getTypeConverter().mandatoryConvertTo(InputStream.class,
exchange, exchange.getIn().getBody());
-                    httpExchange.setRequestContentSource(is);
+                    httpExchange.setRequestContent(is);
                 }
             }
         }
@@ -229,7 +224,7 @@ public class JettyHttpProducer extends DefaultProducer implements AsyncProcessor
         if (LOG.isDebugEnabled()) {
             LOG.debug("Sending HTTP request to: {}", httpExchange.getUrl());
         }
-        client.send(httpExchange);
+        httpExchange.send(client);
     }
 
     public JettyHttpBinding getBinding() {
@@ -262,15 +257,25 @@ public class JettyHttpProducer extends DefaultProducer implements AsyncProcessor
         this.sharedClient = true;
     }
 
+    private Object getClientThreadPool() {
+        try {
+            return client.getClass().getMethod("getThreadPool").invoke(client);
+        } catch (Throwable t) {
+            // not found in Jetty 9 which is OK as the threadpool is auto started on Jetty
9
+        }
+        return null;
+    }
+    
     @Override
     protected void doStart() throws Exception {
         // only start non-shared client
         if (!sharedClient && client != null) {
             client.start();
             // start the thread pool
-            if (client.getThreadPool() instanceof LifeCycle) {
-                LOG.debug("Starting client thread pool {}", client.getThreadPool());
-                ((LifeCycle) client.getThreadPool()).start();
+            Object tp = getClientThreadPool();
+            if (tp instanceof LifeCycle) {
+                LOG.debug("Starting client thread pool {}", tp);
+                ((LifeCycle) tp).start();
             }
         }
         super.doStart();
@@ -283,9 +288,10 @@ public class JettyHttpProducer extends DefaultProducer implements AsyncProcessor
         if (!sharedClient && client != null) {
             client.stop();
             // stop thread pool
-            if (client.getThreadPool() instanceof LifeCycle) {
-                LOG.debug("Stopping client thread pool {}", client.getThreadPool());
-                ((LifeCycle) client.getThreadPool()).stop();
+            Object tp = getClientThreadPool();
+            if (tp instanceof LifeCycle) {
+                LOG.debug("Stopping client thread pool {}", tp);
+                ((LifeCycle) tp).stop();
             }
         }
     }


Mime
View raw message