minify html and js in Interceptor

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

minify html and js in Interceptor

used jersey mvc and jsp, all requests to html or js files did through @Template or Viewable.

  @Path(JS_URL + "{type}")
  @Template(name = "grid")
  public Response buildJSGrid(@DefaultValue("") @PathParam("type") String type) {
     Grid grid = new Grid(type);
     return Response.ok(grid).build();

where grid is grid.jsp file with pure javascript inside
<%@ page contentType="application/javascript;charset=UTF-8" language="java" %>

also possible other variant with html and js, example;
  @Path(FORM_URL + "{type}")
  @Template(name = "form")
  public Response buildAccountForm(@DefaultValue("") @PathParam("type") String type) {
     Form form = new Form(type);
     return Response.ok(form).build();
where form is form.jsp with html and js inside <script>..</script><%@ page contentType="text/html;charset=UTF-8" language="java" %>

i need to minify result js and html/js before send to client, i try to use lib, but there need to pass String to htmlCompressor.compress(input);

tried use WriterInterceptor
public class MinifyJsInterceptor implements WriterInterceptor {
  public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException {
final OutputStream outputStream = context.getOutputStream();
// here need to convert outputStream to InputStream and after to String ?
// result string to htmlCompressor.compress(resultString);
// after that convert result minify string back to resultOutputStream and set to context ?
context.setOutputStream(new GZIPOutputStream(resultOutputStream));

is it correct way ? and i can`t converts that outputstream to string