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;
014
015import org.apache.tapestry5.ComponentResources;
016import org.apache.tapestry5.MetaDataConstants;
017import org.apache.tapestry5.http.ContentType;
018import org.apache.tapestry5.http.TapestryHttpSymbolConstants;
019import org.apache.tapestry5.internal.structure.Page;
020import org.apache.tapestry5.ioc.annotations.Symbol;
021import org.apache.tapestry5.services.MetaDataLocator;
022
023public class PageContentTypeAnalyzerImpl implements PageContentTypeAnalyzer
024{
025    private final MetaDataLocator metaDataLocator;
026
027    private final String outputCharset;
028
029    public PageContentTypeAnalyzerImpl(MetaDataLocator metaDataLocator,
030
031    @Symbol(TapestryHttpSymbolConstants.CHARSET)
032    String outputCharset)
033    {
034        this.metaDataLocator = metaDataLocator;
035        this.outputCharset = outputCharset;
036    }
037
038    public ContentType findContentType(Page page)
039    {
040        ComponentResources pageResources = page.getRootComponent().getComponentResources();
041
042        String contentTypeString = metaDataLocator.findMeta(MetaDataConstants.RESPONSE_CONTENT_TYPE, pageResources,
043                String.class);
044
045        // Draconian but necessary: overwrite the content type they selected with the application-wide output charset.
046
047        return new ContentType(contentTypeString).withCharset(outputCharset);
048    }
049}