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.commons.Resource;
016import org.apache.tapestry5.http.ContentType;
017import org.apache.tapestry5.services.assets.ResourceDependencies;
018import org.apache.tapestry5.services.assets.StreamableResource;
019import org.apache.tapestry5.services.assets.StreamableResourceProcessing;
020import org.apache.tapestry5.services.assets.StreamableResourceSource;
021
022import java.io.IOException;
023
024/**
025 * Adds ;charset=utf-8 for text/* resources with no specific character set. This assumes that all test resources are
026 * in UTF-8.
027 *
028 * @since 5.4
029 */
030public class UTF8ForTextAssets extends DelegatingSRS
031{
032    public UTF8ForTextAssets(StreamableResourceSource delegate)
033    {
034        super(delegate);
035    }
036
037    @Override
038    public StreamableResource getStreamableResource(Resource baseResource, StreamableResourceProcessing processing, ResourceDependencies dependencies) throws IOException
039    {
040        StreamableResource resource = delegate.getStreamableResource(baseResource, processing, dependencies);
041
042        ContentType contentType = resource.getContentType();
043
044        if (contentType.getBaseType().equals("text")
045                && ! contentType.hasParameters()
046                && processing != StreamableResourceProcessing.FOR_AGGREGATION)
047        {
048            return resource.withContentType(contentType.withCharset("utf-8"));
049        }
050
051        return resource;
052    }
053}