You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@knox.apache.org by km...@apache.org on 2013/09/13 03:00:37 UTC
[3/5] KNOX-118: Add rewrite functions to allow use of service
registry within rewrite rules. Also includes initial integration of this with
Oozie.
http://git-wip-us.apache.org/repos/asf/incubator-knox/blob/2f135e16/gateway-provider-rewrite-func-service-registry/src/test/java/org/apache/hadoop/gateway/svcregfunc/impl/ServiceAddressFunctionProcessorTest.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite-func-service-registry/src/test/java/org/apache/hadoop/gateway/svcregfunc/impl/ServiceAddressFunctionProcessorTest.java b/gateway-provider-rewrite-func-service-registry/src/test/java/org/apache/hadoop/gateway/svcregfunc/impl/ServiceAddressFunctionProcessorTest.java
new file mode 100644
index 0000000..21f2bf5
--- /dev/null
+++ b/gateway-provider-rewrite-func-service-registry/src/test/java/org/apache/hadoop/gateway/svcregfunc/impl/ServiceAddressFunctionProcessorTest.java
@@ -0,0 +1,134 @@
+/**
+ * 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.svcregfunc.impl;
+
+import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteEnvironment;
+import org.apache.hadoop.gateway.filter.rewrite.spi.UrlRewriteContext;
+import org.apache.hadoop.gateway.filter.rewrite.spi.UrlRewriteFunctionProcessor;
+import org.apache.hadoop.gateway.services.GatewayServices;
+import org.apache.hadoop.gateway.services.registry.ServiceRegistry;
+import org.apache.hadoop.gateway.svcregfunc.api.ServiceAddressFunctionDescriptor;
+import org.apache.hadoop.gateway.svcregfunc.api.ServiceSchemeFunctionDescriptor;
+import org.easymock.EasyMock;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.Iterator;
+import java.util.ServiceLoader;
+
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.nullValue;
+import static org.hamcrest.CoreMatchers.sameInstance;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.fail;
+
+public class ServiceAddressFunctionProcessorTest {
+
+ ServiceRegistry reg;
+ GatewayServices svc;
+ UrlRewriteEnvironment env;
+ UrlRewriteContext ctx;
+ ServiceAddressFunctionDescriptor desc;
+
+ @Before
+ public void setUp() {
+ reg = EasyMock.createNiceMock( ServiceRegistry.class );
+ EasyMock.expect( reg.lookupServiceURL( "test-cluster", "test-service" ) ).andReturn( "test-scheme://test-host:777/test-path" ).anyTimes();
+
+ svc = EasyMock.createNiceMock( GatewayServices.class );
+ EasyMock.expect( svc.getService( GatewayServices.SERVICE_REGISTRY_SERVICE ) ).andReturn( reg ).anyTimes();
+
+ env = EasyMock.createNiceMock( UrlRewriteEnvironment.class );
+ EasyMock.expect( env.getAttribute( GatewayServices.GATEWAY_SERVICES_ATTRIBUTE ) ).andReturn( svc ).anyTimes();
+ EasyMock.expect( env.getAttribute( GatewayServices.GATEWAY_CLUSTER_ATTRIBUTE ) ).andReturn( "test-cluster" ).anyTimes();
+
+ ctx = EasyMock.createNiceMock( UrlRewriteContext.class );
+
+ desc = EasyMock.createNiceMock( ServiceAddressFunctionDescriptor.class );
+
+ EasyMock.replay( reg, svc, env, desc, ctx );
+ }
+
+ @Test
+ public void testServiceLoader() throws Exception {
+ ServiceLoader loader = ServiceLoader.load( UrlRewriteFunctionProcessor.class );
+ Iterator iterator = loader.iterator();
+ assertThat( "Service iterator empty.", iterator.hasNext() );
+ while( iterator.hasNext() ) {
+ Object object = iterator.next();
+ if( object instanceof ServiceAddressFunctionProcessor ) {
+ return;
+ }
+ }
+ fail( "Failed to find " + ServiceAddressFunctionProcessor.class.getName() + " via service loader." );
+ }
+
+ @Test
+ public void testName() throws Exception {
+ ServiceAddressFunctionProcessor func = new ServiceAddressFunctionProcessor();
+ assertThat( func.name(), is( "serviceAddr" ) );
+ }
+
+ @Test
+ public void testInitialize() throws Exception {
+ ServiceAddressFunctionProcessor func = new ServiceAddressFunctionProcessor();
+ try {
+ func.initialize( null, desc );
+ fail( "Should have thrown an IllegalArgumentException" );
+ } catch( IllegalArgumentException e ) {
+ assertThat( e.getMessage(), containsString( "environment" ) );
+ }
+
+ func = new ServiceAddressFunctionProcessor();
+ try {
+ func.initialize( env, null );
+ } catch( Exception e ) {
+ e.printStackTrace();
+ fail( "Should not have thrown an exception" );
+ }
+
+ func.initialize( env, desc );
+
+ assertThat( func.cluster(), is( "test-cluster" ) );
+ assertThat( func.registry(), sameInstance( reg ) );
+ }
+
+ @Test
+ public void testDestroy() throws Exception {
+ ServiceAddressFunctionProcessor func = new ServiceAddressFunctionProcessor();
+ func.initialize( env, desc );
+ func.destroy();
+
+ assertThat( func.cluster(), nullValue() );
+ assertThat( func.registry(), nullValue() );
+ }
+
+ @Test
+ public void testResolve() throws Exception {
+ ServiceAddressFunctionProcessor func = new ServiceAddressFunctionProcessor();
+ func.initialize( env, desc );
+
+ assertThat( func.resolve( ctx, "test-service" ), is( "test-host:777" ) );
+ assertThat( func.resolve( ctx, "invalid-test-service" ), is( "invalid-test-service" ) );
+ assertThat( func.resolve( ctx, null ), nullValue() );
+
+ func.destroy();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-knox/blob/2f135e16/gateway-provider-rewrite-func-service-registry/src/test/java/org/apache/hadoop/gateway/svcregfunc/impl/ServiceHostFunctionProcessorTest.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite-func-service-registry/src/test/java/org/apache/hadoop/gateway/svcregfunc/impl/ServiceHostFunctionProcessorTest.java b/gateway-provider-rewrite-func-service-registry/src/test/java/org/apache/hadoop/gateway/svcregfunc/impl/ServiceHostFunctionProcessorTest.java
new file mode 100644
index 0000000..9544174
--- /dev/null
+++ b/gateway-provider-rewrite-func-service-registry/src/test/java/org/apache/hadoop/gateway/svcregfunc/impl/ServiceHostFunctionProcessorTest.java
@@ -0,0 +1,135 @@
+/**
+ * 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.svcregfunc.impl;
+
+import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteEnvironment;
+import org.apache.hadoop.gateway.filter.rewrite.spi.UrlRewriteContext;
+import org.apache.hadoop.gateway.filter.rewrite.spi.UrlRewriteFunctionProcessor;
+import org.apache.hadoop.gateway.services.GatewayServices;
+import org.apache.hadoop.gateway.services.registry.ServiceRegistry;
+import org.apache.hadoop.gateway.svcregfunc.api.ServiceAddressFunctionDescriptor;
+import org.apache.hadoop.gateway.svcregfunc.api.ServiceHostFunctionDescriptor;
+import org.apache.hadoop.gateway.svcregfunc.api.ServiceSchemeFunctionDescriptor;
+import org.easymock.EasyMock;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.Iterator;
+import java.util.ServiceLoader;
+
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.nullValue;
+import static org.hamcrest.CoreMatchers.sameInstance;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.fail;
+
+public class ServiceHostFunctionProcessorTest {
+
+ ServiceRegistry reg;
+ GatewayServices svc;
+ UrlRewriteEnvironment env;
+ UrlRewriteContext ctx;
+ ServiceHostFunctionDescriptor desc;
+
+ @Before
+ public void setUp() {
+ reg = EasyMock.createNiceMock( ServiceRegistry.class );
+ EasyMock.expect( reg.lookupServiceURL( "test-cluster", "test-service" ) ).andReturn( "test-scheme://test-host:777/test-path" ).anyTimes();
+
+ svc = EasyMock.createNiceMock( GatewayServices.class );
+ EasyMock.expect( svc.getService( GatewayServices.SERVICE_REGISTRY_SERVICE ) ).andReturn( reg ).anyTimes();
+
+ env = EasyMock.createNiceMock( UrlRewriteEnvironment.class );
+ EasyMock.expect( env.getAttribute( GatewayServices.GATEWAY_SERVICES_ATTRIBUTE ) ).andReturn( svc ).anyTimes();
+ EasyMock.expect( env.getAttribute( GatewayServices.GATEWAY_CLUSTER_ATTRIBUTE ) ).andReturn( "test-cluster" ).anyTimes();
+
+ ctx = EasyMock.createNiceMock( UrlRewriteContext.class );
+
+ desc = EasyMock.createNiceMock( ServiceHostFunctionDescriptor.class );
+
+ EasyMock.replay( reg, svc, env, desc, ctx );
+ }
+
+ @Test
+ public void testServiceLoader() throws Exception {
+ ServiceLoader loader = ServiceLoader.load( UrlRewriteFunctionProcessor.class );
+ Iterator iterator = loader.iterator();
+ assertThat( "Service iterator empty.", iterator.hasNext() );
+ while( iterator.hasNext() ) {
+ Object object = iterator.next();
+ if( object instanceof ServiceHostFunctionProcessor ) {
+ return;
+ }
+ }
+ fail( "Failed to find " + ServiceHostFunctionProcessor.class.getName() + " via service loader." );
+ }
+
+ @Test
+ public void testName() throws Exception {
+ ServiceHostFunctionProcessor func = new ServiceHostFunctionProcessor();
+ assertThat( func.name(), is( "serviceHost" ) );
+ }
+
+ @Test
+ public void testInitialize() throws Exception {
+ ServiceHostFunctionProcessor func = new ServiceHostFunctionProcessor();
+ try {
+ func.initialize( null, desc );
+ fail( "Should have thrown an IllegalArgumentException" );
+ } catch( IllegalArgumentException e ) {
+ assertThat( e.getMessage(), containsString( "environment" ) );
+ }
+
+ func = new ServiceHostFunctionProcessor();
+ try {
+ func.initialize( env, null );
+ } catch( Exception e ) {
+ e.printStackTrace();
+ fail( "Should not have thrown an exception" );
+ }
+
+ func.initialize( env, desc );
+
+ assertThat( func.cluster(), is( "test-cluster" ) );
+ assertThat( func.registry(), sameInstance( reg ) );
+ }
+
+ @Test
+ public void testDestroy() throws Exception {
+ ServiceHostFunctionProcessor func = new ServiceHostFunctionProcessor();
+ func.initialize( env, desc );
+ func.destroy();
+
+ assertThat( func.cluster(), nullValue() );
+ assertThat( func.registry(), nullValue() );
+ }
+
+ @Test
+ public void testResolve() throws Exception {
+ ServiceHostFunctionProcessor func = new ServiceHostFunctionProcessor();
+ func.initialize( env, desc );
+
+ assertThat( func.resolve( ctx, "test-service" ), is( "test-host" ) );
+ assertThat( func.resolve( ctx, "invalid-test-service" ), is( "invalid-test-service" ) );
+ assertThat( func.resolve( ctx, null ), nullValue() );
+
+ func.destroy();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-knox/blob/2f135e16/gateway-provider-rewrite-func-service-registry/src/test/java/org/apache/hadoop/gateway/svcregfunc/impl/ServicePathFunctionProcessorTest.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite-func-service-registry/src/test/java/org/apache/hadoop/gateway/svcregfunc/impl/ServicePathFunctionProcessorTest.java b/gateway-provider-rewrite-func-service-registry/src/test/java/org/apache/hadoop/gateway/svcregfunc/impl/ServicePathFunctionProcessorTest.java
new file mode 100644
index 0000000..ce554ad
--- /dev/null
+++ b/gateway-provider-rewrite-func-service-registry/src/test/java/org/apache/hadoop/gateway/svcregfunc/impl/ServicePathFunctionProcessorTest.java
@@ -0,0 +1,135 @@
+/**
+ * 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.svcregfunc.impl;
+
+import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteEnvironment;
+import org.apache.hadoop.gateway.filter.rewrite.spi.UrlRewriteContext;
+import org.apache.hadoop.gateway.filter.rewrite.spi.UrlRewriteFunctionProcessor;
+import org.apache.hadoop.gateway.services.GatewayServices;
+import org.apache.hadoop.gateway.services.registry.ServiceRegistry;
+import org.apache.hadoop.gateway.svcregfunc.api.ServiceAddressFunctionDescriptor;
+import org.apache.hadoop.gateway.svcregfunc.api.ServicePathFunctionDescriptor;
+import org.apache.hadoop.gateway.svcregfunc.api.ServiceSchemeFunctionDescriptor;
+import org.easymock.EasyMock;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.Iterator;
+import java.util.ServiceLoader;
+
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.nullValue;
+import static org.hamcrest.CoreMatchers.sameInstance;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.fail;
+
+public class ServicePathFunctionProcessorTest {
+
+ ServiceRegistry reg;
+ GatewayServices svc;
+ UrlRewriteEnvironment env;
+ UrlRewriteContext ctx;
+ ServicePathFunctionDescriptor desc;
+
+ @Before
+ public void setUp() {
+ reg = EasyMock.createNiceMock( ServiceRegistry.class );
+ EasyMock.expect( reg.lookupServiceURL( "test-cluster", "test-service" ) ).andReturn( "test-scheme://test-host:777/test-path" ).anyTimes();
+
+ svc = EasyMock.createNiceMock( GatewayServices.class );
+ EasyMock.expect( svc.getService( GatewayServices.SERVICE_REGISTRY_SERVICE ) ).andReturn( reg ).anyTimes();
+
+ env = EasyMock.createNiceMock( UrlRewriteEnvironment.class );
+ EasyMock.expect( env.getAttribute( GatewayServices.GATEWAY_SERVICES_ATTRIBUTE ) ).andReturn( svc ).anyTimes();
+ EasyMock.expect( env.getAttribute( GatewayServices.GATEWAY_CLUSTER_ATTRIBUTE ) ).andReturn( "test-cluster" ).anyTimes();
+
+ ctx = EasyMock.createNiceMock( UrlRewriteContext.class );
+
+ desc = EasyMock.createNiceMock( ServicePathFunctionDescriptor.class );
+
+ EasyMock.replay( reg, svc, env, desc, ctx );
+ }
+
+ @Test
+ public void testServiceLoader() throws Exception {
+ ServiceLoader loader = ServiceLoader.load( UrlRewriteFunctionProcessor.class );
+ Iterator iterator = loader.iterator();
+ assertThat( "Service iterator empty.", iterator.hasNext() );
+ while( iterator.hasNext() ) {
+ Object object = iterator.next();
+ if( object instanceof ServicePathFunctionProcessor ) {
+ return;
+ }
+ }
+ fail( "Failed to find " + ServicePathFunctionProcessor.class.getName() + " via service loader." );
+ }
+
+ @Test
+ public void testName() throws Exception {
+ ServicePathFunctionProcessor func = new ServicePathFunctionProcessor();
+ assertThat( func.name(), is( "servicePath" ) );
+ }
+
+ @Test
+ public void testInitialize() throws Exception {
+ ServicePathFunctionProcessor func = new ServicePathFunctionProcessor();
+ try {
+ func.initialize( null, desc );
+ fail( "Should have thrown an IllegalArgumentException" );
+ } catch( IllegalArgumentException e ) {
+ assertThat( e.getMessage(), containsString( "environment" ) );
+ }
+
+ func = new ServicePathFunctionProcessor();
+ try {
+ func.initialize( env, null );
+ } catch( Exception e ) {
+ e.printStackTrace();
+ fail( "Should not have thrown an exception" );
+ }
+
+ func.initialize( env, desc );
+
+ assertThat( func.cluster(), is( "test-cluster" ) );
+ assertThat( func.registry(), sameInstance( reg ) );
+ }
+
+ @Test
+ public void testDestroy() throws Exception {
+ ServicePathFunctionProcessor func = new ServicePathFunctionProcessor();
+ func.initialize( env, desc );
+ func.destroy();
+
+ assertThat( func.cluster(), nullValue() );
+ assertThat( func.registry(), nullValue() );
+ }
+
+ @Test
+ public void testResolve() throws Exception {
+ ServicePathFunctionProcessor func = new ServicePathFunctionProcessor();
+ func.initialize( env, desc );
+
+// assertThat( func.resolve( ctx, "test-service" ), is( "/test-path" ) );
+ assertThat( func.resolve( ctx, "invalid-test-service" ), is( "invalid-test-service" ) );
+// assertThat( func.resolve( ctx, null ), nullValue() );
+
+ func.destroy();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-knox/blob/2f135e16/gateway-provider-rewrite-func-service-registry/src/test/java/org/apache/hadoop/gateway/svcregfunc/impl/ServicePortFunctionProcessorTest.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite-func-service-registry/src/test/java/org/apache/hadoop/gateway/svcregfunc/impl/ServicePortFunctionProcessorTest.java b/gateway-provider-rewrite-func-service-registry/src/test/java/org/apache/hadoop/gateway/svcregfunc/impl/ServicePortFunctionProcessorTest.java
new file mode 100644
index 0000000..29c993d
--- /dev/null
+++ b/gateway-provider-rewrite-func-service-registry/src/test/java/org/apache/hadoop/gateway/svcregfunc/impl/ServicePortFunctionProcessorTest.java
@@ -0,0 +1,135 @@
+/**
+ * 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.svcregfunc.impl;
+
+import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteEnvironment;
+import org.apache.hadoop.gateway.filter.rewrite.spi.UrlRewriteContext;
+import org.apache.hadoop.gateway.filter.rewrite.spi.UrlRewriteFunctionProcessor;
+import org.apache.hadoop.gateway.services.GatewayServices;
+import org.apache.hadoop.gateway.services.registry.ServiceRegistry;
+import org.apache.hadoop.gateway.svcregfunc.api.ServiceAddressFunctionDescriptor;
+import org.apache.hadoop.gateway.svcregfunc.api.ServicePortFunctionDescriptor;
+import org.apache.hadoop.gateway.svcregfunc.api.ServiceSchemeFunctionDescriptor;
+import org.easymock.EasyMock;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.Iterator;
+import java.util.ServiceLoader;
+
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.nullValue;
+import static org.hamcrest.CoreMatchers.sameInstance;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.fail;
+
+public class ServicePortFunctionProcessorTest {
+
+ ServiceRegistry reg;
+ GatewayServices svc;
+ UrlRewriteEnvironment env;
+ UrlRewriteContext ctx;
+ ServicePortFunctionDescriptor desc;
+
+ @Before
+ public void setUp() {
+ reg = EasyMock.createNiceMock( ServiceRegistry.class );
+ EasyMock.expect( reg.lookupServiceURL( "test-cluster", "test-service" ) ).andReturn( "test-scheme://test-host:777/test-path" ).anyTimes();
+
+ svc = EasyMock.createNiceMock( GatewayServices.class );
+ EasyMock.expect( svc.getService( GatewayServices.SERVICE_REGISTRY_SERVICE ) ).andReturn( reg ).anyTimes();
+
+ env = EasyMock.createNiceMock( UrlRewriteEnvironment.class );
+ EasyMock.expect( env.getAttribute( GatewayServices.GATEWAY_SERVICES_ATTRIBUTE ) ).andReturn( svc ).anyTimes();
+ EasyMock.expect( env.getAttribute( GatewayServices.GATEWAY_CLUSTER_ATTRIBUTE ) ).andReturn( "test-cluster" ).anyTimes();
+
+ ctx = EasyMock.createNiceMock( UrlRewriteContext.class );
+
+ desc = EasyMock.createNiceMock( ServicePortFunctionDescriptor.class );
+
+ EasyMock.replay( reg, svc, env, desc, ctx );
+ }
+
+ @Test
+ public void testServiceLoader() throws Exception {
+ ServiceLoader loader = ServiceLoader.load( UrlRewriteFunctionProcessor.class );
+ Iterator iterator = loader.iterator();
+ assertThat( "Service iterator empty.", iterator.hasNext() );
+ while( iterator.hasNext() ) {
+ Object object = iterator.next();
+ if( object instanceof ServicePortFunctionProcessor ) {
+ return;
+ }
+ }
+ fail( "Failed to find " + ServicePortFunctionProcessor.class.getName() + " via service loader." );
+ }
+
+ @Test
+ public void testName() throws Exception {
+ ServicePortFunctionProcessor func = new ServicePortFunctionProcessor();
+ assertThat( func.name(), is( "servicePort" ) );
+ }
+
+ @Test
+ public void testInitialize() throws Exception {
+ ServicePortFunctionProcessor func = new ServicePortFunctionProcessor();
+ try {
+ func.initialize( null, desc );
+ fail( "Should have thrown an IllegalArgumentException" );
+ } catch( IllegalArgumentException e ) {
+ assertThat( e.getMessage(), containsString( "environment" ) );
+ }
+
+ func = new ServicePortFunctionProcessor();
+ try {
+ func.initialize( env, null );
+ } catch( Exception e ) {
+ e.printStackTrace();
+ fail( "Should not have thrown an exception" );
+ }
+
+ func.initialize( env, desc );
+
+ assertThat( func.cluster(), is( "test-cluster" ) );
+ assertThat( func.registry(), sameInstance( reg ) );
+ }
+
+ @Test
+ public void testDestroy() throws Exception {
+ ServicePortFunctionProcessor func = new ServicePortFunctionProcessor();
+ func.initialize( env, desc );
+ func.destroy();
+
+ assertThat( func.cluster(), nullValue() );
+ assertThat( func.registry(), nullValue() );
+ }
+
+ @Test
+ public void testResolve() throws Exception {
+ ServicePortFunctionProcessor func = new ServicePortFunctionProcessor();
+ func.initialize( env, desc );
+
+ assertThat( func.resolve( ctx, "test-service" ), is( "777" ) );
+ assertThat( func.resolve( ctx, "invalid-test-service" ), is( "invalid-test-service" ) );
+ assertThat( func.resolve( ctx, null ), nullValue() );
+
+ func.destroy();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-knox/blob/2f135e16/gateway-provider-rewrite-func-service-registry/src/test/java/org/apache/hadoop/gateway/svcregfunc/impl/ServiceRegistryFunctionsTest.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite-func-service-registry/src/test/java/org/apache/hadoop/gateway/svcregfunc/impl/ServiceRegistryFunctionsTest.java b/gateway-provider-rewrite-func-service-registry/src/test/java/org/apache/hadoop/gateway/svcregfunc/impl/ServiceRegistryFunctionsTest.java
new file mode 100644
index 0000000..c13d302
--- /dev/null
+++ b/gateway-provider-rewrite-func-service-registry/src/test/java/org/apache/hadoop/gateway/svcregfunc/impl/ServiceRegistryFunctionsTest.java
@@ -0,0 +1,233 @@
+/**
+ * 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.svcregfunc.impl;
+
+import org.apache.hadoop.gateway.filter.AbstractGatewayFilter;
+import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteEnvironment;
+import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteServletContextListener;
+import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteServletFilter;
+import org.apache.hadoop.gateway.filter.rewrite.spi.UrlRewriteContext;
+import org.apache.hadoop.gateway.services.GatewayServices;
+import org.apache.hadoop.gateway.services.registry.ServiceRegistry;
+import org.apache.hadoop.gateway.util.urltemplate.Parser;
+import org.apache.hadoop.test.TestUtils;
+import org.apache.hadoop.test.log.NoOpLogger;
+import org.apache.hadoop.test.mock.MockInteraction;
+import org.apache.hadoop.test.mock.MockServlet;
+import org.apache.http.auth.BasicUserPrincipal;
+import org.easymock.EasyMock;
+import org.eclipse.jetty.servlet.FilterHolder;
+import org.eclipse.jetty.servlet.ServletHolder;
+import org.eclipse.jetty.testing.HttpTester;
+import org.eclipse.jetty.testing.ServletTester;
+import org.eclipse.jetty.util.ArrayQueue;
+import org.eclipse.jetty.util.log.Log;
+import org.hamcrest.core.Is;
+import org.junit.Test;
+
+import javax.security.auth.Subject;
+import javax.servlet.DispatcherType;
+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.HttpServletRequest;
+import java.io.IOException;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.nio.charset.Charset;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
+import java.util.EnumSet;
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+
+
+public class ServiceRegistryFunctionsTest {
+
+ private ServletTester server;
+ private HttpTester request;
+ private HttpTester response;
+ private ArrayQueue<MockInteraction> interactions;
+ private MockInteraction interaction;
+
+ private static URL getTestResource( String name ) {
+ name = ServiceRegistryFunctionsTest.class.getName().replaceAll( "\\.", "/" ) + "/" + name;
+ URL url = ClassLoader.getSystemResource( name );
+ return url;
+ }
+
+ public void setUp( String username, Map<String,String> initParams ) throws Exception {
+ ServiceRegistry mockServiceRegistry = EasyMock.createNiceMock( ServiceRegistry.class );
+ EasyMock.expect( mockServiceRegistry.lookupServiceURL( "test-cluster", "NAMENODE.rpc" ) ).andReturn( "test-nn-scheme://test-nn-host:411" ).anyTimes();
+ EasyMock.expect( mockServiceRegistry.lookupServiceURL( "test-cluster", "JOBTRACKER.rpc" ) ).andReturn( "test-jt-scheme://test-jt-host:511" ).anyTimes();
+
+ GatewayServices mockGatewayServices = EasyMock.createNiceMock( GatewayServices.class );
+ EasyMock.expect( mockGatewayServices.getService( GatewayServices.SERVICE_REGISTRY_SERVICE ) ).andReturn( mockServiceRegistry ).anyTimes();
+
+ EasyMock.replay( mockServiceRegistry, mockGatewayServices );
+
+ String descriptorUrl = getTestResource( "rewrite.xml" ).toExternalForm();
+
+ Log.setLog( new NoOpLogger() );
+
+ server = new ServletTester();
+ server.setContextPath( "/" );
+ server.getContext().addEventListener( new UrlRewriteServletContextListener() );
+ server.getContext().setInitParameter(
+ UrlRewriteServletContextListener.DESCRIPTOR_LOCATION_INIT_PARAM_NAME, descriptorUrl );
+ server.getContext().setAttribute( GatewayServices.GATEWAY_CLUSTER_ATTRIBUTE, "test-cluster" );
+ server.getContext().setAttribute( GatewayServices.GATEWAY_SERVICES_ATTRIBUTE, mockGatewayServices );
+
+ FilterHolder setupFilter = server.addFilter( SetupFilter.class, "/*", EnumSet.of( DispatcherType.REQUEST ) );
+ setupFilter.setFilter( new SetupFilter( username ) );
+ FilterHolder rewriteFilter = server.addFilter( UrlRewriteServletFilter.class, "/*", EnumSet.of( DispatcherType.REQUEST ) );
+ if( initParams != null ) {
+ for( Map.Entry<String,String> entry : initParams.entrySet() ) {
+ rewriteFilter.setInitParameter( entry.getKey(), entry.getValue() );
+ }
+ }
+ rewriteFilter.setFilter( new UrlRewriteServletFilter() );
+
+ interactions = new ArrayQueue<MockInteraction>();
+
+ ServletHolder servlet = server.addServlet( MockServlet.class, "/" );
+ servlet.setServlet( new MockServlet( "mock-servlet", interactions ) );
+
+ server.start();
+
+ interaction = new MockInteraction();
+ request = new HttpTester();
+ response = new HttpTester();
+ }
+
+ @Test
+ public void testServiceRegistryFunctionsOnXmlRequestBody() throws Exception {
+ Map<String,String> initParams = new HashMap<String,String>();
+ initParams.put( "request.body", "oozie-conf" );
+ setUp( "test-user", initParams );
+
+ String input = TestUtils.getResourceString( ServiceRegistryFunctionsTest.class, "test-input-body.xml", "UTF-8" );
+ String expect = TestUtils.getResourceString( ServiceRegistryFunctionsTest.class, "test-expect-body.xml", "UTF-8" );
+
+ // Setup the server side request/response interaction.
+ interaction.expect()
+ .method( "PUT" )
+ .requestUrl( "http://test-host:42/test-path" )
+ .contentType( "text/xml" )
+ .characterEncoding( "UTF-8" )
+ .content( expect, Charset.forName( "UTF-8" ) );
+ interaction.respond()
+ .status( 200 );
+ interactions.add( interaction );
+ request.setMethod( "PUT" );
+ request.setURI( "/test-path" );
+ request.setVersion( "HTTP/1.1" );
+ request.setHeader( "Host", "test-host:42" );
+ request.setContentType( "text/xml; charset=UTF-8" );
+ request.setContent( input );
+
+ response.parse( server.getResponses( request.generate() ) );
+
+ // Test the results.
+ assertThat( response.getStatus(), Is.is( 200 ) );
+ }
+
+ @Test
+ public void testServiceRegistryFunctionsOnJsonRequestBody() throws Exception {
+ Map<String,String> initParams = new HashMap<String,String>();
+ initParams.put( "request.body", "oozie-conf" );
+ setUp( "test-user", initParams );
+
+ String input = TestUtils.getResourceString( ServiceRegistryFunctionsTest.class, "test-input-body.json", "UTF-8" );
+ String expect = TestUtils.getResourceString( ServiceRegistryFunctionsTest.class, "test-expect-body.json", "UTF-8" );
+
+ // Setup the server side request/response interaction.
+ interaction.expect()
+ .method( "PUT" )
+ .requestUrl( "http://test-host:42/test-path" )
+ .contentType( "application/json" )
+ .characterEncoding( "UTF-8" )
+ .content( expect, Charset.forName( "UTF-8" ) );
+ interaction.respond()
+ .status( 200 );
+ interactions.add( interaction );
+ request.setMethod( "PUT" );
+ request.setURI( "/test-path" );
+ request.setVersion( "HTTP/1.1" );
+ request.setHeader( "Host", "test-host:42" );
+ request.setContentType( "application/json; charset=UTF-8" );
+ request.setContent( input );
+
+ response.parse( server.getResponses( request.generate() ) );
+
+ // Test the results.
+ assertThat( response.getStatus(), Is.is( 200 ) );
+ }
+
+ private static class SetupFilter implements Filter {
+ private Subject subject;
+
+ public SetupFilter( String userName ) {
+ subject = new Subject();
+ subject.getPrincipals().add( new BasicUserPrincipal( userName ) );
+ }
+
+ @Override
+ public void init( FilterConfig filterConfig ) throws ServletException {
+ }
+
+ @Override
+ public void doFilter( final ServletRequest request, final ServletResponse response, final FilterChain chain ) throws IOException, ServletException {
+ HttpServletRequest httpRequest = ((HttpServletRequest)request);
+ StringBuffer sourceUrl = httpRequest.getRequestURL();
+ String queryString = httpRequest.getQueryString();
+ if( queryString != null ) {
+ sourceUrl.append( "?" );
+ sourceUrl.append( queryString );
+ }
+ try {
+ request.setAttribute(
+ AbstractGatewayFilter.SOURCE_REQUEST_URL_ATTRIBUTE_NAME,
+ Parser.parse( sourceUrl.toString() ) );
+ } catch( URISyntaxException e ) {
+ throw new ServletException( e );
+ }
+ try {
+ Subject.doAs( subject, new PrivilegedExceptionAction<Void>() {
+ @Override
+ public Void run() throws Exception {
+ chain.doFilter( request, response );
+ return null;
+ }
+ } );
+ } catch( PrivilegedActionException e ) {
+ throw new ServletException( e );
+ }
+ }
+
+ @Override
+ public void destroy() {
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-knox/blob/2f135e16/gateway-provider-rewrite-func-service-registry/src/test/java/org/apache/hadoop/gateway/svcregfunc/impl/ServiceSchemeFunctionProcessorTest.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite-func-service-registry/src/test/java/org/apache/hadoop/gateway/svcregfunc/impl/ServiceSchemeFunctionProcessorTest.java b/gateway-provider-rewrite-func-service-registry/src/test/java/org/apache/hadoop/gateway/svcregfunc/impl/ServiceSchemeFunctionProcessorTest.java
new file mode 100644
index 0000000..021455a
--- /dev/null
+++ b/gateway-provider-rewrite-func-service-registry/src/test/java/org/apache/hadoop/gateway/svcregfunc/impl/ServiceSchemeFunctionProcessorTest.java
@@ -0,0 +1,134 @@
+/**
+ * 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.svcregfunc.impl;
+
+import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteEnvironment;
+import org.apache.hadoop.gateway.filter.rewrite.spi.UrlRewriteContext;
+import org.apache.hadoop.gateway.filter.rewrite.spi.UrlRewriteFunctionProcessor;
+import org.apache.hadoop.gateway.services.GatewayServices;
+import org.apache.hadoop.gateway.services.registry.ServiceRegistry;
+import org.apache.hadoop.gateway.svcregfunc.api.ServiceAddressFunctionDescriptor;
+import org.apache.hadoop.gateway.svcregfunc.api.ServiceSchemeFunctionDescriptor;
+import org.easymock.EasyMock;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.Iterator;
+import java.util.ServiceLoader;
+
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.nullValue;
+import static org.hamcrest.CoreMatchers.sameInstance;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.fail;
+
+public class ServiceSchemeFunctionProcessorTest {
+
+ ServiceRegistry reg;
+ GatewayServices svc;
+ UrlRewriteEnvironment env;
+ UrlRewriteContext ctx;
+ ServiceSchemeFunctionDescriptor desc;
+
+ @Before
+ public void setUp() {
+ reg = EasyMock.createNiceMock( ServiceRegistry.class );
+ EasyMock.expect( reg.lookupServiceURL( "test-cluster", "test-service" ) ).andReturn( "test-scheme://test-host:777/test-path" ).anyTimes();
+
+ svc = EasyMock.createNiceMock( GatewayServices.class );
+ EasyMock.expect( svc.getService( GatewayServices.SERVICE_REGISTRY_SERVICE ) ).andReturn( reg ).anyTimes();
+
+ env = EasyMock.createNiceMock( UrlRewriteEnvironment.class );
+ EasyMock.expect( env.getAttribute( GatewayServices.GATEWAY_SERVICES_ATTRIBUTE ) ).andReturn( svc ).anyTimes();
+ EasyMock.expect( env.getAttribute( GatewayServices.GATEWAY_CLUSTER_ATTRIBUTE ) ).andReturn( "test-cluster" ).anyTimes();
+
+ ctx = EasyMock.createNiceMock( UrlRewriteContext.class );
+
+ desc = EasyMock.createNiceMock( ServiceSchemeFunctionDescriptor.class );
+
+ EasyMock.replay( reg, svc, env, desc, ctx );
+ }
+
+ @Test
+ public void testServiceLoader() throws Exception {
+ ServiceLoader loader = ServiceLoader.load( UrlRewriteFunctionProcessor.class );
+ Iterator iterator = loader.iterator();
+ assertThat( "Service iterator empty.", iterator.hasNext() );
+ while( iterator.hasNext() ) {
+ Object object = iterator.next();
+ if( object instanceof ServiceSchemeFunctionProcessor ) {
+ return;
+ }
+ }
+ fail( "Failed to find " + ServiceSchemeFunctionProcessor.class.getName() + " via service loader." );
+ }
+
+ @Test
+ public void testName() throws Exception {
+ ServiceSchemeFunctionProcessor func = new ServiceSchemeFunctionProcessor();
+ assertThat( func.name(), is( "serviceScheme" ) );
+ }
+
+ @Test
+ public void testInitialize() throws Exception {
+ ServiceSchemeFunctionProcessor func = new ServiceSchemeFunctionProcessor();
+ try {
+ func.initialize( null, desc );
+ fail( "Should have thrown an IllegalArgumentException" );
+ } catch( IllegalArgumentException e ) {
+ assertThat( e.getMessage(), containsString( "environment" ) );
+ }
+
+ func = new ServiceSchemeFunctionProcessor();
+ try {
+ func.initialize( env, null );
+ } catch( Exception e ) {
+ e.printStackTrace(); //NOTE: This is an OK use of e.printStackTrace in a test.
+ fail( "Should not have thrown an exception" );
+ }
+
+ func.initialize( env, desc );
+
+ assertThat( func.cluster(), is( "test-cluster" ) );
+ assertThat( func.registry(), sameInstance( reg ) );
+ }
+
+ @Test
+ public void testDestroy() throws Exception {
+ ServiceSchemeFunctionProcessor func = new ServiceSchemeFunctionProcessor();
+ func.initialize( env, desc );
+ func.destroy();
+
+ assertThat( func.cluster(), nullValue() );
+ assertThat( func.registry(), nullValue() );
+ }
+
+ @Test
+ public void testResolve() throws Exception {
+ ServiceSchemeFunctionProcessor func = new ServiceSchemeFunctionProcessor();
+ func.initialize( env, desc );
+
+ assertThat( func.resolve( ctx, "test-service" ), is( "test-scheme" ) );
+ assertThat( func.resolve( ctx, "invalid-test-service" ), is( "invalid-test-service" ) );
+ assertThat( func.resolve( ctx, null ), nullValue() );
+
+ func.destroy();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-knox/blob/2f135e16/gateway-provider-rewrite-func-service-registry/src/test/java/org/apache/hadoop/gateway/svcregfunc/impl/ServiceUrlFunctionProcessorTest.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite-func-service-registry/src/test/java/org/apache/hadoop/gateway/svcregfunc/impl/ServiceUrlFunctionProcessorTest.java b/gateway-provider-rewrite-func-service-registry/src/test/java/org/apache/hadoop/gateway/svcregfunc/impl/ServiceUrlFunctionProcessorTest.java
new file mode 100644
index 0000000..b7e1dfe
--- /dev/null
+++ b/gateway-provider-rewrite-func-service-registry/src/test/java/org/apache/hadoop/gateway/svcregfunc/impl/ServiceUrlFunctionProcessorTest.java
@@ -0,0 +1,135 @@
+/**
+ * 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.svcregfunc.impl;
+
+import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteEnvironment;
+import org.apache.hadoop.gateway.filter.rewrite.spi.UrlRewriteContext;
+import org.apache.hadoop.gateway.filter.rewrite.spi.UrlRewriteFunctionProcessor;
+import org.apache.hadoop.gateway.services.GatewayServices;
+import org.apache.hadoop.gateway.services.registry.ServiceRegistry;
+import org.apache.hadoop.gateway.svcregfunc.api.ServiceAddressFunctionDescriptor;
+import org.apache.hadoop.gateway.svcregfunc.api.ServiceSchemeFunctionDescriptor;
+import org.apache.hadoop.gateway.svcregfunc.api.ServiceUrlFunctionDescriptor;
+import org.easymock.EasyMock;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.Iterator;
+import java.util.ServiceLoader;
+
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.hamcrest.CoreMatchers.nullValue;
+import static org.hamcrest.CoreMatchers.sameInstance;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.fail;
+
+public class ServiceUrlFunctionProcessorTest {
+
+ ServiceRegistry reg;
+ GatewayServices svc;
+ UrlRewriteEnvironment env;
+ UrlRewriteContext ctx;
+ ServiceUrlFunctionDescriptor desc;
+
+ @Before
+ public void setUp() {
+ reg = EasyMock.createNiceMock( ServiceRegistry.class );
+ EasyMock.expect( reg.lookupServiceURL( "test-cluster", "test-service" ) ).andReturn( "test-scheme://test-host:777/test-path" ).anyTimes();
+
+ svc = EasyMock.createNiceMock( GatewayServices.class );
+ EasyMock.expect( svc.getService( GatewayServices.SERVICE_REGISTRY_SERVICE ) ).andReturn( reg ).anyTimes();
+
+ env = EasyMock.createNiceMock( UrlRewriteEnvironment.class );
+ EasyMock.expect( env.getAttribute( GatewayServices.GATEWAY_SERVICES_ATTRIBUTE ) ).andReturn( svc ).anyTimes();
+ EasyMock.expect( env.getAttribute( GatewayServices.GATEWAY_CLUSTER_ATTRIBUTE ) ).andReturn( "test-cluster" ).anyTimes();
+
+ ctx = EasyMock.createNiceMock( UrlRewriteContext.class );
+
+ desc = EasyMock.createNiceMock( ServiceUrlFunctionDescriptor.class );
+
+ EasyMock.replay( reg, svc, env, desc, ctx );
+ }
+
+ @Test
+ public void testServiceLoader() throws Exception {
+ ServiceLoader loader = ServiceLoader.load( UrlRewriteFunctionProcessor.class );
+ Iterator iterator = loader.iterator();
+ assertThat( "Service iterator empty.", iterator.hasNext() );
+ while( iterator.hasNext() ) {
+ Object object = iterator.next();
+ if( object instanceof ServiceUrlFunctionProcessor ) {
+ return;
+ }
+ }
+ fail( "Failed to find " + ServiceUrlFunctionProcessor.class.getName() + " via service loader." );
+ }
+
+ @Test
+ public void testName() throws Exception {
+ ServiceUrlFunctionProcessor func = new ServiceUrlFunctionProcessor();
+ assertThat( func.name(), is( "serviceUrl" ) );
+ }
+
+ @Test
+ public void testInitialize() throws Exception {
+ ServiceUrlFunctionProcessor func = new ServiceUrlFunctionProcessor();
+ try {
+ func.initialize( null, desc );
+ fail( "Should have thrown an IllegalArgumentException" );
+ } catch( IllegalArgumentException e ) {
+ assertThat( e.getMessage(), containsString( "environment" ) );
+ }
+
+ func = new ServiceUrlFunctionProcessor();
+ try {
+ func.initialize( env, null );
+ } catch( Exception e ) {
+ e.printStackTrace(); //NOTE: OK to use e.printStackTrace for a test failure.
+ fail( "Should not have thrown an exception" );
+ }
+
+ func.initialize( env, desc );
+
+ assertThat( func.cluster(), is( "test-cluster" ) );
+ assertThat( func.registry(), sameInstance( reg ) );
+ }
+
+ @Test
+ public void testDestroy() throws Exception {
+ ServiceUrlFunctionProcessor func = new ServiceUrlFunctionProcessor();
+ func.initialize( env, desc );
+ func.destroy();
+
+ assertThat( func.cluster(), nullValue() );
+ assertThat( func.registry(), nullValue() );
+ }
+
+ @Test
+ public void testResolve() throws Exception {
+ ServiceUrlFunctionProcessor func = new ServiceUrlFunctionProcessor();
+ func.initialize( env, desc );
+
+ assertThat( func.resolve( ctx, "test-service" ), is( "test-scheme://test-host:777/test-path" ) );
+ assertThat( func.resolve( ctx, "invalid-test-service" ), is( "invalid-test-service" ) );
+ assertThat( func.resolve( ctx, null ), nullValue() );
+
+ func.destroy();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-knox/blob/2f135e16/gateway-provider-rewrite-func-service-registry/src/test/resources/org/apache/hadoop/gateway/svcregfunc/impl/ServiceRegistryFunctionsTest/rewrite.xml
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite-func-service-registry/src/test/resources/org/apache/hadoop/gateway/svcregfunc/impl/ServiceRegistryFunctionsTest/rewrite.xml b/gateway-provider-rewrite-func-service-registry/src/test/resources/org/apache/hadoop/gateway/svcregfunc/impl/ServiceRegistryFunctionsTest/rewrite.xml
new file mode 100644
index 0000000..9bab980
--- /dev/null
+++ b/gateway-provider-rewrite-func-service-registry/src/test/resources/org/apache/hadoop/gateway/svcregfunc/impl/ServiceRegistryFunctionsTest/rewrite.xml
@@ -0,0 +1,101 @@
+<!--
+ 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.
+-->
+<rules>
+
+ <rule name="nn-url">
+ <rewrite template="{$serviceUrl[NAMENODE.rpc]}"/>
+ </rule>
+
+ <rule name="nn-addr">
+ <rewrite template="{$serviceAddr[NAMENODE.rpc]}"/>
+ </rule>
+
+ <rule name="nn-scheme">
+ <rewrite template="{$serviceScheme[NAMENODE.rpc]}"/>
+ </rule>
+
+ <rule name="nn-host">
+ <rewrite template="{$serviceHost[NAMENODE.rpc]}"/>
+ </rule>
+
+ <rule name="nn-port">
+ <rewrite template="{$servicePort[NAMENODE.rpc]}"/>
+ </rule>
+
+ <rule name="nn-path">
+ <rewrite template="{$servicePath[NAMENODE.rpc]}"/>
+ </rule>
+
+ <rule name="hdfs-addr">
+ <rewrite template="hdfs://{$serviceAddr[NAMENODE.rpc]}"/>
+ </rule>
+
+ <rule name="hdfs-path">
+ <match pattern="{path=**}"/>
+ <rewrite template="hdfs://{$serviceHost[NAMENODE.rpc]}:{$servicePort[NAMENODE.rpc]}/{path=**}"/>
+ </rule>
+
+ <rule name="jt-addr">
+ <rewrite template="{$serviceAddr[JOBTRACKER.rpc]}"/>
+ </rule>
+
+ <filter name="oozie-conf">
+ <content type="*/xml">
+ <buffer path="/configuration/property">
+ <detect path="name" value="nameNode">
+ <apply path="value" rule="hdfs-addr"/>
+ </detect>
+ <detect path="name" value="jobTracker">
+ <apply path="value" rule="jt-addr"/>
+ </detect>
+ <detect path="name" value="oozie.wf.application.path">
+ <apply path="value" rule="hdfs-path"/>
+ </detect>
+ <detect path="name" value="serviceUrl">
+ <apply path="value" rule="nn-url"/>
+ </detect>
+ <detect path="name" value="serviceAddr">
+ <apply path="value" rule="nn-addr"/>
+ </detect>
+ <detect path="name" value="serviceScheme">
+ <apply path="value" rule="nn-scheme"/>
+ </detect>
+ <detect path="name" value="serviceHost">
+ <apply path="value" rule="nn-host"/>
+ </detect>
+ <detect path="name" value="servicePort">
+ <apply path="value" rule="nn-port"/>
+ </detect>
+ <detect path="name" value="servicePath">
+ <apply path="value" rule="nn-path"/>
+ </detect>
+ </buffer>
+ </content>
+ <content type="*/json">
+ <apply path="$.nameNode" rule="hdfs-addr"/>
+ <apply path="$.jobTracker" rule="jt-addr"/>
+ <apply path="$['oozie.wf.application.path']" rule="hdfs-path"/>
+ <apply path="$.serviceUrl" rule="nn-url"/>
+ <apply path="$.serviceAddr" rule="nn-addr"/>
+ <apply path="$.serviceScheme" rule="nn-scheme"/>
+ <apply path="$.serviceHost" rule="nn-host"/>
+ <apply path="$.servicePort" rule="nn-port"/>
+ <apply path="$.servicePath" rule="nn-path"/>
+ </content>
+ </filter>
+
+</rules>
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-knox/blob/2f135e16/gateway-provider-rewrite-func-service-registry/src/test/resources/org/apache/hadoop/gateway/svcregfunc/impl/ServiceRegistryFunctionsTest/test-expect-body.json
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite-func-service-registry/src/test/resources/org/apache/hadoop/gateway/svcregfunc/impl/ServiceRegistryFunctionsTest/test-expect-body.json b/gateway-provider-rewrite-func-service-registry/src/test/resources/org/apache/hadoop/gateway/svcregfunc/impl/ServiceRegistryFunctionsTest/test-expect-body.json
new file mode 100644
index 0000000..dcafe6a
--- /dev/null
+++ b/gateway-provider-rewrite-func-service-registry/src/test/resources/org/apache/hadoop/gateway/svcregfunc/impl/ServiceRegistryFunctionsTest/test-expect-body.json
@@ -0,0 +1,11 @@
+{
+ "nameNode":"hdfs://test-nn-host:411",
+ "jobTracker":"test-jt-host:511",
+ "oozie.wf.application.path":"hdfs://test-nn-host:411/tmp/test",
+ "serviceUrl":"test-nn-scheme://test-nn-host:411",
+ "serviceAddr":"test-nn-host:411",
+ "serviceScheme":"test-nn-scheme",
+ "serviceHost":"test-nn-host",
+ "servicePort":"411",
+ "servicePath":"/"
+}
http://git-wip-us.apache.org/repos/asf/incubator-knox/blob/2f135e16/gateway-provider-rewrite-func-service-registry/src/test/resources/org/apache/hadoop/gateway/svcregfunc/impl/ServiceRegistryFunctionsTest/test-expect-body.xml
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite-func-service-registry/src/test/resources/org/apache/hadoop/gateway/svcregfunc/impl/ServiceRegistryFunctionsTest/test-expect-body.xml b/gateway-provider-rewrite-func-service-registry/src/test/resources/org/apache/hadoop/gateway/svcregfunc/impl/ServiceRegistryFunctionsTest/test-expect-body.xml
new file mode 100644
index 0000000..189cb54
--- /dev/null
+++ b/gateway-provider-rewrite-func-service-registry/src/test/resources/org/apache/hadoop/gateway/svcregfunc/impl/ServiceRegistryFunctionsTest/test-expect-body.xml
@@ -0,0 +1,40 @@
+<configuration>
+ <property>
+ <name>nameNode</name>
+ <value>hdfs://test-nn-host:411</value>
+ </property>
+ <property>
+ <name>jobTracker</name>
+ <value>test-jt-host:511</value>
+ </property>
+ <property>
+ <name>oozie.wf.application.path</name>
+ <value>hdfs://test-nn-host:411/tmp/test</value>
+ </property>
+
+ <property>
+ <name>serviceUrl</name>
+ <value>test-nn-scheme://test-nn-host:411</value>
+ </property>
+ <property>
+ <name>serviceAddr</name>
+ <value>test-nn-host:411</value>
+ </property>
+ <property>
+ <name>serviceScheme</name>
+ <value>test-nn-scheme</value>
+ </property>
+ <property>
+ <name>serviceHost</name>
+ <value>test-nn-host</value>
+ </property>
+ <property>
+ <name>servicePort</name>
+ <value>411</value>
+ </property>
+ <property>
+ <name>servicePath</name>
+ <value>/</value>
+ </property>
+
+</configuration>
http://git-wip-us.apache.org/repos/asf/incubator-knox/blob/2f135e16/gateway-provider-rewrite-func-service-registry/src/test/resources/org/apache/hadoop/gateway/svcregfunc/impl/ServiceRegistryFunctionsTest/test-input-body.json
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite-func-service-registry/src/test/resources/org/apache/hadoop/gateway/svcregfunc/impl/ServiceRegistryFunctionsTest/test-input-body.json b/gateway-provider-rewrite-func-service-registry/src/test/resources/org/apache/hadoop/gateway/svcregfunc/impl/ServiceRegistryFunctionsTest/test-input-body.json
new file mode 100644
index 0000000..28700e0
--- /dev/null
+++ b/gateway-provider-rewrite-func-service-registry/src/test/resources/org/apache/hadoop/gateway/svcregfunc/impl/ServiceRegistryFunctionsTest/test-input-body.json
@@ -0,0 +1,11 @@
+{
+ "nameNode":"default",
+ "jobTracker":"default",
+ "oozie.wf.application.path":"/tmp/test",
+ "serviceUrl":"",
+ "serviceAddr":"",
+ "serviceScheme":"",
+ "serviceHost":"",
+ "servicePort":"",
+ "servicePath":""
+}
http://git-wip-us.apache.org/repos/asf/incubator-knox/blob/2f135e16/gateway-provider-rewrite-func-service-registry/src/test/resources/org/apache/hadoop/gateway/svcregfunc/impl/ServiceRegistryFunctionsTest/test-input-body.xml
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite-func-service-registry/src/test/resources/org/apache/hadoop/gateway/svcregfunc/impl/ServiceRegistryFunctionsTest/test-input-body.xml b/gateway-provider-rewrite-func-service-registry/src/test/resources/org/apache/hadoop/gateway/svcregfunc/impl/ServiceRegistryFunctionsTest/test-input-body.xml
new file mode 100644
index 0000000..2c0d824
--- /dev/null
+++ b/gateway-provider-rewrite-func-service-registry/src/test/resources/org/apache/hadoop/gateway/svcregfunc/impl/ServiceRegistryFunctionsTest/test-input-body.xml
@@ -0,0 +1,40 @@
+<configuration>
+ <property>
+ <name>nameNode</name>
+ <value>default</value>
+ </property>
+ <property>
+ <name>jobTracker</name>
+ <value>default</value>
+ </property>
+ <property>
+ <name>oozie.wf.application.path</name>
+ <value>/tmp/test</value>
+ </property>
+
+ <property>
+ <name>serviceUrl</name>
+ <value></value>
+ </property>
+ <property>
+ <name>serviceAddr</name>
+ <value></value>
+ </property>
+ <property>
+ <name>serviceScheme</name>
+ <value></value>
+ </property>
+ <property>
+ <name>serviceHost</name>
+ <value></value>
+ </property>
+ <property>
+ <name>servicePort</name>
+ <value></value>
+ </property>
+ <property>
+ <name>servicePath</name>
+ <value></value>
+ </property>
+
+</configuration>
http://git-wip-us.apache.org/repos/asf/incubator-knox/blob/2f135e16/gateway-provider-rewrite-step-secure-query/pom.xml
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite-step-secure-query/pom.xml b/gateway-provider-rewrite-step-secure-query/pom.xml
new file mode 100644
index 0000000..8ac5cd3
--- /dev/null
+++ b/gateway-provider-rewrite-step-secure-query/pom.xml
@@ -0,0 +1,106 @@
+<!--
+ 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.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <artifactId>gateway</artifactId>
+ <groupId>org.apache.hadoop</groupId>
+ <version>0.3.0-SNAPSHOT</version>
+ </parent>
+ <artifactId>gateway-provider-rewrite-step-secure-query</artifactId>
+
+ <name>gateway-provider-rewrite-step-secure-query</name>
+ <description>An extension of the gateway that supports securing query parameters.</description>
+
+ <licenses>
+ <license>
+ <name>The Apache Software License, Version 2.0</name>
+ <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
+ <distribution>repo</distribution>
+ </license>
+ </licenses>
+
+ <dependencies>
+
+ <dependency>
+ <groupId>${gateway-group}</groupId>
+ <artifactId>gateway-util-common</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>${gateway-group}</groupId>
+ <artifactId>gateway-util-urltemplate</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>${gateway-group}</groupId>
+ <artifactId>gateway-i18n</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>${gateway-group}</groupId>
+ <artifactId>gateway-i18n-logging-log4j</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>${gateway-group}</groupId>
+ <artifactId>gateway-spi</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>${gateway-group}</groupId>
+ <artifactId>gateway-provider-rewrite</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>commons-codec</groupId>
+ <artifactId>commons-codec</artifactId>
+ </dependency>
+
+ <!-- ********** ********** ********** ********** ********** ********** -->
+ <!-- ********** Test Dependencies ********** -->
+ <!-- ********** ********** ********** ********** ********** ********** -->
+
+ <dependency>
+ <groupId>${gateway-group}</groupId>
+ <artifactId>gateway-test-utils</artifactId>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>org.hamcrest</groupId>
+ <artifactId>hamcrest-core</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.hamcrest</groupId>
+ <artifactId>hamcrest-library</artifactId>
+ <scope>test</scope>
+ </dependency>
+
+ <!-- This must be after restassured otherwise is messes up the hamcrest dependencies. -->
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>org.easymock</groupId>
+ <artifactId>easymock</artifactId>
+ <scope>test</scope>
+ </dependency>
+
+ </dependencies>
+
+</project>
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-knox/blob/2f135e16/gateway-provider-rewrite-step-secure-query/src/main/java/org/apache/hadoop/gateway/securequery/SecureQueryDecodeDescriptor.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite-step-secure-query/src/main/java/org/apache/hadoop/gateway/securequery/SecureQueryDecodeDescriptor.java b/gateway-provider-rewrite-step-secure-query/src/main/java/org/apache/hadoop/gateway/securequery/SecureQueryDecodeDescriptor.java
new file mode 100644
index 0000000..f026e38
--- /dev/null
+++ b/gateway-provider-rewrite-step-secure-query/src/main/java/org/apache/hadoop/gateway/securequery/SecureQueryDecodeDescriptor.java
@@ -0,0 +1,38 @@
+/**
+ * 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.securequery;
+
+import org.apache.hadoop.gateway.filter.rewrite.ext.UrlRewriteActionDescriptor;
+import org.apache.hadoop.gateway.filter.rewrite.spi.UrlRewriteActionDescriptorBase;
+
+public class SecureQueryDecodeDescriptor
+ extends UrlRewriteActionDescriptorBase
+ implements UrlRewriteActionDescriptor {
+
+ static final String STEP_NAME = "decode-query";
+
+ public SecureQueryDecodeDescriptor() {
+ super( STEP_NAME );
+ }
+
+ @Override
+ public String getParam() {
+ return null;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-knox/blob/2f135e16/gateway-provider-rewrite-step-secure-query/src/main/java/org/apache/hadoop/gateway/securequery/SecureQueryDecodeProcessor.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite-step-secure-query/src/main/java/org/apache/hadoop/gateway/securequery/SecureQueryDecodeProcessor.java b/gateway-provider-rewrite-step-secure-query/src/main/java/org/apache/hadoop/gateway/securequery/SecureQueryDecodeProcessor.java
new file mode 100644
index 0000000..37eec1c
--- /dev/null
+++ b/gateway-provider-rewrite-step-secure-query/src/main/java/org/apache/hadoop/gateway/securequery/SecureQueryDecodeProcessor.java
@@ -0,0 +1,86 @@
+/**
+ * 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.securequery;
+
+import org.apache.commons.codec.binary.Base64;
+import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteEnvironment;
+import org.apache.hadoop.gateway.filter.rewrite.spi.UrlRewriteContext;
+import org.apache.hadoop.gateway.filter.rewrite.spi.UrlRewriteStepProcessor;
+import org.apache.hadoop.gateway.filter.rewrite.spi.UrlRewriteStepStatus;
+import org.apache.hadoop.gateway.util.urltemplate.Builder;
+import org.apache.hadoop.gateway.util.urltemplate.Query;
+import org.apache.hadoop.gateway.util.urltemplate.Template;
+
+import java.io.UnsupportedEncodingException;
+import java.util.Map;
+import java.util.StringTokenizer;
+
+public class SecureQueryDecodeProcessor implements UrlRewriteStepProcessor<SecureQueryDecodeDescriptor> {
+
+ private static final String ENCODED_PARAMETER_NAME = "_";
+
+ @Override
+ public String getType() {
+ return SecureQueryDecodeDescriptor.STEP_NAME;
+ }
+
+ @Override
+ public void initialize( UrlRewriteEnvironment environment, SecureQueryDecodeDescriptor descriptor ) throws Exception {
+ }
+
+ @Override
+ public UrlRewriteStepStatus process( UrlRewriteContext context ) throws Exception {
+ //TODO: Need some way to get a reference to the keystore service and the encryption key in particular.
+ Template currUrl = context.getCurrentUrl();
+ Builder newUrl = new Builder( currUrl );
+ Map<String,Query> map = newUrl.getQuery();
+ Query query = map.remove( ENCODED_PARAMETER_NAME );
+ if( query != null ) {
+ String value = query.getFirstValue().getPattern();
+ value = decode( value );
+ StringTokenizer outerParser = new StringTokenizer( value, "&" );
+ while( outerParser.hasMoreTokens() ) {
+ String pair = outerParser.nextToken();
+ StringTokenizer innerParser = new StringTokenizer( pair, "=" );
+ if( innerParser.hasMoreTokens() ) {
+ String paramName = innerParser.nextToken();
+ if( innerParser.hasMoreTokens() ) {
+ String paramValue = innerParser.nextToken();
+ // Need to take out any existing query param.
+ // If we don't then someone could override something in the encoded param.
+ map.remove( paramName );
+ newUrl.addQuery( paramName, "", paramValue );
+ } else {
+ newUrl.addQuery( paramName, "", null );
+ }
+ }
+ }
+ context.setCurrentUrl( newUrl.build() );
+ }
+ return UrlRewriteStepStatus.SUCCESS;
+ }
+
+ @Override
+ public void destroy() {
+ }
+
+ private static String decode( String string ) throws UnsupportedEncodingException {
+ return new String( Base64.decodeBase64( string ), "UTF-8" );
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-knox/blob/2f135e16/gateway-provider-rewrite-step-secure-query/src/main/java/org/apache/hadoop/gateway/securequery/SecureQueryDecryptDescriptor.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite-step-secure-query/src/main/java/org/apache/hadoop/gateway/securequery/SecureQueryDecryptDescriptor.java b/gateway-provider-rewrite-step-secure-query/src/main/java/org/apache/hadoop/gateway/securequery/SecureQueryDecryptDescriptor.java
new file mode 100644
index 0000000..8db2070
--- /dev/null
+++ b/gateway-provider-rewrite-step-secure-query/src/main/java/org/apache/hadoop/gateway/securequery/SecureQueryDecryptDescriptor.java
@@ -0,0 +1,38 @@
+/**
+ * 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.securequery;
+
+import org.apache.hadoop.gateway.filter.rewrite.ext.UrlRewriteActionDescriptor;
+import org.apache.hadoop.gateway.filter.rewrite.spi.UrlRewriteActionDescriptorBase;
+
+public class SecureQueryDecryptDescriptor
+ extends UrlRewriteActionDescriptorBase
+ implements UrlRewriteActionDescriptor {
+
+ static final String STEP_NAME = "decrypt-query";
+
+ public SecureQueryDecryptDescriptor() {
+ super( STEP_NAME );
+ }
+
+ @Override
+ public String getParam() {
+ return null;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-knox/blob/2f135e16/gateway-provider-rewrite-step-secure-query/src/main/java/org/apache/hadoop/gateway/securequery/SecureQueryDecryptProcessor.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite-step-secure-query/src/main/java/org/apache/hadoop/gateway/securequery/SecureQueryDecryptProcessor.java b/gateway-provider-rewrite-step-secure-query/src/main/java/org/apache/hadoop/gateway/securequery/SecureQueryDecryptProcessor.java
new file mode 100644
index 0000000..031e0fb
--- /dev/null
+++ b/gateway-provider-rewrite-step-secure-query/src/main/java/org/apache/hadoop/gateway/securequery/SecureQueryDecryptProcessor.java
@@ -0,0 +1,94 @@
+/**
+ * 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.securequery;
+
+import org.apache.commons.codec.binary.Base64;
+import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteEnvironment;
+import org.apache.hadoop.gateway.filter.rewrite.spi.UrlRewriteContext;
+import org.apache.hadoop.gateway.filter.rewrite.spi.UrlRewriteStepProcessor;
+import org.apache.hadoop.gateway.filter.rewrite.spi.UrlRewriteStepStatus;
+import org.apache.hadoop.gateway.util.urltemplate.Builder;
+import org.apache.hadoop.gateway.util.urltemplate.Query;
+import org.apache.hadoop.gateway.util.urltemplate.Template;
+
+import java.io.UnsupportedEncodingException;
+import java.util.List;
+import java.util.Map;
+import java.util.StringTokenizer;
+
+public class SecureQueryDecryptProcessor implements UrlRewriteStepProcessor<SecureQueryDecryptDescriptor> {
+
+ private static final String ENCRYPTED_PARAMETER_NAME = "_";
+
+ private String clusterName;
+
+ @Override
+ public String getType() {
+ return SecureQueryDecryptDescriptor.STEP_NAME;
+ }
+
+ @Override
+ public void initialize( UrlRewriteEnvironment environment, SecureQueryDecryptDescriptor descriptor ) throws Exception {
+ List<String> values = environment.resolve( "cluster.name" );
+ if( values != null && values.size() > 0 ) {
+ this.clusterName = environment.resolve( "cluster.name" ).get( 0 );
+ }
+ }
+
+ @Override
+ public UrlRewriteStepStatus process( UrlRewriteContext context ) throws Exception {
+ //TODO: Need some way to get a reference to the keystore service and the encryption key in particular.
+ Template currUrl = context.getCurrentUrl();
+ Builder newUrl = new Builder( currUrl );
+ Map<String,Query> map = newUrl.getQuery();
+ Query query = map.remove( ENCRYPTED_PARAMETER_NAME );
+ if( query != null ) {
+ String value = query.getFirstValue().getPattern();
+ value = decode( value );
+ StringTokenizer outerParser = new StringTokenizer( value, "&" );
+ while( outerParser.hasMoreTokens() ) {
+ String pair = outerParser.nextToken();
+ StringTokenizer innerParser = new StringTokenizer( pair, "=" );
+ if( innerParser.hasMoreTokens() ) {
+ String paramName = innerParser.nextToken();
+ if( innerParser.hasMoreTokens() ) {
+ String paramValue = innerParser.nextToken();
+ // Need to remove from the clear parameters any param name in the encoded params.
+ // If we don't then someone could override something in the encoded param.
+ map.remove( paramName );
+ newUrl.addQuery( paramName, "", paramValue );
+ } else {
+ newUrl.addQuery( paramName, "", null );
+ }
+ }
+ }
+ context.setCurrentUrl( newUrl.build() );
+ context.getParameters().resolve( "gateway.name" );
+ }
+ return UrlRewriteStepStatus.SUCCESS;
+ }
+
+ @Override
+ public void destroy() {
+ }
+
+ private static String decode( String string ) throws UnsupportedEncodingException {
+ return new String( Base64.decodeBase64( string ), "UTF-8" );
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-knox/blob/2f135e16/gateway-provider-rewrite-step-secure-query/src/main/java/org/apache/hadoop/gateway/securequery/SecureQueryDeploymentContributor.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite-step-secure-query/src/main/java/org/apache/hadoop/gateway/securequery/SecureQueryDeploymentContributor.java b/gateway-provider-rewrite-step-secure-query/src/main/java/org/apache/hadoop/gateway/securequery/SecureQueryDeploymentContributor.java
new file mode 100644
index 0000000..2598eb2
--- /dev/null
+++ b/gateway-provider-rewrite-step-secure-query/src/main/java/org/apache/hadoop/gateway/securequery/SecureQueryDeploymentContributor.java
@@ -0,0 +1,91 @@
+/**
+ * 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.securequery;
+
+import org.apache.hadoop.gateway.deploy.DeploymentContext;
+import org.apache.hadoop.gateway.deploy.ProviderDeploymentContributor;
+import org.apache.hadoop.gateway.deploy.ProviderDeploymentContributorBase;
+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.List;
+
+public class SecureQueryDeploymentContributor
+ extends ProviderDeploymentContributorBase
+ implements ProviderDeploymentContributor {
+
+ public static final String PROVIDER_ROLE_NAME = "secure-query";
+ public static final String PROVIDER_IMPL_NAME = "default";
+
+ @Override
+ public String getRole() {
+ return PROVIDER_ROLE_NAME;
+ }
+
+ @Override
+ public String getName() {
+ return PROVIDER_IMPL_NAME;
+ }
+
+ @Override
+ public void contributeProvider( DeploymentContext context, Provider provider ) {
+ if( provider.isEnabled() ) {
+ //TODO: Do something with the keystore service.
+// UrlRewriteRulesDescriptor rules = context.getDescriptor( REWRITE_ROLE_NAME );
+// if( rules != null ) {
+// HostmapFunctionDescriptor func = rules.addFunction( HostmapFunctionDescriptor.FUNCTION_NAME );
+// if( func != null ) {
+// Asset asset = createAsset( provider );
+// context.getWebArchive().addAsWebInfResource(
+// asset, HostmapFunctionProcessor.DESCRIPTOR_DEFAULT_FILE_NAME );
+// func.config( HostmapFunctionProcessor.DESCRIPTOR_DEFAULT_LOCATION );
+// }
+// }
+ }
+ }
+
+// private Asset createAsset( Provider provider ) {
+// StringWriter buffer = new StringWriter();
+// PrintWriter writer = new PrintWriter( buffer );
+// for( Map.Entry<String,String> entry : provider.getParams().entrySet() ) {
+// String externalHosts = entry.getKey();
+// String internalHosts = entry.getValue();
+// writer.print( externalHosts );
+// writer.print( "=" );
+// writer.println( internalHosts ) ;
+// }
+// writer.close();
+// String string = buffer.toString();
+// Asset asset = new StringAsset( string );
+// return asset;
+// }
+
+ @Override
+ public void contributeFilter(
+ DeploymentContext context,
+ Provider provider,
+ Service service,
+ ResourceDescriptor resource,
+ List<FilterParamDescriptor> params ) {
+ //TODO: Might need to add a filter as a way to propigate a keystore service to the processor.
+ // NoOp.
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-knox/blob/2f135e16/gateway-provider-rewrite-step-secure-query/src/main/java/org/apache/hadoop/gateway/securequery/SecureQueryEncodeDescriptor.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite-step-secure-query/src/main/java/org/apache/hadoop/gateway/securequery/SecureQueryEncodeDescriptor.java b/gateway-provider-rewrite-step-secure-query/src/main/java/org/apache/hadoop/gateway/securequery/SecureQueryEncodeDescriptor.java
new file mode 100644
index 0000000..a29be4e
--- /dev/null
+++ b/gateway-provider-rewrite-step-secure-query/src/main/java/org/apache/hadoop/gateway/securequery/SecureQueryEncodeDescriptor.java
@@ -0,0 +1,38 @@
+/**
+ * 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.securequery;
+
+import org.apache.hadoop.gateway.filter.rewrite.ext.UrlRewriteActionDescriptor;
+import org.apache.hadoop.gateway.filter.rewrite.spi.UrlRewriteActionDescriptorBase;
+
+public class SecureQueryEncodeDescriptor
+ extends UrlRewriteActionDescriptorBase
+ implements UrlRewriteActionDescriptor {
+
+ static final String STEP_NAME = "encode-query";
+
+ public SecureQueryEncodeDescriptor() {
+ super( STEP_NAME );
+ }
+
+ @Override
+ public String getParam() {
+ return null;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-knox/blob/2f135e16/gateway-provider-rewrite-step-secure-query/src/main/java/org/apache/hadoop/gateway/securequery/SecureQueryEncodeProcessor.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite-step-secure-query/src/main/java/org/apache/hadoop/gateway/securequery/SecureQueryEncodeProcessor.java b/gateway-provider-rewrite-step-secure-query/src/main/java/org/apache/hadoop/gateway/securequery/SecureQueryEncodeProcessor.java
new file mode 100644
index 0000000..eff1908
--- /dev/null
+++ b/gateway-provider-rewrite-step-secure-query/src/main/java/org/apache/hadoop/gateway/securequery/SecureQueryEncodeProcessor.java
@@ -0,0 +1,77 @@
+/**
+ * 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.securequery;
+
+import org.apache.commons.codec.binary.Base64;
+import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteEnvironment;
+import org.apache.hadoop.gateway.filter.rewrite.spi.UrlRewriteContext;
+import org.apache.hadoop.gateway.filter.rewrite.spi.UrlRewriteStepProcessor;
+import org.apache.hadoop.gateway.filter.rewrite.spi.UrlRewriteStepStatus;
+import org.apache.hadoop.gateway.util.urltemplate.Parser;
+import org.apache.hadoop.gateway.util.urltemplate.Template;
+
+public class SecureQueryEncodeProcessor
+ implements UrlRewriteStepProcessor<SecureQueryEncodeDescriptor> {
+
+ private static final String ENCODED_PARAMETER_NAME = "_";
+
+ @Override
+ public String getType() {
+ return SecureQueryEncodeDescriptor.STEP_NAME;
+ }
+
+ @Override
+ public void initialize( UrlRewriteEnvironment environment, SecureQueryEncodeDescriptor descriptor ) throws Exception {
+ }
+
+ @Override
+ public UrlRewriteStepStatus process( UrlRewriteContext context ) throws Exception {
+ //TODO: Need some way to get a reference to the keystore service and the encryption key in particular.
+ Template url = context.getCurrentUrl();
+ String str = url.toString();
+ String path = str;
+ String query = null;
+ int index = str.indexOf( '?' );
+ if( index >= 0 ) {
+ path = str.substring( 0, index );
+ if( index < str.length() ) {
+ query = str.substring( index + 1 );
+ }
+ }
+ if( query != null ) {
+ query = Base64.encodeBase64String( query.getBytes( "UTF-8" ) );
+ query = removeTrailingEquals( query );
+ url = Parser.parse( path + "?" + ENCODED_PARAMETER_NAME +"=" + query );
+ context.setCurrentUrl( url );
+ }
+ return UrlRewriteStepStatus.SUCCESS;
+ }
+
+ @Override
+ public void destroy() {
+ }
+
+ private static String removeTrailingEquals( String s ) {
+ int i = s.length()-1;
+ while( i > 0 && s.charAt( i ) == '=' ) {
+ i--;
+ }
+ return s.substring( 0, i+1 );
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-knox/blob/2f135e16/gateway-provider-rewrite-step-secure-query/src/main/java/org/apache/hadoop/gateway/securequery/SecureQueryEncryptDescriptor.java
----------------------------------------------------------------------
diff --git a/gateway-provider-rewrite-step-secure-query/src/main/java/org/apache/hadoop/gateway/securequery/SecureQueryEncryptDescriptor.java b/gateway-provider-rewrite-step-secure-query/src/main/java/org/apache/hadoop/gateway/securequery/SecureQueryEncryptDescriptor.java
new file mode 100644
index 0000000..9f3d7b9
--- /dev/null
+++ b/gateway-provider-rewrite-step-secure-query/src/main/java/org/apache/hadoop/gateway/securequery/SecureQueryEncryptDescriptor.java
@@ -0,0 +1,38 @@
+/**
+ * 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.securequery;
+
+import org.apache.hadoop.gateway.filter.rewrite.ext.UrlRewriteActionDescriptor;
+import org.apache.hadoop.gateway.filter.rewrite.spi.UrlRewriteActionDescriptorBase;
+
+public class SecureQueryEncryptDescriptor
+ extends UrlRewriteActionDescriptorBase
+ implements UrlRewriteActionDescriptor {
+
+ static final String STEP_NAME = "encrypt-query";
+
+ public SecureQueryEncryptDescriptor() {
+ super( STEP_NAME );
+ }
+
+ @Override
+ public String getParam() {
+ return null;
+ }
+
+}