    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 org.apache.tapestry5.TapestryConstants;
    016import org.apache.tapestry5.commons.Resource;
    017import org.apache.tapestry5.http.services.Request;
    018import org.apache.tapestry5.services.assets.ResourceDependencies;
    019import org.apache.tapestry5.services.assets.StreamableResource;
    020import org.apache.tapestry5.services.assets.StreamableResourceProcessing;
    021import org.apache.tapestry5.services.assets.StreamableResourceSource;
    022import org.apache.tapestry5.services.javascript.JavaScriptStack;
    023import org.apache.tapestry5.services.javascript.JavaScriptStackSource;
    024
    025import java.io.IOException;
    026
    027/**
    028 * Attempts to match resources against a {@link org.apache.tapestry5.services.javascript.JavaScriptStack}, and
    029 * possibly disabled minimization based on the stack.
    030 *
    031 * @since 5.4
    032 */
    033public class JavaScriptStackMinimizeDisabler extends DelegatingSRS
    034{
    035
    036    private final JavaScriptStackSource javaScriptStackSource;
    037
    038    private final Request request;
    039
    040    public JavaScriptStackMinimizeDisabler(StreamableResourceSource delegate, JavaScriptStackSource javaScriptStackSource, Request request)
    041    {
    042        super(delegate);
    043
    044        this.javaScriptStackSource = javaScriptStackSource;
    045        this.request = request;
    046    }
    047
    048
    049    @Override
    050    public StreamableResource getStreamableResource(Resource baseResource, StreamableResourceProcessing processing, ResourceDependencies dependencies) throws IOException
    051    {
    052        JavaScriptStack stack = javaScriptStackSource.findStackForJavaScriptLibrary(baseResource);
    053
    054        if (stack != null && !stack.getJavaScriptAggregationStrategy().enablesMinimize())
    055        {
    056            request.setAttribute(TapestryConstants.DISABLE_JAVASCRIPT_MINIMIZATION, true);
    057        }
    058
    059        try
    060        {
    061            return delegate.getStreamableResource(baseResource, processing, dependencies);
    062        } finally
    063        {
    064            request.setAttribute(TapestryConstants.DISABLE_JAVASCRIPT_MINIMIZATION, null);
    065        }
    066    }
    067}