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