use of org.apache.knox.gateway.util.urltemplate.Template in project knox by apache.
the class DefaultServiceDefinitionRegistry method init.
@Override
public void init(GatewayConfig config, Map<String, String> options) throws ServiceLifecycleException {
String stacks = config.getGatewayServicesDir();
File stacksDir = new File(stacks);
Set<ServiceDefinition> serviceDefinitions = ServiceDefinitionsLoader.getServiceDefinitions(stacksDir);
for (ServiceDefinition serviceDefinition : serviceDefinitions) {
List<Route> routes = serviceDefinition.getRoutes();
for (Route route : routes) {
try {
Template template = Parser.parseTemplate(route.getPath());
addServiceDefEntry(template, serviceDefinition);
} catch (URISyntaxException e) {
LOG.failedToParsePath(route.getPath(), e);
}
}
}
}
use of org.apache.knox.gateway.util.urltemplate.Template in project knox by apache.
the class UrlRewriteContextImplTest method testResolve.
@Test
public void testResolve() throws Exception {
UrlRewriteEnvironment environment = EasyMock.createNiceMock(UrlRewriteEnvironment.class);
EasyMock.expect(environment.resolve("test-env-param-name")).andReturn(Arrays.asList("test-env-param-value")).anyTimes();
Resolver resolver = EasyMock.createNiceMock(Resolver.class);
EasyMock.expect(resolver.resolve("test-ctx-param-name")).andReturn(Arrays.asList("test-ctx-param-value"));
EasyMock.replay(environment, resolver);
Map<String, UrlRewriteFunctionProcessor> functions = new HashMap<>();
UrlRewriter.Direction direction = UrlRewriter.Direction.OUT;
Template template = Parser.parseLiteral("scheme://host:port/dir/file");
UrlRewriteContextImpl context = new UrlRewriteContextImpl(environment, resolver, functions, direction, template);
Params params = context.getParameters();
List<String> values = params.resolve("test-env-param-name");
assertThat(values, notNullValue());
assertThat(values.size(), is(1));
assertThat(values.get(0), is("test-env-param-value"));
values = params.resolve("test-ctx-param-name");
assertThat(values, notNullValue());
assertThat(values.size(), is(1));
assertThat(values.get(0), is("test-ctx-param-value"));
}
use of org.apache.knox.gateway.util.urltemplate.Template in project knox by apache.
the class UrlRewriteProcessorTest method testSolrRewrite.
/**
* Tests the rewrite pattern used for re-writing Solr urls passed through Knox.
*
* @throws Exception
*/
@Test
public void testSolrRewrite() throws Exception {
URI inputUri, outputUri;
Matcher<Void> matcher;
Matcher<Void>.Match match;
Template input, pattern, template;
inputUri = new URI("https://hortonworks.sandbox.hdp.24.test:8443/gateway/sandbox/solr/TestCollection/select?q=*.*&wt=json&indent=true");
input = Parser.parseLiteral(inputUri.toString());
pattern = Parser.parseTemplate("*://*:*/**/solr/{collection=**}/{query=**}?{**}");
template = Parser.parseTemplate("http://sandbox.hortonworks.com/solr/{collection=**}/{query=**}?{**}");
matcher = new Matcher<Void>();
matcher.add(pattern, null);
match = matcher.match(input);
outputUri = Expander.expand(template, match.getParams(), null);
final String reWrittenScheme = outputUri.getScheme();
assertEquals("http", reWrittenScheme);
final String reWrittenHost = outputUri.getHost();
assertEquals("sandbox.hortonworks.com", reWrittenHost);
final String reWrittenPath = outputUri.getPath();
assertEquals("/solr/TestCollection/select", reWrittenPath);
// Whole thing is (non-deterministicly ordered around the &s):
// "q=*.*&wt=json&indent=true"
final String reWrittenQuery = outputUri.getQuery();
// Check individual parameters are present, and have the right value.
final Map<String, String> reWrittenParams = mapUrlParameters(reWrittenQuery);
assertTrue(reWrittenParams.containsKey("q"));
assertEquals("*.*", reWrittenParams.get("q"));
assertTrue(reWrittenParams.containsKey("wt"));
assertEquals("json", reWrittenParams.get("wt"));
assertEquals("true", reWrittenParams.get("indent"));
}
use of org.apache.knox.gateway.util.urltemplate.Template in project knox by apache.
the class UrlRewriteProcessorTest method testRewriteViaRuleWithComplexFlow.
@Test
public void testRewriteViaRuleWithComplexFlow() throws Exception {
UrlRewriteEnvironment environment = EasyMock.createNiceMock(UrlRewriteEnvironment.class);
HttpServletRequest request = EasyMock.createNiceMock(HttpServletRequest.class);
HttpServletResponse response = EasyMock.createNiceMock(HttpServletResponse.class);
EasyMock.replay(environment, request, response);
UrlRewriteProcessor processor = new UrlRewriteProcessor();
UrlRewriteRulesDescriptor config = UrlRewriteRulesDescriptorFactory.load("xml", getTestResourceReader("rewrite.xml", "UTF-8"));
processor.initialize(environment, config);
Template inputUrl;
Template outputUrl;
inputUrl = Parser.parseLiteral("test-scheme://test-host:777/test-path");
outputUrl = processor.rewrite(null, inputUrl, UrlRewriter.Direction.IN, "test-rule-with-complex-flow");
assertThat("Expect rewrite to contain the correct path.", outputUrl.toString(), is("test-scheme-output://test-host-output:42/test-path-output/test-path"));
inputUrl = Parser.parseLiteral("test-scheme://test-host:42/~/test-path");
outputUrl = processor.rewrite(null, inputUrl, UrlRewriter.Direction.IN, "test-rule-with-complex-flow");
assertThat("Expect rewrite to contain the correct path.", outputUrl.toString(), is("test-scheme-output://test-host-output:777/test-path-output/test-home/test-path"));
processor.destroy();
}
use of org.apache.knox.gateway.util.urltemplate.Template in project knox by apache.
the class UrlRewriteProcessorTest method testRewriteViaRuleWithWildcardTemplateAndOptionalQuery.
@Test
public void testRewriteViaRuleWithWildcardTemplateAndOptionalQuery() throws Exception {
UrlRewriteEnvironment environment = EasyMock.createNiceMock(UrlRewriteEnvironment.class);
HttpServletRequest request = EasyMock.createNiceMock(HttpServletRequest.class);
HttpServletResponse response = EasyMock.createNiceMock(HttpServletResponse.class);
EasyMock.replay(environment, request, response);
UrlRewriteProcessor processor = new UrlRewriteProcessor();
UrlRewriteRulesDescriptor config = UrlRewriteRulesDescriptorFactory.load("xml", getTestResourceReader("rewrite.xml", "UTF-8"));
processor.initialize(environment, config);
Template inputUrl;
Template outputUrl;
inputUrl = Parser.parseLiteral("test-scheme-input://test-host-input:42/test-path-input-one/test-path-input-two?test-query-name=test-query-value");
outputUrl = processor.rewrite(null, inputUrl, UrlRewriter.Direction.OUT, "test-rule-2");
assertThat("Expect rewrite to contain the correct path and query.", outputUrl.toString(), is("test-scheme-output://test-host-output:777/test-path-output/test-path-input-one/test-path-input-two?test-query-name=test-query-value"));
inputUrl = Parser.parseLiteral("test-scheme-input://test-host-input:42/test-path-input-one/test-path-input-two");
outputUrl = processor.rewrite(null, inputUrl, UrlRewriter.Direction.OUT, "test-rule-2");
assertThat("Expect rewrite to contain the correct path.", outputUrl.toString(), is("test-scheme-output://test-host-output:777/test-path-output/test-path-input-one/test-path-input-two"));
processor.destroy();
}
Aggregations