August 15, 2017

Gzip compression working Spring MVC

GZIP compression

public static String gzipCompression(String strNew) throws IOException {
    if (strNew == null || strNew.length() == 0) {
        return str;
    }
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    GZIPOutputStream gzip = new GZIPOutputStream(out);
    gzip.write(str.getBytes());
    gzip.close();
    String outStr = out.toString("UTF-8");
    return outStr;
 }


Using BufferedWriter

public static String compressString(String str) throws IOException{
if (str == null || str.length() == 0) {
    return str;
}

BufferedWriter writer = null;

try{
    File file =  new File("your.gzip")
    GZIPOutputStream zip = new GZIPOutputStream(new FileOutputStream(file));

    writer = new BufferedWriter(new OutputStreamWriter(zip, "UTF-8"));

    writer.append(str);
}
finally{        
    if(writer != null){
     writer.close();
     }
  }
 }


Using compression filter

package : com.planetj.servlet.filter.compression
spring compression filter


How to enable HTTP response compression

Read 69.18

http://docs.spring.io/spring-boot/docs/1.3.x/reference/htmlsingle/#how-to-enable-http-response-compression

GZip compression in spring

http://www.oodlestechnologies.com/blogs/Gzip-Servlet-Filter-in-Spring-MVC

Handling filters in spring MVC

https://www.mkyong.com/spring-mvc/how-to-register-a-servlet-filter-in-spring-mvc/

Working with @controllerAdvice and ResponseAdvice in spring 4

https://sdqali.in/blog/2016/06/08/filtering-responses-in-spring-mvc/

Interceptors

Interceptors are use to manipulate entities like inputstream and outputstreams. There are two kinds of interceptors ReaderInterceptor and WriterInterceptors


public class GzipWriterInterceptor implements writerinterceptors {
@override
public void aroundwriteto(writerinterceptorcontext context){
outputstream os = context.getoutputstream();
context.setoutputstream(new Gzipoutputsream(os));
context.proceed();
}
}


Using ResponseWrapper

https://stackoverflow.com/questions/25020331/spring-mvc-how-to-modify-json-response-sent-from-controller

@Override
public void doFilter(ServletRequest request, ServletResponse response,
                     FilterChain chain) throws IOException, ServletException {

    ResponseWrapper responseWrapper = new ResponseWrapper((HttpServletResponse) response);

    chain.doFilter(request, responseWrapper);

    String responseContent = new String(responseWrapper.getDataStream());

    RestResponse fullResponse = new RestResponse(/*status*/, /*message*/,responseContent);

    byte[] responseToSend = restResponseBytes(fullResponse);

    response.getOutputStream().write(responseToSend);

}


using response body advice

https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/mvc/method/annotation/ResponseBodyAdvice.html

@controllerAdvice +  implements ResponseBodyAdvice<Object>

https://mtyurt.net/2015/07/20/spring-modify-response-headers-after-processing/

@ControllerAdvice
public class HeaderModifierAdvice implements ResponseBodyAdvice<Object> {

    @Override
    public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
        return true;
    }

    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
        response.getHeaders().add("dummy-header","dummy-value");
        return body;
    }
}


Rest Easy spring integration

http://www.concretepage.com/spring-4/spring-4-resteasy-3-jackson-json-integration-example-with-tomcat

Facebook comments