Repository: knox Updated Branches: refs/heads/master 4bdf635ce -> 9d385e457
KNOX-1305 - Add and rewrite functions Project: http://git-wip-us.apache.org/repos/asf/knox/repo Commit: http://git-wip-us.apache.org/repos/asf/knox/commit/9d385e45 Tree: http://git-wip-us.apache.org/repos/asf/knox/tree/9d385e45 Diff: http://git-wip-us.apache.org/repos/asf/knox/diff/9d385e45 Branch: refs/heads/master Commit: 9d385e457d9bed289fca2730c9d2be6c3996c76a Parents: 4bdf635 Author: Sandeep More <[email protected]> Authored: Wed May 9 16:36:14 2018 -0400 Committer: Sandeep More <[email protected]> Committed: Wed May 9 16:36:14 2018 -0400 ---------------------------------------------------------------------- .../rewrite/impl/html/HtmlInfixDescriptor.java | 48 +++++++++ .../rewrite/impl/html/HtmlInfixProcessor.java | 104 +++++++++++++++++++ .../impl/html/HtmlPostfixDescriptor.java | 48 +++++++++ .../rewrite/impl/html/HtmlPostfixProcessor.java | 103 ++++++++++++++++++ ...ter.rewrite.api.UrlRewriteFunctionDescriptor | 4 +- ...lter.rewrite.spi.UrlRewriteFunctionProcessor | 4 +- .../api/UrlRewriteServletFilterTest.java | 82 ++++++++++++++- .../impl/html/HtmlInfixProcessorTest.java | 57 ++++++++++ .../impl/html/HtmlPostfixProcessorTest.java | 57 ++++++++++ .../api/UrlRewriteServletFilterTest/rewrite.xml | 19 ++++ 10 files changed, 523 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/knox/blob/9d385e45/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/html/HtmlInfixDescriptor.java ---------------------------------------------------------------------- diff --git a/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/html/HtmlInfixDescriptor.java b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/html/HtmlInfixDescriptor.java new file mode 100644 index 0000000..df68d9e --- /dev/null +++ b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/html/HtmlInfixDescriptor.java @@ -0,0 +1,48 @@ +package org.apache.knox.gateway.filter.rewrite.impl.html; + +import org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFunctionDescriptor; + +/** + * 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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. + */ + +/** + * {@link UrlRewriteFunctionDescriptor} for the variable {@link + * HtmlInfixDescriptor#FUNCTION_NAME} + * + * @since 1.1.0 + */ +public class HtmlInfixDescriptor + implements UrlRewriteFunctionDescriptor<HtmlInfixDescriptor> { + + /** + * variable name used in rewrite.xml + */ + public static final String FUNCTION_NAME = "infix"; + + /** + * Create an instance + */ + public HtmlInfixDescriptor() { + super(); + } + + @Override + public String name() { + return FUNCTION_NAME; + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/9d385e45/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/html/HtmlInfixProcessor.java ---------------------------------------------------------------------- diff --git a/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/html/HtmlInfixProcessor.java b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/html/HtmlInfixProcessor.java new file mode 100644 index 0000000..8cf9906 --- /dev/null +++ b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/html/HtmlInfixProcessor.java @@ -0,0 +1,104 @@ +/** + * 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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.filter.rewrite.impl.html; + +import org.apache.knox.gateway.filter.rewrite.api.FrontendFunctionDescriptor; +import org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment; +import org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFunctionDescriptor; +import org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFunctionDescriptorFactory; +import org.apache.knox.gateway.filter.rewrite.impl.UrlRewriteFunctionProcessorFactory; +import org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteContext; +import org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteFunctionProcessor; + +import java.util.Arrays; +import java.util.List; + +/** + * This function enhances the 'frontend' function with the ability to add a + * prefix and post-fix to the rewritten frontend. + * + * The rewrite rule could then contain the $infix function that would delegate + * to the frontend function. + * e.g. {$infix[pre,url,post]} + * where + * pre = prefix + * post = postfix + * url = frontend url (resolved by Knox) + * The parameter to the function would be the symbol used as a prefix. + */ + +public class HtmlInfixProcessor + implements UrlRewriteFunctionProcessor<HtmlInfixDescriptor> { + + private UrlRewriteFunctionProcessor frontend; + + /** + * Create an instance + */ + public HtmlInfixProcessor() { + super(); + } + + @Override + public void initialize(final UrlRewriteEnvironment environment, + final HtmlInfixDescriptor descriptor) throws Exception { + + final UrlRewriteFunctionDescriptor frontendDescriptor = UrlRewriteFunctionDescriptorFactory + .create(FrontendFunctionDescriptor.FUNCTION_NAME); + + frontend = UrlRewriteFunctionProcessorFactory + .create(FrontendFunctionDescriptor.FUNCTION_NAME, frontendDescriptor); + + frontend.initialize(environment, frontendDescriptor); + } + + @Override + public String name() { + return HtmlInfixDescriptor.FUNCTION_NAME; + } + + @Override + public void destroy() throws Exception { + frontend.destroy(); + } + + @Override + public List<String> resolve(UrlRewriteContext context, + List<String> parameters) throws Exception { + String prefix = ""; + String postfix = ""; + + if ((parameters != null) && (parameters.size() > 1)) { + prefix = parameters.get(0); + postfix = parameters.get(parameters.size() - 1); + parameters = parameters.subList(1, parameters.size()-1); + } + + final List<String> frontendValues = frontend.resolve(context, parameters); + + final StringBuffer buffer = new StringBuffer(); + buffer.append(prefix); + if (frontendValues != null && frontendValues.size() > 0) { + for (final String value : frontendValues) { + buffer.append(value); + } + } + buffer.append(postfix); + + return Arrays.asList(buffer.toString()); + } +} http://git-wip-us.apache.org/repos/asf/knox/blob/9d385e45/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/html/HtmlPostfixDescriptor.java ---------------------------------------------------------------------- diff --git a/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/html/HtmlPostfixDescriptor.java b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/html/HtmlPostfixDescriptor.java new file mode 100644 index 0000000..f41e5e7 --- /dev/null +++ b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/html/HtmlPostfixDescriptor.java @@ -0,0 +1,48 @@ +package org.apache.knox.gateway.filter.rewrite.impl.html; + +import org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFunctionDescriptor; + +/** + * 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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. + */ + +/** + * {@link UrlRewriteFunctionDescriptor} for the variable {@link + * HtmlPostfixDescriptor#FUNCTION_NAME} + * + * @since 1.1.0 + */ +public class HtmlPostfixDescriptor + implements UrlRewriteFunctionDescriptor<HtmlPostfixDescriptor> { + + /** + * variable name used in rewrite.xml + */ + public static final String FUNCTION_NAME = "postfix"; + + /** + * Create an instance + */ + public HtmlPostfixDescriptor() { + super(); + } + + @Override + public String name() { + return FUNCTION_NAME; + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/9d385e45/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/html/HtmlPostfixProcessor.java ---------------------------------------------------------------------- diff --git a/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/html/HtmlPostfixProcessor.java b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/html/HtmlPostfixProcessor.java new file mode 100644 index 0000000..76455f8 --- /dev/null +++ b/gateway-provider-rewrite/src/main/java/org/apache/knox/gateway/filter/rewrite/impl/html/HtmlPostfixProcessor.java @@ -0,0 +1,103 @@ +/** + * 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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.filter.rewrite.impl.html; + +import org.apache.knox.gateway.filter.rewrite.api.FrontendFunctionDescriptor; +import org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment; +import org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFunctionDescriptor; +import org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFunctionDescriptorFactory; +import org.apache.knox.gateway.filter.rewrite.impl.UrlRewriteFunctionProcessorFactory; +import org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteContext; +import org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteFunctionProcessor; + +import java.util.Arrays; +import java.util.List; + +/** + * This function enhances the 'frontend' function with the ability to add a + * postfix to the rewritten frontend url along with the literals + * provided as an argument. + * The rewrite rule could then contain the $postfix function that would delegate + * to the frontend function. + * <p> + * e.g. {$postfix[url,post]} + * where + * url = frontend url (resolved by Knox) + * post = postfix + * The parameter to the function would be the symbol/string (some symbols might need encoding) + * used as a postfix. + */ + +public class HtmlPostfixProcessor + implements UrlRewriteFunctionProcessor<HtmlPostfixDescriptor> { + + private UrlRewriteFunctionProcessor frontend; + + /** + * Create an instance + */ + public HtmlPostfixProcessor() { + super(); + } + + @Override + public void initialize(final UrlRewriteEnvironment environment, + final HtmlPostfixDescriptor descriptor) throws Exception { + + final UrlRewriteFunctionDescriptor frontendDescriptor = UrlRewriteFunctionDescriptorFactory + .create(FrontendFunctionDescriptor.FUNCTION_NAME); + + frontend = UrlRewriteFunctionProcessorFactory + .create(FrontendFunctionDescriptor.FUNCTION_NAME, frontendDescriptor); + + frontend.initialize(environment, frontendDescriptor); + } + + @Override + public String name() { + return HtmlPostfixDescriptor.FUNCTION_NAME; + } + + @Override + public void destroy() throws Exception { + frontend.destroy(); + } + + @Override + public List<String> resolve(UrlRewriteContext context, + List<String> parameters) throws Exception { + String postfix = ""; + + if ((parameters != null) && (parameters.size() > 1)) { + /* last parameter is postfix */ + postfix = parameters.get(parameters.size() - 1); + parameters = parameters.subList(0, parameters.size() - 1); + } + + final List<String> frontendValues = frontend.resolve(context, parameters); + + final StringBuffer buffer = new StringBuffer(); + if (frontendValues != null && frontendValues.size() > 0) { + for (final String value : frontendValues) { + buffer.append(value); + } + } + buffer.append(postfix); + + return Arrays.asList(buffer.toString()); + } +} http://git-wip-us.apache.org/repos/asf/knox/blob/9d385e45/gateway-provider-rewrite/src/main/resources/META-INF/services/org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFunctionDescriptor ---------------------------------------------------------------------- diff --git a/gateway-provider-rewrite/src/main/resources/META-INF/services/org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFunctionDescriptor b/gateway-provider-rewrite/src/main/resources/META-INF/services/org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFunctionDescriptor index 3b67183..2252d8b 100644 --- a/gateway-provider-rewrite/src/main/resources/META-INF/services/org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFunctionDescriptor +++ b/gateway-provider-rewrite/src/main/resources/META-INF/services/org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFunctionDescriptor @@ -18,4 +18,6 @@ org.apache.knox.gateway.filter.rewrite.api.FrontendFunctionDescriptor org.apache.knox.gateway.filter.rewrite.impl.html.HtmlImportFunctionDescriptor -org.apache.knox.gateway.filter.rewrite.impl.html.HtmlPrefixDescriptor \ No newline at end of file +org.apache.knox.gateway.filter.rewrite.impl.html.HtmlPrefixDescriptor +org.apache.knox.gateway.filter.rewrite.impl.html.HtmlPostfixDescriptor +org.apache.knox.gateway.filter.rewrite.impl.html.HtmlInfixDescriptor \ No newline at end of file http://git-wip-us.apache.org/repos/asf/knox/blob/9d385e45/gateway-provider-rewrite/src/main/resources/META-INF/services/org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteFunctionProcessor ---------------------------------------------------------------------- diff --git a/gateway-provider-rewrite/src/main/resources/META-INF/services/org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteFunctionProcessor b/gateway-provider-rewrite/src/main/resources/META-INF/services/org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteFunctionProcessor index 099f40d..a8ded8a 100644 --- a/gateway-provider-rewrite/src/main/resources/META-INF/services/org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteFunctionProcessor +++ b/gateway-provider-rewrite/src/main/resources/META-INF/services/org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteFunctionProcessor @@ -18,4 +18,6 @@ org.apache.knox.gateway.filter.rewrite.impl.FrontendFunctionProcessor org.apache.knox.gateway.filter.rewrite.impl.html.HtmlImportFunctionProcessor -org.apache.knox.gateway.filter.rewrite.impl.html.HtmlPrefixProcessor \ No newline at end of file +org.apache.knox.gateway.filter.rewrite.impl.html.HtmlPrefixProcessor +org.apache.knox.gateway.filter.rewrite.impl.html.HtmlPostfixProcessor +org.apache.knox.gateway.filter.rewrite.impl.html.HtmlInfixProcessor \ No newline at end of file http://git-wip-us.apache.org/repos/asf/knox/blob/9d385e45/gateway-provider-rewrite/src/test/java/org/apache/knox/gateway/filter/rewrite/api/UrlRewriteServletFilterTest.java ---------------------------------------------------------------------- diff --git a/gateway-provider-rewrite/src/test/java/org/apache/knox/gateway/filter/rewrite/api/UrlRewriteServletFilterTest.java b/gateway-provider-rewrite/src/test/java/org/apache/knox/gateway/filter/rewrite/api/UrlRewriteServletFilterTest.java index 1df1c20..5ab8662 100644 --- a/gateway-provider-rewrite/src/test/java/org/apache/knox/gateway/filter/rewrite/api/UrlRewriteServletFilterTest.java +++ b/gateway-provider-rewrite/src/test/java/org/apache/knox/gateway/filter/rewrite/api/UrlRewriteServletFilterTest.java @@ -815,7 +815,6 @@ public class UrlRewriteServletFilterTest { public void testResponseHtmlBodyRewritePrefixFunctionTestPrefix() throws Exception { Map<String,String> initParams = new HashMap<>(); - initParams.put( "response.body", "test-filter-6" ); setUp( initParams ); String responseHtml = "<html><div src=\"'components/navbar/navbar.html?v=1496201914075\"></div></html>"; @@ -845,6 +844,87 @@ public class UrlRewriteServletFilterTest { } + /** + * Test the postfix function + * @since 1.1.0 + * KNOX-1305 + */ + @Test + public void testFunctionTestPostfix() throws Exception { + + Map<String,String> initParams = new HashMap<>(); + initParams.put( "response.headers", "test-filter-6" ); + setUp( initParams ); + + String locationHeader = "https://localhost:8443/gateway/knoxsso/api/v1/websso?originalUrl=http://localhost:20070/index.html&doAs=anonymous"; + String responseHtml = "<html>Hello There !</html>"; + String expectedLocationHeader = "https://localhost:8443/gateway/knoxsso/api/v1/websso?originalUrl=http%3A%2F%2F0.0.0.0%3A0%2Fsparkhistory%2F"; + + // Setup the server side request/response interaction. + interaction.expect() + .method( "GET" ) + .requestUrl( "http://mock-host:42/test-output-path-1" ); + interaction.respond() + .contentType( "application/html" ) + .header("Location", locationHeader ) + .content( responseHtml, Charset.forName( "UTF-8" ) ) + .status( 200 ); + interactions.add( interaction ); + request.setMethod( "GET" ); + request.setURI( "/test-input-path" ); + request.setHeader( "Host", "mock-host:42" ); + request.setHeader( "Content-Type", "application/html" ); + + // Execute the request. + response = TestUtils.execute( server, request ); + + assertThat( response.getStatus(), is( 200 ) ); + String content = response.get("Location"); + + assertThat(content, is(expectedLocationHeader)); + } + + /** + * Test the infix function + * @since 1.1.0 + * KNOX-1305 + */ + @Test + public void testFunctionTestInfix() throws Exception { + + Map<String,String> initParams = new HashMap<>(); + initParams.put( "response.headers", "test-filter-6" ); + setUp( initParams ); + + String customHeader = "https://localhost:8443/gateway/sandbox/?query=http://localhost:20070"; + String responseHtml = "<html>Hello There !</html>"; + String expectedCustomHeader = "https://localhost:8443/gateway/sandbox/?query=%27http%3A%2F%2F0.0.0.0%3A0%2Fsparkhistory%2F%27"; + + // Setup the server side request/response interaction. + interaction.expect() + .method( "GET" ) + .requestUrl( "http://mock-host:42/test-output-path-1" ); + interaction.respond() + .contentType( "application/html" ) + .header("CustomHeader", customHeader ) + .content( responseHtml, Charset.forName( "UTF-8" ) ) + .status( 200 ); + interactions.add( interaction ); + request.setMethod( "GET" ); + request.setURI( "/test-input-path" ); + request.setHeader( "Host", "mock-host:42" ); + request.setHeader( "Content-Type", "application/html" ); + + // Execute the request. + response = TestUtils.execute( server, request ); + + assertThat( response.getStatus(), is( 200 ) ); + String content = response.get("CustomHeader"); + + assertThat(content, is(expectedCustomHeader)); + } + + /** * See KNOX-791 http://git-wip-us.apache.org/repos/asf/knox/blob/9d385e45/gateway-provider-rewrite/src/test/java/org/apache/knox/gateway/filter/rewrite/impl/html/HtmlInfixProcessorTest.java ---------------------------------------------------------------------- diff --git a/gateway-provider-rewrite/src/test/java/org/apache/knox/gateway/filter/rewrite/impl/html/HtmlInfixProcessorTest.java b/gateway-provider-rewrite/src/test/java/org/apache/knox/gateway/filter/rewrite/impl/html/HtmlInfixProcessorTest.java new file mode 100644 index 0000000..0c2b149 --- /dev/null +++ b/gateway-provider-rewrite/src/test/java/org/apache/knox/gateway/filter/rewrite/impl/html/HtmlInfixProcessorTest.java @@ -0,0 +1,57 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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.filter.rewrite.impl.html; + +import org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteFunctionProcessor; +import org.junit.Test; + +import java.util.Iterator; +import java.util.ServiceLoader; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.fail; + +public class HtmlInfixProcessorTest { + + public HtmlInfixProcessorTest() { + super(); + } + + @SuppressWarnings("rawtypes") + @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 HtmlInfixProcessor) { + return; + } + } + fail( "Failed to find " + HtmlInfixProcessor.class.getName() + " via service loader." ); + } + + @Test + public void testName() throws Exception { + HtmlInfixProcessor processor = new HtmlInfixProcessor(); + assertThat( processor.name(), is( "infix" ) ); + } + + +} http://git-wip-us.apache.org/repos/asf/knox/blob/9d385e45/gateway-provider-rewrite/src/test/java/org/apache/knox/gateway/filter/rewrite/impl/html/HtmlPostfixProcessorTest.java ---------------------------------------------------------------------- diff --git a/gateway-provider-rewrite/src/test/java/org/apache/knox/gateway/filter/rewrite/impl/html/HtmlPostfixProcessorTest.java b/gateway-provider-rewrite/src/test/java/org/apache/knox/gateway/filter/rewrite/impl/html/HtmlPostfixProcessorTest.java new file mode 100644 index 0000000..872fa63 --- /dev/null +++ b/gateway-provider-rewrite/src/test/java/org/apache/knox/gateway/filter/rewrite/impl/html/HtmlPostfixProcessorTest.java @@ -0,0 +1,57 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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.filter.rewrite.impl.html; + +import org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteFunctionProcessor; +import org.junit.Test; + +import java.util.Iterator; +import java.util.ServiceLoader; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.fail; + +public class HtmlPostfixProcessorTest { + + public HtmlPostfixProcessorTest() { + super(); + } + + @SuppressWarnings("rawtypes") + @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 HtmlPostfixProcessor) { + return; + } + } + fail( "Failed to find " + HtmlPostfixProcessor.class.getName() + " via service loader." ); + } + + @Test + public void testName() throws Exception { + HtmlPostfixProcessor processor = new HtmlPostfixProcessor(); + assertThat( processor.name(), is( "postfix" ) ); + } + + +} http://git-wip-us.apache.org/repos/asf/knox/blob/9d385e45/gateway-provider-rewrite/src/test/resources/org/apache/knox/gateway/filter/rewrite/api/UrlRewriteServletFilterTest/rewrite.xml ---------------------------------------------------------------------- diff --git a/gateway-provider-rewrite/src/test/resources/org/apache/knox/gateway/filter/rewrite/api/UrlRewriteServletFilterTest/rewrite.xml b/gateway-provider-rewrite/src/test/resources/org/apache/knox/gateway/filter/rewrite/api/UrlRewriteServletFilterTest/rewrite.xml index 3b77eb3..4fa8b25 100644 --- a/gateway-provider-rewrite/src/test/resources/org/apache/knox/gateway/filter/rewrite/api/UrlRewriteServletFilterTest/rewrite.xml +++ b/gateway-provider-rewrite/src/test/resources/org/apache/knox/gateway/filter/rewrite/api/UrlRewriteServletFilterTest/rewrite.xml @@ -62,6 +62,18 @@ <rewrite template="{$prefix[',url]}/zeppelin/components/{**}?{**}"/> </rule> + <!-- HTML postfix attribute tests, since 1.1.0 --> + <rule dir="OUT" name="test-rule-html-postfix-attribute/1" > + <match pattern="{scheme}://{host}:{port}/{gateway}/{knoxsso}/{api}/{v1}/{websso}?originalUrl={**}"/> + <rewrite template="{scheme}://{host}:{port}/{gateway}/{knoxsso}/{api}/{v1}/{websso}?originalUrl={$postfix[url,/sparkhistory/]}"/> + </rule> + + <!-- HTML infix attribute tests, since 1.1.0 --> + <rule dir="OUT" name="test-rule-html-infix-attribute/1" > + <match pattern="{scheme}://{host}:{port}/{gateway}/{sandbox}/?query={**}"/> + <rewrite template="{scheme}://{host}:{port}/{gateway}/{sandbox}/?query={$infix[',url,/sparkhistory/']}"/> + </rule> + <filter name="test-filter-1"> <content type="application/json"> <apply path="$.url" rule="test-rule-1"/> @@ -106,4 +118,11 @@ </content> </filter> + <filter name="test-filter-6"> + <content type="application/x-http-headers"> + <apply path="Location" rule="test-rule-html-postfix-attribute/1"/> + <apply path="CustomHeader" rule="test-rule-html-infix-attribute/1"/> + </content> + </filter> + </rules> \ No newline at end of file
