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
015package org.apache.tapestry5.internal.services.assets;
016
017import java.util.Map;
018
019import org.apache.tapestry5.commons.Resource;
020import org.apache.tapestry5.http.services.Context;
021import org.apache.tapestry5.internal.TapestryInternalUtils;
022import org.apache.tapestry5.services.assets.ContentTypeAnalyzer;
023
024public class ContentTypeAnalyzerImpl implements ContentTypeAnalyzer
025{
026    private final Context context;
027
028    private final Map<String, String> configuration;
029
030    public ContentTypeAnalyzerImpl(Context context, Map<String, String> configuration)
031    {
032        this.context = context;
033        this.configuration = configuration;
034    }
035
036    public String getContentType(Resource resource)
037    {
038        String extension = TapestryInternalUtils.toFileSuffix(resource.getFile());
039
040        String contentType = configuration.get(extension);
041
042        if (contentType != null)
043            return contentType;
044
045        contentType = context.getMimeType(resource.getFile());
046
047        if (contentType != null)
048            return contentType;
049
050        return "application/octet-stream";
051    }
052
053}