camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ningji...@apache.org
Subject svn commit: r1347896 - in /camel/trunk/components/camel-cometd: ./ src/main/java/org/apache/camel/component/cometd/ src/test/java/org/apache/camel/component/cometd/
Date Fri, 08 Jun 2012 07:01:09 GMT
Author: ningjiang
Date: Fri Jun  8 07:01:08 2012
New Revision: 1347896

URL: http://svn.apache.org/viewvc?rev=1347896&view=rev
Log:
CAMEL-5340 Add support for cross-origin filtering

Added:
    camel/trunk/components/camel-cometd/src/test/java/org/apache/camel/component/cometd/CometdCrossOriginConsumerTest.java
Modified:
    camel/trunk/components/camel-cometd/pom.xml
    camel/trunk/components/camel-cometd/src/main/java/org/apache/camel/component/cometd/CometdComponent.java
    camel/trunk/components/camel-cometd/src/main/java/org/apache/camel/component/cometd/CometdEndpoint.java

Modified: camel/trunk/components/camel-cometd/pom.xml
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cometd/pom.xml?rev=1347896&r1=1347895&r2=1347896&view=diff
==============================================================================
--- camel/trunk/components/camel-cometd/pom.xml (original)
+++ camel/trunk/components/camel-cometd/pom.xml Fri Jun  8 07:01:08 2012
@@ -50,6 +50,11 @@
         </dependency>
         <dependency>
             <groupId>org.eclipse.jetty</groupId>
+            <artifactId>jetty-servlets</artifactId>
+            <version>${jetty-version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.eclipse.jetty</groupId>
             <artifactId>jetty-util</artifactId>
         </dependency>
         <dependency>

Modified: camel/trunk/components/camel-cometd/src/main/java/org/apache/camel/component/cometd/CometdComponent.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cometd/src/main/java/org/apache/camel/component/cometd/CometdComponent.java?rev=1347896&r1=1347895&r2=1347896&view=diff
==============================================================================
--- camel/trunk/components/camel-cometd/src/main/java/org/apache/camel/component/cometd/CometdComponent.java
(original)
+++ camel/trunk/components/camel-cometd/src/main/java/org/apache/camel/component/cometd/CometdComponent.java
Fri Jun  8 07:01:08 2012
@@ -18,6 +18,7 @@ package org.apache.camel.component.comet
 
 import java.net.URL;
 import java.util.ArrayList;
+import java.util.EnumSet;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
@@ -34,6 +35,7 @@ import org.cometd.server.BayeuxServerImp
 import org.cometd.server.CometdServlet;
 import org.eclipse.jetty.http.ssl.SslContextFactory;
 import org.eclipse.jetty.server.Connector;
+import org.eclipse.jetty.server.DispatcherType;
 import org.eclipse.jetty.server.Server;
 import org.eclipse.jetty.server.handler.ContextHandlerCollection;
 import org.eclipse.jetty.server.nio.SelectChannelConnector;
@@ -41,8 +43,10 @@ import org.eclipse.jetty.server.session.
 import org.eclipse.jetty.server.session.SessionHandler;
 import org.eclipse.jetty.server.ssl.SslConnector;
 import org.eclipse.jetty.server.ssl.SslSelectChannelConnector;
+import org.eclipse.jetty.servlet.FilterHolder;
 import org.eclipse.jetty.servlet.ServletContextHandler;
 import org.eclipse.jetty.servlet.ServletHolder;
+import org.eclipse.jetty.servlets.CrossOriginFilter;
 import org.eclipse.jetty.util.resource.Resource;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -191,6 +195,8 @@ public class CometdComponent extends Def
             }
         }
 
+        applyCrossOriginFiltering(endpoint, context);
+
         context.addServlet(holder, "/cometd/*");
         context.addServlet("org.eclipse.jetty.servlet.DefaultServlet", "/");
         context.setSessionHandler(new SessionHandler(new HashSessionManager()));
@@ -307,7 +313,17 @@ public class CometdComponent extends Def
     protected void doStart() throws Exception {
         super.doStart();
     }
-    
+
+    private void applyCrossOriginFiltering(CometdEndpoint endpoint, ServletContextHandler
context) {
+        if (endpoint.isCrossOriginFilterOn()) {
+            FilterHolder filterHolder = new FilterHolder();
+            CrossOriginFilter filter = new CrossOriginFilter();
+            filterHolder.setFilter(filter);
+            filterHolder.setInitParameter("allowedOrigins", endpoint.getAllowedOrigins());
+            context.addFilter(filterHolder, endpoint.getFilterPath(), EnumSet.allOf(DispatcherType.class));
+        }
+    }
+
     /**
      * Override the key/trust store check method as it does not account for a factory that
has
      * a pre-configured {@link SSLContext}.

Modified: camel/trunk/components/camel-cometd/src/main/java/org/apache/camel/component/cometd/CometdEndpoint.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cometd/src/main/java/org/apache/camel/component/cometd/CometdEndpoint.java?rev=1347896&r1=1347895&r2=1347896&view=diff
==============================================================================
--- camel/trunk/components/camel-cometd/src/main/java/org/apache/camel/component/cometd/CometdEndpoint.java
(original)
+++ camel/trunk/components/camel-cometd/src/main/java/org/apache/camel/component/cometd/CometdEndpoint.java
Fri Jun  8 07:01:08 2012
@@ -41,6 +41,9 @@ public class CometdEndpoint extends Defa
     private int logLevel = 1;
     private URI uri;
     private CometdComponent component;
+    private boolean crossOriginFilterOn;
+    private String allowedOrigins;
+    private String filterPath;
 
     public CometdEndpoint(CometdComponent component, String uri, String remaining, Map<String,
Object> parameters) {
         super(uri, component);
@@ -166,4 +169,28 @@ public class CometdEndpoint extends Defa
     public void setLogLevel(int logLevel) {
         this.logLevel = logLevel;
     }
+
+    public String getAllowedOrigins() {
+        return allowedOrigins;
+    }
+
+    public void setAllowedOrigins(String allowedOrigins) {
+        this.allowedOrigins = allowedOrigins;
+    }
+
+    public boolean isCrossOriginFilterOn() {
+        return crossOriginFilterOn;
+    }
+
+    public void setCrossOriginFilterOn(boolean crossOriginFilterOn) {
+        this.crossOriginFilterOn = crossOriginFilterOn;
+    }
+
+    public String getFilterPath() {
+        return filterPath;
+    }
+
+    public void setFilterPath(String filterPath) {
+        this.filterPath = filterPath;
+    }
 }

Added: camel/trunk/components/camel-cometd/src/test/java/org/apache/camel/component/cometd/CometdCrossOriginConsumerTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cometd/src/test/java/org/apache/camel/component/cometd/CometdCrossOriginConsumerTest.java?rev=1347896&view=auto
==============================================================================
--- camel/trunk/components/camel-cometd/src/test/java/org/apache/camel/component/cometd/CometdCrossOriginConsumerTest.java
(added)
+++ camel/trunk/components/camel-cometd/src/test/java/org/apache/camel/component/cometd/CometdCrossOriginConsumerTest.java
Fri Jun  8 07:01:08 2012
@@ -0,0 +1,110 @@
+/**
+ * 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.cometd;
+
+import javax.servlet.Filter;
+
+import org.apache.camel.Endpoint;
+import org.apache.camel.test.AvailablePortFinder;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.eclipse.jetty.server.Connector;
+import org.eclipse.jetty.server.Server;
+import org.eclipse.jetty.servlet.FilterHolder;
+import org.eclipse.jetty.servlet.ServletContextHandler;
+import org.eclipse.jetty.servlets.CrossOriginFilter;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
+
+
+import static org.mockito.Mockito.when;
+
+/**
+ * Unit testing for using a CometdProducer and a CometdConsumer
+ */
+@RunWith(MockitoJUnitRunner.class)
+public class CometdCrossOriginConsumerTest extends CamelTestSupport {
+
+    private static final String FILTER_PATH = "testFilterPath";
+    private static final String ALLOWED_ORIGINS = "testAllowedOrigins";
+
+    @Mock
+    Connector connector;
+
+    @Mock
+    CometdEndpoint endpoint;
+
+    @Test
+    public void testFilterArgumentsSetOnEndpoint() throws Exception {
+        // setup
+        CometdComponent component = context.getComponent("cometd", CometdComponent.class);
+        int port = AvailablePortFinder.getNextAvailable(23500);
+
+        // act
+        Endpoint result = component
+            .createEndpoint(String.format("cometd://127.0.0.1:%s?crossOriginFilterOn=true&allowedOrigins=%s&filterPath=%s",
+                 port, ALLOWED_ORIGINS, FILTER_PATH));
+
+        // assert
+        assertTrue(result instanceof CometdEndpoint);
+        CometdEndpoint cometdEndpoint = (CometdEndpoint)result;
+        assertTrue(cometdEndpoint.isCrossOriginFilterOn());
+        assertEquals(ALLOWED_ORIGINS, cometdEndpoint.getAllowedOrigins());
+        assertEquals(FILTER_PATH, cometdEndpoint.getFilterPath());
+    }
+
+
+    @Test
+    public void testCrossOriginFilterAddedWhenOn() throws Exception {
+        // setup
+        CometdComponent component = context.getComponent("cometd", CometdComponent.class);
+        Server server = new Server();
+
+        when(endpoint.isCrossOriginFilterOn()).thenReturn(true);
+        when(endpoint.getFilterPath()).thenReturn(FILTER_PATH);
+        when(endpoint.getAllowedOrigins()).thenReturn(ALLOWED_ORIGINS);
+
+        // act
+        component.createServletForConnector(server, connector, endpoint);
+
+        // assert
+        ServletContextHandler handler = (ServletContextHandler) server.getHandler();
+        assertEquals(1, handler.getServletHandler().getFilters().length);
+
+        FilterHolder filterHolder = handler.getServletHandler().getFilters()[0];
+        Filter filter = filterHolder.getFilter();
+        assertTrue(filter instanceof CrossOriginFilter);
+    }
+
+    @Test
+    public void testCrossOriginFilterNotAddedWhenOff() throws Exception {
+        // setup
+        CometdComponent component = context.getComponent("cometd", CometdComponent.class);
+        Server server = new Server();
+
+        when(endpoint.isCrossOriginFilterOn()).thenReturn(false);
+
+        // act
+        component.createServletForConnector(server, connector, endpoint);
+
+        // assert
+        ServletContextHandler handler = (ServletContextHandler) server.getHandler();
+        assertEquals(0, handler.getServletHandler().getFilters().length);
+    }
+}
+



Mime
View raw message