camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ningji...@apache.org
Subject [1/2] git commit: CAMEL-7964 support Post Redirect Get WebDesign pattern in camel-jetty
Date Mon, 27 Oct 2014 03:48:20 GMT
Repository: camel
Updated Branches:
  refs/heads/master dc073a749 -> b5f1da228


CAMEL-7964 support Post Redirect Get WebDesign pattern in camel-jetty


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

Branch: refs/heads/master
Commit: bc6c03a3fa37452ea8a095e71d1258be0f6091d7
Parents: dc073a7
Author: Willem Jiang <willem.jiang@gmail.com>
Authored: Mon Oct 27 11:44:36 2014 +0800
Committer: Willem Jiang <willem.jiang@gmail.com>
Committed: Mon Oct 27 11:44:36 2014 +0800

----------------------------------------------------------------------
 .../camel/component/jetty/CamelHttpClient.java  | 15 +++++-
 .../component/jetty/CamelRedirectListener.java  | 52 ++++++++++++++++++
 .../jetty/rest/JettyRestRedirectTest.java       | 57 ++++++++++++++++++++
 3 files changed, 123 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/bc6c03a3/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelHttpClient.java
----------------------------------------------------------------------
diff --git a/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelHttpClient.java
b/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelHttpClient.java
index 386ed03..204921c 100644
--- a/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelHttpClient.java
+++ b/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelHttpClient.java
@@ -20,11 +20,13 @@ import javax.net.ssl.SSLContext;
 
 import org.apache.camel.util.ObjectHelper;
 import org.eclipse.jetty.client.HttpClient;
+import org.eclipse.jetty.client.RedirectListener;
 import org.eclipse.jetty.util.thread.QueuedThreadPool;
 
 public class CamelHttpClient extends HttpClient {
     
     private SSLContext context;
+    private boolean supportRedirect;
 
     @Override
     protected SSLContext getSSLContext() {
@@ -51,7 +53,18 @@ public class CamelHttpClient extends HttpClient {
             qtp.setName("CamelJettyClient(" + ObjectHelper.getIdentityHashCode(this) + ")");
             setThreadPool(qtp);
         }
-
+        if (isSupportRedirect()) {
+            // setup the listener for it
+            this.registerListener(CamelRedirectListener.class.getName());
+        }
         super.doStart();
     }
+
+    public boolean isSupportRedirect() {
+        return supportRedirect;
+    }
+
+    public void setSupportRedirect(boolean supportRedirect) {
+        this.supportRedirect = supportRedirect;
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/bc6c03a3/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelRedirectListener.java
----------------------------------------------------------------------
diff --git a/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelRedirectListener.java
b/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelRedirectListener.java
new file mode 100644
index 0000000..7e1b93d
--- /dev/null
+++ b/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelRedirectListener.java
@@ -0,0 +1,52 @@
+/**
+ * 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 org.eclipse.jetty.client.HttpDestination;
+import org.eclipse.jetty.client.HttpExchange;
+import org.eclipse.jetty.client.RedirectListener;
+import org.eclipse.jetty.http.HttpStatus;
+import org.eclipse.jetty.io.Buffer;
+
+public class CamelRedirectListener extends RedirectListener {
+    private final HttpExchange exchange;
+    
+    public CamelRedirectListener(HttpDestination destination, HttpExchange ex) {
+        super(destination, ex);
+        exchange = ex;
+    }
+
+    @Override
+    public void onResponseStatus(Buffer version, int status, Buffer reason) throws IOException
{
+        // Update the exchange method to get to support the Post/Redirect/Get
+        // http://en.wikipedia.org/wiki/Post/Redirect/Get
+        if (exchange.getMethod().equals("POST") && (status == HttpStatus.SEE_OTHER_303
|| status == HttpStatus.MOVED_TEMPORARILY_302)) {
+            exchange.setMethod("GET");
+        }
+        
+        // Since the default RedirectListener only cares about http
+        // response codes 301 and 302, we override this method and
+        // trick the super class into handling this case for us.
+        if (status == HttpStatus.SEE_OTHER_303) {
+            status = HttpStatus.MOVED_TEMPORARILY_302;
+        }
+
+        super.onResponseStatus(version, status, reason);
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/bc6c03a3/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/JettyRestRedirectTest.java
----------------------------------------------------------------------
diff --git a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/JettyRestRedirectTest.java
b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/JettyRestRedirectTest.java
new file mode 100644
index 0000000..86296c4
--- /dev/null
+++ b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/JettyRestRedirectTest.java
@@ -0,0 +1,57 @@
+/**
+ * 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.rest;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.jetty.BaseJettyTest;
+import org.junit.Test;
+
+public class JettyRestRedirectTest extends BaseJettyTest {
+   
+    @Test
+    public void testClasspath() throws Exception {
+        String response = template.requestBody("jetty:http://localhost:" + getPort2() + "/metadata/profile/tag?httpClient.supportRedirect=true",
"<hello>Camel</hello>", String.class);
+        assertEquals("It should support the redirect out of box.", "Mock profile", response);
+    }
+
+    
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+
+        return new RouteBuilder() {
+            public void configure() {
+                restConfiguration().component("jetty").scheme("http").port(getPort2());
+                rest("/metadata/profile")
+                    .get("/{id}").to("direct:profileLookup")
+                    .post("/tag").to("direct:tag");
+
+                from("direct:profileLookup").transform().constant("Mock profile");
+                from("direct:tag").log("${headers}").process(new Processor() {
+                    @Override
+                    public void process(Exchange ex) throws Exception {
+                        ex.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 302);
+                        ex.getOut().setHeader("location", "/metadata/profile/1");
+                    }
+                }).log("${headers}").transform().constant("Redirecting...");
+            }
+        };
+    }
+    
+}
+


Mime
View raw message