001// Copyright 2013 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.services.assets.AssetChecksumGenerator;
018import org.apache.tapestry5.services.assets.CompressionStatus;
019import org.apache.tapestry5.services.assets.StreamableResource;
020
021import java.io.IOException;
022import java.io.InputStream;
023import java.io.OutputStream;
024
025public class StreamableResourceImpl implements StreamableResource
026{
027    protected final String description, contentType;
028
029    protected final CompressionStatus compression;
030
031    protected final long lastModified;
032
033    protected final BytestreamCache bytestreamCache;
034
035    protected final AssetChecksumGenerator assetChecksumGenerator;
036
037    public StreamableResourceImpl(String description, String contentType, CompressionStatus compression, long lastModified, BytestreamCache bytestreamCache, AssetChecksumGenerator assetChecksumGenerator)
038    {
039        this.lastModified = lastModified;
040        this.description = description;
041        this.bytestreamCache = bytestreamCache;
042        this.contentType = contentType;
043        this.compression = compression;
044        this.assetChecksumGenerator = assetChecksumGenerator;
045    }
046
047    public String getDescription()
048    {
049        return description;
050    }
051
052    public CompressionStatus getCompression()
053    {
054        return compression;
055    }
056
057    public String getContentType()
058    {
059        return contentType;
060    }
061
062    public int getSize()
063    {
064        return bytestreamCache.size();
065    }
066
067    public long getLastModified()
068    {
069        return lastModified;
070    }
071
072    public void streamTo(OutputStream os) throws IOException
073    {
074        bytestreamCache.writeTo(os);
075    }
076
077    public InputStream openStream() throws IOException
078    {
079        return bytestreamCache.openStream();
080    }
081
082    @Override
083    public String toString()
084    {
085        return String.format("StreamableResource<%s %s %s lastModified: %tc size: %d>", contentType, description, compression.name(),
086                lastModified, getSize());
087    }
088
089    public String getChecksum() throws IOException
090    {
091        // Currently, we rely on AssetChecksumGenerator to manage a cache, but that may be better done
092        // here (but must be threads-afe).
093        return assetChecksumGenerator.generateChecksum(this);
094    }
095}