Search in sources :

Example 1 with ResourceDependencies

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

the class LessResourceTransformer method invokeLessCompiler.

private BytestreamCache invokeLessCompiler(Resource source, ResourceDependencies dependencies) throws IOException {
    try {
        LessSource lessSource = new ResourceLessSource(source, dependencies);
        LessCompiler.CompilationResult compilationResult = compile(compiler, lessSource);
        return new BytestreamCache(compilationResult.getCss().getBytes("utf-8"));
    } catch (Less4jException ex) {
        throw new IOException(ex);
    } catch (UnsupportedEncodingException ex) {
        throw new IOException(ex);
    }
}
Also used : LessSource(com.github.sommeri.less4j.LessSource) BytestreamCache(org.apache.tapestry5.internal.services.assets.BytestreamCache) LessCompiler(com.github.sommeri.less4j.LessCompiler) DefaultLessCompiler(com.github.sommeri.less4j.core.DefaultLessCompiler) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) Less4jException(com.github.sommeri.less4j.Less4jException)

Example 2 with ResourceDependencies

use of org.apache.tapestry5.services.assets.ResourceDependencies 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 3 with ResourceDependencies

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

the class ResourceTransformerFactoryImpl method wrapWithFileSystemCaching.

private ResourceTransformer wrapWithFileSystemCaching(ResourceTransformer core, final String targetName) {
    return new DelegatingResourceTransformer(core) {

        @Override
        public InputStream transform(Resource source, ResourceDependencies dependencies) throws IOException {
            long checksum = ResourceTransformUtils.toChecksum(source);
            String fileName = Long.toHexString(checksum) + "-" + source.getFile();
            File cacheFile = new File(cacheDir, fileName);
            if (cacheFile.exists()) {
                logger.debug(String.format("Serving up compiled %s content for %s from file system cache", targetName, source));
                return new BufferedInputStream(new FileInputStream(cacheFile));
            }
            InputStream compiled = delegate.transform(source, dependencies);
            // We need the InputStream twice; once to return, and once to write out to the cache file for later.
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            TapestryInternalUtils.copy(compiled, bos);
            compiled.close();
            BytestreamCache cache = new BytestreamCache(bos);
            writeToCacheFile(cacheFile, cache.openStream());
            return cache.openStream();
        }
    };
}
Also used : ResourceDependencies(org.apache.tapestry5.services.assets.ResourceDependencies) BytestreamCache(org.apache.tapestry5.internal.services.assets.BytestreamCache) Resource(org.apache.tapestry5.commons.Resource)

Example 4 with ResourceDependencies

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

the class SRSCachingInterceptor method getStreamableResource.

public StreamableResource getStreamableResource(Resource baseResource, StreamableResourceProcessing processing, ResourceDependencies dependencies) throws IOException {
    if (!enableCache(processing)) {
        return delegate.getStreamableResource(baseResource, processing, dependencies);
    }
    StreamableResource result = TapestryInternalUtils.getAndDeref(cache, baseResource);
    if (result == null) {
        result = delegate.getStreamableResource(baseResource, processing, dependencies);
        if (isCacheable(result)) {
            dependencies.addDependency(baseResource);
            cache.put(baseResource, new SoftReference<StreamableResource>(result));
        }
    }
    return result;
}
Also used : StreamableResource(org.apache.tapestry5.services.assets.StreamableResource)

Example 5 with ResourceDependencies

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

the class UTF8ForTextAssets method getStreamableResource.

@Override
public StreamableResource getStreamableResource(Resource baseResource, StreamableResourceProcessing processing, ResourceDependencies dependencies) throws IOException {
    StreamableResource resource = delegate.getStreamableResource(baseResource, processing, dependencies);
    ContentType contentType = resource.getContentType();
    if (contentType.getBaseType().equals("text") && !contentType.hasParameters() && processing != StreamableResourceProcessing.FOR_AGGREGATION) {
        return resource.withContentType(contentType.withCharset("utf-8"));
    }
    return resource;
}
Also used : StreamableResource(org.apache.tapestry5.services.assets.StreamableResource) ContentType(org.apache.tapestry5.http.ContentType)

Aggregations

IOException (java.io.IOException)2 ContentType (org.apache.tapestry5.http.ContentType)2 BytestreamCache (org.apache.tapestry5.internal.services.assets.BytestreamCache)2 StreamableResource (org.apache.tapestry5.services.assets.StreamableResource)2 Less4jException (com.github.sommeri.less4j.Less4jException)1 LessCompiler (com.github.sommeri.less4j.LessCompiler)1 LessSource (com.github.sommeri.less4j.LessSource)1 DefaultLessCompiler (com.github.sommeri.less4j.core.DefaultLessCompiler)1 InputStream (java.io.InputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Resource (org.apache.tapestry5.commons.Resource)1 ResourceDependencies (org.apache.tapestry5.services.assets.ResourceDependencies)1