http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-identity-assertion-switchcase/src/test/java/org/apache/hadoop/gateway/identityasserter/switchcase/SwitchCaseIdentityAssertionFilterTest.java ---------------------------------------------------------------------- diff --git a/gateway-provider-identity-assertion-switchcase/src/test/java/org/apache/hadoop/gateway/identityasserter/switchcase/SwitchCaseIdentityAssertionFilterTest.java b/gateway-provider-identity-assertion-switchcase/src/test/java/org/apache/hadoop/gateway/identityasserter/switchcase/SwitchCaseIdentityAssertionFilterTest.java deleted file mode 100644 index 8637f62..0000000 --- a/gateway-provider-identity-assertion-switchcase/src/test/java/org/apache/hadoop/gateway/identityasserter/switchcase/SwitchCaseIdentityAssertionFilterTest.java +++ /dev/null @@ -1,242 +0,0 @@ -/** - * 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.identityasserter.switchcase; - -import java.security.Principal; -import javax.security.auth.Subject; -import javax.servlet.FilterConfig; -import javax.servlet.ServletContext; - -import org.apache.hadoop.gateway.security.GroupPrincipal; -import org.apache.hadoop.gateway.security.PrimaryPrincipal; -import org.easymock.EasyMock; -import org.junit.Test; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.nullValue; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.arrayContainingInAnyOrder; - -public class SwitchCaseIdentityAssertionFilterTest { - - @Test - public void testDefaultConfig() throws Exception { - FilterConfig config = EasyMock.createNiceMock( FilterConfig.class ); - EasyMock.expect(config.getInitParameter("principal.mapping") ).andReturn( "" ).anyTimes(); - ServletContext context = EasyMock.createNiceMock(ServletContext.class); - EasyMock.expect(config.getServletContext() ).andReturn( context ).anyTimes(); - EasyMock.expect(context.getInitParameter("principal.mapping") ).andReturn( "" ).anyTimes(); - EasyMock.replay( config ); - EasyMock.replay( context ); - - SwitchCaseIdentityAssertionFilter filter = new SwitchCaseIdentityAssertionFilter(); - - Subject subject = new Subject(); - subject.getPrincipals().add( new PrimaryPrincipal( "mem...@us.apache.org" ) ); - subject.getPrincipals().add( new GroupPrincipal( "users" ) ); - subject.getPrincipals().add( new GroupPrincipal( "Admin" ) ); - - filter.init(config); - String actual = filter.mapUserPrincipal(((Principal) subject.getPrincipals(PrimaryPrincipal.class).toArray()[0]).getName()); - String[] groups = filter.mapGroupPrincipals(actual, subject); - assertThat( actual, is( "mem...@us.apache.org" ) ); - assertThat( groups, is( arrayContainingInAnyOrder( "admin", "users" ) ) ); - - } - - @Test - public void testUpperPrincipalAndGroups() throws Exception { - FilterConfig config = EasyMock.createNiceMock( FilterConfig.class ); - EasyMock.expect( config.getInitParameter( "principal.case" ) ).andReturn( "Upper" ).anyTimes(); - EasyMock.expect( config.getInitParameter( "group.principal.case" ) ).andReturn( "Upper" ).anyTimes(); - EasyMock.expect(config.getInitParameter("principal.mapping") ).andReturn( "" ).anyTimes(); - ServletContext context = EasyMock.createNiceMock(ServletContext.class); - EasyMock.expect(config.getServletContext() ).andReturn( context ).anyTimes(); - EasyMock.expect(context.getInitParameter("principal.mapping") ).andReturn( "" ).anyTimes(); - EasyMock.replay( config ); - EasyMock.replay( context ); - - SwitchCaseIdentityAssertionFilter filter = new SwitchCaseIdentityAssertionFilter(); - - Subject subject = new Subject(); - subject.getPrincipals().add( new PrimaryPrincipal( "mem...@us.apache.org" ) ); - subject.getPrincipals().add( new GroupPrincipal( "users" ) ); - subject.getPrincipals().add( new GroupPrincipal( "Admin" ) ); - - filter.init(config); - String actual = filter.mapUserPrincipal(((Principal) subject.getPrincipals(PrimaryPrincipal.class).toArray()[0]).getName()); - String[] groups = filter.mapGroupPrincipals(actual, subject); - assertThat( actual, is( "mem...@us.apache.org" ) ); - assertThat( groups, is( arrayContainingInAnyOrder( "ADMIN", "USERS" ) ) ); - - } - - @Test - public void testLowerPrincipalAndGroups() throws Exception { - FilterConfig config = EasyMock.createNiceMock( FilterConfig.class ); - EasyMock.expect( config.getInitParameter( "principal.case" ) ).andReturn( "lower" ).anyTimes(); - EasyMock.expect( config.getInitParameter( "group.principal.case" ) ).andReturn( "LOWER" ).anyTimes(); - ServletContext context = EasyMock.createNiceMock(ServletContext.class); - EasyMock.expect(config.getServletContext() ).andReturn( context ).anyTimes(); - EasyMock.expect(context.getInitParameter("principal.mapping") ).andReturn( "" ).anyTimes(); - EasyMock.replay( config ); - EasyMock.replay( context ); - - SwitchCaseIdentityAssertionFilter filter = new SwitchCaseIdentityAssertionFilter(); - - Subject subject = new Subject(); - subject.getPrincipals().add(new PrimaryPrincipal( "mem...@us.apache.org" ) ); - subject.getPrincipals().add(new GroupPrincipal( "users" ) ); - subject.getPrincipals().add( new GroupPrincipal( "Admin" ) ); - - filter.init(config); - String actual = filter.mapUserPrincipal(((Principal) subject.getPrincipals(PrimaryPrincipal.class).toArray()[0]).getName()); - String[] groups = filter.mapGroupPrincipals(actual, subject); - assertThat( actual, is( "mem...@us.apache.org" ) ); - assertThat( groups, is( arrayContainingInAnyOrder( "admin", "users" ) ) ); - - } - - @Test - public void testNonePrincipalAndGroups() throws Exception { - FilterConfig config = EasyMock.createNiceMock( FilterConfig.class ); - EasyMock.expect( config.getInitParameter( "principal.case" ) ).andReturn( "none" ).anyTimes(); - EasyMock.expect( config.getInitParameter( "group.principal.case" ) ).andReturn( "NONE" ).anyTimes(); - ServletContext context = EasyMock.createNiceMock(ServletContext.class); - EasyMock.expect(config.getServletContext() ).andReturn( context ).anyTimes(); - EasyMock.expect(context.getInitParameter("principal.mapping") ).andReturn( "" ).anyTimes(); - EasyMock.replay( config ); - EasyMock.replay( context ); - - SwitchCaseIdentityAssertionFilter filter = new SwitchCaseIdentityAssertionFilter(); - - Subject subject = new Subject(); - subject.getPrincipals().add(new PrimaryPrincipal( "mem...@us.apache.org" ) ); - - filter.init(config); - String actual = filter.mapUserPrincipal(((Principal) subject.getPrincipals(PrimaryPrincipal.class).toArray()[0]).getName()); - String[] groups = filter.mapGroupPrincipals(actual, subject); - assertThat( actual, is( "mem...@us.apache.org" ) ); - assertThat( groups, is( nullValue() ) ); - - } - - @Test - public void testDefaultGroupsConfFromUsers() throws Exception { - FilterConfig config = EasyMock.createNiceMock( FilterConfig.class ); - EasyMock.expect( config.getInitParameter( "principal.case" ) ).andReturn( "UPPER" ).anyTimes(); - EasyMock.expect( config.getInitParameter( "group.principal.case" ) ).andReturn( null ).anyTimes(); - ServletContext context = EasyMock.createNiceMock(ServletContext.class); - EasyMock.expect(config.getServletContext() ).andReturn( context ).anyTimes(); - EasyMock.expect(context.getInitParameter("principal.mapping") ).andReturn( "" ).anyTimes(); - EasyMock.replay( config ); - EasyMock.replay( context ); - - SwitchCaseIdentityAssertionFilter filter = new SwitchCaseIdentityAssertionFilter(); - - Subject subject = new Subject(); - subject.getPrincipals().add(new PrimaryPrincipal( "mem...@us.apache.org" ) ); - subject.getPrincipals().add(new GroupPrincipal( "users" ) ); - subject.getPrincipals().add( new GroupPrincipal( "Admin" ) ); - - filter.init(config); - String actual = filter.mapUserPrincipal(((Principal) subject.getPrincipals(PrimaryPrincipal.class).toArray()[0]).getName()); - String[] groups = filter.mapGroupPrincipals(actual, subject); - assertThat( actual, is( "mem...@us.apache.org" ) ); - assertThat( groups, is( arrayContainingInAnyOrder( "ADMIN", "USERS" ) ) ); - - } - - @Test - public void testDefaultGroupsConfOverride() throws Exception { - FilterConfig config = EasyMock.createNiceMock( FilterConfig.class ); - EasyMock.expect( config.getInitParameter( "principal.case" ) ).andReturn( "UPPER" ).anyTimes(); - EasyMock.expect( config.getInitParameter( "group.principal.case" ) ).andReturn( "none" ).anyTimes(); - ServletContext context = EasyMock.createNiceMock(ServletContext.class); - EasyMock.expect(config.getServletContext() ).andReturn( context ).anyTimes(); - EasyMock.expect(context.getInitParameter("principal.mapping") ).andReturn( "" ).anyTimes(); - EasyMock.replay( config ); - EasyMock.replay( context ); - - SwitchCaseIdentityAssertionFilter filter = new SwitchCaseIdentityAssertionFilter(); - - Subject subject = new Subject(); - subject.getPrincipals().add(new PrimaryPrincipal( "mem...@us.apache.org" ) ); - subject.getPrincipals().add(new GroupPrincipal( "users" ) ); - subject.getPrincipals().add( new GroupPrincipal( "Admin" ) ); - - filter.init(config); - String actual = filter.mapUserPrincipal(((Principal) subject.getPrincipals(PrimaryPrincipal.class).toArray()[0]).getName()); - String[] groups = filter.mapGroupPrincipals(actual, subject); - assertThat( actual, is( "mem...@us.apache.org" ) ); - assertThat( groups, is( nullValue() ) ); - - } - - @Test - public void testNone() throws Exception { - FilterConfig config = EasyMock.createNiceMock( FilterConfig.class ); - EasyMock.expect( config.getInitParameter( "principal.case" ) ).andReturn( "none" ).anyTimes(); - EasyMock.expect( config.getInitParameter( "group.principal.case" ) ).andReturn( "none" ).anyTimes(); - ServletContext context = EasyMock.createNiceMock(ServletContext.class); - EasyMock.expect(config.getServletContext() ).andReturn( context ).anyTimes(); - EasyMock.expect(context.getInitParameter("principal.mapping") ).andReturn( "" ).anyTimes(); - EasyMock.replay( config ); - EasyMock.replay( context ); - - SwitchCaseIdentityAssertionFilter filter = new SwitchCaseIdentityAssertionFilter(); - - Subject subject = new Subject(); - subject.getPrincipals().add(new PrimaryPrincipal( "mem...@us.apache.org" ) ); - subject.getPrincipals().add(new GroupPrincipal( "users" ) ); - subject.getPrincipals().add( new GroupPrincipal( "Admin" ) ); - - filter.init(config); - String actual = filter.mapUserPrincipal(((Principal) subject.getPrincipals(PrimaryPrincipal.class).toArray()[0]).getName()); - String[] groups = filter.mapGroupPrincipals(actual, subject); - assertThat( actual, is( "mem...@us.apache.org" ) ); - assertThat( groups, is( nullValue() ) ); - - } - - @Test - public void testNoGroups() throws Exception { - FilterConfig config = EasyMock.createNiceMock( FilterConfig.class ); - EasyMock.expect( config.getInitParameter( "principal.case" ) ).andReturn( "upper" ).anyTimes(); - EasyMock.expect( config.getInitParameter( "group.principal.case" ) ).andReturn( "upper" ).anyTimes(); - EasyMock.expect(config.getInitParameter("principal.mapping") ).andReturn( "" ).anyTimes(); - ServletContext context = EasyMock.createNiceMock(ServletContext.class); - EasyMock.expect(config.getServletContext() ).andReturn( context ).anyTimes(); - EasyMock.expect(context.getInitParameter("principal.mapping") ).andReturn( "" ).anyTimes(); - EasyMock.replay( config ); - EasyMock.replay( context ); - - SwitchCaseIdentityAssertionFilter filter = new SwitchCaseIdentityAssertionFilter(); - - Subject subject = new Subject(); - subject.getPrincipals().add(new PrimaryPrincipal( "mem...@us.apache.org" ) ); - - filter.init(config); - String actual = filter.mapUserPrincipal(((Principal) subject.getPrincipals(PrimaryPrincipal.class).toArray()[0]).getName()); - String[] groups = filter.mapGroupPrincipals(actual, subject); - assertThat( actual, is( "mem...@us.apache.org" ) ); - assertThat( groups, is( nullValue() ) ); - - } - -}
http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-identity-assertion-switchcase/src/test/java/org/apache/knox/gateway/identityasserter/switchcase/SwitchCaseIdentityAsserterDeploymentContributorTest.java ---------------------------------------------------------------------- diff --git a/gateway-provider-identity-assertion-switchcase/src/test/java/org/apache/knox/gateway/identityasserter/switchcase/SwitchCaseIdentityAsserterDeploymentContributorTest.java b/gateway-provider-identity-assertion-switchcase/src/test/java/org/apache/knox/gateway/identityasserter/switchcase/SwitchCaseIdentityAsserterDeploymentContributorTest.java new file mode 100644 index 0000000..14a0600 --- /dev/null +++ b/gateway-provider-identity-assertion-switchcase/src/test/java/org/apache/knox/gateway/identityasserter/switchcase/SwitchCaseIdentityAsserterDeploymentContributorTest.java @@ -0,0 +1,44 @@ +/** + * 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.knox.gateway.identityasserter.switchcase; + +import java.util.Iterator; +import java.util.ServiceLoader; + +import org.apache.knox.gateway.deploy.ProviderDeploymentContributor; +import org.junit.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.fail; + +public class SwitchCaseIdentityAsserterDeploymentContributorTest { + + @Test + public void testServiceLoader() throws Exception { + ServiceLoader<ProviderDeploymentContributor> loader = ServiceLoader.load( ProviderDeploymentContributor.class ); + Iterator<ProviderDeploymentContributor> iterator = loader.iterator(); + assertThat( "Service iterator empty.", iterator.hasNext() ); + while( iterator.hasNext() ) { + Object object = iterator.next(); + if( object instanceof SwitchCaseIdentityAsserterDeploymentContributor ) { + return; + } + } + fail( "Failed to find " + SwitchCaseIdentityAsserterDeploymentContributor.class.getName() + " via service loader." ); + } +} http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-identity-assertion-switchcase/src/test/java/org/apache/knox/gateway/identityasserter/switchcase/SwitchCaseIdentityAssertionFilterTest.java ---------------------------------------------------------------------- diff --git a/gateway-provider-identity-assertion-switchcase/src/test/java/org/apache/knox/gateway/identityasserter/switchcase/SwitchCaseIdentityAssertionFilterTest.java b/gateway-provider-identity-assertion-switchcase/src/test/java/org/apache/knox/gateway/identityasserter/switchcase/SwitchCaseIdentityAssertionFilterTest.java new file mode 100644 index 0000000..d1a9462 --- /dev/null +++ b/gateway-provider-identity-assertion-switchcase/src/test/java/org/apache/knox/gateway/identityasserter/switchcase/SwitchCaseIdentityAssertionFilterTest.java @@ -0,0 +1,242 @@ +/** + * 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.knox.gateway.identityasserter.switchcase; + +import java.security.Principal; +import javax.security.auth.Subject; +import javax.servlet.FilterConfig; +import javax.servlet.ServletContext; + +import org.apache.knox.gateway.security.GroupPrincipal; +import org.apache.knox.gateway.security.PrimaryPrincipal; +import org.easymock.EasyMock; +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.arrayContainingInAnyOrder; + +public class SwitchCaseIdentityAssertionFilterTest { + + @Test + public void testDefaultConfig() throws Exception { + FilterConfig config = EasyMock.createNiceMock( FilterConfig.class ); + EasyMock.expect(config.getInitParameter("principal.mapping") ).andReturn( "" ).anyTimes(); + ServletContext context = EasyMock.createNiceMock(ServletContext.class); + EasyMock.expect(config.getServletContext() ).andReturn( context ).anyTimes(); + EasyMock.expect(context.getInitParameter("principal.mapping") ).andReturn( "" ).anyTimes(); + EasyMock.replay( config ); + EasyMock.replay( context ); + + SwitchCaseIdentityAssertionFilter filter = new SwitchCaseIdentityAssertionFilter(); + + Subject subject = new Subject(); + subject.getPrincipals().add( new PrimaryPrincipal( "mem...@us.apache.org" ) ); + subject.getPrincipals().add( new GroupPrincipal( "users" ) ); + subject.getPrincipals().add( new GroupPrincipal( "Admin" ) ); + + filter.init(config); + String actual = filter.mapUserPrincipal(((Principal) subject.getPrincipals(PrimaryPrincipal.class).toArray()[0]).getName()); + String[] groups = filter.mapGroupPrincipals(actual, subject); + assertThat( actual, is( "mem...@us.apache.org" ) ); + assertThat( groups, is( arrayContainingInAnyOrder( "admin", "users" ) ) ); + + } + + @Test + public void testUpperPrincipalAndGroups() throws Exception { + FilterConfig config = EasyMock.createNiceMock( FilterConfig.class ); + EasyMock.expect( config.getInitParameter( "principal.case" ) ).andReturn( "Upper" ).anyTimes(); + EasyMock.expect( config.getInitParameter( "group.principal.case" ) ).andReturn( "Upper" ).anyTimes(); + EasyMock.expect(config.getInitParameter("principal.mapping") ).andReturn( "" ).anyTimes(); + ServletContext context = EasyMock.createNiceMock(ServletContext.class); + EasyMock.expect(config.getServletContext() ).andReturn( context ).anyTimes(); + EasyMock.expect(context.getInitParameter("principal.mapping") ).andReturn( "" ).anyTimes(); + EasyMock.replay( config ); + EasyMock.replay( context ); + + SwitchCaseIdentityAssertionFilter filter = new SwitchCaseIdentityAssertionFilter(); + + Subject subject = new Subject(); + subject.getPrincipals().add( new PrimaryPrincipal( "mem...@us.apache.org" ) ); + subject.getPrincipals().add( new GroupPrincipal( "users" ) ); + subject.getPrincipals().add( new GroupPrincipal( "Admin" ) ); + + filter.init(config); + String actual = filter.mapUserPrincipal(((Principal) subject.getPrincipals(PrimaryPrincipal.class).toArray()[0]).getName()); + String[] groups = filter.mapGroupPrincipals(actual, subject); + assertThat( actual, is( "mem...@us.apache.org" ) ); + assertThat( groups, is( arrayContainingInAnyOrder( "ADMIN", "USERS" ) ) ); + + } + + @Test + public void testLowerPrincipalAndGroups() throws Exception { + FilterConfig config = EasyMock.createNiceMock( FilterConfig.class ); + EasyMock.expect( config.getInitParameter( "principal.case" ) ).andReturn( "lower" ).anyTimes(); + EasyMock.expect( config.getInitParameter( "group.principal.case" ) ).andReturn( "LOWER" ).anyTimes(); + ServletContext context = EasyMock.createNiceMock(ServletContext.class); + EasyMock.expect(config.getServletContext() ).andReturn( context ).anyTimes(); + EasyMock.expect(context.getInitParameter("principal.mapping") ).andReturn( "" ).anyTimes(); + EasyMock.replay( config ); + EasyMock.replay( context ); + + SwitchCaseIdentityAssertionFilter filter = new SwitchCaseIdentityAssertionFilter(); + + Subject subject = new Subject(); + subject.getPrincipals().add(new PrimaryPrincipal( "mem...@us.apache.org" ) ); + subject.getPrincipals().add(new GroupPrincipal( "users" ) ); + subject.getPrincipals().add( new GroupPrincipal( "Admin" ) ); + + filter.init(config); + String actual = filter.mapUserPrincipal(((Principal) subject.getPrincipals(PrimaryPrincipal.class).toArray()[0]).getName()); + String[] groups = filter.mapGroupPrincipals(actual, subject); + assertThat( actual, is( "mem...@us.apache.org" ) ); + assertThat( groups, is( arrayContainingInAnyOrder( "admin", "users" ) ) ); + + } + + @Test + public void testNonePrincipalAndGroups() throws Exception { + FilterConfig config = EasyMock.createNiceMock( FilterConfig.class ); + EasyMock.expect( config.getInitParameter( "principal.case" ) ).andReturn( "none" ).anyTimes(); + EasyMock.expect( config.getInitParameter( "group.principal.case" ) ).andReturn( "NONE" ).anyTimes(); + ServletContext context = EasyMock.createNiceMock(ServletContext.class); + EasyMock.expect(config.getServletContext() ).andReturn( context ).anyTimes(); + EasyMock.expect(context.getInitParameter("principal.mapping") ).andReturn( "" ).anyTimes(); + EasyMock.replay( config ); + EasyMock.replay( context ); + + SwitchCaseIdentityAssertionFilter filter = new SwitchCaseIdentityAssertionFilter(); + + Subject subject = new Subject(); + subject.getPrincipals().add(new PrimaryPrincipal( "mem...@us.apache.org" ) ); + + filter.init(config); + String actual = filter.mapUserPrincipal(((Principal) subject.getPrincipals(PrimaryPrincipal.class).toArray()[0]).getName()); + String[] groups = filter.mapGroupPrincipals(actual, subject); + assertThat( actual, is( "mem...@us.apache.org" ) ); + assertThat( groups, is( nullValue() ) ); + + } + + @Test + public void testDefaultGroupsConfFromUsers() throws Exception { + FilterConfig config = EasyMock.createNiceMock( FilterConfig.class ); + EasyMock.expect( config.getInitParameter( "principal.case" ) ).andReturn( "UPPER" ).anyTimes(); + EasyMock.expect( config.getInitParameter( "group.principal.case" ) ).andReturn( null ).anyTimes(); + ServletContext context = EasyMock.createNiceMock(ServletContext.class); + EasyMock.expect(config.getServletContext() ).andReturn( context ).anyTimes(); + EasyMock.expect(context.getInitParameter("principal.mapping") ).andReturn( "" ).anyTimes(); + EasyMock.replay( config ); + EasyMock.replay( context ); + + SwitchCaseIdentityAssertionFilter filter = new SwitchCaseIdentityAssertionFilter(); + + Subject subject = new Subject(); + subject.getPrincipals().add(new PrimaryPrincipal( "mem...@us.apache.org" ) ); + subject.getPrincipals().add(new GroupPrincipal( "users" ) ); + subject.getPrincipals().add( new GroupPrincipal( "Admin" ) ); + + filter.init(config); + String actual = filter.mapUserPrincipal(((Principal) subject.getPrincipals(PrimaryPrincipal.class).toArray()[0]).getName()); + String[] groups = filter.mapGroupPrincipals(actual, subject); + assertThat( actual, is( "mem...@us.apache.org" ) ); + assertThat( groups, is( arrayContainingInAnyOrder( "ADMIN", "USERS" ) ) ); + + } + + @Test + public void testDefaultGroupsConfOverride() throws Exception { + FilterConfig config = EasyMock.createNiceMock( FilterConfig.class ); + EasyMock.expect( config.getInitParameter( "principal.case" ) ).andReturn( "UPPER" ).anyTimes(); + EasyMock.expect( config.getInitParameter( "group.principal.case" ) ).andReturn( "none" ).anyTimes(); + ServletContext context = EasyMock.createNiceMock(ServletContext.class); + EasyMock.expect(config.getServletContext() ).andReturn( context ).anyTimes(); + EasyMock.expect(context.getInitParameter("principal.mapping") ).andReturn( "" ).anyTimes(); + EasyMock.replay( config ); + EasyMock.replay( context ); + + SwitchCaseIdentityAssertionFilter filter = new SwitchCaseIdentityAssertionFilter(); + + Subject subject = new Subject(); + subject.getPrincipals().add(new PrimaryPrincipal( "mem...@us.apache.org" ) ); + subject.getPrincipals().add(new GroupPrincipal( "users" ) ); + subject.getPrincipals().add( new GroupPrincipal( "Admin" ) ); + + filter.init(config); + String actual = filter.mapUserPrincipal(((Principal) subject.getPrincipals(PrimaryPrincipal.class).toArray()[0]).getName()); + String[] groups = filter.mapGroupPrincipals(actual, subject); + assertThat( actual, is( "mem...@us.apache.org" ) ); + assertThat( groups, is( nullValue() ) ); + + } + + @Test + public void testNone() throws Exception { + FilterConfig config = EasyMock.createNiceMock( FilterConfig.class ); + EasyMock.expect( config.getInitParameter( "principal.case" ) ).andReturn( "none" ).anyTimes(); + EasyMock.expect( config.getInitParameter( "group.principal.case" ) ).andReturn( "none" ).anyTimes(); + ServletContext context = EasyMock.createNiceMock(ServletContext.class); + EasyMock.expect(config.getServletContext() ).andReturn( context ).anyTimes(); + EasyMock.expect(context.getInitParameter("principal.mapping") ).andReturn( "" ).anyTimes(); + EasyMock.replay( config ); + EasyMock.replay( context ); + + SwitchCaseIdentityAssertionFilter filter = new SwitchCaseIdentityAssertionFilter(); + + Subject subject = new Subject(); + subject.getPrincipals().add(new PrimaryPrincipal( "mem...@us.apache.org" ) ); + subject.getPrincipals().add(new GroupPrincipal( "users" ) ); + subject.getPrincipals().add( new GroupPrincipal( "Admin" ) ); + + filter.init(config); + String actual = filter.mapUserPrincipal(((Principal) subject.getPrincipals(PrimaryPrincipal.class).toArray()[0]).getName()); + String[] groups = filter.mapGroupPrincipals(actual, subject); + assertThat( actual, is( "mem...@us.apache.org" ) ); + assertThat( groups, is( nullValue() ) ); + + } + + @Test + public void testNoGroups() throws Exception { + FilterConfig config = EasyMock.createNiceMock( FilterConfig.class ); + EasyMock.expect( config.getInitParameter( "principal.case" ) ).andReturn( "upper" ).anyTimes(); + EasyMock.expect( config.getInitParameter( "group.principal.case" ) ).andReturn( "upper" ).anyTimes(); + EasyMock.expect(config.getInitParameter("principal.mapping") ).andReturn( "" ).anyTimes(); + ServletContext context = EasyMock.createNiceMock(ServletContext.class); + EasyMock.expect(config.getServletContext() ).andReturn( context ).anyTimes(); + EasyMock.expect(context.getInitParameter("principal.mapping") ).andReturn( "" ).anyTimes(); + EasyMock.replay( config ); + EasyMock.replay( context ); + + SwitchCaseIdentityAssertionFilter filter = new SwitchCaseIdentityAssertionFilter(); + + Subject subject = new Subject(); + subject.getPrincipals().add(new PrimaryPrincipal( "mem...@us.apache.org" ) ); + + filter.init(config); + String actual = filter.mapUserPrincipal(((Principal) subject.getPrincipals(PrimaryPrincipal.class).toArray()[0]).getName()); + String[] groups = filter.mapGroupPrincipals(actual, subject); + assertThat( actual, is( "mem...@us.apache.org" ) ); + assertThat( groups, is( nullValue() ) ); + + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-jersey/src/main/java/org/apache/hadoop/gateway/jersey/JerseyDispatchDeploymentContributor.java ---------------------------------------------------------------------- diff --git a/gateway-provider-jersey/src/main/java/org/apache/hadoop/gateway/jersey/JerseyDispatchDeploymentContributor.java b/gateway-provider-jersey/src/main/java/org/apache/hadoop/gateway/jersey/JerseyDispatchDeploymentContributor.java deleted file mode 100644 index 1f40fd7..0000000 --- a/gateway-provider-jersey/src/main/java/org/apache/hadoop/gateway/jersey/JerseyDispatchDeploymentContributor.java +++ /dev/null @@ -1,55 +0,0 @@ -/** - * 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.jersey; - -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 org.glassfish.jersey.servlet.ServletContainer; - -import java.util.List; - -public class JerseyDispatchDeploymentContributor extends ProviderDeploymentContributorBase { - - private static final String FILTER_CLASS_NAME = ServletContainer.class.getName(); - - @Override - public String getRole() { - return "pivot"; - } - - @Override - public String getName() { - return "jersey"; - } - - @Override - public void contributeFilter( DeploymentContext context, Provider provider, Service service, ResourceDescriptor resource, List<FilterParamDescriptor> params ) { - FilterDescriptor filter = resource.addFilter().name( getName() ).role( getRole() ).impl( FILTER_CLASS_NAME ); - if( params != null ) { - for( FilterParamDescriptor param : params ) { - filter.param().name( param.name() ).value( param.value() ); - } - } - } - -} http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-jersey/src/main/java/org/apache/hadoop/gateway/jersey/JerseyServiceDeploymentContributorBase.java ---------------------------------------------------------------------- diff --git a/gateway-provider-jersey/src/main/java/org/apache/hadoop/gateway/jersey/JerseyServiceDeploymentContributorBase.java b/gateway-provider-jersey/src/main/java/org/apache/hadoop/gateway/jersey/JerseyServiceDeploymentContributorBase.java deleted file mode 100644 index 99c5d4b..0000000 --- a/gateway-provider-jersey/src/main/java/org/apache/hadoop/gateway/jersey/JerseyServiceDeploymentContributorBase.java +++ /dev/null @@ -1,67 +0,0 @@ -/** - * 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.jersey; - -import org.apache.commons.lang3.StringUtils; -import org.apache.hadoop.gateway.deploy.DeploymentContext; -import org.apache.hadoop.gateway.deploy.ServiceDeploymentContributorBase; -import org.apache.hadoop.gateway.descriptor.FilterParamDescriptor; -import org.apache.hadoop.gateway.descriptor.ResourceDescriptor; -import org.apache.hadoop.gateway.topology.Service; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -public abstract class JerseyServiceDeploymentContributorBase extends ServiceDeploymentContributorBase { - - private static final String PACKAGES_PARAM = "jersey.config.server.provider.packages"; -// private static final String TRACE_LOGGING_PARAM = "jersey.config.server.tracing"; - - protected abstract String[] getPackages(); - - protected abstract String[] getPatterns(); - - public void contributeService( DeploymentContext context, Service service ) throws Exception { - String packages = StringUtils.join( getPackages(), ";" ); - for( String pattern : getPatterns() ) { - ResourceDescriptor resource = context.getGatewayDescriptor().addResource(); - resource.role( service.getRole() ); - resource.pattern( pattern ); - addWebAppSecFilters( context, service, resource ); - addXForwardedFilter( context, service, resource ); - addAuthenticationFilter( context, service, resource ); - addIdentityAssertionFilter( context, service, resource ); - addAuthorizationFilter( context, service, resource ); - // addRewriteFilter( context, service, resource, null ); - List<FilterParamDescriptor> params = new ArrayList<FilterParamDescriptor>(); - FilterParamDescriptor param = resource.createFilterParam(); - param.name( PACKAGES_PARAM ); - param.value( packages ); - params.add( param ); -// FilterParamDescriptor trace = resource.createFilterParam(); -// param.name( TRACE_LOGGING_PARAM ); -// param.value( "ALL" ); -// params.add( trace ); - for ( Map.Entry<String,String> serviceParam : service.getParams().entrySet() ) { - context.getWebAppDescriptor().createContextParam().paramName(serviceParam.getKey()).paramValue(serviceParam.getValue()); - } - context.contributeFilter( service, resource, "pivot", "jersey", params ); - } - } -} http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-jersey/src/main/java/org/apache/knox/gateway/jersey/JerseyDispatchDeploymentContributor.java ---------------------------------------------------------------------- diff --git a/gateway-provider-jersey/src/main/java/org/apache/knox/gateway/jersey/JerseyDispatchDeploymentContributor.java b/gateway-provider-jersey/src/main/java/org/apache/knox/gateway/jersey/JerseyDispatchDeploymentContributor.java new file mode 100644 index 0000000..19debc9 --- /dev/null +++ b/gateway-provider-jersey/src/main/java/org/apache/knox/gateway/jersey/JerseyDispatchDeploymentContributor.java @@ -0,0 +1,56 @@ +/** + * 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.knox.gateway.jersey; + +import org.apache.knox.gateway.deploy.DeploymentContext; +import org.apache.knox.gateway.deploy.ProviderDeploymentContributorBase; +import org.apache.knox.gateway.descriptor.FilterDescriptor; +import org.apache.knox.gateway.descriptor.FilterParamDescriptor; +import org.apache.knox.gateway.descriptor.ResourceDescriptor; +import org.apache.knox.gateway.topology.Provider; +import org.apache.knox.gateway.topology.Service; +import org.glassfish.jersey.servlet.ServletContainer; + +import java.util.List; + +public class JerseyDispatchDeploymentContributor extends + ProviderDeploymentContributorBase { + + private static final String FILTER_CLASS_NAME = ServletContainer.class.getName(); + + @Override + public String getRole() { + return "pivot"; + } + + @Override + public String getName() { + return "jersey"; + } + + @Override + public void contributeFilter( DeploymentContext context, Provider provider, Service service, ResourceDescriptor resource, List<FilterParamDescriptor> params ) { + FilterDescriptor filter = resource.addFilter().name( getName() ).role( getRole() ).impl( FILTER_CLASS_NAME ); + if( params != null ) { + for( FilterParamDescriptor param : params ) { + filter.param().name( param.name() ).value( param.value() ); + } + } + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-jersey/src/main/java/org/apache/knox/gateway/jersey/JerseyServiceDeploymentContributorBase.java ---------------------------------------------------------------------- diff --git a/gateway-provider-jersey/src/main/java/org/apache/knox/gateway/jersey/JerseyServiceDeploymentContributorBase.java b/gateway-provider-jersey/src/main/java/org/apache/knox/gateway/jersey/JerseyServiceDeploymentContributorBase.java new file mode 100644 index 0000000..95a372a --- /dev/null +++ b/gateway-provider-jersey/src/main/java/org/apache/knox/gateway/jersey/JerseyServiceDeploymentContributorBase.java @@ -0,0 +1,67 @@ +/** + * 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.knox.gateway.jersey; + +import org.apache.commons.lang3.StringUtils; +import org.apache.knox.gateway.deploy.DeploymentContext; +import org.apache.knox.gateway.deploy.ServiceDeploymentContributorBase; +import org.apache.knox.gateway.descriptor.FilterParamDescriptor; +import org.apache.knox.gateway.descriptor.ResourceDescriptor; +import org.apache.knox.gateway.topology.Service; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +public abstract class JerseyServiceDeploymentContributorBase extends ServiceDeploymentContributorBase { + + private static final String PACKAGES_PARAM = "jersey.config.server.provider.packages"; +// private static final String TRACE_LOGGING_PARAM = "jersey.config.server.tracing"; + + protected abstract String[] getPackages(); + + protected abstract String[] getPatterns(); + + public void contributeService( DeploymentContext context, Service service ) throws Exception { + String packages = StringUtils.join( getPackages(), ";" ); + for( String pattern : getPatterns() ) { + ResourceDescriptor resource = context.getGatewayDescriptor().addResource(); + resource.role( service.getRole() ); + resource.pattern( pattern ); + addWebAppSecFilters( context, service, resource ); + addXForwardedFilter( context, service, resource ); + addAuthenticationFilter( context, service, resource ); + addIdentityAssertionFilter( context, service, resource ); + addAuthorizationFilter( context, service, resource ); + // addRewriteFilter( context, service, resource, null ); + List<FilterParamDescriptor> params = new ArrayList<FilterParamDescriptor>(); + FilterParamDescriptor param = resource.createFilterParam(); + param.name( PACKAGES_PARAM ); + param.value( packages ); + params.add( param ); +// FilterParamDescriptor trace = resource.createFilterParam(); +// param.name( TRACE_LOGGING_PARAM ); +// param.value( "ALL" ); +// params.add( trace ); + for ( Map.Entry<String,String> serviceParam : service.getParams().entrySet() ) { + context.getWebAppDescriptor().createContextParam().paramName(serviceParam.getKey()).paramValue(serviceParam.getValue()); + } + context.contributeFilter( service, resource, "pivot", "jersey", params ); + } + } +} http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-jersey/src/main/resources/META-INF/services/org.apache.hadoop.gateway.deploy.ProviderDeploymentContributor ---------------------------------------------------------------------- diff --git a/gateway-provider-jersey/src/main/resources/META-INF/services/org.apache.hadoop.gateway.deploy.ProviderDeploymentContributor b/gateway-provider-jersey/src/main/resources/META-INF/services/org.apache.hadoop.gateway.deploy.ProviderDeploymentContributor deleted file mode 100644 index 70af7b4..0000000 --- a/gateway-provider-jersey/src/main/resources/META-INF/services/org.apache.hadoop.gateway.deploy.ProviderDeploymentContributor +++ /dev/null @@ -1,19 +0,0 @@ -########################################################################## -# 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. -########################################################################## - -org.apache.hadoop.gateway.jersey.JerseyDispatchDeploymentContributor \ No newline at end of file http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-jersey/src/main/resources/META-INF/services/org.apache.knox.gateway.deploy.ProviderDeploymentContributor ---------------------------------------------------------------------- diff --git a/gateway-provider-jersey/src/main/resources/META-INF/services/org.apache.knox.gateway.deploy.ProviderDeploymentContributor b/gateway-provider-jersey/src/main/resources/META-INF/services/org.apache.knox.gateway.deploy.ProviderDeploymentContributor new file mode 100644 index 0000000..27a190a --- /dev/null +++ b/gateway-provider-jersey/src/main/resources/META-INF/services/org.apache.knox.gateway.deploy.ProviderDeploymentContributor @@ -0,0 +1,19 @@ +########################################################################## +# 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. +########################################################################## + +org.apache.knox.gateway.jersey.JerseyDispatchDeploymentContributor \ No newline at end of file http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-jersey/src/test/java/org/apache/hadoop/gateway/jersey/JerseyDeploymentContributorTest.java ---------------------------------------------------------------------- diff --git a/gateway-provider-jersey/src/test/java/org/apache/hadoop/gateway/jersey/JerseyDeploymentContributorTest.java b/gateway-provider-jersey/src/test/java/org/apache/hadoop/gateway/jersey/JerseyDeploymentContributorTest.java deleted file mode 100644 index 0721ddc..0000000 --- a/gateway-provider-jersey/src/test/java/org/apache/hadoop/gateway/jersey/JerseyDeploymentContributorTest.java +++ /dev/null @@ -1,259 +0,0 @@ -/** - * 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.jersey; - -import org.apache.hadoop.gateway.config.GatewayConfig; -import org.apache.hadoop.gateway.deploy.DeploymentContext; -import org.apache.hadoop.gateway.deploy.ProviderDeploymentContributor; -import org.apache.hadoop.gateway.descriptor.FilterDescriptor; -import org.apache.hadoop.gateway.descriptor.FilterParamDescriptor; -import org.apache.hadoop.gateway.descriptor.GatewayDescriptor; -import org.apache.hadoop.gateway.descriptor.GatewayDescriptorFactory; -import org.apache.hadoop.gateway.descriptor.ResourceDescriptor; -import org.apache.hadoop.gateway.topology.Provider; -import org.apache.hadoop.gateway.topology.Service; -import org.apache.hadoop.gateway.topology.Topology; -import org.easymock.EasyMock; -import org.jboss.shrinkwrap.api.ShrinkWrap; -import org.jboss.shrinkwrap.api.spec.WebArchive; -import org.jboss.shrinkwrap.descriptor.api.webapp30.WebAppDescriptor; -import org.junit.Test; - -import java.util.Iterator; -import java.util.List; -import java.util.ServiceLoader; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.Assert.fail; - -public class JerseyDeploymentContributorTest { - - @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 JerseyDispatchDeploymentContributor ) { - return; - } - } - fail( "Failed to find " + JerseyDispatchDeploymentContributor.class.getName() + " via service loader." ); - } - - @Test - public void testDeploymentContributors() throws Exception { - JerseyDispatchDeploymentContributor providerContributor = new JerseyDispatchDeploymentContributor(); - assertThat( providerContributor.getRole(), is( "pivot" ) ); - assertThat( providerContributor.getName(), is( "jersey" ) ); - - MockJerseyService serviceContributor = new MockJerseyService(); - - WebArchive webArchive = ShrinkWrap.create( WebArchive.class, "test-archive" ); - - Topology topology = new Topology(); - topology.setName( "test-topology" ); - Provider provider = new Provider(); - provider.setRole( "pivot" ); - provider.setName( "jersey" ); - provider.setEnabled( true ); - topology.addProvider( provider ); - - GatewayDescriptor descriptor = GatewayDescriptorFactory.create(); - - DeploymentContext context = EasyMock.createNiceMock( DeploymentContext.class ); - EasyMock.expect( context.getWebArchive() ).andReturn( webArchive ).anyTimes(); - EasyMock.expect( context.getTopology() ).andReturn( topology ).anyTimes(); - EasyMock.expect( context.getGatewayDescriptor() ).andReturn( descriptor ).anyTimes(); - context.contributeFilter( - EasyMock.<Service> isA( Service.class ), - EasyMock.<ResourceDescriptor> isA( ResourceDescriptor.class ), - EasyMock.<String> isA( String.class ), - EasyMock.<String> isA( String.class ), - EasyMock.<List> isA( List.class ) ); - EasyMock.expectLastCall().andDelegateTo( - new MockDeploymentContext( context, providerContributor, provider ) ).anyTimes(); - - EasyMock.replay( context ); - - // Just make sure they don't blow up. - providerContributor.initializeContribution( context ); - serviceContributor.initializeContribution( context ); - - Service service = new Service(); - service.setRole( "test-service-role" ); - service.setName( "test-service-name" ); - service.addUrl( "http://test-service-host:777/test-service-path" ); - - // This should end up calling providerContributor.contributeFilter - serviceContributor.contributeService( context, service ); - ResourceDescriptor resource = context.getGatewayDescriptor().resources().get( 0 ); - - // Just make sure they don't blow up. - serviceContributor.finalizeContribution( context ); - providerContributor.finalizeContribution( context ); - - /* - GatewayDescriptorFactory.store( descriptor, "xml", new PrintWriter( System.out ) ); - <?xml version="1.0" encoding="UTF-8" standalone="yes"?> - <gateway> - <resource> - <role>test-service-role</role> - <pattern>test-service/?**</pattern> - <filter> - <role>dispatch</role> - <name>jersey</name> - <class>org.glassfish.jersey.servlet.ServletContainer</class> - <param> - <name>jersey.config.server.provider.packages</name> - <value>test-package-1;test-package-2</value> - </param> - </filter> - </resource> - <resource> - <role>test-service-role</role> - <pattern>test-service/**?**</pattern> - <filter> - <role>dispatch</role> - <name>jersey</name> - <class>org.glassfish.jersey.servlet.ServletContainer</class> - <param> - <name>jersey.config.server.provider.packages</name> - <value>test-package-1;test-package-2</value> - </param> - </filter> - </resource> - </gateway> - */ - List<ResourceDescriptor> resources = context.getGatewayDescriptor().resources(); - assertThat( resources.size(), is( 2 ) ); - - resource = resources.get( 0 ); - assertThat( resource.role(), is( "test-service-role" ) ); - assertThat( resource.pattern(), is( "test-service/?**" ) ); - List<FilterDescriptor> filters = resource.filters(); - assertThat( filters.size(), is( 1 ) ); - FilterDescriptor filter = filters.get( 0 ); - assertThat( filter.role(), is( "pivot") ); - assertThat( filter.name(), is( "jersey" ) ); - assertThat( filter.impl(), is( "org.glassfish.jersey.servlet.ServletContainer" ) ); - List<FilterParamDescriptor> params = filter.params(); - assertThat( params.size(), is( 1 ) ); - FilterParamDescriptor param = params.get( 0 ); - assertThat( param.name(), is( "jersey.config.server.provider.packages" ) ); - assertThat( param.value(), is( "test-package-1;test-package-2" ) ); - - resource = resources.get( 1 ); - assertThat( resource.role(), is( "test-service-role" ) ); - assertThat( resource.pattern(), is( "test-service/**?**" ) ); - filters = resource.filters(); - assertThat( filters.size(), is( 1 ) ); - filter = filters.get( 0 ); - assertThat( filter.role(), is( "pivot") ); - assertThat( filter.name(), is( "jersey" ) ); - assertThat( filter.impl(), is( "org.glassfish.jersey.servlet.ServletContainer" ) ); - params = filter.params(); - assertThat( params.size(), is( 1 ) ); - param = params.get( 0 ); - assertThat( param.name(), is( "jersey.config.server.provider.packages" ) ); - assertThat( param.value(), is( "test-package-1;test-package-2" ) ); - } - - private static class MockJerseyService extends JerseyServiceDeploymentContributorBase { - - @Override - protected String[] getPatterns() { - return new String[]{ "test-service/?**", "test-service/**?**" }; - } - - @Override - protected String[] getPackages() { - return new String[]{ "test-package-1", "test-package-2" }; - } - - @Override - public String getRole() { - return "test-service-role"; - } - - @Override - public String getName() { - return "test-service-name"; - } - - } - - private static class MockDeploymentContext implements DeploymentContext { - - DeploymentContext context; - ProviderDeploymentContributor providerContributor; - Provider provider; - - public MockDeploymentContext( - DeploymentContext context, - ProviderDeploymentContributor providerContributor, - Provider provider ) { - this.context = context; - this.providerContributor = providerContributor; - this.provider = provider; - } - - @Override - public GatewayConfig getGatewayConfig() { - return null; - } - - @Override - public Topology getTopology() { - return null; - } - - @Override - public WebArchive getWebArchive() { - return null; - } - - @Override - public WebAppDescriptor getWebAppDescriptor() { - return null; - } - - @Override - public GatewayDescriptor getGatewayDescriptor() { - return null; - } - - @Override - public void contributeFilter( Service service, ResourceDescriptor resource, String role, String name, List<FilterParamDescriptor> params ) { - providerContributor.contributeFilter( context, provider, service, resource, params ); - } - - @Override - public void addDescriptor( String name, Object descriptor ) { - } - - @Override - public <T> T getDescriptor( String name ) { - return null; - } - - } - -} http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-jersey/src/test/java/org/apache/knox/gateway/jersey/JerseyDeploymentContributorTest.java ---------------------------------------------------------------------- diff --git a/gateway-provider-jersey/src/test/java/org/apache/knox/gateway/jersey/JerseyDeploymentContributorTest.java b/gateway-provider-jersey/src/test/java/org/apache/knox/gateway/jersey/JerseyDeploymentContributorTest.java new file mode 100644 index 0000000..3f48e36 --- /dev/null +++ b/gateway-provider-jersey/src/test/java/org/apache/knox/gateway/jersey/JerseyDeploymentContributorTest.java @@ -0,0 +1,259 @@ +/** + * 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.knox.gateway.jersey; + +import org.apache.knox.gateway.config.GatewayConfig; +import org.apache.knox.gateway.deploy.DeploymentContext; +import org.apache.knox.gateway.deploy.ProviderDeploymentContributor; +import org.apache.knox.gateway.descriptor.FilterDescriptor; +import org.apache.knox.gateway.descriptor.FilterParamDescriptor; +import org.apache.knox.gateway.descriptor.GatewayDescriptor; +import org.apache.knox.gateway.descriptor.GatewayDescriptorFactory; +import org.apache.knox.gateway.descriptor.ResourceDescriptor; +import org.apache.knox.gateway.topology.Provider; +import org.apache.knox.gateway.topology.Service; +import org.apache.knox.gateway.topology.Topology; +import org.easymock.EasyMock; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.spec.WebArchive; +import org.jboss.shrinkwrap.descriptor.api.webapp30.WebAppDescriptor; +import org.junit.Test; + +import java.util.Iterator; +import java.util.List; +import java.util.ServiceLoader; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.fail; + +public class JerseyDeploymentContributorTest { + + @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 JerseyDispatchDeploymentContributor ) { + return; + } + } + fail( "Failed to find " + JerseyDispatchDeploymentContributor.class.getName() + " via service loader." ); + } + + @Test + public void testDeploymentContributors() throws Exception { + JerseyDispatchDeploymentContributor providerContributor = new JerseyDispatchDeploymentContributor(); + assertThat( providerContributor.getRole(), is( "pivot" ) ); + assertThat( providerContributor.getName(), is( "jersey" ) ); + + MockJerseyService serviceContributor = new MockJerseyService(); + + WebArchive webArchive = ShrinkWrap.create( WebArchive.class, "test-archive" ); + + Topology topology = new Topology(); + topology.setName( "test-topology" ); + Provider provider = new Provider(); + provider.setRole( "pivot" ); + provider.setName( "jersey" ); + provider.setEnabled( true ); + topology.addProvider( provider ); + + GatewayDescriptor descriptor = GatewayDescriptorFactory.create(); + + DeploymentContext context = EasyMock.createNiceMock( DeploymentContext.class ); + EasyMock.expect( context.getWebArchive() ).andReturn( webArchive ).anyTimes(); + EasyMock.expect( context.getTopology() ).andReturn( topology ).anyTimes(); + EasyMock.expect( context.getGatewayDescriptor() ).andReturn( descriptor ).anyTimes(); + context.contributeFilter( + EasyMock.<Service> isA( Service.class ), + EasyMock.<ResourceDescriptor> isA( ResourceDescriptor.class ), + EasyMock.<String> isA( String.class ), + EasyMock.<String> isA( String.class ), + EasyMock.<List> isA( List.class ) ); + EasyMock.expectLastCall().andDelegateTo( + new MockDeploymentContext( context, providerContributor, provider ) ).anyTimes(); + + EasyMock.replay( context ); + + // Just make sure they don't blow up. + providerContributor.initializeContribution( context ); + serviceContributor.initializeContribution( context ); + + Service service = new Service(); + service.setRole( "test-service-role" ); + service.setName( "test-service-name" ); + service.addUrl( "http://test-service-host:777/test-service-path" ); + + // This should end up calling providerContributor.contributeFilter + serviceContributor.contributeService( context, service ); + ResourceDescriptor resource = context.getGatewayDescriptor().resources().get( 0 ); + + // Just make sure they don't blow up. + serviceContributor.finalizeContribution( context ); + providerContributor.finalizeContribution( context ); + + /* + GatewayDescriptorFactory.store( descriptor, "xml", new PrintWriter( System.out ) ); + <?xml version="1.0" encoding="UTF-8" standalone="yes"?> + <gateway> + <resource> + <role>test-service-role</role> + <pattern>test-service/?**</pattern> + <filter> + <role>dispatch</role> + <name>jersey</name> + <class>org.glassfish.jersey.servlet.ServletContainer</class> + <param> + <name>jersey.config.server.provider.packages</name> + <value>test-package-1;test-package-2</value> + </param> + </filter> + </resource> + <resource> + <role>test-service-role</role> + <pattern>test-service/**?**</pattern> + <filter> + <role>dispatch</role> + <name>jersey</name> + <class>org.glassfish.jersey.servlet.ServletContainer</class> + <param> + <name>jersey.config.server.provider.packages</name> + <value>test-package-1;test-package-2</value> + </param> + </filter> + </resource> + </gateway> + */ + List<ResourceDescriptor> resources = context.getGatewayDescriptor().resources(); + assertThat( resources.size(), is( 2 ) ); + + resource = resources.get( 0 ); + assertThat( resource.role(), is( "test-service-role" ) ); + assertThat( resource.pattern(), is( "test-service/?**" ) ); + List<FilterDescriptor> filters = resource.filters(); + assertThat( filters.size(), is( 1 ) ); + FilterDescriptor filter = filters.get( 0 ); + assertThat( filter.role(), is( "pivot") ); + assertThat( filter.name(), is( "jersey" ) ); + assertThat( filter.impl(), is( "org.glassfish.jersey.servlet.ServletContainer" ) ); + List<FilterParamDescriptor> params = filter.params(); + assertThat( params.size(), is( 1 ) ); + FilterParamDescriptor param = params.get( 0 ); + assertThat( param.name(), is( "jersey.config.server.provider.packages" ) ); + assertThat( param.value(), is( "test-package-1;test-package-2" ) ); + + resource = resources.get( 1 ); + assertThat( resource.role(), is( "test-service-role" ) ); + assertThat( resource.pattern(), is( "test-service/**?**" ) ); + filters = resource.filters(); + assertThat( filters.size(), is( 1 ) ); + filter = filters.get( 0 ); + assertThat( filter.role(), is( "pivot") ); + assertThat( filter.name(), is( "jersey" ) ); + assertThat( filter.impl(), is( "org.glassfish.jersey.servlet.ServletContainer" ) ); + params = filter.params(); + assertThat( params.size(), is( 1 ) ); + param = params.get( 0 ); + assertThat( param.name(), is( "jersey.config.server.provider.packages" ) ); + assertThat( param.value(), is( "test-package-1;test-package-2" ) ); + } + + private static class MockJerseyService extends JerseyServiceDeploymentContributorBase { + + @Override + protected String[] getPatterns() { + return new String[]{ "test-service/?**", "test-service/**?**" }; + } + + @Override + protected String[] getPackages() { + return new String[]{ "test-package-1", "test-package-2" }; + } + + @Override + public String getRole() { + return "test-service-role"; + } + + @Override + public String getName() { + return "test-service-name"; + } + + } + + private static class MockDeploymentContext implements DeploymentContext { + + DeploymentContext context; + ProviderDeploymentContributor providerContributor; + Provider provider; + + public MockDeploymentContext( + DeploymentContext context, + ProviderDeploymentContributor providerContributor, + Provider provider ) { + this.context = context; + this.providerContributor = providerContributor; + this.provider = provider; + } + + @Override + public GatewayConfig getGatewayConfig() { + return null; + } + + @Override + public Topology getTopology() { + return null; + } + + @Override + public WebArchive getWebArchive() { + return null; + } + + @Override + public WebAppDescriptor getWebAppDescriptor() { + return null; + } + + @Override + public GatewayDescriptor getGatewayDescriptor() { + return null; + } + + @Override + public void contributeFilter( Service service, ResourceDescriptor resource, String role, String name, List<FilterParamDescriptor> params ) { + providerContributor.contributeFilter( context, provider, service, resource, params ); + } + + @Override + public void addDescriptor( String name, Object descriptor ) { + } + + @Override + public <T> T getDescriptor( String name ) { + return null; + } + + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-rewrite-func-hostmap-static/src/main/java/org/apache/hadoop/gateway/hostmap/api/HostmapFunctionDescriptor.java ---------------------------------------------------------------------- diff --git a/gateway-provider-rewrite-func-hostmap-static/src/main/java/org/apache/hadoop/gateway/hostmap/api/HostmapFunctionDescriptor.java b/gateway-provider-rewrite-func-hostmap-static/src/main/java/org/apache/hadoop/gateway/hostmap/api/HostmapFunctionDescriptor.java deleted file mode 100644 index 01d41b9..0000000 --- a/gateway-provider-rewrite-func-hostmap-static/src/main/java/org/apache/hadoop/gateway/hostmap/api/HostmapFunctionDescriptor.java +++ /dev/null @@ -1,50 +0,0 @@ -/** - * 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.hostmap.api; - -import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteFunctionDescriptor; - -public class HostmapFunctionDescriptor implements UrlRewriteFunctionDescriptor<HostmapFunctionDescriptor> { - - public static final String FUNCTION_NAME = "hostmap"; - - private String configLocation; - - @Override - public String name() { - return FUNCTION_NAME; - } - - public HostmapFunctionDescriptor config( String configLocation ) { - this.configLocation = configLocation; - return this; - } - - public String config() { - return configLocation; - } - - public String getConfig() { - return config(); - } - - public void setConfig( String configLocation ) { - config( configLocation ); - } - -} http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-rewrite-func-hostmap-static/src/main/java/org/apache/hadoop/gateway/hostmap/impl/HostmapDeploymentContributor.java ---------------------------------------------------------------------- diff --git a/gateway-provider-rewrite-func-hostmap-static/src/main/java/org/apache/hadoop/gateway/hostmap/impl/HostmapDeploymentContributor.java b/gateway-provider-rewrite-func-hostmap-static/src/main/java/org/apache/hadoop/gateway/hostmap/impl/HostmapDeploymentContributor.java deleted file mode 100644 index fdd0b03..0000000 --- a/gateway-provider-rewrite-func-hostmap-static/src/main/java/org/apache/hadoop/gateway/hostmap/impl/HostmapDeploymentContributor.java +++ /dev/null @@ -1,99 +0,0 @@ -/** - * 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.hostmap.impl; - -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.filter.rewrite.api.UrlRewriteRulesDescriptor; -import org.apache.hadoop.gateway.hostmap.api.HostmapFunctionDescriptor; -import org.apache.hadoop.gateway.topology.Provider; -import org.apache.hadoop.gateway.topology.Service; -import org.jboss.shrinkwrap.api.asset.Asset; -import org.jboss.shrinkwrap.api.asset.StringAsset; - -import java.io.PrintWriter; -import java.io.StringWriter; -import java.util.List; -import java.util.Map; - -public class HostmapDeploymentContributor - extends ProviderDeploymentContributorBase - implements ProviderDeploymentContributor { - - public static final String PROVIDER_ROLE_NAME = HostmapFunctionDescriptor.FUNCTION_NAME; - public static final String PROVIDER_IMPL_NAME = "static"; - private static final String REWRITE_ROLE_NAME = "rewrite"; - - @Override - public String getRole() { - return PROVIDER_ROLE_NAME; - } - - @Override - public String getName() { - return PROVIDER_IMPL_NAME; - } - - // Write the provider init params to the hostmap.txt file. - // Add the function to the rewrite descriptor providing the location of the hostmap.txt file. - @Override - public void contributeProvider( DeploymentContext context, Provider provider ) { - if( provider.isEnabled() ) { - 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 ) { - // NoOp. - } - -} http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-rewrite-func-hostmap-static/src/main/java/org/apache/hadoop/gateway/hostmap/impl/HostmapFunctionProcessor.java ---------------------------------------------------------------------- diff --git a/gateway-provider-rewrite-func-hostmap-static/src/main/java/org/apache/hadoop/gateway/hostmap/impl/HostmapFunctionProcessor.java b/gateway-provider-rewrite-func-hostmap-static/src/main/java/org/apache/hadoop/gateway/hostmap/impl/HostmapFunctionProcessor.java deleted file mode 100644 index ca55c16..0000000 --- a/gateway-provider-rewrite-func-hostmap-static/src/main/java/org/apache/hadoop/gateway/hostmap/impl/HostmapFunctionProcessor.java +++ /dev/null @@ -1,91 +0,0 @@ -/** - * 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.hostmap.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.hostmap.api.HostmapFunctionDescriptor; -import org.apache.hadoop.gateway.services.GatewayServices; -import org.apache.hadoop.gateway.services.hostmap.FileBasedHostMapper; -import org.apache.hadoop.gateway.services.hostmap.HostMapper; -import org.apache.hadoop.gateway.services.hostmap.HostMapperService; - -import java.net.URL; -import java.util.ArrayList; -import java.util.List; - -public class HostmapFunctionProcessor - implements UrlRewriteFunctionProcessor<HostmapFunctionDescriptor> { - - public static final String DESCRIPTOR_DEFAULT_FILE_NAME = "hostmap.txt"; - public static final String DESCRIPTOR_DEFAULT_LOCATION = "/WEB-INF/" + DESCRIPTOR_DEFAULT_FILE_NAME; - - private HostMapperService hostMapperService; - private HostMapper hostMapper = null; - private String clusterName; - - @Override - public String name() { - return HostmapFunctionDescriptor.FUNCTION_NAME; - } - - @Override - public void initialize( UrlRewriteEnvironment environment, HostmapFunctionDescriptor descriptor ) throws Exception { - URL url = environment.getResource( DESCRIPTOR_DEFAULT_LOCATION ); - hostMapper = new FileBasedHostMapper( url ); - clusterName = environment.getAttribute( GatewayServices.GATEWAY_CLUSTER_ATTRIBUTE ); - GatewayServices services = environment.getAttribute( GatewayServices.GATEWAY_SERVICES_ATTRIBUTE ); - if( clusterName != null && services != null ) { - hostMapperService = services.getService( GatewayServices.HOST_MAPPING_SERVICE ); - if( hostMapperService != null ) { - hostMapperService.registerHostMapperForCluster( clusterName, hostMapper ); - } - } - } - - @Override - public void destroy() throws Exception { - if( hostMapperService != null && clusterName != null ) { - hostMapperService.removeHostMapperForCluster( clusterName ); - } - } - - @Override - public List<String> resolve( UrlRewriteContext context, List<String> parameters ) throws Exception { - List<String> result = null; - if( parameters != null ) { - result = new ArrayList<String>( parameters.size() ); - for( String parameter : parameters ) { - switch( context.getDirection() ) { - case IN: - parameter = hostMapper.resolveInboundHostName( parameter ); - break; - case OUT: - parameter = hostMapper.resolveOutboundHostName( parameter ); - break; - } - result.add( parameter ); - } -// System.out.println( "HOSTMAP: " + parameter + "->" + value ); - } - return result; - } - -} - http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-rewrite-func-hostmap-static/src/main/java/org/apache/knox/gateway/hostmap/api/HostmapFunctionDescriptor.java ---------------------------------------------------------------------- diff --git a/gateway-provider-rewrite-func-hostmap-static/src/main/java/org/apache/knox/gateway/hostmap/api/HostmapFunctionDescriptor.java b/gateway-provider-rewrite-func-hostmap-static/src/main/java/org/apache/knox/gateway/hostmap/api/HostmapFunctionDescriptor.java new file mode 100644 index 0000000..1071bc6 --- /dev/null +++ b/gateway-provider-rewrite-func-hostmap-static/src/main/java/org/apache/knox/gateway/hostmap/api/HostmapFunctionDescriptor.java @@ -0,0 +1,51 @@ +/** + * 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.knox.gateway.hostmap.api; + +import org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFunctionDescriptor; + +public class HostmapFunctionDescriptor implements + UrlRewriteFunctionDescriptor<HostmapFunctionDescriptor> { + + public static final String FUNCTION_NAME = "hostmap"; + + private String configLocation; + + @Override + public String name() { + return FUNCTION_NAME; + } + + public HostmapFunctionDescriptor config( String configLocation ) { + this.configLocation = configLocation; + return this; + } + + public String config() { + return configLocation; + } + + public String getConfig() { + return config(); + } + + public void setConfig( String configLocation ) { + config( configLocation ); + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-rewrite-func-hostmap-static/src/main/java/org/apache/knox/gateway/hostmap/impl/HostmapDeploymentContributor.java ---------------------------------------------------------------------- diff --git a/gateway-provider-rewrite-func-hostmap-static/src/main/java/org/apache/knox/gateway/hostmap/impl/HostmapDeploymentContributor.java b/gateway-provider-rewrite-func-hostmap-static/src/main/java/org/apache/knox/gateway/hostmap/impl/HostmapDeploymentContributor.java new file mode 100644 index 0000000..d39090e --- /dev/null +++ b/gateway-provider-rewrite-func-hostmap-static/src/main/java/org/apache/knox/gateway/hostmap/impl/HostmapDeploymentContributor.java @@ -0,0 +1,99 @@ +/** + * 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.knox.gateway.hostmap.impl; + +import org.apache.knox.gateway.deploy.DeploymentContext; +import org.apache.knox.gateway.deploy.ProviderDeploymentContributor; +import org.apache.knox.gateway.deploy.ProviderDeploymentContributorBase; +import org.apache.knox.gateway.descriptor.FilterParamDescriptor; +import org.apache.knox.gateway.descriptor.ResourceDescriptor; +import org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor; +import org.apache.knox.gateway.hostmap.api.HostmapFunctionDescriptor; +import org.apache.knox.gateway.topology.Provider; +import org.apache.knox.gateway.topology.Service; +import org.jboss.shrinkwrap.api.asset.Asset; +import org.jboss.shrinkwrap.api.asset.StringAsset; + +import java.io.PrintWriter; +import java.io.StringWriter; +import java.util.List; +import java.util.Map; + +public class HostmapDeploymentContributor + extends ProviderDeploymentContributorBase + implements ProviderDeploymentContributor { + + public static final String PROVIDER_ROLE_NAME = HostmapFunctionDescriptor.FUNCTION_NAME; + public static final String PROVIDER_IMPL_NAME = "static"; + private static final String REWRITE_ROLE_NAME = "rewrite"; + + @Override + public String getRole() { + return PROVIDER_ROLE_NAME; + } + + @Override + public String getName() { + return PROVIDER_IMPL_NAME; + } + + // Write the provider init params to the hostmap.txt file. + // Add the function to the rewrite descriptor providing the location of the hostmap.txt file. + @Override + public void contributeProvider( DeploymentContext context, Provider provider ) { + if( provider.isEnabled() ) { + 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 ) { + // NoOp. + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/af9b0c3d/gateway-provider-rewrite-func-hostmap-static/src/main/java/org/apache/knox/gateway/hostmap/impl/HostmapFunctionProcessor.java ---------------------------------------------------------------------- diff --git a/gateway-provider-rewrite-func-hostmap-static/src/main/java/org/apache/knox/gateway/hostmap/impl/HostmapFunctionProcessor.java b/gateway-provider-rewrite-func-hostmap-static/src/main/java/org/apache/knox/gateway/hostmap/impl/HostmapFunctionProcessor.java new file mode 100644 index 0000000..5b16b47 --- /dev/null +++ b/gateway-provider-rewrite-func-hostmap-static/src/main/java/org/apache/knox/gateway/hostmap/impl/HostmapFunctionProcessor.java @@ -0,0 +1,92 @@ +/** + * 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.knox.gateway.hostmap.impl; + +import org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment; +import org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteContext; +import org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteFunctionProcessor; +import org.apache.knox.gateway.hostmap.api.HostmapFunctionDescriptor; +import org.apache.knox.gateway.services.GatewayServices; +import org.apache.knox.gateway.services.hostmap.FileBasedHostMapper; +import org.apache.knox.gateway.services.hostmap.HostMapper; +import org.apache.knox.gateway.services.hostmap.HostMapperService; +import org.apache.knox.gateway.filter.rewrite.api.UrlRewriter; + +import java.net.URL; +import java.util.ArrayList; +import java.util.List; + +public class HostmapFunctionProcessor + implements UrlRewriteFunctionProcessor<HostmapFunctionDescriptor> { + + public static final String DESCRIPTOR_DEFAULT_FILE_NAME = "hostmap.txt"; + public static final String DESCRIPTOR_DEFAULT_LOCATION = "/WEB-INF/" + DESCRIPTOR_DEFAULT_FILE_NAME; + + private HostMapperService hostMapperService; + private HostMapper hostMapper = null; + private String clusterName; + + @Override + public String name() { + return HostmapFunctionDescriptor.FUNCTION_NAME; + } + + @Override + public void initialize( UrlRewriteEnvironment environment, HostmapFunctionDescriptor descriptor ) throws Exception { + URL url = environment.getResource( DESCRIPTOR_DEFAULT_LOCATION ); + hostMapper = new FileBasedHostMapper( url ); + clusterName = environment.getAttribute( GatewayServices.GATEWAY_CLUSTER_ATTRIBUTE ); + GatewayServices services = environment.getAttribute( GatewayServices.GATEWAY_SERVICES_ATTRIBUTE ); + if( clusterName != null && services != null ) { + hostMapperService = services.getService( GatewayServices.HOST_MAPPING_SERVICE ); + if( hostMapperService != null ) { + hostMapperService.registerHostMapperForCluster( clusterName, hostMapper ); + } + } + } + + @Override + public void destroy() throws Exception { + if( hostMapperService != null && clusterName != null ) { + hostMapperService.removeHostMapperForCluster( clusterName ); + } + } + + @Override + public List<String> resolve( UrlRewriteContext context, List<String> parameters ) throws Exception { + List<String> result = null; + if( parameters != null ) { + result = new ArrayList<String>( parameters.size() ); + for( String parameter : parameters ) { + switch( context.getDirection() ) { + case UrlRewriter.Direction.IN: + parameter = hostMapper.resolveInboundHostName( parameter ); + break; + case UrlRewriter.Direction.OUT: + parameter = hostMapper.resolveOutboundHostName( parameter ); + break; + } + result.add( parameter ); + } +// System.out.println( "HOSTMAP: " + parameter + "->" + value ); + } + return result; + } + +} +