camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ningji...@apache.org
Subject svn commit: r1161022 - in /camel/trunk/components/camel-jetty/src: main/java/org/apache/camel/component/jetty/ test/java/org/apache/camel/component/jetty/
Date Wed, 24 Aug 2011 09:03:18 GMT
Author: ningjiang
Date: Wed Aug 24 09:03:17 2011
New Revision: 1161022

URL: http://svn.apache.org/viewvc?rev=1161022&view=rev
Log:
CAMEL-4372 Add FiltersRef option for the Jetty component

Added:
    camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelFilterWrapper.java
      - copied, changed from r1161010, camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelMultipartFilter.java
    camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/CustomFiltersTest.java
  (with props)
Modified:
    camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelMultipartFilter.java
    camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java
    camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpEndpoint.java

Copied: camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelFilterWrapper.java
(from r1161010, camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelMultipartFilter.java)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelFilterWrapper.java?p2=camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelFilterWrapper.java&p1=camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelMultipartFilter.java&r1=1161010&r2=1161022&rev=1161022&view=diff
==============================================================================
--- camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelMultipartFilter.java
(original)
+++ camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelFilterWrapper.java
Wed Aug 24 09:03:17 2011
@@ -1,54 +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;
-
-import java.io.IOException;
-
-import javax.servlet.Filter;
-import javax.servlet.FilterChain;
-import javax.servlet.FilterConfig;
-import javax.servlet.ServletException;
-import javax.servlet.ServletRequest;
-import javax.servlet.ServletResponse;
-
-/**
- * A multipart filter that processes only initially dispatched requests.
- * Re-dispatched requests are ignored.
- */
-class CamelMultipartFilter implements Filter {
-    private Filter wrapped;
-
-    public CamelMultipartFilter(Filter wrapped) {
-        this.wrapped = wrapped;
-    }
-    
-    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
-        if (request.getAttribute(CamelContinuationServlet.EXCHANGE_ATTRIBUTE_NAME) == null)
{
-            wrapped.doFilter(request, response, chain);
-        } else {
-            chain.doFilter(request, response);
-        }
-    }
-
-    public void destroy() {
-        wrapped.destroy();
-    }
-
-    public void init(FilterConfig config) throws ServletException {
-        wrapped.init(config);
-    }
-}
+/**
+ * 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.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+
+/**
+ * The camel filter wrapper that processes only initially dispatched requests.
+ * Re-dispatched requests are ignored.
+ */
+
+public class CamelFilterWrapper implements Filter {
+    
+    private Filter wrapped;
+
+    public CamelFilterWrapper(Filter wrapped) {
+        this.wrapped = wrapped;
+    }
+    
+    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
+        if (request.getAttribute(CamelContinuationServlet.EXCHANGE_ATTRIBUTE_NAME) == null)
{
+            wrapped.doFilter(request, response, chain);
+        } else {
+            chain.doFilter(request, response);
+        }
+    }
+
+    public void destroy() {
+        wrapped.destroy();
+    }
+
+    public void init(FilterConfig config) throws ServletException {
+        wrapped.init(config);
+    }
+
+}

Modified: camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelMultipartFilter.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelMultipartFilter.java?rev=1161022&r1=1161021&r2=1161022&view=diff
==============================================================================
--- camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelMultipartFilter.java
(original)
+++ camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelMultipartFilter.java
Wed Aug 24 09:03:17 2011
@@ -24,31 +24,14 @@ import javax.servlet.FilterConfig;
 import javax.servlet.ServletException;
 import javax.servlet.ServletRequest;
 import javax.servlet.ServletResponse;
-
 /**
- * A multipart filter that processes only initially dispatched requests.
- * Re-dispatched requests are ignored.
+ * Please use the CamelFilterWrapper instead of using this classs
  */
-class CamelMultipartFilter implements Filter {
-    private Filter wrapped;
-
-    public CamelMultipartFilter(Filter wrapped) {
-        this.wrapped = wrapped;
-    }
+@Deprecated 
+class CamelMultipartFilter extends CamelFilterWrapper {
     
-    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
-        if (request.getAttribute(CamelContinuationServlet.EXCHANGE_ATTRIBUTE_NAME) == null)
{
-            wrapped.doFilter(request, response, chain);
-        } else {
-            chain.doFilter(request, response);
-        }
-    }
-
-    public void destroy() {
-        wrapped.destroy();
-    }
-
-    public void init(FilterConfig config) throws ServletException {
-        wrapped.init(config);
+    public CamelMultipartFilter(Filter wrapped) {
+        super(wrapped);
     }
+   
 }

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=1161022&r1=1161021&r2=1161022&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
Wed Aug 24 09:03:17 2011
@@ -143,6 +143,7 @@ public class JettyHttpComponent extends 
         Boolean enableMultipartFilter = getAndRemoveParameter(parameters, "enableMultipartFilter",
                                                               Boolean.class, true);
         Filter multipartFilter = resolveAndRemoveReferenceParameter(parameters, "multipartFilterRef",
Filter.class);
+        List<Filter> filters = resolveAndRemoveReferenceListParameter(parameters, "filtersRef",
Filter.class);
         Long continuationTimeout = getAndRemoveParameter(parameters, "continuationTimeout",
Long.class);
         Boolean useContinuation = getAndRemoveParameter(parameters, "useContinuation", Boolean.class);
         SSLContextParameters sslContextParameters = resolveAndRemoveReferenceParameter(parameters,
"sslContextParametersRef", SSLContextParameters.class);
@@ -234,7 +235,6 @@ public class JettyHttpComponent extends 
         if (matchOnUriPrefix != null) {
             endpoint.setMatchOnUriPrefix(matchOnUriPrefix);
         }
-        
         if (enableJmx != null) {
             endpoint.setEnableJmx(enableJmx);
         } else { 
@@ -248,6 +248,10 @@ public class JettyHttpComponent extends 
             endpoint.setMultipartFilter(multipartFilter);
             endpoint.setEnableMultipartFilter(true);
         }
+        
+        if (filters != null) {
+            endpoint.setFilters(filters);
+        }
 
         if (continuationTimeout != null) {
             endpoint.setContinuationTimeout(continuationTimeout);
@@ -316,10 +320,15 @@ public class JettyHttpComponent extends 
             if (endpoint.isEnableMultipartFilter()) {
                 enableMultipartFilter(endpoint, connectorRef.server, connectorKey);
             }
+            
+            if (endpoint.getFilters() != null && endpoint.getFilters().size() >
0) {
+                setFilters(endpoint, connectorRef.server, connectorKey);
+            }
             connectorRef.servlet.connect(consumer);
         }
     }
     
+
     private void enableJmx(Server server) {
         MBeanContainer containerToRegister = getMbContainer();
         if (containerToRegister != null) {
@@ -343,6 +352,25 @@ public class JettyHttpComponent extends 
         }
     }
     
+    private void setFilters(JettyHttpEndpoint endpoint, Server server, String connectorKey)
{
+        ServletContextHandler context = (ServletContextHandler) server
+            .getChildHandlerByClass(ServletContextHandler.class);
+        List<Filter> filters = endpoint.getFilters();
+        for (Filter filter : filters) {
+            FilterHolder filterHolder = new FilterHolder();
+            filterHolder.setFilter(new CamelFilterWrapper(filter));
+            String pathSpec = endpoint.getPath();
+            if (pathSpec == null || "".equals(pathSpec)) {
+                pathSpec = "/";
+            }
+            if (endpoint.isMatchOnUriPrefix()) {
+                pathSpec = pathSpec.endsWith("/") ? pathSpec + "*" : pathSpec + "/*";
+            }
+            context.addFilter(filterHolder, pathSpec, 0);
+        }
+        
+    }
+    
     private void enableMultipartFilter(HttpEndpoint endpoint, Server server, String connectorKey)
throws Exception {
         ServletContextHandler context = (ServletContextHandler) server
                 .getChildHandlerByClass(ServletContextHandler.class);
@@ -364,7 +392,7 @@ public class JettyHttpComponent extends 
             // if no filter ref was provided, use the default filter
             filter = new MultiPartFilter();
         }
-        filterHolder.setFilter(new CamelMultipartFilter(filter));
+        filterHolder.setFilter(new CamelFilterWrapper(filter));
         String pathSpec = endpoint.getPath();
         if (pathSpec == null || "".equals(pathSpec)) {
             pathSpec = "/";

Modified: camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpEndpoint.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpEndpoint.java?rev=1161022&r1=1161021&r2=1161022&view=diff
==============================================================================
--- camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpEndpoint.java
(original)
+++ camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpEndpoint.java
Wed Aug 24 09:03:17 2011
@@ -44,6 +44,7 @@ public class JettyHttpEndpoint extends H
     private boolean enableJmx;
     private boolean enableMultipartFilter;
     private Filter multipartFilter;
+    private List<Filter> filters;
     private Long continuationTimeout;
     private Boolean useContinuation;
     private SSLContextParameters sslContextParameters;
@@ -137,6 +138,14 @@ public class JettyHttpEndpoint extends H
     public Filter getMultipartFilter() {
         return multipartFilter;
     }
+    
+    public void setFilters(List<Filter> filterList) {
+        this.filters = filterList;
+    }
+    
+    public List<Filter> getFilters() {
+        return filters;
+    }
 
     public Long getContinuationTimeout() {
         return continuationTimeout;

Added: camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/CustomFiltersTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/CustomFiltersTest.java?rev=1161022&view=auto
==============================================================================
--- camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/CustomFiltersTest.java
(added)
+++ camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/CustomFiltersTest.java
Wed Aug 24 09:03:17 2011
@@ -0,0 +1,117 @@
+/**
+ * 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.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.activation.DataHandler;
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletResponse;
+
+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.impl.JndiRegistry;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.methods.PostMethod;
+import org.apache.commons.httpclient.methods.StringRequestEntity;
+import org.apache.commons.httpclient.methods.multipart.FilePart;
+import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
+import org.apache.commons.httpclient.methods.multipart.Part;
+import org.apache.commons.httpclient.methods.multipart.StringPart;
+import org.eclipse.jetty.servlets.MultiPartFilter;
+import org.junit.Test;
+
+public class CustomFiltersTest extends BaseJettyTest {
+
+    private static class MyTestFilter implements Filter {
+        @Override
+        public void doFilter(ServletRequest request, ServletResponse response, FilterChain
chain) throws IOException, ServletException {            
+            // set a marker attribute to show that this filter class was used
+            ((HttpServletResponse)response).addHeader("MyTestFilter", "true");
+            chain.doFilter(request , response);
+        }
+
+        @Override
+        public void init(FilterConfig filterConfig) throws ServletException {
+            // do nothing here
+        }
+
+        @Override
+        public void destroy() {
+            // do nothing here
+        }        
+    }
+    
+    private void sendRequestAndVerify(String url) throws Exception {
+        HttpClient httpclient = new HttpClient();
+        
+        PostMethod httppost = new PostMethod(url);
+        
+        StringRequestEntity reqEntity = new StringRequestEntity("This is a test", null, null);
+        httppost.setRequestEntity(reqEntity);
+
+        int status = httpclient.executeMethod(httppost);
+
+        assertEquals("Get a wrong response status", 200, status);
+
+        String result = httppost.getResponseBodyAsString();
+        assertEquals("Get a wrong result", "This is a test response", result);
+        assertNotNull("Did not use custom multipart filter", httppost.getResponseHeader("MyTestFilter"));
+    }
+    
+    @Test
+    public void testFilters() throws Exception {
+        sendRequestAndVerify("http://localhost:" + getPort() + "/testFilters");
+    }
+
+    @Override
+    protected JndiRegistry createRegistry() throws Exception {
+        JndiRegistry jndi = super.createRegistry();
+        List<Filter> filters = new ArrayList<Filter>();
+        filters.add(new MyTestFilter());
+        jndi.bind("myFilters", filters);
+        return jndi;
+    }
+    
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() throws Exception {
+                                
+                // Test the filter list options
+                from("jetty://http://localhost:{{port}}/testFilters?filtersRef=myFilters").process(new
Processor() {
+                    public void process(Exchange exchange) throws Exception {
+                        Message in = exchange.getIn();
+                        String request = in.getBody(String.class);
+                        // The other form date can be get from the message header
+                        exchange.getOut().setBody(request + " response");
+                    }
+                });
+            }
+        };
+    }
+}

Propchange: camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/CustomFiltersTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/CustomFiltersTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date



Mime
View raw message