minify html and js in Interceptor

classic Classic list List threaded Threaded
1 message Options
nnn
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

minify html and js in Interceptor

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

  @GET
  @Path(JS_URL + "{type}")
  @Template(name = "grid")
  @Produces("application/javascript")
  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;
  @GET
  @Path(FORM_URL + "{type}")
  @Template(name = "form")
  @Produces(MediaType.TEXT_HTML)
  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 https://code.google.com/archive/p/htmlcompressor/ lib, but there need to pass String to htmlCompressor.compress(input);

tried use WriterInterceptor
public class MinifyJsInterceptor implements WriterInterceptor {
  @Override
  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
thanks

Loading...