Search in sources :

Example 61 with Inflater

use of java.util.zip.Inflater in project jetty.project by eclipse.

the class GzipTester method getResponseMetadata.

public ContentMetadata getResponseMetadata(Response response) throws Exception {
    long size = response.getContentBytes().length;
    String contentEncoding = response.get("Content-Encoding");
    ByteArrayInputStream bais = null;
    InputStream in = null;
    DigestOutputStream digester = null;
    ByteArrayOutputStream uncompressedStream = null;
    try {
        MessageDigest digest = MessageDigest.getInstance("SHA1");
        bais = new ByteArrayInputStream(response.getContentBytes());
        if (contentEncoding == null) {
            LOG.debug("No response content-encoding");
            in = new PassThruInputStream(bais);
        } else if (contentEncoding.contains(GzipHandler.GZIP)) {
            in = new GZIPInputStream(bais);
        } else if (contentEncoding.contains(GzipHandler.DEFLATE)) {
            in = new InflaterInputStream(bais, new Inflater(true));
        } else {
            assertThat("Unexpected response content-encoding", contentEncoding, isEmptyOrNullString());
        }
        uncompressedStream = new ByteArrayOutputStream((int) size);
        digester = new DigestOutputStream(uncompressedStream, digest);
        IO.copy(in, digester);
        byte[] output = uncompressedStream.toByteArray();
        String actualSha1Sum = Hex.asHex(digest.digest());
        return new ContentMetadata(output.length, actualSha1Sum);
    } finally {
        IO.close(digester);
        IO.close(in);
        IO.close(bais);
        IO.close(uncompressedStream);
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) InflaterInputStream(java.util.zip.InflaterInputStream) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) DigestOutputStream(java.security.DigestOutputStream) Inflater(java.util.zip.Inflater) MessageDigest(java.security.MessageDigest)

Example 62 with Inflater

use of java.util.zip.Inflater in project jetty.project by eclipse.

the class GzipTester method assertIsResponseGzipCompressed.

public HttpTester.Response assertIsResponseGzipCompressed(String method, String requestedFilename, String serverFilename, long ifmodifiedsince) throws Exception {
    HttpTester.Request request = HttpTester.newRequest();
    HttpTester.Response response;
    request.setMethod(method);
    request.setVersion("HTTP/1.0");
    request.setHeader("Host", "tester");
    request.setHeader("Accept-Encoding", compressionType);
    if (ifmodifiedsince > 0)
        request.setHeader(HttpHeader.IF_MODIFIED_SINCE.asString(), DateGenerator.formatDate(ifmodifiedsince));
    if (this.userAgent != null)
        request.setHeader("User-Agent", this.userAgent);
    request.setURI("/context/" + requestedFilename);
    // Issue the request
    response = HttpTester.parseResponse(tester.getResponses(request.generate()));
    // Assert the response headers
    // Assert.assertThat("Response.status",response.getStatus(),is(HttpServletResponse.SC_OK));
    // Response headers should have either a Transfer-Encoding indicating chunked OR a Content-Length
    /*
         * TODO need to check for the 3rd option of EOF content. To do this properly you might need to look at both HTTP/1.1 and HTTP/1.0 requests String
         * contentLength = response.get("Content-Length"); String transferEncoding = response.get("Transfer-Encoding");
         * 
         * boolean chunked = (transferEncoding != null) && (transferEncoding.indexOf("chunk") >= 0); if(!chunked) {
         * Assert.assertThat("Response.header[Content-Length]",contentLength,notNullValue()); } else {
         * Assert.assertThat("Response.header[Transfer-Encoding]",transferEncoding,notNullValue()); }
         */
    int qindex = compressionType.indexOf(";");
    if (qindex < 0)
        Assert.assertThat("Response.header[Content-Encoding]", response.get("Content-Encoding"), containsString(compressionType));
    else
        Assert.assertThat("Response.header[Content-Encoding]", response.get("Content-Encoding"), containsString(compressionType.substring(0, qindex)));
    Assert.assertThat(response.get("ETag"), Matchers.startsWith("W/"));
    // Assert that the decompressed contents are what we expect.
    File serverFile = testdir.getPathFile(serverFilename).toFile();
    String expected = IO.readToString(serverFile);
    String actual = null;
    ByteArrayInputStream bais = null;
    InputStream in = null;
    ByteArrayOutputStream out = null;
    try {
        bais = new ByteArrayInputStream(response.getContentBytes());
        if (compressionType.startsWith(GzipHandler.GZIP)) {
            in = new GZIPInputStream(bais);
        } else if (compressionType.startsWith(GzipHandler.DEFLATE)) {
            in = new InflaterInputStream(bais, new Inflater(true));
        }
        out = new ByteArrayOutputStream();
        IO.copy(in, out);
        actual = out.toString(encoding);
        assertThat("Uncompressed contents", actual, equalTo(expected));
    } finally {
        IO.close(out);
        IO.close(in);
        IO.close(bais);
    }
    return response;
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) InflaterInputStream(java.util.zip.InflaterInputStream) Inflater(java.util.zip.Inflater) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) File(java.io.File) Response(org.eclipse.jetty.http.HttpTester.Response) HttpTester(org.eclipse.jetty.http.HttpTester)

Example 63 with Inflater

use of java.util.zip.Inflater in project jetty.project by eclipse.

the class GzipTester method assertNonStaticContentIsResponseGzipCompressed.

public HttpTester.Response assertNonStaticContentIsResponseGzipCompressed(String method, String path, String expected) throws Exception {
    HttpTester.Request request = HttpTester.newRequest();
    HttpTester.Response response;
    request.setMethod(method);
    request.setVersion("HTTP/1.0");
    request.setHeader("Host", "tester");
    request.setHeader("Accept-Encoding", accept);
    if (this.userAgent != null)
        request.setHeader("User-Agent", this.userAgent);
    request.setURI("/context/" + path);
    // Issue the request
    response = HttpTester.parseResponse(tester.getResponses(request.generate()));
    int qindex = compressionType.indexOf(";");
    if (qindex < 0)
        Assert.assertThat("Response.header[Content-Encoding]", response.get("Content-Encoding"), containsString(compressionType));
    else
        Assert.assertThat("Response.header[Content-Encoding]", response.get("Content-Encoding"), containsString(compressionType.substring(0, qindex)));
    ByteArrayInputStream bais = null;
    InputStream in = null;
    ByteArrayOutputStream out = null;
    String actual = null;
    try {
        bais = new ByteArrayInputStream(response.getContentBytes());
        if (compressionType.startsWith(GzipHandler.GZIP)) {
            in = new GZIPInputStream(bais);
        } else if (compressionType.startsWith(GzipHandler.DEFLATE)) {
            in = new InflaterInputStream(bais, new Inflater(true));
        }
        out = new ByteArrayOutputStream();
        IO.copy(in, out);
        actual = out.toString(encoding);
        assertThat("Uncompressed contents", actual, equalTo(expected));
    } finally {
        IO.close(out);
        IO.close(in);
        IO.close(bais);
    }
    return response;
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) InflaterInputStream(java.util.zip.InflaterInputStream) Inflater(java.util.zip.Inflater) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Matchers.containsString(org.hamcrest.Matchers.containsString) Response(org.eclipse.jetty.http.HttpTester.Response) HttpTester(org.eclipse.jetty.http.HttpTester)

Example 64 with Inflater

use of java.util.zip.Inflater in project jetty.project by eclipse.

the class IncludedGzipTest method testGzip.

@Test
public void testGzip() throws Exception {
    // generated and parsed test
    ByteBuffer request = BufferUtil.toBuffer("GET /context/file.txt HTTP/1.0\r\n" + "Host: tester\r\n" + "Accept-Encoding: " + compressionType + "\r\n" + "\r\n");
    HttpTester.Response response = HttpTester.parseResponse(tester.getResponses(request));
    assertEquals(HttpServletResponse.SC_OK, response.getStatus());
    assertEquals(compressionType, response.get("Content-Encoding"));
    InputStream testIn = null;
    ByteArrayInputStream compressedResponseStream = new ByteArrayInputStream(response.getContentBytes());
    if (compressionType.equals(GzipHandler.GZIP)) {
        testIn = new GZIPInputStream(compressedResponseStream);
    } else if (compressionType.equals(GzipHandler.DEFLATE)) {
        testIn = new InflaterInputStream(compressedResponseStream, new Inflater(true));
    }
    ByteArrayOutputStream testOut = new ByteArrayOutputStream();
    IO.copy(testIn, testOut);
    assertEquals(__content, testOut.toString("ISO8859_1"));
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) InflaterInputStream(java.util.zip.InflaterInputStream) Inflater(java.util.zip.Inflater) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer) HttpTester(org.eclipse.jetty.http.HttpTester) Test(org.junit.Test)

Example 65 with Inflater

use of java.util.zip.Inflater in project dropwizard by dropwizard.

the class BiDiGzipHandler method wrapDeflatedRequest.

private WrappedServletRequest wrapDeflatedRequest(HttpServletRequest request) throws IOException {
    final Inflater inflater = buildInflater();
    final InflaterInputStream input = new InflaterInputStream(request.getInputStream(), inflater, inputBufferSize) {

        @Override
        public void close() throws IOException {
            super.close();
            localInflater.set(inflater);
        }
    };
    return new WrappedServletRequest(request, input);
}
Also used : InflaterInputStream(java.util.zip.InflaterInputStream) Inflater(java.util.zip.Inflater)

Aggregations

Inflater (java.util.zip.Inflater)108 InflaterInputStream (java.util.zip.InflaterInputStream)36 IOException (java.io.IOException)33 ByteArrayOutputStream (java.io.ByteArrayOutputStream)31 DataFormatException (java.util.zip.DataFormatException)30 InputStream (java.io.InputStream)24 ByteArrayInputStream (java.io.ByteArrayInputStream)14 GZIPInputStream (java.util.zip.GZIPInputStream)14 Deflater (java.util.zip.Deflater)9 Test (org.junit.Test)7 DataInputStream (java.io.DataInputStream)6 OutputStream (java.io.OutputStream)6 BufferedInputStream (java.io.BufferedInputStream)5 BufferedReader (java.io.BufferedReader)5 InputStreamReader (java.io.InputStreamReader)5 HttpURLConnection (java.net.HttpURLConnection)5 ByteBuffer (java.nio.ByteBuffer)5 JSONObject (org.json.JSONObject)5 DataOutputStream (java.io.DataOutputStream)4 OutputStreamWriter (java.io.OutputStreamWriter)4