001// Licensed under the Apache License, Version 2.0 (the "License");
002// you may not use this file except in compliance with the License.
003// You may obtain a copy of the License at
004//
005// http://www.apache.org/licenses/LICENSE-2.0
006//
007// Unless required by applicable law or agreed to in writing, software
008// distributed under the License is distributed on an "AS IS" BASIS,
009// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
010// See the License for the specific language governing permissions and
011// limitations under the License.
012
013package org.apache.tapestry5.internal.services.assets;
014
015import java.io.IOException;
016import java.util.Map;
017
018import org.apache.tapestry5.ioc.annotations.Marker;
019import org.apache.tapestry5.ioc.annotations.Primary;
020import org.apache.tapestry5.services.assets.ResourceMinimizer;
021import org.apache.tapestry5.services.assets.StreamableResource;
022
023/**
024 * Implementation that delegates, via its configuration, to a real implementation based on the content type of the
025 * resource.
026 */
027@Marker(Primary.class)
028public class MasterResourceMinimizer implements ResourceMinimizer
029{
030    private final Map<String, ResourceMinimizer> configuration;
031
032    public MasterResourceMinimizer(Map<String, ResourceMinimizer> configuration)
033    {
034        this.configuration = configuration;
035    }
036
037    /** Does nothing; an override of this service can be installed to provide minimization. */
038    public StreamableResource minimize(StreamableResource resource) throws IOException
039    {
040        ResourceMinimizer minimizer = configuration.get(resource.getContentType().getMimeType());
041
042        return minimizer == null ? resource : minimizer.minimize(resource);
043    }
044}