001// Copyright 2011, 2012 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007// http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.tapestry5.internal.services.assets;
016
017import org.apache.tapestry5.internal.TapestryInternalUtils;
018import org.apache.tapestry5.ioc.Resource;
019import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
020import org.apache.tapestry5.services.assets.*;
021
022import java.io.ByteArrayOutputStream;
023import java.io.IOException;
024import java.io.InputStream;
025import java.util.Map;
026import java.util.Set;
027
028public class StreamableResourceSourceImpl implements StreamableResourceSource
029{
030    private final Map<String, ResourceTransformer> configuration;
031
032    private final ContentTypeAnalyzer contentTypeAnalyzer;
033
034    private final CompressionAnalyzer compressionAnalyzer;
035
036    private final ResourceChangeTracker resourceChangeTracker;
037
038    private final AssetChecksumGenerator checksumGenerator;
039
040    public StreamableResourceSourceImpl(Map<String, ResourceTransformer> configuration,
041                                        ContentTypeAnalyzer contentTypeAnalyzer, CompressionAnalyzer compressionAnalyzer,
042                                        ResourceChangeTracker resourceChangeTracker, AssetChecksumGenerator checksumGenerator)
043    {
044        this.configuration = configuration;
045        this.contentTypeAnalyzer = contentTypeAnalyzer;
046        this.compressionAnalyzer = compressionAnalyzer;
047        this.resourceChangeTracker = resourceChangeTracker;
048        this.checksumGenerator = checksumGenerator;
049    }
050
051    public Set<String> fileExtensionsForContentType(String contentType)
052    {
053        Set<String> result = CollectionFactory.newSet();
054
055        for (Map.Entry<String, ResourceTransformer> me : configuration.entrySet())
056        {
057            if (me.getValue().getTransformedContentType().equals(contentType))
058            {
059                result.add(me.getKey());
060            }
061        }
062
063        return result;
064    }
065
066    public StreamableResource getStreamableResource(Resource baseResource, StreamableResourceProcessing processing, ResourceDependencies dependencies)
067            throws IOException
068    {
069        assert baseResource != null;
070
071        if (!baseResource.exists())
072        {
073            throw new IOException(String.format("Resource %s does not exist.", baseResource));
074        }
075
076        String fileSuffix = TapestryInternalUtils.toFileSuffix(baseResource.getFile());
077
078        // Optionally, transform the resource. The main driver for this is to allow
079        // for libraries like LessJS (http://lesscss.org/) or
080        // http://jashkenas.github.com/coffee-script/
081        ResourceTransformer rt = configuration.get(fileSuffix);
082
083        InputStream transformed = rt == null ? baseResource.openStream() : rt.transform(baseResource, dependencies);
084
085        assert transformed != null;
086
087        BytestreamCache bytestreamCache = readStream(transformed);
088
089        transformed.close();
090
091        String contentType = rt == null ? contentTypeAnalyzer.getContentType(baseResource) : rt.getTransformedContentType();
092
093        boolean compressable = compressionAnalyzer.isCompressable(contentType);
094
095        long lastModified = resourceChangeTracker.trackResource(baseResource);
096
097        return new StreamableResourceImpl(baseResource.toString(), contentType, compressable ? CompressionStatus.COMPRESSABLE
098                : CompressionStatus.NOT_COMPRESSABLE, lastModified, bytestreamCache, checksumGenerator);
099    }
100
101    private BytestreamCache readStream(InputStream stream) throws IOException
102    {
103        ByteArrayOutputStream bos = new ByteArrayOutputStream();
104
105        TapestryInternalUtils.copy(stream, bos);
106
107        stream.close();
108
109        return new BytestreamCache(bos);
110    }
111
112}