users@jersey.java.net

[Jersey] minify html and js in Interceptor

From: nnn <nikita.nn_at_gmail.com>
Date: Sun, 3 Apr 2016 10:40:44 -0700 (MST)

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
<%@ 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





--
View this message in context: http://jersey.576304.n2.nabble.com/minify-html-and-js-in-Interceptor-tp7583678.html
Sent from the Jersey mailing list archive at Nabble.com.