knox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kmin...@apache.org
Subject git commit: KNOX-179: Simple way to introduce new provider/servlet filters into the chains
Date Tue, 26 Nov 2013 17:46:06 GMT
Updated Branches:
  refs/heads/master 4288cfe14 -> ef3ec3546


KNOX-179: Simple way to introduce new provider/servlet filters into the chains


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

Branch: refs/heads/master
Commit: ef3ec3546e3dedcc2b8c399fa386dd38b161d83b
Parents: 4288cfe
Author: Kevin Minder <kevin.minder@hortonworks.com>
Authored: Tue Nov 26 12:43:55 2013 -0500
Committer: Kevin Minder <kevin.minder@hortonworks.com>
Committed: Tue Nov 26 12:43:55 2013 -0500

----------------------------------------------------------------------
 .../gateway/deploy/DeploymentFactory.java       |  10 +-
 .../GenericProviderDeploymentContributor.java   |  93 ++++++++++++
 ...gateway.deploy.ProviderDeploymentContributor |   1 +
 ...enericProviderDeploymentContributorTest.java |  45 ++++++
 .../deploy/DeploymentFactoryFuncTest.java       | 147 ++++++++++++++++++-
 5 files changed, 287 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-knox/blob/ef3ec354/gateway-server/src/main/java/org/apache/hadoop/gateway/deploy/DeploymentFactory.java
----------------------------------------------------------------------
diff --git a/gateway-server/src/main/java/org/apache/hadoop/gateway/deploy/DeploymentFactory.java
b/gateway-server/src/main/java/org/apache/hadoop/gateway/deploy/DeploymentFactory.java
index 3ee564b..10cfb00 100644
--- a/gateway-server/src/main/java/org/apache/hadoop/gateway/deploy/DeploymentFactory.java
+++ b/gateway-server/src/main/java/org/apache/hadoop/gateway/deploy/DeploymentFactory.java
@@ -98,8 +98,7 @@ public abstract class DeploymentFactory {
   // Scan through all of the loaded providers.  For each that doesn't have an existing provider
in the role
   // list add it.
   private static Map<String,List<ProviderDeploymentContributor>> selectContextProviders(
Topology topology ) {
-    Map<String,List<ProviderDeploymentContributor>> providers
-        = new HashMap<String,List<ProviderDeploymentContributor>>();
+    Map<String,List<ProviderDeploymentContributor>> providers = new HashMap<String,List<ProviderDeploymentContributor>>();
     collectTopologyProviders( topology, providers );
     collectDefaultProviders( providers );
     return providers;
@@ -114,6 +113,13 @@ public abstract class DeploymentFactory {
         Map<String,ProviderDeploymentContributor> nameMap = PROVIDER_CONTRIBUTOR_MAP.get(
role );
         if( nameMap != null ) {
           ProviderDeploymentContributor contributor = nameMap.get( name );
+          // If there isn't a contributor with this role/name try to find a "*" contributor.
+          if( contributor == null ) {
+            nameMap = PROVIDER_CONTRIBUTOR_MAP.get( "*" );
+            if( nameMap != null ) {
+              contributor = nameMap.get( name );
+            }
+          }
           if( contributor != null ) {
             List list = defaults.get( role );
             if( list == null ) {

http://git-wip-us.apache.org/repos/asf/incubator-knox/blob/ef3ec354/gateway-server/src/main/java/org/apache/hadoop/gateway/deploy/impl/GenericProviderDeploymentContributor.java
----------------------------------------------------------------------
diff --git a/gateway-server/src/main/java/org/apache/hadoop/gateway/deploy/impl/GenericProviderDeploymentContributor.java
b/gateway-server/src/main/java/org/apache/hadoop/gateway/deploy/impl/GenericProviderDeploymentContributor.java
new file mode 100644
index 0000000..ba9733c
--- /dev/null
+++ b/gateway-server/src/main/java/org/apache/hadoop/gateway/deploy/impl/GenericProviderDeploymentContributor.java
@@ -0,0 +1,93 @@
+/**
+ * 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.hadoop.gateway.deploy.impl;
+
+import org.apache.hadoop.gateway.deploy.DeploymentContext;
+import org.apache.hadoop.gateway.deploy.ProviderDeploymentContributorBase;
+import org.apache.hadoop.gateway.descriptor.FilterDescriptor;
+import org.apache.hadoop.gateway.descriptor.FilterParamDescriptor;
+import org.apache.hadoop.gateway.descriptor.ResourceDescriptor;
+import org.apache.hadoop.gateway.topology.Provider;
+import org.apache.hadoop.gateway.topology.Service;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+public class GenericProviderDeploymentContributor extends ProviderDeploymentContributorBase
{
+
+  public static String FILTER_PARAM = "filter";
+
+  @Override
+  public String getRole() {
+    return "*";
+  }
+
+  @Override
+  public String getName() {
+    return "generic";
+  }
+
+  @Override
+  public void contributeFilter(
+      DeploymentContext context,
+      Provider provider,
+      Service service,
+      ResourceDescriptor resource,
+      List<FilterParamDescriptor> params ) {
+    String filterClassName = getFilterClassName( provider.getParams() );
+    if( filterClassName != null ) {
+      FilterDescriptor filter = resource.addFilter();
+      filter.name( getName() );
+      filter.role( provider.getRole() );
+      filter.impl( filterClassName );
+      filter.params( getFilterInitParams( filter, provider.getParams(), params ) );
+    }
+  }
+
+  private String getFilterClassName( Map<String,String> params ) {
+    String filterClassName = null;
+    if( params != null ) {
+      filterClassName = params.get( FILTER_PARAM );
+    }
+    if( filterClassName == null ) {
+      throw new IllegalArgumentException( FILTER_PARAM + "==null" );
+    }
+    return filterClassName;
+  }
+
+  private List<FilterParamDescriptor> getFilterInitParams(
+      FilterDescriptor filter,
+      Map<String,String> providerParams,
+      List<FilterParamDescriptor> filterParams ) {
+    List<FilterParamDescriptor> aggregateParams = new ArrayList<FilterParamDescriptor>();
+    if( providerParams != null ) {
+      for( Map.Entry<String,String> param : providerParams.entrySet() ) {
+        String name = param.getKey();
+        if( !FILTER_PARAM.equalsIgnoreCase( name ) ) {
+          aggregateParams.add( filter.createParam().name( name ).value( param.getValue()
) );
+        }
+      }
+    }
+    if( filterParams != null ) {
+      aggregateParams.addAll( filterParams );
+    }
+    return aggregateParams;
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-knox/blob/ef3ec354/gateway-server/src/main/resources/META-INF/services/org.apache.hadoop.gateway.deploy.ProviderDeploymentContributor
----------------------------------------------------------------------
diff --git a/gateway-server/src/main/resources/META-INF/services/org.apache.hadoop.gateway.deploy.ProviderDeploymentContributor
b/gateway-server/src/main/resources/META-INF/services/org.apache.hadoop.gateway.deploy.ProviderDeploymentContributor
index b4172eb..4ac8cbf 100644
--- a/gateway-server/src/main/resources/META-INF/services/org.apache.hadoop.gateway.deploy.ProviderDeploymentContributor
+++ b/gateway-server/src/main/resources/META-INF/services/org.apache.hadoop.gateway.deploy.ProviderDeploymentContributor
@@ -16,5 +16,6 @@
 # limitations under the License.
 ##########################################################################
 
+org.apache.hadoop.gateway.deploy.impl.GenericProviderDeploymentContributor
 org.apache.hadoop.gateway.deploy.impl.DispatchDeploymentContributor
 org.apache.hadoop.gateway.deploy.impl.WebHdfsDispatchDeploymentContributor

http://git-wip-us.apache.org/repos/asf/incubator-knox/blob/ef3ec354/gateway-server/src/test/java/org/apache/hadoop/gateway/deploy/impl/GenericProviderDeploymentContributorTest.java
----------------------------------------------------------------------
diff --git a/gateway-server/src/test/java/org/apache/hadoop/gateway/deploy/impl/GenericProviderDeploymentContributorTest.java
b/gateway-server/src/test/java/org/apache/hadoop/gateway/deploy/impl/GenericProviderDeploymentContributorTest.java
new file mode 100644
index 0000000..007dc7d
--- /dev/null
+++ b/gateway-server/src/test/java/org/apache/hadoop/gateway/deploy/impl/GenericProviderDeploymentContributorTest.java
@@ -0,0 +1,45 @@
+/**
+ * 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.hadoop.gateway.deploy.impl;
+
+import org.apache.hadoop.gateway.deploy.ProviderDeploymentContributor;
+import org.junit.Test;
+
+import java.util.Iterator;
+import java.util.ServiceLoader;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.fail;
+
+public class GenericProviderDeploymentContributorTest {
+
+  @Test
+  public void testServiceLoader() throws Exception {
+    ServiceLoader loader = ServiceLoader.load( ProviderDeploymentContributor.class );
+    Iterator iterator = loader.iterator();
+    assertThat( "Service iterator empty.", iterator.hasNext() );
+    while( iterator.hasNext() ) {
+      Object object = iterator.next();
+      if( object instanceof GenericProviderDeploymentContributor ) {
+        return;
+      }
+    }
+    fail( "Failed to find " + GenericProviderDeploymentContributor.class.getName() + " via
service loader." );
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-knox/blob/ef3ec354/gateway-test/src/test/java/org/apache/hadoop/gateway/deploy/DeploymentFactoryFuncTest.java
----------------------------------------------------------------------
diff --git a/gateway-test/src/test/java/org/apache/hadoop/gateway/deploy/DeploymentFactoryFuncTest.java
b/gateway-test/src/test/java/org/apache/hadoop/gateway/deploy/DeploymentFactoryFuncTest.java
index d2e1ca8..e4305f3 100644
--- a/gateway-test/src/test/java/org/apache/hadoop/gateway/deploy/DeploymentFactoryFuncTest.java
+++ b/gateway-test/src/test/java/org/apache/hadoop/gateway/deploy/DeploymentFactoryFuncTest.java
@@ -25,6 +25,8 @@ import org.apache.hadoop.gateway.topology.Provider;
 import org.apache.hadoop.gateway.topology.ProviderParam;
 import org.apache.hadoop.gateway.topology.Service;
 import org.apache.hadoop.gateway.topology.Topology;
+import org.apache.hadoop.test.log.NoOpAppender;
+import org.apache.log4j.Appender;
 import org.jboss.shrinkwrap.api.spec.WebArchive;
 import org.junit.Test;
 import org.w3c.dom.Document;
@@ -34,10 +36,18 @@ import org.xml.sax.SAXException;
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.StringWriter;
 import java.net.URISyntaxException;
+import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.UUID;
@@ -45,10 +55,123 @@ import java.util.UUID;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.core.IsEqual.equalTo;
 import static org.hamcrest.xml.HasXPath.hasXPath;
+import static org.junit.Assert.fail;
 
 public class DeploymentFactoryFuncTest {
 
   @Test
+  public void testGenericProviderDeploymentContributor() throws ParserConfigurationException,
SAXException, IOException, TransformerException {
+    GatewayConfig config = new GatewayTestConfig();
+    File targetDir = new File( System.getProperty( "user.dir" ), "target" );
+    File gatewayDir = new File( targetDir, "gateway-home-" + UUID.randomUUID() );
+    gatewayDir.mkdirs();
+    File deployDir = new File( gatewayDir, "clusters" );
+    deployDir.mkdirs();
+
+    ((GatewayTestConfig) config).setGatewayHomeDir( gatewayDir.getAbsolutePath() );
+    ((GatewayTestConfig) config).setDeploymentDir( "clusters" );
+
+    DefaultGatewayServices srvcs = new DefaultGatewayServices();
+    Map<String,String> options = new HashMap<String,String>();
+    options.put("persist-master", "false");
+    options.put("master", "password");
+    try {
+      DeploymentFactory.setGatewayServices(srvcs);
+      srvcs.init(config, options);
+    } catch (ServiceLifecycleException e) {
+      e.printStackTrace(); // I18N not required.
+    }
+
+    Topology topology = new Topology();
+    topology.setName( "test-cluster" );
+    Service service = new Service();
+    service.setRole( "WEBHDFS" );
+    service.setUrl( "http://localhost:50070/test-service-url" );
+    topology.addService( service );
+
+    Provider provider = new Provider();
+    provider.setRole( "authentication" );
+    provider.setName( "generic" );
+    provider.setEnabled( true );
+    ProviderParam param = new ProviderParam();
+    param.setName( "filter" );
+    param.setValue( "org.opensource.ExistingFilter" );
+    provider.addParam( param );
+    param = new ProviderParam();
+    param.setName( "test-param-name" );
+    param.setValue( "test-param-value" );
+    provider.addParam( param );
+    topology.addProvider( provider );
+
+    WebArchive war = DeploymentFactory.createDeployment( config, topology );
+
+    Document gateway = parse( war.get( "WEB-INF/gateway.xml" ).getAsset().openStream() );
+    //dump( gateway );
+
+    assertThat( gateway, hasXPath( "/gateway/resource[1]/filter[1]/role", equalTo( "authentication"
) ) );
+    assertThat( gateway, hasXPath( "/gateway/resource[1]/filter[1]/name", equalTo( "generic"
) ) );
+    assertThat( gateway, hasXPath( "/gateway/resource[1]/filter[1]/class", equalTo( "org.opensource.ExistingFilter"
) ) );
+    assertThat( gateway, hasXPath( "/gateway/resource[1]/filter[1]/param[1]/name", equalTo(
"test-param-name" ) ) );
+    assertThat( gateway, hasXPath( "/gateway/resource[1]/filter[1]/param[1]/value", equalTo(
"test-param-value" ) ) );
+  }
+
+  @Test
+  public void testInvalidGenericProviderDeploymentContributor() throws ParserConfigurationException,
SAXException, IOException, TransformerException {
+    GatewayConfig config = new GatewayTestConfig();
+    File targetDir = new File( System.getProperty( "user.dir" ), "target" );
+    File gatewayDir = new File( targetDir, "gateway-home-" + UUID.randomUUID() );
+    gatewayDir.mkdirs();
+    File deployDir = new File( gatewayDir, "clusters" );
+    deployDir.mkdirs();
+
+    ((GatewayTestConfig) config).setGatewayHomeDir( gatewayDir.getAbsolutePath() );
+    ((GatewayTestConfig) config).setDeploymentDir( "clusters" );
+
+    DefaultGatewayServices srvcs = new DefaultGatewayServices();
+    Map<String,String> options = new HashMap<String,String>();
+    options.put("persist-master", "false");
+    options.put("master", "password");
+    try {
+      DeploymentFactory.setGatewayServices(srvcs);
+      srvcs.init(config, options);
+    } catch (ServiceLifecycleException e) {
+      e.printStackTrace(); // I18N not required.
+    }
+
+    Topology topology = new Topology();
+    topology.setName( "test-cluster" );
+    Service service = new Service();
+    service.setRole( "WEBHDFS" );
+    service.setUrl( "http://localhost:50070/test-service-url" );
+    topology.addService( service );
+
+    Provider provider = new Provider();
+    provider.setRole( "authentication" );
+    provider.setName( "generic" );
+    provider.setEnabled( true );
+    ProviderParam param; // = new ProviderParam();
+    // Missing filter param.
+    //param.setName( "filter" );
+    //param.setValue( "org.opensource.ExistingFilter" );
+    //provider.addParam( param );
+    param = new ProviderParam();
+    param.setName( "test-param-name" );
+    param.setValue( "test-param-value" );
+    provider.addParam( param );
+    topology.addProvider( provider );
+
+    Enumeration<Appender> appenders = NoOpAppender.setUp();
+    try {
+      DeploymentFactory.createDeployment( config, topology );
+      fail( "Should have throws IllegalArgumentException" );
+    } catch ( DeploymentException e ) {
+      // Expected.
+    } finally {
+      NoOpAppender.tearDown( appenders );
+    }
+  }
+
+  @Test
   public void testSimpleTopology() throws IOException, SAXException, ParserConfigurationException,
URISyntaxException {
     GatewayConfig config = new GatewayTestConfig();
     File targetDir = new File( System.getProperty( "user.dir" ), "target" );
@@ -101,13 +224,13 @@ public class DeploymentFactoryFuncTest {
     //File dir = new File( System.getProperty( "user.dir" ) );
     //File file = war.as( ExplodedExporter.class ).exportExploded( dir, "test-cluster.war"
);
 
-    Document wad = parse( war.get( "WEB-INF/web.xml" ).getAsset().openStream() );
-    assertThat( wad, hasXPath( "/web-app/servlet/servlet-name", equalTo( "test-cluster" )
) );
-    assertThat( wad, hasXPath( "/web-app/servlet/servlet-class", equalTo( "org.apache.hadoop.gateway.GatewayServlet"
) ) );
-    assertThat( wad, hasXPath( "/web-app/servlet/init-param/param-name", equalTo( "gatewayDescriptorLocation"
) ) );
-    assertThat( wad, hasXPath( "/web-app/servlet/init-param/param-value", equalTo( "gateway.xml"
) ) );
-    assertThat( wad, hasXPath( "/web-app/servlet-mapping/servlet-name", equalTo( "test-cluster"
) ) );
-    assertThat( wad, hasXPath( "/web-app/servlet-mapping/url-pattern", equalTo( "/*" ) )
);
+    Document web = parse( war.get( "WEB-INF/web.xml" ).getAsset().openStream() );
+    assertThat( web, hasXPath( "/web-app/servlet/servlet-name", equalTo( "test-cluster" )
) );
+    assertThat( web, hasXPath( "/web-app/servlet/servlet-class", equalTo( "org.apache.hadoop.gateway.GatewayServlet"
) ) );
+    assertThat( web, hasXPath( "/web-app/servlet/init-param/param-name", equalTo( "gatewayDescriptorLocation"
) ) );
+    assertThat( web, hasXPath( "/web-app/servlet/init-param/param-value", equalTo( "gateway.xml"
) ) );
+    assertThat( web, hasXPath( "/web-app/servlet-mapping/servlet-name", equalTo( "test-cluster"
) ) );
+    assertThat( web, hasXPath( "/web-app/servlet-mapping/url-pattern", equalTo( "/*" ) )
);
 
     Document gateway = parse( war.get( "WEB-INF/gateway.xml" ).getAsset().openStream() );
 
@@ -171,4 +294,14 @@ public class DeploymentFactoryFuncTest {
     return builder.parse( source );
   }
 
+//  private void dump( Document document ) throws TransformerException {
+//    Transformer transformer = TransformerFactory.newInstance().newTransformer();
+//    transformer.setOutputProperty( OutputKeys.INDENT, "yes" );
+//    StreamResult result = new StreamResult( new StringWriter() );
+//    DOMSource source = new DOMSource( document );
+//    transformer.transform( source, result );
+//    String xmlString = result.getWriter().toString();
+//    System.out.println( xmlString );
+//  }
+
 }


Mime
View raw message