Search in sources :

Example 11 with UrlRewriteRuleDescriptor

use of org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRuleDescriptor in project knox by apache.

the class HostmapFunctionProcessorTest method testInvalidFunctionNameUseCase.

@Test
public void testInvalidFunctionNameUseCase() throws Exception {
    URL configUrl = TestUtils.getResourceUrl(this.getClass(), "hostmap.txt");
    UrlRewriteEnvironment environment = EasyMock.createNiceMock(UrlRewriteEnvironment.class);
    EasyMock.expect(environment.getResource("/WEB-INF/hostmap.txt")).andReturn(configUrl).anyTimes();
    Resolver resolver = EasyMock.createNiceMock(Resolver.class);
    EasyMock.expect(resolver.resolve("host")).andReturn(Arrays.asList("test-inbound-host")).anyTimes();
    EasyMock.replay(environment, resolver);
    UrlRewriteRulesDescriptor descriptor = UrlRewriteRulesDescriptorFactory.create();
    UrlRewriteRuleDescriptor rule = descriptor.addRule("test-rule");
    rule.pattern("{*}://{host}:{*}/{**}?{**}");
    UrlRewriteActionRewriteDescriptorExt rewrite = rule.addStep("rewrite");
    rewrite.template("{*}://{$invalid-function(host)}:{*}/{**}?{**}");
    UrlRewriteProcessor rewriter = new UrlRewriteProcessor();
    rewriter.initialize(environment, descriptor);
    Template input = Parser.parseLiteral("test-scheme://test-inbound-host:42/test-path/test-file?test-name=test-value");
    Template output = rewriter.rewrite(resolver, input, UrlRewriter.Direction.IN, null);
    // System.out.println( output );
    assertThat(output, notNullValue());
    assertThat(output.getHost().getFirstValue().getPattern(), is("$invalid-function(host)"));
}
Also used : UrlRewriteEnvironment(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment) UrlRewriteProcessor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteProcessor) UrlRewriteActionRewriteDescriptorExt(org.apache.knox.gateway.filter.rewrite.ext.UrlRewriteActionRewriteDescriptorExt) Resolver(org.apache.knox.gateway.util.urltemplate.Resolver) UrlRewriteRuleDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRuleDescriptor) UrlRewriteRulesDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor) URL(java.net.URL) Template(org.apache.knox.gateway.util.urltemplate.Template) Test(org.junit.Test)

Example 12 with UrlRewriteRuleDescriptor

use of org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRuleDescriptor in project knox by apache.

the class InboundUrlFunctionProcessorTest method testQueryParam.

@Test
public void testQueryParam() throws Exception {
    GatewayServices gatewayServices = EasyMock.createNiceMock(GatewayServices.class);
    UrlRewriteEnvironment environment = EasyMock.createNiceMock(UrlRewriteEnvironment.class);
    EasyMock.expect(environment.getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE)).andReturn(gatewayServices).anyTimes();
    EasyMock.expect(environment.resolve("cluster.name")).andReturn(Collections.singletonList("test-cluster-name")).anyTimes();
    Resolver resolver = EasyMock.createNiceMock(Resolver.class);
    EasyMock.expect(resolver.resolve("query.param.host")).andReturn(Lists.newArrayList("http://foo:50075")).anyTimes();
    EasyMock.replay(gatewayServices, environment, resolver);
    UrlRewriteRulesDescriptor descriptor = UrlRewriteRulesDescriptorFactory.create();
    UrlRewriteRuleDescriptor rule = descriptor.addRule("test-location");
    rule.pattern("{*}://{*}:{*}/{**}/?{**}");
    UrlRewriteActionRewriteDescriptorExt rewrite = rule.addStep("rewrite");
    rewrite.template("{$inboundurl[host]}");
    UrlRewriteProcessor rewriter = new UrlRewriteProcessor();
    rewriter.initialize(environment, descriptor);
    Template input = Parser.parseLiteral("https://localhost:8443/gateway/default/datanode/?host=http://foo:50075");
    Template output = rewriter.rewrite(resolver, input, UrlRewriter.Direction.OUT, "test-location");
    assertThat(output.toString(), is("http://foo:50075"));
}
Also used : UrlRewriteEnvironment(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment) GatewayServices(org.apache.knox.gateway.services.GatewayServices) UrlRewriteProcessor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteProcessor) UrlRewriteActionRewriteDescriptorExt(org.apache.knox.gateway.filter.rewrite.ext.UrlRewriteActionRewriteDescriptorExt) Resolver(org.apache.knox.gateway.util.urltemplate.Resolver) UrlRewriteRuleDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRuleDescriptor) UrlRewriteRulesDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor) Template(org.apache.knox.gateway.util.urltemplate.Template) Test(org.junit.Test)

Example 13 with UrlRewriteRuleDescriptor

use of org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRuleDescriptor in project knox by apache.

the class XmlUrlRewriteRulesExporterTest method testMatchStep.

@Test
public void testMatchStep() throws Exception {
    UrlRewriteRulesDescriptor rules = UrlRewriteRulesDescriptorFactory.create();
    UrlRewriteRuleDescriptor rule = rules.addRule("test-rule").pattern("test-pattern-rule");
    UrlRewriteMatchDescriptor match = rule.addStep("match");
    match.operation("test-operation").pattern("test-pattern-step").flow("all");
    StringWriter writer = new StringWriter();
    UrlRewriteRulesDescriptorFactory.store(rules, "xml", writer);
    String str = writer.toString();
    // System.out.println( str );
    Source xml = XmlConverters.the(str);
    assertThat(xml, XmlMatchers.hasXPath("/rules"));
    assertThat(xml, XmlMatchers.hasXPath("/rules/rule"));
    assertThat(xml, XmlMatchers.hasXPath("count(/rules/rule)", is("1")));
    assertThat(xml, XmlMatchers.hasXPath("/rules/rule/@name", is("test-rule")));
    assertThat(xml, XmlMatchers.hasXPath("/rules/rule/@pattern", is("test-pattern-rule")));
    assertThat(xml, XmlMatchers.hasXPath("/rules/rule/match"));
    assertThat(xml, XmlMatchers.hasXPath("count(/rules/rule/match)", is("1")));
    assertThat(xml, XmlMatchers.hasXPath("/rules/rule/match/@flow", is("ALL")));
    assertThat(xml, XmlMatchers.hasXPath("/rules/rule/match/@oper", is("test-operation")));
    assertThat(xml, XmlMatchers.hasXPath("/rules/rule/match/@pattern", is("test-pattern-step")));
}
Also used : UrlRewriteMatchDescriptor(org.apache.knox.gateway.filter.rewrite.ext.UrlRewriteMatchDescriptor) UrlRewriteRuleDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRuleDescriptor) StringWriter(java.io.StringWriter) UrlRewriteRulesDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor) Source(javax.xml.transform.Source) Test(org.junit.Test)

Example 14 with UrlRewriteRuleDescriptor

use of org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRuleDescriptor in project knox by apache.

the class XmlUrlRewriteRulesExporterTest method testNestedStep.

@Test
public void testNestedStep() throws Exception {
    UrlRewriteRulesDescriptor rules = UrlRewriteRulesDescriptorFactory.create();
    UrlRewriteRuleDescriptor rule = rules.addRule("test-rule");
    UrlRewriteMatchDescriptor match = rule.addStep("match");
    UrlRewriteMatchDescriptor matchMatch = match.addStep("match");
    UrlRewriteCheckDescriptor matchCheck = match.addStep("check");
    UrlRewriteControlDescriptor matchControl = match.addStep("control");
    UrlRewriteActionDescriptor matchRewrite = match.addStep("rewrite");
    UrlRewriteCheckDescriptor check = rule.addStep("check");
    UrlRewriteMatchDescriptor checkMatch = check.addStep("match");
    UrlRewriteCheckDescriptor checkCheck = check.addStep("check");
    UrlRewriteControlDescriptor checkControl = check.addStep("control");
    UrlRewriteActionDescriptor checkRewrite = check.addStep("rewrite");
    UrlRewriteControlDescriptor control = rule.addStep("control");
    UrlRewriteMatchDescriptor controlMatch = control.addStep("match");
    UrlRewriteCheckDescriptor controlCheck = control.addStep("check");
    UrlRewriteControlDescriptor controlControl = control.addStep("control");
    UrlRewriteActionDescriptor controlRewrite = control.addStep("rewrite");
    UrlRewriteActionDescriptor rewrite = rule.addStep("rewrite");
    StringWriter writer = new StringWriter();
    UrlRewriteRulesDescriptorFactory.store(rules, "xml", writer);
    String str = writer.toString();
    // System.out.println( str );
    Source xml = XmlConverters.the(str);
    assertThat(xml, XmlMatchers.hasXPath("/rules"));
    assertThat(xml, XmlMatchers.hasXPath("/rules/rule"));
    assertThat(xml, XmlMatchers.hasXPath("count(/rules/rule)", is("1")));
    assertThat(xml, XmlMatchers.hasXPath("/rules/rule/@name", is("test-rule")));
    assertThat(xml, XmlMatchers.hasXPath("/rules/rule/match"));
    assertThat(xml, XmlMatchers.hasXPath("/rules/rule/match/match"));
    assertThat(xml, XmlMatchers.hasXPath("/rules/rule/match/check"));
    assertThat(xml, XmlMatchers.hasXPath("/rules/rule/match/control"));
    assertThat(xml, XmlMatchers.hasXPath("/rules/rule/match/rewrite"));
    assertThat(xml, XmlMatchers.hasXPath("/rules/rule/check"));
    assertThat(xml, XmlMatchers.hasXPath("/rules/rule/check/match"));
    assertThat(xml, XmlMatchers.hasXPath("/rules/rule/check/check"));
    assertThat(xml, XmlMatchers.hasXPath("/rules/rule/check/control"));
    assertThat(xml, XmlMatchers.hasXPath("/rules/rule/check/rewrite"));
    assertThat(xml, XmlMatchers.hasXPath("/rules/rule/control"));
    assertThat(xml, XmlMatchers.hasXPath("/rules/rule/control/match"));
    assertThat(xml, XmlMatchers.hasXPath("/rules/rule/control/check"));
    assertThat(xml, XmlMatchers.hasXPath("/rules/rule/control/control"));
    assertThat(xml, XmlMatchers.hasXPath("/rules/rule/control/rewrite"));
    assertThat(xml, XmlMatchers.hasXPath("/rules/rule/rewrite"));
}
Also used : UrlRewriteMatchDescriptor(org.apache.knox.gateway.filter.rewrite.ext.UrlRewriteMatchDescriptor) UrlRewriteRuleDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRuleDescriptor) StringWriter(java.io.StringWriter) UrlRewriteControlDescriptor(org.apache.knox.gateway.filter.rewrite.ext.UrlRewriteControlDescriptor) UrlRewriteRulesDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor) UrlRewriteActionDescriptor(org.apache.knox.gateway.filter.rewrite.ext.UrlRewriteActionDescriptor) UrlRewriteCheckDescriptor(org.apache.knox.gateway.filter.rewrite.ext.UrlRewriteCheckDescriptor) Source(javax.xml.transform.Source) Test(org.junit.Test)

Example 15 with UrlRewriteRuleDescriptor

use of org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRuleDescriptor in project knox by apache.

the class XmlUrlRewriteRulesExporterTest method testRewriteStep.

@Test
public void testRewriteStep() throws Exception {
    UrlRewriteRulesDescriptor rules = UrlRewriteRulesDescriptorFactory.create();
    UrlRewriteRuleDescriptor rule = rules.addRule("test-rule");
    UrlRewriteActionRewriteDescriptorExt step = rule.addStep("rewrite");
    step.operation("test-operation").parameter("test-param");
    StringWriter writer = new StringWriter();
    UrlRewriteRulesDescriptorFactory.store(rules, "xml", writer);
    String str = writer.toString();
    // System.out.println( str );
    Source xml = XmlConverters.the(str);
    assertThat(xml, XmlMatchers.hasXPath("/rules"));
    assertThat(xml, XmlMatchers.hasXPath("/rules/rule"));
    assertThat(xml, XmlMatchers.hasXPath("count(/rules/rule)", is("1")));
    assertThat(xml, XmlMatchers.hasXPath("/rules/rule/@name", is("test-rule")));
    assertThat(xml, XmlMatchers.hasXPath("/rules/rule/rewrite"));
    assertThat(xml, XmlMatchers.hasXPath("/rules/rule/rewrite/@oper", is("test-operation")));
    assertThat(xml, XmlMatchers.hasXPath("/rules/rule/rewrite/@template", is("test-param")));
}
Also used : UrlRewriteActionRewriteDescriptorExt(org.apache.knox.gateway.filter.rewrite.ext.UrlRewriteActionRewriteDescriptorExt) UrlRewriteRuleDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRuleDescriptor) StringWriter(java.io.StringWriter) UrlRewriteRulesDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor) Source(javax.xml.transform.Source) Test(org.junit.Test)

Aggregations

UrlRewriteRuleDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRuleDescriptor)15 UrlRewriteRulesDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor)13 Test (org.junit.Test)13 UrlRewriteActionRewriteDescriptorExt (org.apache.knox.gateway.filter.rewrite.ext.UrlRewriteActionRewriteDescriptorExt)9 UrlRewriteEnvironment (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment)8 UrlRewriteProcessor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteProcessor)8 Resolver (org.apache.knox.gateway.util.urltemplate.Resolver)8 Template (org.apache.knox.gateway.util.urltemplate.Template)8 URL (java.net.URL)7 StringWriter (java.io.StringWriter)5 Source (javax.xml.transform.Source)5 UrlRewriteCheckDescriptor (org.apache.knox.gateway.filter.rewrite.ext.UrlRewriteCheckDescriptor)2 UrlRewriteControlDescriptor (org.apache.knox.gateway.filter.rewrite.ext.UrlRewriteControlDescriptor)2 UrlRewriteMatchDescriptor (org.apache.knox.gateway.filter.rewrite.ext.UrlRewriteMatchDescriptor)2 GatewayServices (org.apache.knox.gateway.services.GatewayServices)2 IntrospectionException (java.beans.IntrospectionException)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 TransformerException (javax.xml.transform.TransformerException)1