Search in sources :

Example 1 with MCRServletContextResourceImporter

use of org.mycore.sass.MCRServletContextResourceImporter in project mycore by MyCoRe-Org.

the class MCRSassResource method getCSS.

@GET
@Path("{fileName:.+}")
@Produces("text/css")
public Response getCSS(@PathParam("fileName") String name, @Context Request request) {
    try {
        MCRServletContextResourceImporter importer = new MCRServletContextResourceImporter(context);
        Optional<String> cssFile = MCRSassCompilerManager.getInstance().getCSSFile(name, Stream.of(importer).collect(Collectors.toList()));
        if (cssFile.isPresent()) {
            CacheControl cc = new CacheControl();
            cc.setMaxAge(SECONDS_OF_ONE_DAY);
            String etagString = MCRSassCompilerManager.getInstance().getLastMD5(name).get();
            EntityTag etag = new EntityTag(etagString);
            Response.ResponseBuilder builder = request.evaluatePreconditions(etag);
            if (builder != null) {
                return builder.cacheControl(cc).tag(etag).build();
            }
            return Response.ok().status(Response.Status.OK).cacheControl(cc).tag(etag).entity(cssFile.get()).build();
        } else {
            return Response.status(Response.Status.NOT_FOUND).build();
        }
    } catch (IOException | CompilationException e) {
        StreamingOutput so = (OutputStream os) -> e.printStackTrace(new PrintStream(os, true, "UTF-8"));
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(so).build();
    }
}
Also used : CompilationException(io.bit3.jsass.CompilationException) PrintStream(java.io.PrintStream) OutputStream(java.io.OutputStream) StreamingOutput(javax.ws.rs.core.StreamingOutput) IOException(java.io.IOException) MCRServletContextResourceImporter(org.mycore.sass.MCRServletContextResourceImporter) Response(javax.ws.rs.core.Response) EntityTag(javax.ws.rs.core.EntityTag) CacheControl(javax.ws.rs.core.CacheControl) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

CompilationException (io.bit3.jsass.CompilationException)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 PrintStream (java.io.PrintStream)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 CacheControl (javax.ws.rs.core.CacheControl)1 EntityTag (javax.ws.rs.core.EntityTag)1 Response (javax.ws.rs.core.Response)1 StreamingOutput (javax.ws.rs.core.StreamingOutput)1 MCRServletContextResourceImporter (org.mycore.sass.MCRServletContextResourceImporter)1