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.services.assets;
016
017import org.apache.tapestry5.ioc.Resource;
018import org.apache.tapestry5.ioc.annotations.UsesMappedConfiguration;
019
020import java.io.IOException;
021import java.util.Set;
022
023/**
024 * Converts {@link Resource}s into {@link StreamableResource}s, and may be responsible for
025 * {@linkplain ResourceTransformer transforming} resources based on file extension. Contributions map a file extension
026 * (such as "coffee") to a transformer for that file extension.
027 * Service decorators added to this service may provide additional processing (compression, minimization, and caching).
028 *
029 * @since 5.3
030 */
031@UsesMappedConfiguration(ResourceTransformer.class)
032public interface StreamableResourceSource
033{
034    /**
035     * Given a desired content type, identify which file extensions can be mapped to that extension based on
036     * contributed {@link ResourceTransformer}s that can
037     * {@linkplain org.apache.tapestry5.services.assets.ResourceTransformer#getTransformedContentType() produce the content type}
038     * based for a file with that extension.
039     *
040     * @param contentType
041     *         to search for
042     * @return set of file extension, possibly empty, in no particular order. These are the bare extensions, i.e.
043     *         "js", "coffee".
044     * @since 5.4
045     */
046    Set<String> fileExtensionsForContentType(String contentType);
047
048    /**
049     * Converts a Resource (which must be non-null and exist) into a streamable resource, along with
050     * some additional optional behaviors.
051     *
052     * @param baseResource
053     *         the resource to convert
054     * @param processing
055     *         defines additional processing after the resource has been read and possibly transformed
056     * @param dependencies
057     *         Passed to any {@link ResourceTransformer} to track additional dependencies of the base resource
058     * @return the contents of the Resource, possibly transformed, in a streamable format.
059     * @throws IOException
060     *         if the resource does not exist or a URL for the content is not available
061     */
062    StreamableResource getStreamableResource(Resource baseResource, StreamableResourceProcessing processing, ResourceDependencies dependencies)
063            throws IOException;
064}