use of io.bit3.jsass.CompilationException in project mycore by MyCoRe-Org.
the class MCRSassCompilerManager method compile.
/**
* Just compiles a file and fills all maps.
*
* @param name the name of the file (with .min.css or .css ending)
* @return the compiled css
* @throws CompilationException
*/
private String compile(String name, List<Importer> importer) throws CompilationException, IOException {
Options options = new Options();
Collection<Importer> importerList = options.getImporters();
importerList.addAll(importer);
String realFileName = name.replace(".min.css", ".scss").replace(".css", ".scss");
Optional<Import> importedStartStylesheet = importer.stream().map(i -> i.apply(realFileName, null)).filter(Objects::nonNull).map(i -> i.stream().findFirst()).filter(Optional::isPresent).map(Optional::get).findFirst();
if (!importedStartStylesheet.isPresent()) {
return null;
}
Import firstImport = importedStartStylesheet.get();
StringContext context = new StringContext(firstImport.getContents(), firstImport.getAbsoluteUri(), firstImport.getAbsoluteUri(), options);
Compiler compiler = new Compiler();
Output output = compiler.compile(context);
String css = output.getCss();
boolean compress = name.endsWith(".min.css");
if (compress) {
try {
CssCompressor cssCompressor = new CssCompressor(new StringReader(css));
int lineBreaks = MCR_CONFIGURATION.getInt(LINE_BREAK_CONFIG_KEY, DEFAULT_LINE_BREAK_CONFIG_VALUE);
StringWriter out = new StringWriter(css.length());
cssCompressor.compress(out, lineBreaks);
css = out.toString();
} catch (IOException e) {
throw new MCRException("Read from StringReader produces IOException! (this should not happen)", e);
}
}
this.fileCompiledContentMap.put(name, css);
this.fileLastCompileDateMap.put(name, new Date());
try {
this.fileMD5Map.put(name, MCRUtils.asMD5String(1, null, css));
} catch (NoSuchAlgorithmException e) {
throw new MCRException("Error while generating md5 of result css!", e);
}
return css;
}
use of io.bit3.jsass.CompilationException 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();
}
}
Aggregations