jersey gzip setting

jersey gzip setting如下


import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.zip.GZIPOutputStream;
import javax.validation.constraints.NotNull;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.ext.WriterInterceptor;
import javax.ws.rs.ext.WriterInterceptorContext;
import org.slf4j.LoggerFactory;
public class CompressionWriterInterceptor implements WriterInterceptor {
    private static final org.slf4j.Logger log = LoggerFactory.getLogger(CompressionWriterInterceptor.class);
    private HttpHeaders httpHeaders;
 
    public CompressionWriterInterceptor (@Context @NotNull HttpHeaders httpHeaders) {
        this.httpHeaders = httpHeaders;
    }
    @Override
    public void aroundWriteTo (WriterInterceptorContext context)
            throws IOException, WebApplicationException {
        List<String> encodings = this.httpHeaders.getRequestHeader(HttpHeaders.ACCEPT_ENCODING);
     
        try {
            final OutputStream outputStream = context.getOutputStream();
         
            // Compress the response using 'gzip' if the client accepts it.
            if(encodings.contains("gzip")) {
                log.trace("Compress the response using gzip.");
                context.getHeaders().add(HttpHeaders.CONTENT_ENCODING, "gzip");
                context.setOutputStream(new GZIPOutputStream(outputStream));
            }
        } finally {
            context.proceed();
        }
    }
}

留言

這個網誌中的熱門文章

[java]BIG5 字集判斷

[java]半型轉全型

[javascript]當月第一天、當月最後一天