Search in sources :

Example 1 with UrlRewriteRuleDescriptor

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

the class UrlRewriteRulesDescriptorImpl method addRule.

@Override
public UrlRewriteRuleDescriptor addRule(String name) {
    UrlRewriteRuleDescriptor rule = newRule();
    rule.name(name);
    addRule(rule);
    return rule;
}
Also used : UrlRewriteRuleDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRuleDescriptor)

Example 2 with UrlRewriteRuleDescriptor

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

the class XmlUrlRewriteRulesExporterTest method testCheckStep.

@Test
public void testCheckStep() throws Exception {
    UrlRewriteRulesDescriptor rules = UrlRewriteRulesDescriptorFactory.create();
    UrlRewriteRuleDescriptor rule = rules.addRule("test-rule");
    UrlRewriteCheckDescriptor step = rule.addStep("check");
    step.operation("test-operation").input("test-input").value("test-value").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/check"));
    assertThat(xml, XmlMatchers.hasXPath("/rules/rule/check/@oper", is("test-operation")));
    assertThat(xml, XmlMatchers.hasXPath("/rules/rule/check/@input", is("test-input")));
    assertThat(xml, XmlMatchers.hasXPath("/rules/rule/check/@value", is("test-value")));
    assertThat(xml, XmlMatchers.hasXPath("/rules/rule/check/@flow", is("ALL")));
}
Also used : UrlRewriteRuleDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRuleDescriptor) StringWriter(java.io.StringWriter) UrlRewriteRulesDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor) UrlRewriteCheckDescriptor(org.apache.knox.gateway.filter.rewrite.ext.UrlRewriteCheckDescriptor) Source(javax.xml.transform.Source) Test(org.junit.Test)

Example 3 with UrlRewriteRuleDescriptor

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

the class XmlUrlRewriteRulesExporterTest method testControlStep.

@Test
public void testControlStep() throws Exception {
    UrlRewriteRulesDescriptor rules = UrlRewriteRulesDescriptorFactory.create();
    UrlRewriteRuleDescriptor rule = rules.addRule("test-rule");
    UrlRewriteControlDescriptor control = rule.addStep("control");
    control.flow("or");
    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/control"));
    assertThat(xml, XmlMatchers.hasXPath("count(/rules/rule/control)", is("1")));
    assertThat(xml, XmlMatchers.hasXPath("/rules/rule/control/@flow", is("OR")));
}
Also used : 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) Source(javax.xml.transform.Source) Test(org.junit.Test)

Example 4 with UrlRewriteRuleDescriptor

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

the class XmlUrlRewriteRulesExporter method store.

@Override
public void store(UrlRewriteRulesDescriptor descriptor, Writer writer) throws IOException {
    try {
        Document document = XmlUtils.createDocument();
        Element root = document.createElement(ROOT);
        document.appendChild(root);
        if (!descriptor.getFunctions().isEmpty()) {
            Element functionsElement = document.createElement(FUNCTIONS);
            root.appendChild(functionsElement);
            for (UrlRewriteFunctionDescriptor function : descriptor.getFunctions()) {
                Element functionElement = createElement(document, function.name(), function);
                functionsElement.appendChild(functionElement);
            }
        }
        if (!descriptor.getRules().isEmpty()) {
            for (UrlRewriteRuleDescriptor rule : descriptor.getRules()) {
                Element ruleElement = createRule(document, rule);
                root.appendChild(ruleElement);
            }
        }
        if (!descriptor.getFilters().isEmpty()) {
            for (UrlRewriteFilterDescriptor filter : descriptor.getFilters()) {
                Element filterElement = createFilter(document, filter);
                root.appendChild(filterElement);
            }
        }
        XmlUtils.writeXml(document, writer);
    } catch (ParserConfigurationException e) {
        throw new IOException(e);
    } catch (TransformerException e) {
        throw new IOException(e);
    } catch (InvocationTargetException e) {
        LOG.failedToWriteRulesDescriptor(e);
    } catch (NoSuchMethodException e) {
        LOG.failedToWriteRulesDescriptor(e);
    } catch (IntrospectionException e) {
        LOG.failedToWriteRulesDescriptor(e);
    } catch (IllegalAccessException e) {
        LOG.failedToWriteRulesDescriptor(e);
    }
}
Also used : UrlRewriteRuleDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRuleDescriptor) Element(org.w3c.dom.Element) IntrospectionException(java.beans.IntrospectionException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) Document(org.w3c.dom.Document) UrlRewriteFunctionDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFunctionDescriptor) TransformerException(javax.xml.transform.TransformerException) InvocationTargetException(java.lang.reflect.InvocationTargetException) UrlRewriteFilterDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteFilterDescriptor)

Example 5 with UrlRewriteRuleDescriptor

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

the class HostmapFunctionProcessorTest method testQueryToPathRewriteWithFunction.

@Test
public void testQueryToPathRewriteWithFunction() throws Exception {
    URL configUrl = TestUtils.getResourceUrl(this.getClass(), "hdfs-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-internal-host")).anyTimes();
    EasyMock.replay(environment, resolver);
    UrlRewriteRulesDescriptor descriptor = UrlRewriteRulesDescriptorFactory.create();
    UrlRewriteRuleDescriptor rule = descriptor.addRule("test-rule");
    rule.pattern("{*}://{host}:{*}/{**}?{qp1}&{qp2}&{**}");
    UrlRewriteActionRewriteDescriptorExt rewrite = rule.addStep("rewrite");
    rewrite.template("{*}://test-static-host:{*}/{qp1}/{qp2}/{**}?server={$hostmap(host)}&{**}");
    UrlRewriteProcessor rewriter = new UrlRewriteProcessor();
    rewriter.initialize(environment, descriptor);
    Template input = Parser.parseLiteral("test-scheme://test-external-host:42/test-path/test-file?qp1=qp1-val&qp2=qp2-val&test-name-1=test-value-1&test-name-2=test-value-2");
    Template output = rewriter.rewrite(resolver, input, UrlRewriter.Direction.OUT, "test-rule");
    // System.out.println( output );
    assertThat(output, notNullValue());
    assertThat(output.getHost().getFirstValue().getPattern(), is("test-static-host"));
    assertThat(output.getQuery().get("server").getFirstValue().getPattern(), is("test-external-host"));
    assertThat(output.getQuery().get("server").getValues().size(), is(1));
    assertThat(output.getQuery().get("test-name-1").getFirstValue().getPattern(), is("test-value-1"));
    assertThat(output.getQuery().get("test-name-1").getValues().size(), is(1));
    assertThat(output.getQuery().get("test-name-2").getFirstValue().getPattern(), is("test-value-2"));
    assertThat(output.getQuery().get("test-name-2").getValues().size(), is(1));
    assertThat(output.getQuery().size(), is(3));
}
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)

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