package com.wizzardo.http.filter;

import com.wizzardo.http.Filter;
import com.wizzardo.http.request.Header;
import com.wizzardo.http.request.Request;
import com.wizzardo.http.response.Response;
import com.wizzardo.tools.misc.pool.*;

import java.io.ByteArrayOutputStream;
import java.util.zip.GZIPOutputStream;

/**
 * Created by wizzardo on 30.06.15.
 */
public class GzipFilter implements Filter {

    protected Pool<ByteArrayOutputStream> buffers = new PoolBuilder<ByteArrayOutputStream>()
            .supplier(ByteArrayOutputStream::new)
            .resetter(ByteArrayOutputStream::reset)
            .queue(PoolBuilder.createThreadLocalQueueSupplier())
            .build();

    @Override
    public boolean filter(Request request, Response response) {
        return buffers.provide(out -> {
            GZIPOutputStream gout = new GZIPOutputStream(out);
            gout.write(response.getBody());
            gout.close();
            response.setBody(out.toByteArray());
            response.appendHeader(Header.KV_CONTENT_ENCODING_GZIP);
            return true;
        });
    }
}

@@ -756,16 +756,279 @@
 onse) %7B%0A
+        if (%22gzip%22.equals(response.header(Header.KEY_CONTENT_ENCODING)))%0A            return true;%0A        if (response.header(Header.KV_CONTENT_ENCODING_GZIP) != null)%0A            return true;%0A        if (response.contentLength() == 0)%0A            return true;%0A%0A%0A
         

add checks for gzip filter