List of usage examples for org.springframework.web.reactive.resource TransformedResource TransformedResource
public TransformedResource(Resource original, byte[] transformedContent)
From source file:org.springframework.web.reactive.resource.AppCacheManifestTransformer.java
private Mono<? extends Resource> transform(String content, Resource resource, ResourceTransformerChain chain, ServerWebExchange exchange) {//from www.ja v a 2 s.co m if (!content.startsWith(MANIFEST_HEADER)) { if (logger.isTraceEnabled()) { logger.trace("Manifest should start with 'CACHE MANIFEST', skip: " + resource); } return Mono.just(resource); } if (logger.isTraceEnabled()) { logger.trace("Transforming resource: " + 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); if (logger.isTraceEnabled()) { logger.trace("AppCache file: [" + resource.getFilename() + "] hash: [" + hash + "]"); } return new TransformedResource(resource, out.toByteArray()); }); }
From source file:org.springframework.web.reactive.resource.CssLinkResourceTransformer.java
private Mono<? extends Resource> transformContent(String cssContent, Resource resource, ResourceTransformerChain chain, ServerWebExchange exchange) { List<ContentChunkInfo> contentChunkInfos = parseContent(cssContent); if (contentChunkInfos.isEmpty()) { if (logger.isTraceEnabled()) { logger.trace("No links found."); }/* w w w .ja v a 2 s. c o m*/ 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); }); }