Search in sources :

Example 46 with Template

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);
            }
        }
    }
}
Also used : URISyntaxException(java.net.URISyntaxException) File(java.io.File) ServiceDefinition(org.apache.knox.gateway.service.definition.ServiceDefinition) Route(org.apache.knox.gateway.service.definition.Route) Template(org.apache.knox.gateway.util.urltemplate.Template)

Example 47 with Template

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"));
}
Also used : UrlRewriteEnvironment(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment) UrlRewriteFunctionProcessor(org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteFunctionProcessor) Resolver(org.apache.knox.gateway.util.urltemplate.Resolver) HashMap(java.util.HashMap) UrlRewriter(org.apache.knox.gateway.filter.rewrite.api.UrlRewriter) Params(org.apache.knox.gateway.util.urltemplate.Params) Template(org.apache.knox.gateway.util.urltemplate.Template) Test(org.junit.Test)

Example 48 with Template

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"));
}
Also used : Matcher(org.apache.knox.gateway.util.urltemplate.Matcher) URI(java.net.URI) Template(org.apache.knox.gateway.util.urltemplate.Template) Test(org.junit.Test)

Example 49 with Template

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();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) Template(org.apache.knox.gateway.util.urltemplate.Template) Test(org.junit.Test)

Example 50 with Template

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();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) Template(org.apache.knox.gateway.util.urltemplate.Template) Test(org.junit.Test)

Aggregations

Template (org.apache.knox.gateway.util.urltemplate.Template)50 Test (org.junit.Test)23 UrlRewriteEnvironment (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment)15 Resolver (org.apache.knox.gateway.util.urltemplate.Resolver)10 URISyntaxException (java.net.URISyntaxException)9 URL (java.net.URL)8 UrlRewriteProcessor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteProcessor)8 UrlRewriteRuleDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRuleDescriptor)8 UrlRewriteRulesDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor)8 UrlRewriteActionRewriteDescriptorExt (org.apache.knox.gateway.filter.rewrite.ext.UrlRewriteActionRewriteDescriptorExt)8 HttpServletRequest (javax.servlet.http.HttpServletRequest)7 HttpServletResponse (javax.servlet.http.HttpServletResponse)7 UrlRewriteContext (org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteContext)6 GatewayServices (org.apache.knox.gateway.services.GatewayServices)5 Capture (org.easymock.Capture)5 Host (org.apache.knox.gateway.util.urltemplate.Host)4 Matcher (org.apache.knox.gateway.util.urltemplate.Matcher)4 Query (org.apache.knox.gateway.util.urltemplate.Query)4 URI (java.net.URI)3 UrlRewriteStepStatus (org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteStepStatus)3