Search in sources :

Example 1 with InputStreamEntity

use of org.apache.knox.gateway.dispatch.InputStreamEntity in project knox by apache.

the class UrlRewriteRequestTest method testContentEncoding.

/*
   * Test the case where a request has
   * Content-Type:text/xml and Content-Encoding:gzip
   */
@Test
public void testContentEncoding() throws Exception {
    /* copy results */
    final ByteArrayOutputStream results = new ByteArrayOutputStream();
    final InputStream input = Files.newInputStream(Paths.get(ClassLoader.getSystemResource("KNOX-1412.xml.gz").toURI()));
    final ServletInputStream payload = new ServletInputStream() {

        @Override
        public int read() throws IOException {
            return input.read();
        }

        @Override
        public boolean isFinished() {
            return false;
        }

        @Override
        public boolean isReady() {
            return false;
        }

        @Override
        public void setReadListener(ReadListener readListener) {
        }
    };
    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.getAttribute("org.apache.knox.gateway.frontend.uri")).andReturn(new URI("https://test-location")).anyTimes();
    EasyMock.expect(environment.resolve("cluster.name")).andReturn(Collections.singletonList("test-cluster-name")).anyTimes();
    EasyMock.replay(gatewayServices, environment);
    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);
    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();
    /* Request wih Content-Type:text/xml and Content-Encoding:gzip */
    HttpServletRequest request1 = EasyMock.createNiceMock(HttpServletRequest.class);
    EasyMock.expect(request1.getScheme()).andReturn("https").anyTimes();
    EasyMock.expect(request1.getServerName()).andReturn("targethost.com").anyTimes();
    EasyMock.expect(request1.getServerPort()).andReturn(80).anyTimes();
    EasyMock.expect(request1.getRequestURI()).andReturn("/").anyTimes();
    EasyMock.expect(request1.getQueryString()).andReturn(null).anyTimes();
    EasyMock.expect(request1.getInputStream()).andReturn(payload).anyTimes();
    EasyMock.expect(request1.getContentLength()).andReturn(input.available()).anyTimes();
    EasyMock.expect(request1.getContentType()).andReturn("text/xml").anyTimes();
    EasyMock.expect(request1.getHeader("Content-Encoding")).andReturn("gzip").anyTimes();
    EasyMock.expect(request1.getHeader("Host")).andReturn("sourcehost.com").anyTimes();
    /* Request wih Content-Type:application/gzip and Content-Encoding:gzip */
    HttpServletRequest request2 = EasyMock.createNiceMock(HttpServletRequest.class);
    EasyMock.expect(request2.getScheme()).andReturn("https").anyTimes();
    EasyMock.expect(request2.getServerName()).andReturn("targethost.com").anyTimes();
    EasyMock.expect(request2.getServerPort()).andReturn(80).anyTimes();
    EasyMock.expect(request2.getRequestURI()).andReturn("/").anyTimes();
    EasyMock.expect(request2.getQueryString()).andReturn(null).anyTimes();
    EasyMock.expect(request2.getInputStream()).andReturn(payload).anyTimes();
    EasyMock.expect(request2.getContentLength()).andReturn(input.available()).anyTimes();
    EasyMock.expect(request2.getContentType()).andReturn("application/gzip").anyTimes();
    EasyMock.expect(request2.getHeader("Content-Encoding")).andReturn("gzip").anyTimes();
    EasyMock.expect(request2.getHeader("Host")).andReturn("sourcehost.com").anyTimes();
    /* Request wih Content-Type:application/gzip no content encoding */
    HttpServletRequest request3 = EasyMock.createNiceMock(HttpServletRequest.class);
    EasyMock.expect(request3.getScheme()).andReturn("https").anyTimes();
    EasyMock.expect(request3.getServerName()).andReturn("targethost.com").anyTimes();
    EasyMock.expect(request3.getServerPort()).andReturn(80).anyTimes();
    EasyMock.expect(request3.getRequestURI()).andReturn("/").anyTimes();
    EasyMock.expect(request3.getQueryString()).andReturn(null).anyTimes();
    EasyMock.expect(request3.getInputStream()).andReturn(payload).anyTimes();
    EasyMock.expect(request3.getContentLength()).andReturn(input.available()).anyTimes();
    EasyMock.expect(request3.getContentType()).andReturn("application/gzip").anyTimes();
    EasyMock.expect(request3.getHeader("Host")).andReturn("sourcehost.com").anyTimes();
    HttpServletResponse response = EasyMock.createNiceMock(HttpServletResponse.class);
    EasyMock.replay(context, config, response, request1, request2, request3);
    /* make sure the following exception is not thrown
     * java.lang.RuntimeException: com.ctc.wstx.exc.WstxUnexpectedCharException:
     * Illegal character ((CTRL-CHAR, code 31))
     */
    /* Test for condition where Content-Type:text/xml and Content-Encoding:gzip */
    UrlRewriteRequest rewriteRequest = new UrlRewriteRequest(config, request1);
    ServletInputStream inputStream = rewriteRequest.getInputStream();
    HttpEntity entity = new InputStreamEntity(inputStream, request1.getContentLength(), ContentType.parse("text/xml"));
    entity.writeTo(results);
    /* Test for condition where Content-Type:application/gzip and Content-Encoding:gzip */
    rewriteRequest = new UrlRewriteRequest(config, request2);
    inputStream = rewriteRequest.getInputStream();
    entity = new InputStreamEntity(inputStream, request1.getContentLength(), ContentType.parse("application/gzip"));
    entity.writeTo(results);
    /* Test for condition where Content-Type:application/gzip no content encoding */
    rewriteRequest = new UrlRewriteRequest(config, request3);
    inputStream = rewriteRequest.getInputStream();
    entity = new InputStreamEntity(inputStream, request1.getContentLength(), ContentType.parse("application/gzip"));
    entity.writeTo(results);
}
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) UrlRewriteRuleDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRuleDescriptor) HttpEntity(org.apache.http.HttpEntity) ServletInputStream(javax.servlet.ServletInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) UrlRewriteRulesDescriptor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor) HttpServletResponse(javax.servlet.http.HttpServletResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ReadListener(javax.servlet.ReadListener) URI(java.net.URI) InputStreamEntity(org.apache.knox.gateway.dispatch.InputStreamEntity) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletInputStream(javax.servlet.ServletInputStream) ServletContext(javax.servlet.ServletContext) FilterConfig(javax.servlet.FilterConfig) Test(org.junit.Test)

Example 2 with InputStreamEntity

use of org.apache.knox.gateway.dispatch.InputStreamEntity in project knox by apache.

the class UrlRewriteRequestTest method testEmptyPayload.

@Test
public void testEmptyPayload() throws Exception {
    /* copy results */
    final ByteArrayOutputStream results = new ByteArrayOutputStream();
    final ByteArrayInputStream bai = new ByteArrayInputStream("".getBytes(StandardCharsets.UTF_8));
    final ServletInputStream payload = new ServletInputStream() {

        @Override
        public int read() {
            return bai.read();
        }

        @Override
        public int available() {
            return bai.available();
        }

        @Override
        public boolean isFinished() {
            return false;
        }

        @Override
        public boolean isReady() {
            return false;
        }

        @Override
        public void setReadListener(ReadListener readListener) {
        }
    };
    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);
    EasyMock.expect(request.getScheme()).andReturn("https").anyTimes();
    EasyMock.expect(request.getServerName()).andReturn("targethost.com").anyTimes();
    EasyMock.expect(request.getServerPort()).andReturn(80).anyTimes();
    EasyMock.expect(request.getRequestURI()).andReturn("/").anyTimes();
    EasyMock.expect(request.getQueryString()).andReturn(null).anyTimes();
    EasyMock.expect(request.getHeader("Host")).andReturn("sourcehost.com").anyTimes();
    EasyMock.expect(request.getMethod()).andReturn("POST").anyTimes();
    EasyMock.expect(request.getContentType()).andReturn("application/xml").anyTimes();
    EasyMock.expect(request.getInputStream()).andReturn(payload).anyTimes();
    EasyMock.expect(request.getContentLength()).andReturn(-1).anyTimes();
    HttpServletResponse response = EasyMock.createNiceMock(HttpServletResponse.class);
    EasyMock.replay(rewriter, context, config, request, response);
    // instantiate UrlRewriteRequest so that we can use it as a Template factory for targetUrl
    UrlRewriteRequest rewriteRequest = new UrlRewriteRequest(config, request);
    ServletInputStream inputStream = rewriteRequest.getInputStream();
    HttpEntity entity = new InputStreamEntity(inputStream, request.getContentLength(), ContentType.parse("application/xml"));
    entity.writeTo(results);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) UrlRewriteProcessor(org.apache.knox.gateway.filter.rewrite.api.UrlRewriteProcessor) ServletInputStream(javax.servlet.ServletInputStream) HttpEntity(org.apache.http.HttpEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) ServletContext(javax.servlet.ServletContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) FilterConfig(javax.servlet.FilterConfig) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ReadListener(javax.servlet.ReadListener) InputStreamEntity(org.apache.knox.gateway.dispatch.InputStreamEntity) Test(org.junit.Test)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 FilterConfig (javax.servlet.FilterConfig)2 ReadListener (javax.servlet.ReadListener)2 ServletContext (javax.servlet.ServletContext)2 ServletInputStream (javax.servlet.ServletInputStream)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 HttpEntity (org.apache.http.HttpEntity)2 InputStreamEntity (org.apache.knox.gateway.dispatch.InputStreamEntity)2 UrlRewriteProcessor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteProcessor)2 Test (org.junit.Test)2 InputStream (java.io.InputStream)1 URI (java.net.URI)1 UrlRewriteEnvironment (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteEnvironment)1 UrlRewriteRuleDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRuleDescriptor)1 UrlRewriteRulesDescriptor (org.apache.knox.gateway.filter.rewrite.api.UrlRewriteRulesDescriptor)1 UrlRewriteActionRewriteDescriptorExt (org.apache.knox.gateway.filter.rewrite.ext.UrlRewriteActionRewriteDescriptorExt)1 GatewayServices (org.apache.knox.gateway.services.GatewayServices)1