    001    // Copyright 2011 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
    015    package org.apache.tapestry5.internal.services.assets;
    016
    017    import java.io.IOException;
    018    import java.util.Map;
    019
    020    import org.apache.tapestry5.ioc.annotations.Marker;
    021    import org.apache.tapestry5.ioc.annotations.Primary;
    022    import org.apache.tapestry5.services.assets.ResourceMinimizer;
    023    import org.apache.tapestry5.services.assets.StreamableResource;
    024
    025    /**
    026     * Implementation that delegates, via its configuration, to a real implementation based on the content type of the
    027     * resource.
    028     */
    029    @Marker(Primary.class)
    030    public class MasterResourceMinimizer implements ResourceMinimizer
    031    {
    032        private final Map<String, ResourceMinimizer> configuration;
    033
    034        public MasterResourceMinimizer(Map<String, ResourceMinimizer> configuration)
    035        {
    036            this.configuration = configuration;
    037        }
    038
    039        /** Does nothing; an override of this service can be installed to provide minimization. */
    040        public StreamableResource minimize(StreamableResource resource) throws IOException
    041        {
    042            ResourceMinimizer minimizer = configuration.get(resource.getContentType());
    043
    044            return minimizer == null ? resource : minimizer.minimize(resource);
    045        }
    046    }