Author: hadrian
Date: Fri Dec 17 04:57:58 2010
New Revision: 1050272

URL: http://svn.apache.org/viewvc?rev=1050272&view=rev
Log:
CAMEL-3436. Patch applied with thanks to David Valeri

Added:
    
camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpBindingRefTest.java
Modified:
    
camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java
    
camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/JettyHttpBindingRefTest.java

Modified: 
camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java?rev=1050272&r1=1050271&r2=1050272&view=diff
==============================================================================
--- 
camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java
 (original)
+++ 
camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java
 Fri Dec 17 04:57:58 2010
@@ -93,6 +93,7 @@ public class JettyHttpComponent extends 
     protected ThreadPool threadPool;
     protected MBeanContainer mbContainer;
     protected boolean enableJmx;
+    protected JettyHttpBinding jettyHttpBinding;
 
     class ConnectorRef {
         Server server;
@@ -128,6 +129,7 @@ public class JettyHttpComponent extends 
         // must extract well known parameters before we create the endpoint
         List<Handler> handlerList = 
resolveAndRemoveReferenceListParameter(parameters, "handlers", Handler.class);
         HttpBinding binding = resolveAndRemoveReferenceParameter(parameters, 
"httpBindingRef", HttpBinding.class);
+        JettyHttpBinding jettyBinding = 
resolveAndRemoveReferenceParameter(parameters, "jettyHttpBindingRef", 
JettyHttpBinding.class);
         Boolean throwExceptionOnFailure = getAndRemoveParameter(parameters, 
"throwExceptionOnFailure", Boolean.class);
         Boolean bridgeEndpoint = getAndRemoveParameter(parameters, 
"bridgeEndpoint", Boolean.class);
         Boolean matchOnUriPrefix = getAndRemoveParameter(parameters, 
"matchOnUriPrefix", Boolean.class);
@@ -174,6 +176,14 @@ public class JettyHttpComponent extends 
         if (binding != null) {
             endpoint.setBinding(binding);
         }
+        // prefer to use endpoint configured over component configured
+        if (jettyBinding == null) {
+            // fallback to component configured
+            jettyBinding = getJettyHttpBinding();
+        }
+        if (jettyBinding != null) {
+            endpoint.setJettyBinding(jettyBinding);
+        }
         // should we use an exception for failed error codes?
         if (throwExceptionOnFailure != null) {
             endpoint.setThrowExceptionOnFailure(throwExceptionOnFailure);
@@ -559,8 +569,16 @@ public class JettyHttpComponent extends 
     public boolean isEnableJmx() {
         return enableJmx;
     }
+    
+    public JettyHttpBinding getJettyHttpBinding() {
+        return jettyHttpBinding;
+       }
+
+       public void setJettyHttpBinding(JettyHttpBinding jettyHttpBinding) {
+        this.jettyHttpBinding = jettyHttpBinding;
+       }
 
-    public synchronized MBeanContainer getMbContainer() {
+       public synchronized MBeanContainer getMbContainer() {
         // If null, provide the default implementation.
         if (mbContainer == null) {
             MBeanServer mbs = null;

Added: 
camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpBindingRefTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpBindingRefTest.java?rev=1050272&view=auto
==============================================================================
--- 
camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpBindingRefTest.java
 (added)
+++ 
camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/HttpBindingRefTest.java
 Fri Dec 17 04:57:58 2010
@@ -0,0 +1,89 @@
+/**
+ * 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 java.io.IOException;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.http.DefaultHttpBinding;
+import org.apache.camel.impl.JndiRegistry;
+import org.junit.Test;
+
+/**
+ * Unit test for http binding ref option.
+ */
+public class HttpBindingRefTest extends BaseJettyTest {
+
+    @Test
+    public void testDefaultHttpBinding() throws Exception {
+        Object out = 
template.requestBody("http://localhost:{{port}}/myapp/myservice";, "Hello 
World");
+        assertEquals("Bye World", 
context.getTypeConverter().convertTo(String.class, out));
+    }
+
+    @Test
+    public void testCustomHttpBinding() throws Exception {
+        Object out = 
template.requestBody("http://localhost:{{port}}/myapp/myotherservice";, "Hello 
World");
+        assertEquals("Something went wrong but we dont care", 
context.getTypeConverter().convertTo(String.class, out));
+    }
+
+    @Override
+    protected JndiRegistry createRegistry() throws Exception {
+        JndiRegistry jndi = super.createRegistry();
+        jndi.bind("default", new DefaultHttpBinding());
+        jndi.bind("myownbinder", new MyHttpBinding());
+        return jndi;
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                errorHandler(noErrorHandler());
+
+                
from("jetty:http://localhost:{{port}}/myapp/myservice?httpBindingRef=default";).transform().constant("Bye
 World");
+
+                
from("jetty:http://localhost:{{port}}/myapp/myotherservice?httpBindingRef=myownbinder";).process(new
 Processor() {
+                    public void process(Exchange exchange) throws Exception {
+                        throw new IllegalStateException("Not implemented");
+                    }
+                });
+            }
+        };
+    }
+
+    // START SNIPPET: e1
+    public class MyHttpBinding extends DefaultHttpBinding {
+
+        @Override
+        public void doWriteExceptionResponse(Throwable exception, 
HttpServletResponse response) throws IOException {
+            // we override the doWriteExceptionResponse as we only want to 
alter the binding how exceptions is
+            // written back to the client. 
+
+            // we just return HTTP 200 so the client thinks its okay
+            response.setStatus(200);
+            // and we return this fixed text
+            response.getWriter().write("Something went wrong but we dont 
care");
+        }
+    }
+    // END SNIPPET: e1
+
+}
+

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=1050272&r1=1050271&r2=1050272&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
 Fri Dec 17 04:57:58 2010
@@ -17,37 +17,51 @@
 package org.apache.camel.component.jetty;
 
 import java.io.IOException;
-import javax.servlet.http.HttpServletResponse;
 
+import org.apache.camel.CamelExecutionException;
 import org.apache.camel.Exchange;
+import org.apache.camel.Message;
 import org.apache.camel.Processor;
 import org.apache.camel.builder.RouteBuilder;
-import org.apache.camel.component.http.DefaultHttpBinding;
+import org.apache.camel.component.http.HttpOperationFailedException;
 import org.apache.camel.impl.JndiRegistry;
+import org.apache.camel.spi.HeaderFilterStrategy;
 import org.junit.Test;
 
 /**
- * Unit test for http binding ref option.
+ * Unit test for jetty http binding ref option.
  */
 public class JettyHttpBindingRefTest extends BaseJettyTest {
 
     @Test
-    public void testDefaultHttpBinding() throws Exception {
-        Object out = 
template.requestBody("http://localhost:{{port}}/myapp/myservice";, "Hello 
World");
+    public void testDefaultJettyHttpBinding() throws Exception {
+        Object out = 
template.requestBody("jetty:http://localhost:{{port}}/myapp/myservice?jettyHttpBindingRef=default";,
 "Hello World");
         assertEquals("Bye World", 
context.getTypeConverter().convertTo(String.class, out));
+
+        try {
+            
template.requestBody("jetty:http://localhost:{{port}}/myapp/myotherservice";, 
"Hello World");
+            fail();
+        }
+        catch (CamelExecutionException e) {
+            assertNotNull(e.getCause());
+            assertTrue(e.getCause() instanceof HttpOperationFailedException);
+
+            assertFalse("Not exactly the message the server 
returned.".equals(((HttpOperationFailedException) 
e.getCause()).getResponseBody()));
+        }
     }
 
     @Test
-    public void testCustomHttpBinding() throws Exception {
-        Object out = 
template.requestBody("http://localhost:{{port}}/myapp/myotherservice";, "Hello 
World");
-        assertEquals("Something went wrong but we dont care", 
context.getTypeConverter().convertTo(String.class, out));
+    public void testCustomJettyHttpBinding() throws Exception {
+        
+       Object out = 
template.requestBody("jetty:http://localhost:{{port}}/myapp/myotherservice?jettyHttpBindingRef=myownbinder";,
 "Hello World");
+        assertEquals("Not exactly the message the server returned.", 
context.getTypeConverter().convertTo(String.class, out));
     }
 
     @Override
     protected JndiRegistry createRegistry() throws Exception {
         JndiRegistry jndi = super.createRegistry();
-        jndi.bind("default", new DefaultHttpBinding());
-        jndi.bind("myownbinder", new MyHttpBinding());
+        jndi.bind("default", new DefaultJettyHttpBinding());
+        jndi.bind("myownbinder", new MyJettyHttpBinding());
         return jndi;
     }
 
@@ -58,9 +72,9 @@ public class JettyHttpBindingRefTest ext
             public void configure() throws Exception {
                 errorHandler(noErrorHandler());
 
-                
from("jetty:http://localhost:{{port}}/myapp/myservice?httpBindingRef=default";).transform().constant("Bye
 World");
+                
from("jetty:http://localhost:{{port}}/myapp/myservice";).transform().constant("Bye
 World");
 
-                
from("jetty:http://localhost:{{port}}/myapp/myotherservice?httpBindingRef=myownbinder";).process(new
 Processor() {
+                
from("jetty:http://localhost:{{port}}/myapp/myotherservice";).process(new 
Processor() {
                     public void process(Exchange exchange) throws Exception {
                         throw new IllegalStateException("Not implemented");
                     }
@@ -70,17 +84,16 @@ public class JettyHttpBindingRefTest ext
     }
 
     // START SNIPPET: e1
-    public class MyHttpBinding extends DefaultHttpBinding {
-
+    public class MyJettyHttpBinding extends DefaultJettyHttpBinding {
         @Override
-        public void doWriteExceptionResponse(Throwable exception, 
HttpServletResponse response) throws IOException {
-            // we override the doWriteExceptionResponse as we only want to 
alter the binding how exceptions is
-            // written back to the client. 
-
-            // we just return HTTP 200 so the client thinks its okay
-            response.setStatus(200);
-            // and we return this fixed text
-            response.getWriter().write("Something went wrong but we dont 
care");
+        protected void populateResponse(Exchange exchange, 
JettyContentExchange httpExchange, Message in,
+                                        HeaderFilterStrategy strategy, int 
responseCode) throws IOException {
+
+            Message answer = exchange.getOut();
+
+               answer.setHeaders(in.getHeaders());
+               answer.setHeader(Exchange.HTTP_RESPONSE_CODE, responseCode);
+               answer.setBody("Not exactly the message the server returned.");
         }
     }
     // END SNIPPET: e1


Reply via email to