Search in sources :

Example 11 with UrlRewriteProcessor

use of org.apache.knox.gateway.filter.rewrite.api.UrlRewriteProcessor 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)

Example 12 with UrlRewriteProcessor

use of org.apache.knox.gateway.filter.rewrite.api.UrlRewriteProcessor in project knox by apache.

the class InboundUrlFunctionProcessorTest method testQueryParam.

@Test
public void testQueryParam() throws Exception {
    GatewayServices gatewayServices = EasyMock.createNiceMock(GatewayServices.class);
    UrlRewriteEnvironment environment = EasyMock.createNiceMock(UrlRewriteEnvironment.class);
    EasyMock.expect(environment.getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE)).andReturn(gatewayServices).anyTimes();
    EasyMock.expect(environment.resolve("cluster.name")).andReturn(Collections.singletonList("test-cluster-name")).anyTimes();
    Resolver resolver = EasyMock.createNiceMock(Resolver.class);
    EasyMock.expect(resolver.resolve("query.param.host")).andReturn(Lists.newArrayList("http://foo:50075")).anyTimes();
    EasyMock.replay(gatewayServices, environment, resolver);
    UrlRewriteRulesDescriptor descriptor = UrlRewriteRulesDescriptorFactory.create();
    UrlRewriteRuleDescriptor rule = descriptor.addRule("test-location");
    rule.pattern("{*}://{*}:{*}/{**}/?{**}");
    UrlRewriteActionRewriteDescriptorExt rewrite = rule.addStep("rewrite");
    rewrite.template("{$inboundurl[host]}");
    UrlRewriteProcessor rewriter = new UrlRewriteProcessor();
    rewriter.initialize(environment, descriptor);
    Template input = Parser.parseLiteral("https://localhost:8443/gateway/default/datanode/?host=http://foo:50075");
    Template output = rewriter.rewrite(resolver, input, UrlRewriter.Direction.OUT, "test-location");
    assertThat(output.toString(), is("http://foo:50075"));
}
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) UrlRewriteRulesDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor) Template(org.apache.knox.gateway.util.urltemplate.Template) Test(org.junit.Test)

Example 13 with UrlRewriteProcessor

use of org.apache.knox.gateway.filter.rewrite.api.UrlRewriteProcessor in project knox by apache.

the class UrlRewriteResponseTest method testResolve.

@Test
public void testResolve() throws Exception {
    UrlRewriteProcessor rewriter = EasyMock.createNiceMock(UrlRewriteProcessor.class);
    ServletContext context = EasyMock.createNiceMock(ServletContext.class);
    EasyMock.expect(context.getServletContextName()).andReturn("test-cluster-name").anyTimes();
    EasyMock.expect(context.getInitParameter("test-init-param-name")).andReturn("test-init-param-value").anyTimes();
    EasyMock.expect(context.getAttribute(UrlRewriteServletContextListener.PROCESSOR_ATTRIBUTE_NAME)).andReturn(rewriter).anyTimes();
    FilterConfig config = EasyMock.createNiceMock(FilterConfig.class);
    EasyMock.expect(config.getInitParameter("test-filter-init-param-name")).andReturn("test-filter-init-param-value").anyTimes();
    EasyMock.expect(config.getServletContext()).andReturn(context).anyTimes();
    HttpServletRequest request = EasyMock.createNiceMock(HttpServletRequest.class);
    HttpServletResponse response = EasyMock.createNiceMock(HttpServletResponse.class);
    EasyMock.replay(rewriter, context, config, request, response);
    UrlRewriteResponse rewriteResponse = new UrlRewriteResponse(config, request, response);
    List<String> names = rewriteResponse.resolve("test-filter-init-param-name");
    assertThat(names.size(), is(1));
    assertThat(names.get(0), is("test-filter-init-param-value"));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) UrlRewriteProcessor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteProcessor) ServletContext(javax.servlet.ServletContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) FilterConfig(javax.servlet.FilterConfig) Test(org.junit.Test)

Example 14 with UrlRewriteProcessor

use of org.apache.knox.gateway.filter.rewrite.api.UrlRewriteProcessor in project knox by apache.

the class UrlRewriteResponseTest method testResolveGatewayParams.

@Test
public void testResolveGatewayParams() throws Exception {
    UrlRewriteProcessor rewriter = EasyMock.createNiceMock(UrlRewriteProcessor.class);
    ServletContext context = EasyMock.createNiceMock(ServletContext.class);
    EasyMock.expect(context.getAttribute(UrlRewriteServletContextListener.PROCESSOR_ATTRIBUTE_NAME)).andReturn(rewriter).anyTimes();
    FilterConfig config = EasyMock.createNiceMock(FilterConfig.class);
    EasyMock.expect(config.getServletContext()).andReturn(context).anyTimes();
    HttpServletRequest request = EasyMock.createNiceMock(HttpServletRequest.class);
    EasyMock.expect(request.getScheme()).andReturn("mock-scheme").anyTimes();
    EasyMock.expect(request.getLocalName()).andReturn("mock-host").anyTimes();
    EasyMock.expect(request.getLocalPort()).andReturn(42).anyTimes();
    EasyMock.expect(request.getContextPath()).andReturn("/mock-path").anyTimes();
    HttpServletResponse response = EasyMock.createNiceMock(HttpServletResponse.class);
    EasyMock.replay(rewriter, context, config, request, response);
    UrlRewriteResponse rewriteResponse = new UrlRewriteResponse(config, request, response);
    List<String> url = rewriteResponse.resolve("gateway.url");
    assertThat(url, hasItems(new String[] { "mock-scheme://mock-host:42/mock-path" }));
    List<String> scheme = rewriteResponse.resolve("gateway.scheme");
    assertThat(scheme, hasItems(new String[] { "mock-scheme" }));
    List<String> host = rewriteResponse.resolve("gateway.host");
    assertThat(host, hasItems(new String[] { "mock-host" }));
    List<String> port = rewriteResponse.resolve("gateway.port");
    assertThat(port, hasItems(new String[] { "42" }));
    List<String> addr = rewriteResponse.resolve("gateway.addr");
    assertThat(addr, hasItems(new String[] { "mock-host:42" }));
    List<String> address = rewriteResponse.resolve("gateway.addr");
    assertThat(address, hasItems(new String[] { "mock-host:42" }));
    List<String> path = rewriteResponse.resolve("gateway.path");
    assertThat(path, hasItems(new String[] { "/mock-path" }));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) UrlRewriteProcessor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteProcessor) ServletContext(javax.servlet.ServletContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) FilterConfig(javax.servlet.FilterConfig) Test(org.junit.Test)

Example 15 with UrlRewriteProcessor

use of org.apache.knox.gateway.filter.rewrite.api.UrlRewriteProcessor in project knox by apache.

the class OozieServiceDefinitionTest method testOozieRewriteRulesForLiteralTemplateValuesBugKnox394.

@Test(timeout = TestUtils.MEDIUM_TIMEOUT)
public void testOozieRewriteRulesForLiteralTemplateValuesBugKnox394() throws Exception {
    LOG_ENTER();
    // This is a unique part of this test.
    String testResource = "oozie-request-with-var.xml";
    // Mock out the service url registry which is required for several url rewrite functions to work.
    ServiceRegistry registry = EasyMock.createNiceMock(ServiceRegistry.class);
    EasyMock.expect(registry.lookupServiceURL("test-cluster", "NAMENODE")).andReturn("test-scheme://test-host:42").anyTimes();
    // Mock out the gateway services registry which is required for several url rewrite functions to work.
    GatewayServices services = EasyMock.createNiceMock(GatewayServices.class);
    EasyMock.expect(services.getService(GatewayServices.SERVICE_REGISTRY_SERVICE)).andReturn(registry).anyTimes();
    UrlRewriteProcessor rewriteProcessor = new UrlRewriteProcessor();
    ServletContext servletContext = EasyMock.createNiceMock(ServletContext.class);
    EasyMock.expect(servletContext.getAttribute(UrlRewriteServletContextListener.PROCESSOR_ATTRIBUTE_NAME)).andReturn(rewriteProcessor).anyTimes();
    EasyMock.expect(servletContext.getAttribute(GatewayServices.GATEWAY_SERVICES_ATTRIBUTE)).andReturn(services).anyTimes();
    EasyMock.expect(servletContext.getAttribute(GatewayServices.GATEWAY_CLUSTER_ATTRIBUTE)).andReturn("test-cluster").anyTimes();
    HttpServletRequest servletRequest = EasyMock.createNiceMock(HttpServletRequest.class);
    EasyMock.expect(servletRequest.getInputStream()).andReturn(new MockServletInputStream(TestUtils.getResourceStream(OozieServiceDefinitionTest.class, testResource))).anyTimes();
    EasyMock.expect(servletRequest.getContentType()).andReturn("text/xml").anyTimes();
    FilterConfig filterConfig = EasyMock.createNiceMock(FilterConfig.class);
    EasyMock.expect(filterConfig.getServletContext()).andReturn(servletContext).anyTimes();
    EasyMock.expect(filterConfig.getInitParameter(UrlRewriteServletFilter.REQUEST_BODY_FILTER_PARAM)).andReturn("OOZIE/oozie/configuration").anyTimes();
    EasyMock.replay(registry, services, servletContext, servletRequest, filterConfig);
    UrlRewriteEnvironment rewriteEnvironment = new UrlRewriteServletEnvironment(servletContext);
    Reader rulesReader = TestUtils.getResourceReader("services/oozie/4.0.0/rewrite.xml", "UTF-8");
    UrlRewriteRulesDescriptor rewriteRules = UrlRewriteRulesDescriptorFactory.load("xml", rulesReader);
    rulesReader.close();
    rewriteProcessor.initialize(rewriteEnvironment, rewriteRules);
    UrlRewriteRequest rewriteRequest = new UrlRewriteRequest(filterConfig, servletRequest);
    InputStream stream = rewriteRequest.getInputStream();
    Document document = XmlUtils.readXml(stream);
    assertThat(document, hasXPath("/configuration/property[name='oozie.wf.application.path']/value", equalTo("${appPath}/workflow.xml")));
    LOG_EXIT();
}
Also used : MockServletInputStream(org.apache.knox.test.mock.MockServletInputStream) UrlRewriteEnvironment(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment) GatewayServices(org.apache.knox.gateway.services.GatewayServices) UrlRewriteRequest(org.apache.knox.gateway.filter.rewrite.impl.UrlRewriteRequest) UrlRewriteProcessor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteProcessor) MockServletInputStream(org.apache.knox.test.mock.MockServletInputStream) InputStream(java.io.InputStream) Reader(java.io.Reader) UrlRewriteRulesDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor) UrlRewriteServletEnvironment(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteServletEnvironment) Document(org.w3c.dom.Document) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletContext(javax.servlet.ServletContext) FilterConfig(javax.servlet.FilterConfig) ServiceRegistry(org.apache.knox.gateway.services.registry.ServiceRegistry) Test(org.junit.Test)

Aggregations

UrlRewriteProcessor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteProcessor)15 Test (org.junit.Test)14 UrlRewriteEnvironment (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment)11 UrlRewriteRulesDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor)11 UrlRewriteRuleDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRuleDescriptor)8 UrlRewriteActionRewriteDescriptorExt (org.apache.knox.gateway.filter.rewrite.ext.UrlRewriteActionRewriteDescriptorExt)8 Resolver (org.apache.knox.gateway.util.urltemplate.Resolver)8 Template (org.apache.knox.gateway.util.urltemplate.Template)8 URL (java.net.URL)7 FilterConfig (javax.servlet.FilterConfig)7 ServletContext (javax.servlet.ServletContext)7 HttpServletRequest (javax.servlet.http.HttpServletRequest)7 GatewayServices (org.apache.knox.gateway.services.GatewayServices)5 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 InputStream (java.io.InputStream)3 Reader (java.io.Reader)3 UrlRewriteServletEnvironment (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteServletEnvironment)3 UrlRewriteRequest (org.apache.knox.gateway.filter.rewrite.impl.UrlRewriteRequest)3 ServiceRegistry (org.apache.knox.gateway.services.registry.ServiceRegistry)3 MockServletInputStream (org.apache.knox.test.mock.MockServletInputStream)3