001/* 002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl-2.1.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * <a href="mailto:grenard@nuxeo.com">Guillaume Renard</a> 016 * 017 */ 018 019package org.nuxeo.ecm.web.resources.wro.processor; 020 021import java.io.IOException; 022import java.io.Reader; 023import java.io.Writer; 024import java.util.Map; 025 026import org.apache.commons.lang.StringUtils; 027import org.nuxeo.common.utils.URIUtils; 028import org.nuxeo.ecm.web.resources.wro.provider.NuxeoUriLocator; 029 030import ro.isdc.wro.config.Context; 031import ro.isdc.wro.config.ReadOnlyContext; 032import ro.isdc.wro.config.jmx.WroConfiguration; 033import ro.isdc.wro.model.group.Inject; 034import ro.isdc.wro.model.resource.Resource; 035import ro.isdc.wro.model.resource.processor.ResourcePreProcessor; 036 037/** 038 * 039 * Extends this class to implement a flavor-based processor. 040 * 041 * @since 7.4 042 */ 043public abstract class AbstractFlavorProcessor implements ResourcePreProcessor { 044 045 @Inject 046 protected ReadOnlyContext context; 047 048 public abstract String getAlias(); 049 050 @Override 051 public void process(Resource resource, Reader reader, Writer writer) throws IOException { 052 String flavor = getFlavor(); 053 if (!StringUtils.isBlank(flavor) && isEnabled(resource)) { 054 process(resource, reader, writer, flavor); 055 } else { 056 process(resource, reader, writer, null); 057 } 058 } 059 060 protected abstract void process(final Resource resource, final Reader reader, final Writer writer, 061 String flavorName) throws IOException; 062 063 public String getEncoding() { 064 return Context.isContextSet() ? context.getConfig().getEncoding() : WroConfiguration.DEFAULT_ENCODING; 065 } 066 067 protected String getFlavor() { 068 String queryString = context.getRequest().getQueryString(); 069 if (queryString != null) { 070 Map<String, String> params = URIUtils.getRequestParameters(queryString); 071 if (params != null && params.containsKey("flavor")) { 072 return params.get("flavor"); 073 } 074 } 075 return null; 076 } 077 078 protected boolean isEnabled(final Resource resource) { 079 return NuxeoUriLocator.isProcessorEnabled(getAlias(), resource.getUri()); 080 } 081}