camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cschnei...@apache.org
Subject camel git commit: Fix header and multi part tests
Date Tue, 13 Jan 2015 08:27:47 GMT
Repository: camel
Updated Branches:
  refs/heads/temp-jetty9 41e3c0db4 -> dd7f2011d


Fix header and multi part tests


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

Branch: refs/heads/temp-jetty9
Commit: dd7f2011d3cd84186112b2652698b9638771dccf
Parents: 41e3c0d
Author: Christian Schneider <chris@die-schneider.net>
Authored: Tue Jan 13 09:27:31 2015 +0100
Committer: Christian Schneider <chris@die-schneider.net>
Committed: Tue Jan 13 09:27:31 2015 +0100

----------------------------------------------------------------------
 .../component/jetty/AttachmentHttpBinding.java  | 75 ++++++++++++++++++++
 .../component/jetty/JettyHttpEndpoint.java      |  6 ++
 .../camel/component/jetty/BaseJettyTest.java    | 17 +++++
 .../jetty/HttpProducerSendEmptyHeaderTest.java  |  8 ++-
 .../JettyHttpProducerSendEmptyHeaderTest.java   |  4 +-
 5 files changed, 107 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/dd7f2011/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/AttachmentHttpBinding.java
----------------------------------------------------------------------
diff --git a/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/AttachmentHttpBinding.java
b/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/AttachmentHttpBinding.java
new file mode 100644
index 0000000..98afd26
--- /dev/null
+++ b/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/AttachmentHttpBinding.java
@@ -0,0 +1,75 @@
+package org.apache.camel.component.jetty;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.Collection;
+
+import javax.activation.DataHandler;
+import javax.activation.DataSource;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.Part;
+
+import org.apache.camel.component.http.DefaultHttpBinding;
+import org.apache.camel.component.http.HttpEndpoint;
+import org.apache.camel.component.http.HttpMessage;
+import org.eclipse.jetty.util.MultiPartInputStreamParser;
+
+final class AttachmentHttpBinding extends DefaultHttpBinding {
+    AttachmentHttpBinding(HttpEndpoint endpoint) {
+        super(endpoint);
+    }
+
+    @Override
+    protected void populateAttachments(HttpServletRequest request, HttpMessage message) {
+        Object object = request.getAttribute("org.eclipse.jetty.servlet.MultiPartFile.multiPartInputStream");
+        if (object instanceof MultiPartInputStreamParser) {
+                MultiPartInputStreamParser parser = (MultiPartInputStreamParser)object;
+                Collection<Part> parts;
+                try {
+                    parts = parser.getParts();
+                    for (Part part : parts) {
+                        String contentType = part.getContentType();
+                        if (!contentType.startsWith("application/octet-stream")) {
+                            continue;
+                        }
+                       
+                        DataSource ds = new PartDataSource(part);
+                        message.addAttachment(part.getName(), new DataHandler(ds));
+                    }
+                } catch (Exception e) {
+                    e.printStackTrace();
+                }
+                
+                
+        }
+    }
+    
+    final class PartDataSource implements DataSource {
+        private final Part part;
+
+        PartDataSource(Part part) {
+            this.part = part;
+        }
+
+        @Override
+        public OutputStream getOutputStream() throws IOException {
+            return null;
+        }
+
+        @Override
+        public String getName() {
+            return part.getName();
+        }
+
+        @Override
+        public InputStream getInputStream() throws IOException {
+            return part.getInputStream();
+        }
+
+        @Override
+        public String getContentType() {
+            return part.getContentType();
+        }
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/dd7f2011/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpEndpoint.java
----------------------------------------------------------------------
diff --git a/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpEndpoint.java
b/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpEndpoint.java
index 9933b98..8cc89a2 100644
--- a/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpEndpoint.java
+++ b/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpEndpoint.java
@@ -21,12 +21,14 @@ import java.net.URISyntaxException;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+
 import javax.servlet.Filter;
 
 import org.apache.camel.Consumer;
 import org.apache.camel.Processor;
 import org.apache.camel.Producer;
 import org.apache.camel.ResolveEndpointFailedException;
+import org.apache.camel.component.http.HttpBinding;
 import org.apache.camel.component.http.HttpConsumer;
 import org.apache.camel.component.http.HttpEndpoint;
 import org.apache.camel.impl.SynchronousDelegateProducer;
@@ -77,6 +79,10 @@ public class JettyHttpEndpoint extends HttpEndpoint {
     public JettyHttpComponent getComponent() {
         return (JettyHttpComponent) super.getComponent();
     }
+    
+    public HttpBinding getBinding() {
+        return new AttachmentHttpBinding(this);
+    }
 
     @Override
     public Producer createProducer() throws Exception {

http://git-wip-us.apache.org/repos/asf/camel/blob/dd7f2011/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/BaseJettyTest.java
----------------------------------------------------------------------
diff --git a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/BaseJettyTest.java
b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/BaseJettyTest.java
index 7d7e9d1..365b1fa 100644
--- a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/BaseJettyTest.java
+++ b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/BaseJettyTest.java
@@ -20,10 +20,13 @@ import java.util.Properties;
 import java.util.concurrent.atomic.AtomicInteger;
 
 import org.apache.camel.CamelContext;
+import org.apache.camel.component.http.DefaultHttpBinding;
+import org.apache.camel.component.http.HttpHeaderFilterStrategy;
 import org.apache.camel.component.properties.PropertiesComponent;
 import org.apache.camel.impl.JndiRegistry;
 import org.apache.camel.test.AvailablePortFinder;
 import org.apache.camel.test.junit4.CamelTestSupport;
+import org.eclipse.jetty.server.Server;
 import org.junit.BeforeClass;
 
 public abstract class BaseJettyTest extends CamelTestSupport {
@@ -98,4 +101,18 @@ public abstract class BaseJettyTest extends CamelTestSupport {
             return 9;
         }
     }
+
+    protected void allowNullHeaders() {
+        JettyHttpComponent jetty = (JettyHttpComponent)context.getComponent("jetty");
+        HttpHeaderFilterStrategy filterStrat = new HttpHeaderFilterStrategy();
+        filterStrat.setAllowNullValues(true);
+        @SuppressWarnings("deprecation")
+        DefaultHttpBinding httpBinding = new DefaultHttpBinding(filterStrat);
+        jetty.setHttpBinding(httpBinding);
+    }
+
+    protected boolean isJetty8() {
+        String majorVersion = Server.getVersion().split("\\.")[0];
+        return "8".equals(majorVersion);
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/dd7f2011/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpProducerSendEmptyHeaderTest.java
----------------------------------------------------------------------
diff --git a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpProducerSendEmptyHeaderTest.java
b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpProducerSendEmptyHeaderTest.java
index 43de6b7..c27cf52 100644
--- a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpProducerSendEmptyHeaderTest.java
+++ b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpProducerSendEmptyHeaderTest.java
@@ -29,7 +29,10 @@ public class HttpProducerSendEmptyHeaderTest extends BaseJettyTest {
     public void testHttpProducerSendEmptyHeader() throws Exception {
         MockEndpoint mock = getMockEndpoint("mock:result");
         mock.expectedMessageCount(1);
-        mock.expectedHeaderReceived("foo", "");
+        
+        // Jetty 8 treats an empty header as "" while Jetty 9 treats it as null
+        String expectedValue = isJetty8() ? "" : null; 
+        mock.expectedHeaderReceived("foo", expectedValue);
 
         template.sendBodyAndHeader("http://localhost:{{port}}/myapp/mytest", "Hello World",
"foo", "");
 
@@ -38,10 +41,11 @@ public class HttpProducerSendEmptyHeaderTest extends BaseJettyTest {
 
     @Override
     protected RouteBuilder createRouteBuilder() throws Exception {
+        allowNullHeaders();
         return new RouteBuilder() {
             @Override
             public void configure() throws Exception {
-                from("jetty:http://localhost:{{port}}/myapp/mytest")
+                from("jetty:http://localhost:{{port}}/myapp/mytest?allowNullValues=true")
                     .convertBodyTo(String.class)
                     .to("mock:result");
             }

http://git-wip-us.apache.org/repos/asf/camel/blob/dd7f2011/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/jettyproducer/JettyHttpProducerSendEmptyHeaderTest.java
----------------------------------------------------------------------
diff --git a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/jettyproducer/JettyHttpProducerSendEmptyHeaderTest.java
b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/jettyproducer/JettyHttpProducerSendEmptyHeaderTest.java
index ac52d4f..6bc0e17 100644
--- a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/jettyproducer/JettyHttpProducerSendEmptyHeaderTest.java
+++ b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/jettyproducer/JettyHttpProducerSendEmptyHeaderTest.java
@@ -30,7 +30,8 @@ public class JettyHttpProducerSendEmptyHeaderTest extends BaseJettyTest
{
     public void testHttpProducerSendEmptyHeader() throws Exception {
         MockEndpoint mock = getMockEndpoint("mock:result");
         mock.expectedMessageCount(1);
-        mock.expectedHeaderReceived("foo", "");
+        String expectedValue = isJetty8() ? "" : null; 
+        mock.expectedHeaderReceived("foo", expectedValue);
 
         template.sendBodyAndHeader("jetty:http://localhost:{{port}}/myapp/mytest", "Hello
World", "foo", "");
 
@@ -42,6 +43,7 @@ public class JettyHttpProducerSendEmptyHeaderTest extends BaseJettyTest
{
         return new RouteBuilder() {
             @Override
             public void configure() throws Exception {
+                allowNullHeaders();
                 from("jetty:http://localhost:{{port}}/myapp/mytest")
                     .convertBodyTo(String.class)
                     .to("mock:result");


Mime
View raw message