Search in sources :

Example 6 with Matcher

use of org.apache.knox.gateway.util.urltemplate.Matcher 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)

Aggregations

Matcher (org.apache.knox.gateway.util.urltemplate.Matcher)6 Template (org.apache.knox.gateway.util.urltemplate.Template)4 URI (java.net.URI)2 UrlRewriteStepStatus (org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteStepStatus)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 FilterChain (javax.servlet.FilterChain)1 ServletException (javax.servlet.ServletException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 AuditContext (org.apache.knox.gateway.audit.api.AuditContext)1 ScopedMatcher (org.apache.knox.gateway.filter.rewrite.ext.ScopedMatcher)1 UrlRewriteContextImpl (org.apache.knox.gateway.filter.rewrite.impl.UrlRewriteContextImpl)1 UrlRewriteRuleProcessorHolder (org.apache.knox.gateway.filter.rewrite.impl.UrlRewriteRuleProcessorHolder)1 UrlRewriteStepProcessorHolder (org.apache.knox.gateway.filter.rewrite.impl.UrlRewriteStepProcessorHolder)1 UrlRewriteContext (org.apache.knox.gateway.filter.rewrite.spi.UrlRewriteContext)1 Topology (org.apache.knox.gateway.topology.Topology)1