Search in sources :

Example 21 with Template

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

the class HostmapFunctionProcessorTest method testInvalidFunctionNameAndEmptyHostmapUseCase.

@Test
public void testInvalidFunctionNameAndEmptyHostmapUseCase() throws Exception {
    URL configUrl = TestUtils.getResourceUrl(this.getClass(), "empty-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-inbound-host")).anyTimes();
    EasyMock.replay(environment, resolver);
    UrlRewriteRulesDescriptor descriptor = UrlRewriteRulesDescriptorFactory.create();
    UrlRewriteRuleDescriptor rule = descriptor.addRule("test-rule");
    rule.pattern("{*}://{host}:{*}/{**}?{**}");
    UrlRewriteActionRewriteDescriptorExt rewrite = rule.addStep("rewrite");
    rewrite.template("{*}://{$invalid-function(host)}:{*}/{**}?{**}");
    UrlRewriteProcessor rewriter = new UrlRewriteProcessor();
    rewriter.initialize(environment, descriptor);
    Template input = Parser.parseLiteral("test-scheme://test-inbound-host:42/test-path/test-file?test-name=test-value");
    Template output = rewriter.rewrite(resolver, input, UrlRewriter.Direction.IN, null);
    // System.out.println( output );
    assertThat(output, notNullValue());
    assertThat(output.getHost().getFirstValue().getPattern(), is("$invalid-function(host)"));
}
Also used : UrlRewriteEnvironment(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment) UrlRewriteProcessor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteProcessor) UrlRewriteActionRewriteDescriptorExt(org.apache.knox.gateway.filter.rewrite.ext.UrlRewriteActionRewriteDescriptorExt) Resolver(org.apache.knox.gateway.util.urltemplate.Resolver) UrlRewriteRuleDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRuleDescriptor) UrlRewriteRulesDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor) URL(java.net.URL) Template(org.apache.knox.gateway.util.urltemplate.Template) Test(org.junit.Test)

Example 22 with Template

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

the class UrlConnectionDispatch method doGet.

public void doGet(URI url, HttpServletRequest request, HttpServletResponse response) throws IOException, URISyntaxException {
    String sourcePathInfo = request.getPathInfo();
    String sourcePattern = getConfig().getInitParameter("pattern");
    String targetPattern = getConfig().getInitParameter("target");
    // TODO: Some of the compilation should be done at servlet init for performance reasons.
    Template sourceTemplate = Parser.parseTemplate(sourcePattern);
    Template targetTemplate = Parser.parseTemplate(targetPattern);
    Resolver resolver = new DispatchParamResolver(getConfig(), request);
    URI sourceUri = new URI(sourcePathInfo);
    URI targetUri = Rewriter.rewrite(sourceUri, sourceTemplate, targetTemplate, resolver, null);
    // //TODO: This should be more at filter init.
    // Pattern sourceRegex = UrlRewriter.compileUrlRegex( sourcePattern );
    // Matcher matcher = sourceRegex.matcher( sourcePathInfo );
    // String targetUrl = MessageFormat.format( targetPattern, Regex.toGroupArray( matcher ) );
    // System.out.println( "Source URI: " + expect.getRequestURI() );
    // System.out.println( "Source URL: " + expect.getRequestURL() );
    // System.out.println( "Source Query: " + expect.getQueryString() );
    // System.out.println( "Source pathInfo: " + sourcePathInfo );
    // System.out.println( "Source pattern: " + sourcePattern );
    // System.out.println( "Target pattern: " + targetPattern );
    // System.out.println( "Resolved target: " + targetUrl );
    StringBuilder paramStr = new StringBuilder();
    Enumeration paramNames = request.getParameterNames();
    if (paramNames.hasMoreElements()) {
        paramStr.append("?");
    }
    while (paramNames.hasMoreElements()) {
        String paramName = (String) paramNames.nextElement();
        String paramValue = request.getParameter(paramName);
        paramStr.append(paramName);
        paramStr.append("=");
        paramStr.append(URLEncoder.encode(paramValue, "UTF-8"));
        if (paramNames.hasMoreElements()) {
            paramStr.append("&");
        }
    }
    String urlStr = targetUri.toString() + paramStr.toString();
    try {
        URL clientUrl = new URL(urlStr);
        // System.out.println( "Resolved query: " + clientUrl );
        AuthenticatedURL.Token token = new AuthenticatedURL.Token();
        KerberosAuthenticator authenticator = new KerberosAuthenticator();
        auditor.audit(Action.DISPATCH, urlStr, ResourceType.URI, ActionOutcome.UNAVAILABLE);
        HttpURLConnection conn = new AuthenticatedURL(authenticator).openConnection(clientUrl, token);
        // System.out.println( "STATUS=" + conn.getResponseCode() );
        InputStream input = conn.getInputStream();
        if (input != null) {
            OutputStream output = response.getOutputStream();
            try {
                IOUtils.copy(input, output);
            } finally {
                // KNOX-685: output.flush();
                input.close();
            }
        }
        auditor.audit(Action.DISPATCH, urlStr, ResourceType.URI, ActionOutcome.SUCCESS);
    } catch (AuthenticationException e) {
        response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
        LOG.failedToEstablishConnectionToUrl(urlStr, e);
        auditor.audit(Action.DISPATCH, urlStr, ResourceType.URI, ActionOutcome.FAILURE, RES.responseStatus(HttpServletResponse.SC_UNAUTHORIZED));
    } catch (FileNotFoundException e) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        LOG.failedToEstablishConnectionToUrl(urlStr, e);
        auditor.audit(Action.DISPATCH, urlStr, ResourceType.URI, ActionOutcome.FAILURE, RES.responseStatus(HttpServletResponse.SC_NOT_FOUND));
    }
}
Also used : Enumeration(java.util.Enumeration) Resolver(org.apache.knox.gateway.util.urltemplate.Resolver) AuthenticationException(org.apache.hadoop.security.authentication.client.AuthenticationException) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileNotFoundException(java.io.FileNotFoundException) URI(java.net.URI) URL(java.net.URL) AuthenticatedURL(org.apache.hadoop.security.authentication.client.AuthenticatedURL) Template(org.apache.knox.gateway.util.urltemplate.Template) HttpURLConnection(java.net.HttpURLConnection) KerberosAuthenticator(org.apache.hadoop.security.authentication.client.KerberosAuthenticator) AuthenticatedURL(org.apache.hadoop.security.authentication.client.AuthenticatedURL)

Example 23 with Template

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

the class HostmapFunctionProcessorTest method testBasicUseCase.

@Test
public void testBasicUseCase() throws Exception {
    URL configUrl = TestUtils.getResourceUrl(this.getClass(), "hostmap.txt");
    HostMapper hm = EasyMock.createNiceMock(HostMapper.class);
    EasyMock.expect(hm.resolveInboundHostName("test-inbound-host")).andReturn("test-inbound-rewritten-host").anyTimes();
    HostMapperService hms = EasyMock.createNiceMock(HostMapperService.class);
    GatewayServices gatewayServices = EasyMock.createNiceMock(GatewayServices.class);
    EasyMock.expect(gatewayServices.getService(GatewayServices.HOST_MAPPING_SERVICE)).andReturn(hms).anyTimes();
    UrlRewriteEnvironment environment = EasyMock.createNiceMock(UrlRewriteEnvironment.class);
    EasyMock.expect(environment.getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE)).andReturn(gatewayServices).anyTimes();
    EasyMock.expect(environment.resolve("cluster.name")).andReturn(Arrays.asList("test-cluster-name")).anyTimes();
    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-inbound-host")).anyTimes();
    EasyMock.replay(gatewayServices, hm, hms, environment, resolver);
    UrlRewriteRulesDescriptor descriptor = UrlRewriteRulesDescriptorFactory.create();
    UrlRewriteRuleDescriptor rule = descriptor.addRule("test-rule");
    rule.pattern("{*}://{host}:{*}/{**}?{**}");
    UrlRewriteActionRewriteDescriptorExt rewrite = rule.addStep("rewrite");
    rewrite.template("{*}://{$hostmap(host)}:{*}/{**}?{**}");
    UrlRewriteProcessor rewriter = new UrlRewriteProcessor();
    rewriter.initialize(environment, descriptor);
    Template input = Parser.parseLiteral("test-scheme://test-inbound-host:42/test-path/test-file?test-name=test-value");
    Template output = rewriter.rewrite(resolver, input, UrlRewriter.Direction.IN, null);
    // System.out.println( output );
    assertThat(output, notNullValue());
    assertThat(output.getHost().getFirstValue().getPattern(), is("test-inbound-rewritten-host"));
}
Also used : UrlRewriteEnvironment(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment) GatewayServices(org.apache.knox.gateway.services.GatewayServices) UrlRewriteProcessor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteProcessor) UrlRewriteActionRewriteDescriptorExt(org.apache.knox.gateway.filter.rewrite.ext.UrlRewriteActionRewriteDescriptorExt) Resolver(org.apache.knox.gateway.util.urltemplate.Resolver) UrlRewriteRuleDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRuleDescriptor) HostMapper(org.apache.knox.gateway.services.hostmap.HostMapper) UrlRewriteRulesDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor) HostMapperService(org.apache.knox.gateway.services.hostmap.HostMapperService) URL(java.net.URL) Template(org.apache.knox.gateway.util.urltemplate.Template) Test(org.junit.Test)

Example 24 with Template

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

the class HostmapFunctionProcessorTest method testHdfsUseCase.

@Test
public void testHdfsUseCase() 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}:{*}/{**}?{**}");
    UrlRewriteActionRewriteDescriptorExt rewrite = rule.addStep("rewrite");
    rewrite.template("{*}://test-static-host:{*}/{**}?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?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));
}
Also used : UrlRewriteEnvironment(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment) UrlRewriteProcessor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteProcessor) UrlRewriteActionRewriteDescriptorExt(org.apache.knox.gateway.filter.rewrite.ext.UrlRewriteActionRewriteDescriptorExt) Resolver(org.apache.knox.gateway.util.urltemplate.Resolver) UrlRewriteRuleDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRuleDescriptor) UrlRewriteRulesDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor) URL(java.net.URL) Template(org.apache.knox.gateway.util.urltemplate.Template) Test(org.junit.Test)

Example 25 with Template

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

the class HostmapFunctionProcessorTest method testInvalidFunctionNameUseCase.

@Test
public void testInvalidFunctionNameUseCase() throws Exception {
    URL configUrl = TestUtils.getResourceUrl(this.getClass(), "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-inbound-host")).anyTimes();
    EasyMock.replay(environment, resolver);
    UrlRewriteRulesDescriptor descriptor = UrlRewriteRulesDescriptorFactory.create();
    UrlRewriteRuleDescriptor rule = descriptor.addRule("test-rule");
    rule.pattern("{*}://{host}:{*}/{**}?{**}");
    UrlRewriteActionRewriteDescriptorExt rewrite = rule.addStep("rewrite");
    rewrite.template("{*}://{$invalid-function(host)}:{*}/{**}?{**}");
    UrlRewriteProcessor rewriter = new UrlRewriteProcessor();
    rewriter.initialize(environment, descriptor);
    Template input = Parser.parseLiteral("test-scheme://test-inbound-host:42/test-path/test-file?test-name=test-value");
    Template output = rewriter.rewrite(resolver, input, UrlRewriter.Direction.IN, null);
    // System.out.println( output );
    assertThat(output, notNullValue());
    assertThat(output.getHost().getFirstValue().getPattern(), is("$invalid-function(host)"));
}
Also used : UrlRewriteEnvironment(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment) UrlRewriteProcessor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteProcessor) UrlRewriteActionRewriteDescriptorExt(org.apache.knox.gateway.filter.rewrite.ext.UrlRewriteActionRewriteDescriptorExt) Resolver(org.apache.knox.gateway.util.urltemplate.Resolver) UrlRewriteRuleDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRuleDescriptor) UrlRewriteRulesDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor) URL(java.net.URL) 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