001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Anahide Tchertchian
018 */
019package org.nuxeo.ecm.web.resources.wro.processor;
020
021import java.io.IOException;
022import java.io.InputStream;
023import java.io.OutputStream;
024import java.io.Reader;
025import java.io.Writer;
026import java.util.Map;
027import java.util.regex.Matcher;
028import java.util.regex.Pattern;
029
030import org.apache.commons.io.IOUtils;
031import org.apache.commons.io.input.ProxyInputStream;
032import org.apache.commons.io.input.ReaderInputStream;
033import org.apache.commons.io.output.ProxyOutputStream;
034import org.apache.commons.io.output.WriterOutputStream;
035import org.apache.commons.logging.Log;
036import org.apache.commons.logging.LogFactory;
037import org.nuxeo.runtime.api.Framework;
038import org.nuxeo.theme.styling.service.ThemeStylingService;
039
040import ro.isdc.wro.WroRuntimeException;
041import ro.isdc.wro.model.resource.Resource;
042import ro.isdc.wro.model.resource.ResourceType;
043import ro.isdc.wro.model.resource.SupportedResourceType;
044
045/**
046 * Wro processor to replace flavor variables inside linked CSS file.
047 *
048 * @since 7.3
049 */
050@SupportedResourceType(ResourceType.CSS)
051public class FlavorResourceProcessor extends AbstractFlavorProcessor {
052
053    private static final Log log = LogFactory.getLog(FlavorResourceProcessor.class);
054
055    public static final String ALIAS = "flavor";
056
057    @Override
058    protected void process(final Resource resource, final Reader reader, final Writer writer, String flavorName)
059            throws IOException {
060        final InputStream is = new ProxyInputStream(new ReaderInputStream(reader, getEncoding())) {
061        };
062        final OutputStream os = new ProxyOutputStream(new WriterOutputStream(writer, getEncoding()));
063        try {
064            Map<String, String> presets = null;
065            if (flavorName != null) {
066                ThemeStylingService s = Framework.getService(ThemeStylingService.class);
067                presets = s.getPresetVariables(flavorName);
068            }
069            if (presets == null || presets.isEmpty()) {
070                IOUtils.copy(is, os);
071            } else {
072                String content = IOUtils.toString(reader);
073                for (Map.Entry<String, String> preset : presets.entrySet()) {
074                    content = Pattern.compile("\"" + preset.getKey() + "\"", Pattern.LITERAL)
075                                     .matcher(content)
076                                     .replaceAll(Matcher.quoteReplacement(preset.getValue()));
077                }
078                writer.write(content);
079                writer.flush();
080            }
081            is.close();
082        } catch (final Exception e) {
083            log.error("Error while serving resource " + resource.getUri(), e);
084            throw WroRuntimeException.wrap(e);
085        } finally {
086            IOUtils.closeQuietly(is);
087            IOUtils.closeQuietly(os);
088        }
089    }
090
091    @Override
092    public String getAlias() {
093        return ALIAS;
094    }
095
096}