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;
}
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")));
}
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")));
}
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);
}
}
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));
}
Aggregations