Search in sources :

Example 1 with StreamableResourceImpl

use of org.apache.tapestry5.internal.services.assets.StreamableResourceImpl in project tapestry-5 by apache.

the class StreamableResourceSourceImpl method getStreamableResource.

public StreamableResource getStreamableResource(Resource baseResource, StreamableResourceProcessing processing, ResourceDependencies dependencies) throws IOException {
    assert baseResource != null;
    if (!baseResource.exists()) {
        throw new IOException(String.format("Resource %s does not exist.", baseResource));
    }
    String fileSuffix = TapestryInternalUtils.toFileSuffix(baseResource.getFile());
    // Optionally, transform the resource. The main driver for this is to allow
    // for libraries like LessJS (http://lesscss.org/) or
    // http://jashkenas.github.com/coffee-script/
    ResourceTransformer rt = configuration.get(fileSuffix);
    InputStream transformed = rt == null ? baseResource.openStream() : rt.transform(baseResource, dependencies);
    assert transformed != null;
    BytestreamCache bytestreamCache = readStream(transformed);
    transformed.close();
    ContentType contentType = rt == null ? new ContentType(contentTypeAnalyzer.getContentType(baseResource)) : rt.getTransformedContentType();
    boolean compressable = compressionAnalyzer.isCompressable(contentType.getMimeType());
    long lastModified = resourceChangeTracker.trackResource(baseResource);
    return new StreamableResourceImpl(baseResource.toString(), contentType, compressable ? CompressionStatus.COMPRESSABLE : CompressionStatus.NOT_COMPRESSABLE, lastModified, bytestreamCache, checksumGenerator, null);
}
Also used : ContentType(org.apache.tapestry5.http.ContentType) InputStream(java.io.InputStream) IOException(java.io.IOException)

Example 2 with StreamableResourceImpl

use of org.apache.tapestry5.internal.services.assets.StreamableResourceImpl in project tapestry-5 by apache.

the class AbstractMinimizer method minimize.

@Override
public StreamableResource minimize(final StreamableResource input) throws IOException {
    if (!isEnabled(input)) {
        return input;
    }
    long startNanos = System.nanoTime();
    final ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
    tracker.perform("Minimizing " + input, new IOOperation<Void>() {

        @Override
        public Void perform() throws IOException {
            InputStream in = doMinimize(input);
            TapestryInternalUtils.copy(in, bos);
            in.close();
            return null;
        }
    });
    // The content is minimized, but can still be (GZip) compressed.
    StreamableResource output = new StreamableResourceImpl("minimized " + input.getDescription(), input.getContentType(), CompressionStatus.COMPRESSABLE, input.getLastModified(), new BytestreamCache(bos), checksumGenerator, input.getResponseCustomizer());
    if (logger.isInfoEnabled()) {
        long elapsedNanos = System.nanoTime() - startNanos;
        int inputSize = input.getSize();
        int outputSize = output.getSize();
        double elapsedMillis = ((double) elapsedNanos) * NANOS_TO_MILLIS;
        // e.g., reducing 100 bytes to 25 would be a (100-25)/100 reduction, or 75%
        double reduction = 100d * ((double) (inputSize - outputSize)) / ((double) inputSize);
        logger.info(String.format("Minimized %s (%,d input bytes of %s to %,d output bytes in %.2f ms, %.2f%% reduction)", input.getDescription(), inputSize, resourceType, outputSize, elapsedMillis, reduction));
    }
    return output;
}
Also used : StreamableResource(org.apache.tapestry5.services.assets.StreamableResource) BytestreamCache(org.apache.tapestry5.internal.services.assets.BytestreamCache) InputStream(java.io.InputStream) StreamableResourceImpl(org.apache.tapestry5.internal.services.assets.StreamableResourceImpl) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ContentType (org.apache.tapestry5.http.ContentType)1 BytestreamCache (org.apache.tapestry5.internal.services.assets.BytestreamCache)1 StreamableResourceImpl (org.apache.tapestry5.internal.services.assets.StreamableResourceImpl)1 StreamableResource (org.apache.tapestry5.services.assets.StreamableResource)1