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 *     Anahide Tchertchian
016 */
017package org.nuxeo.ecm.web.resources.jsf;
018
019import java.io.IOException;
020import java.util.Map;
021
022import javax.faces.component.UIComponent;
023import javax.faces.context.FacesContext;
024
025import org.apache.commons.lang.StringUtils;
026import org.apache.commons.logging.Log;
027import org.apache.commons.logging.LogFactory;
028import org.nuxeo.ecm.web.resources.api.ResourceType;
029
030/**
031 * Renderer for pages, handling several types of resources (css and js for now).
032 *
033 * @since 7.10
034 */
035public class PageResourceRenderer extends ResourceBundleRenderer {
036
037    public static final String RENDERER_TYPE = "org.nuxeo.ecm.web.resources.jsf.PageResource";
038
039    private static final Log log = LogFactory.getLog(PageResourceRenderer.class);
040
041    @Override
042    public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
043        Map<String, Object> attributes = component.getAttributes();
044        String name = (String) attributes.get("name");
045        String type = (String) attributes.get("type");
046        if (StringUtils.isBlank(type)) {
047            log.error(String.format("Cannot encode page with empty type at %s", component.getClientId()));
048            return;
049        }
050        if (!ResourceType.css.equals(type) && !ResourceType.js.equals(type)) {
051            log.error(String.format("Unsupported type '%s' to encode page '%s' at %s", type, name,
052                    component.getClientId()));
053            return;
054        }
055        if (ResourceType.css.equals(type)) {
056            encodeEnd(context, component, ResourceType.css, PAGE_ENDPOINT_PATH + name + ".css");
057        } else if (ResourceType.js.equals(type)) {
058            encodeEnd(context, component, ResourceType.js, PAGE_ENDPOINT_PATH + name + ".js");
059        }
060        super.encodeEnd(context, component);
061    }
062
063}