Search in sources :

Example 11 with Template

use of org.apache.knox.gateway.util.urltemplate.Template in project knox by apache.

the class UrlRewriteRequest method getSourceUrl.

private Template getSourceUrl() {
    Template urlTemplate;
    // KNOX-439[
    // StringBuffer urlString = super.getRequestURL();
    StringBuffer urlString = new StringBuffer(128);
    urlString.append(getScheme());
    urlString.append("://");
    urlString.append(getServerName());
    urlString.append(":");
    urlString.append(getServerPort());
    urlString.append(super.getRequestURI());
    // ]
    String queryString = super.getQueryString();
    if (queryString != null) {
        urlString.append('?');
        urlString.append(queryString);
    }
    try {
        urlTemplate = Parser.parseLiteral(urlString.toString());
    } catch (URISyntaxException e) {
        LOG.failedToParseValueForUrlRewrite(urlString.toString());
        // Shouldn't be possible given that the URL is constructed from parts of an existing URL.
        urlTemplate = null;
    }
    return urlTemplate;
}
Also used : URISyntaxException(java.net.URISyntaxException) Template(org.apache.knox.gateway.util.urltemplate.Template)

Example 12 with Template

use of org.apache.knox.gateway.util.urltemplate.Template in project knox by apache.

the class UrlRewriteResponse method rewriteValue.

private String rewriteValue(String value, String rule) {
    try {
        Template input = Parser.parseLiteral(value);
        Template output = rewriter.rewrite(this, input, UrlRewriter.Direction.OUT, rule);
        if (output != null) {
            value = output.toString();
        }
    } catch (URISyntaxException e) {
        LOG.failedToParseValueForUrlRewrite(value);
    }
    return value;
}
Also used : URISyntaxException(java.net.URISyntaxException) Template(org.apache.knox.gateway.util.urltemplate.Template)

Example 13 with Template

use of org.apache.knox.gateway.util.urltemplate.Template in project knox by apache.

the class UrlRewriteProcessorTest method testRewriteViaRuleNameWithAmbiguousRules.

@Test
public void testRewriteViaRuleNameWithAmbiguousRules() throws IOException, URISyntaxException {
    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-with-same-rules.xml", "UTF-8"));
    processor.initialize(environment, config);
    Template inputUrl = Parser.parseLiteral("input-mock-scheme-1://input-mock-host-1:42/test-input-path");
    Template outputUrl = processor.rewrite(null, inputUrl, UrlRewriter.Direction.OUT, "test-rule-2");
    assertThat("Expect rewrite to produce a new URL", outputUrl, notNullValue());
    assertThat("Expect rewrite to contain the correct path.", outputUrl.toString(), is("output-mock-scheme-2://output-mock-host-2:42/test-input-path"));
    outputUrl = processor.rewrite(null, inputUrl, UrlRewriter.Direction.OUT, "test-rule-1");
    assertThat("Expect rewrite to produce a new URL", outputUrl, notNullValue());
    assertThat("Expect rewrite to contain the correct path.", outputUrl.toString(), is("output-mock-scheme-1://output-mock-host-1:42/test-input-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 14 with Template

use of org.apache.knox.gateway.util.urltemplate.Template in project knox by apache.

the class UrlRewriteProcessorTest method testSolrRewriteDefaultPort.

@Test
public void testSolrRewriteDefaultPort() 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/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 15 with Template

use of org.apache.knox.gateway.util.urltemplate.Template in project knox by apache.

the class UrlRewriteProcessorTest method testBasicPathRewrite.

@Test
public void testBasicPathRewrite() throws IOException, URISyntaxException {
    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 = Parser.parseLiteral("test-scheme://test-host:1/test-input-path");
    Template outputUrl = processor.rewrite(null, inputUrl, UrlRewriter.Direction.IN, null);
    assertThat("Expect rewrite to produce a new URL", outputUrl, notNullValue());
    assertThat("Expect rewrite to contain the correct path.", outputUrl.toString(), is("test-scheme://test-host:1/test-output-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)

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