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 * <a href="mailto:grenard@nuxeo.com">Guillaume Renard</a> 018 * 019 */ 020 021package org.nuxeo.ecm.web.resources.wro.processor; 022 023import java.io.IOException; 024import java.io.Reader; 025import java.io.Writer; 026import java.util.Map; 027 028import org.apache.commons.lang.StringUtils; 029import org.nuxeo.common.utils.URIUtils; 030import org.nuxeo.ecm.web.resources.wro.provider.NuxeoUriLocator; 031 032import ro.isdc.wro.config.Context; 033import ro.isdc.wro.config.ReadOnlyContext; 034import ro.isdc.wro.config.jmx.WroConfiguration; 035import ro.isdc.wro.model.group.Inject; 036import ro.isdc.wro.model.resource.Resource; 037import ro.isdc.wro.model.resource.processor.ResourcePreProcessor; 038 039/** 040 * 041 * Extends this class to implement a flavor-based processor. 042 * 043 * @since 7.4 044 */ 045public abstract class AbstractFlavorProcessor implements ResourcePreProcessor { 046 047 @Inject 048 protected ReadOnlyContext context; 049 050 public abstract String getAlias(); 051 052 @Override 053 public void process(Resource resource, Reader reader, Writer writer) throws IOException { 054 String flavor = getFlavor(); 055 if (!StringUtils.isBlank(flavor) && isEnabled(resource)) { 056 process(resource, reader, writer, flavor); 057 } else { 058 process(resource, reader, writer, null); 059 } 060 } 061 062 protected abstract void process(final Resource resource, final Reader reader, final Writer writer, 063 String flavorName) throws IOException; 064 065 public String getEncoding() { 066 return Context.isContextSet() ? context.getConfig().getEncoding() : WroConfiguration.DEFAULT_ENCODING; 067 } 068 069 protected String getFlavor() { 070 String queryString = context.getRequest().getQueryString(); 071 if (queryString != null) { 072 Map<String, String> params = URIUtils.getRequestParameters(queryString); 073 if (params != null && params.containsKey("flavor")) { 074 return params.get("flavor"); 075 } 076 } 077 return null; 078 } 079 080 protected boolean isEnabled(final Resource resource) { 081 return NuxeoUriLocator.isProcessorEnabled(getAlias(), resource.getUri()); 082 } 083}