private Mono<? extends Resource> transform(String content, Resource resource, ResourceTransformerChain chain, ServerWebExchange exchange) { if (!content.startsWith(MANIFEST_HEADER)) { if (logger.isTraceEnabled()) { logger.trace(exchange.getLogPrefix() + "Skipping " + resource + ": Manifest does not start with 'CACHE MANIFEST'"); } return Mono.just(resource); } return Flux.generate(new LineInfoGenerator(content)) .concatMap(info -> processLine(info, exchange, resource, chain)) .reduce(new ByteArrayOutputStream(), (out, line) -> { writeToByteArrayOutputStream(out, line + "\n"); return out; }) .map(out -> { String hash = DigestUtils.md5DigestAsHex(out.toByteArray()); writeToByteArrayOutputStream(out, "\n" + "# Hash: " + hash); return new TransformedResource(resource, out.toByteArray()); }); }
private Mono<? extends Resource> transformContent(String cssContent, Resource resource, ResourceTransformerChain chain, ServerWebExchange exchange) { List<ContentChunkInfo> contentChunkInfos = parseContent(cssContent); if (contentChunkInfos.isEmpty()) { return Mono.just(resource); } return Flux.fromIterable(contentChunkInfos) .concatMap(contentChunkInfo -> { String contentChunk = contentChunkInfo.getContent(cssContent); if (contentChunkInfo.isLink() && !hasScheme(contentChunk)) { String link = toAbsolutePath(contentChunk, exchange); return resolveUrlPath(link, exchange, resource, chain).defaultIfEmpty(contentChunk); } else { return Mono.just(contentChunk); } }) .reduce(new StringWriter(), (writer, chunk) -> { writer.write(chunk); return writer; }) .map(writer -> { byte[] newContent = writer.toString().getBytes(DEFAULT_CHARSET); return new TransformedResource(resource, newContent); }); }
public TransformedResource createResource() { String hash = DigestUtils.md5DigestAsHex(this.baos.toByteArray()); this.writer.write("\n" + "# Hash: " + hash); if (logger.isTraceEnabled()) { logger.trace("AppCache file: [" + resource.getFilename()+ "] hash: [" + hash + "]"); } byte[] bytes = this.writer.toString().getBytes(DEFAULT_CHARSET); return new TransformedResource(this.resource, bytes); } }
.then(writer -> { byte[] newContent = writer.toString().getBytes(DEFAULT_CHARSET); return Mono.just(new TransformedResource(resource, newContent)); }); });