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.util.Map;
016
017import org.apache.tapestry5.services.assets.CompressionAnalyzer;
018
019public class CompressionAnalyzerImpl implements CompressionAnalyzer
020{
021    private final Map<String, Boolean> configuration;
022
023    public CompressionAnalyzerImpl(Map<String, Boolean> configuration)
024    {
025        this.configuration = configuration;
026    }
027
028    public boolean isCompressable(String contentType)
029    {
030        assert contentType != null;
031
032        int x = contentType.indexOf(';');
033
034        String key = x < 0 ? contentType : contentType.substring(0, x);
035
036        Boolean result = configuration.get(key);
037
038        if (result != null) {
039            return result.booleanValue();
040        }
041
042        // Now look for a wild card.
043
044        x = contentType.indexOf('/');
045
046        String wildKey = contentType.substring(0, x) + "/*";
047
048        result = configuration.get(wildKey);
049
050        return result == null ? true : result.booleanValue();
051    }
052}