Implement gzip compression through filter and httpservletresponsewrapper

Source: Internet
Author: User

The key to achieving custom output is to encapsulate httpservletresponse, intercept all outputs, and wait until the filter chain completes processing,
Filter. dofilter: captures the output for processing and writes it to the real httpservletresponse. Already exists in J2EE
Httpservletresponsewrapper makes it easier to wrap httpservletresponse

Public class gzipfilter implements filter {
Public void destroy (){
}

Public void dofilter (servletrequest request, servletresponse response, filterchain chain) throws ioexception,
Servletexception {
Httpservletresponse resp = (httpservletresponse) response;
Wrapper wrapper = new wrapper (RESP );
Chain. dofilter (request, wrapper );
Byte [] gzipdata = gzip (wrapper. getresponsedata ());
Resp. addheader ("content-encoding", "gzip ");
Resp. setcontentlength (gzipdata. Length );
Servletoutputstream output = response. getoutputstream ();
Output. Write (gzipdata );
Output. Flush ();
}

Private byte [] gzip (byte [] data ){
Bytearrayoutputstream byteoutput = new bytearrayoutputstream (10240 );
Gzipoutputstream output = NULL;
Try {
Output = new gzipoutputstream (byteoutput );
Output. Write (data );
} Catch (ioexception e ){
E. printstacktrace ();
} Finally {
If (output! = NULL ){
Try {
Output. Close ();
} Catch (ioexception e ){
}
}
}
Return byteoutput. tobytearray ();
}

Public void Init (filterconfig fconfig) throws servletexception {
}
}


Wrapper class:
Public class wrapper extends httpservletresponsewrapper {
Public static final int ot_none = 0, ot_writer = 1, ot_stream = 2;
Private int outputtype = ot_none;
Private servletoutputstream output = NULL;
Private printwriter writer = NULL;
Private bytearrayoutputstream buffer = NULL;

Public wrapper (httpservletresponse resp) throws ioexception {
Super (RESP );
Buffer = new bytearrayoutputstream ();
}

Public printwriter getwriter () throws ioexception {
If (outputtype = ot_stream)
Throw new illegalstateexception ();
Else if (outputtype = ot_writer)
Return writer;
Else {
Outputtype = ot_writer;
Writer = new printwriter (New outputstreamwriter (buffer, getcharacterencoding ()));
Return writer;
}
}

Public servletoutputstream getoutputstream () throws ioexception {
If (outputtype = ot_writer)
Throw new illegalstateexception ();
Else if (outputtype = ot_stream)
Return output;
Else {
Outputtype = ot_stream;
Output = new wrappedoutputstream (buffer );
Return output;
}
}

Public void flushbuffer () throws ioexception {
If (outputtype = ot_writer)
Writer. Flush ();
If (outputtype = ot_stream)
Output. Flush ();
}

Public void reset (){
Outputtype = ot_none;
Buffer. Reset ();
}

Public byte [] getresponsedata () throws ioexception {
Flushbuffer ();
Return buffer. tobytearray ();
}

// Internal class, extended servletoutputstream
Class wrappedoutputstream extends servletoutputstream {
Private bytearrayoutputstream buffer;

Public wrappedoutputstream (bytearrayoutputstream buffer ){
This. Buffer = buffer;
}

Public void write (int B) throws ioexception {
Buffer. Write (B );
}

Public byte [] tobytearray (){
Return buffer. tobytearray ();
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.