001/* 002 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 003 * 004 * Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved. 005 * 006 * The contents of this file are subject to the terms of either the GNU 007 * General Public License Version 2 only ("GPL") or the Common Development 008 * and Distribution License("CDDL") (collectively, the "License"). You 009 * may not use this file except in compliance with the License. You can 010 * obtain a copy of the License at 011 * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html 012 * or packager/legal/LICENSE.txt. See the License for the specific 013 * language governing permissions and limitations under the License. 014 * 015 * When distributing the software, include this License Header Notice in each 016 * file and include the License file at packager/legal/LICENSE.txt. 017 * 018 * GPL Classpath Exception: 019 * Oracle designates this particular file as subject to the "Classpath" 020 * exception as provided by Oracle in the GPL Version 2 section of the License 021 * file that accompanied this code. 022 * 023 * Modifications: 024 * If applicable, add the following below the License Header, with the fields 025 * enclosed by brackets [] replaced by your own identifying information: 026 * "Portions Copyright [year] [name of copyright owner]" 027 * 028 * Contributor(s): 029 * If you wish your version of this file to be governed by only the CDDL or 030 * only the GPL Version 2, indicate your decision by adding "[Contributor] 031 * elects to include this software in this distribution under the [CDDL or GPL 032 * Version 2] license." If you don't indicate a single choice of license, a 033 * recipient has the option to distribute your version of this file under 034 * either the CDDL, the GPL Version 2 or to extend the choice of license to 035 * its licensees as provided above. However, if you add GPL Version 2 code 036 * and therefore, elected the GPL Version 2 license, then the option applies 037 * only if the new code is made subject to such option by the copyright 038 * holder. 039 */ 040 041package org.nuxeo.ecm.web.resources.jsf; 042 043import java.io.IOException; 044import java.util.Map; 045import java.util.logging.Level; 046import java.util.logging.Logger; 047 048import javax.faces.application.FacesMessage; 049import javax.faces.application.ProjectStage; 050import javax.faces.component.UIComponent; 051import javax.faces.context.FacesContext; 052import javax.faces.context.ResponseWriter; 053import javax.faces.event.AbortProcessingException; 054import javax.faces.event.ComponentSystemEvent; 055import javax.faces.event.ComponentSystemEventListener; 056import javax.faces.event.ListenerFor; 057import javax.faces.event.PostAddToViewEvent; 058import javax.faces.render.Renderer; 059 060import org.apache.commons.lang.StringUtils; 061 062import com.sun.faces.util.FacesLogger; 063 064/** 065 * Override of the default JSF class to override #encodeChildren 066 * 067 * @since 7.4 068 */ 069@ListenerFor(systemEventClass = PostAddToViewEvent.class) 070public abstract class ScriptStyleBaseRenderer extends Renderer implements ComponentSystemEventListener { 071 072 private static final String COMP_KEY = ScriptStyleBaseRenderer.class.getName() + "_COMPOSITE_COMPONENT"; 073 074 // Log instance for this class 075 protected static final Logger logger = FacesLogger.RENDERKIT.getLogger(); 076 077 /* 078 * Indicates that the component associated with this Renderer has already been added to the facet in the view. 079 */ 080 081 /* 082 * When this method is called, we know that there is a component with a script renderer somewhere in the view. We 083 * need to make it so that when an element with a name given by the value of the optional "target" component 084 * attribute is encountered, this component can be called upon to render itself. This method will add the component 085 * (associated with this Renderer) to a facet in the view only if a "target" component attribute is set. 086 */ 087 public void processEvent(ComponentSystemEvent event) throws AbortProcessingException { 088 UIComponent component = event.getComponent(); 089 FacesContext context = FacesContext.getCurrentInstance(); 090 091 String target = verifyTarget((String) component.getAttributes().get("target")); 092 if (target != null) { 093 // We're checking for a composite component here as if the resource 094 // is relocated, it may still require it's composite component context 095 // in order to properly render. Store it for later use by 096 // encodeBegin() and encodeEnd(). 097 UIComponent cc = UIComponent.getCurrentCompositeComponent(context); 098 if (cc != null) { 099 component.getAttributes().put(COMP_KEY, cc.getClientId(context)); 100 } 101 context.getViewRoot().addComponentResource(context, component, target); 102 103 } 104 } 105 106 @Override 107 public final void decode(FacesContext context, UIComponent component) { 108 // no-op 109 } 110 111 @Override 112 public final boolean getRendersChildren() { 113 return true; 114 } 115 116 /** 117 * If overridden, this method (i.e. super.encodeEnd) should be called <em>last</em> within the overridding 118 * implementation. 119 */ 120 @Override 121 public void encodeEnd(FacesContext context, UIComponent component) throws IOException { 122 123 // Remove the key to prevent issues with state saving... 124 String ccID = (String) component.getAttributes().get(COMP_KEY); 125 if (ccID != null) { 126 // the first pop maps to the component we're rendering. 127 // the second pop maps to the composite component that was pushed 128 // in this renderer's encodeBegin implementation. 129 // re-push the current component to reset the original context 130 component.popComponentFromEL(context); 131 component.popComponentFromEL(context); 132 component.pushComponentToEL(context, component); 133 } 134 } 135 136 /** 137 * If overridden, this method (i.e. super.encodeBegin) should be called <em>first</em> within the overridding 138 * implementation. 139 */ 140 @Override 141 public void encodeBegin(FacesContext context, UIComponent component) throws IOException { 142 143 String ccID = (String) component.getAttributes().get(COMP_KEY); 144 if (null != ccID) { 145 UIComponent cc = context.getViewRoot().findComponent(':' + ccID); 146 UIComponent curCC = UIComponent.getCurrentCompositeComponent(context); 147 if (cc != curCC) { 148 // the first pop maps to the component we're rendering. 149 // push the composite component to the 'stack' and then re-push 150 // the component we're rendering so the current component is 151 // correct. 152 component.popComponentFromEL(context); 153 component.pushComponentToEL(context, cc); 154 component.pushComponentToEL(context, component); 155 } 156 } 157 158 } 159 160 @Override 161 public void encodeChildren(FacesContext context, UIComponent component) throws IOException { 162 encodeChildren(context, component, false); 163 } 164 165 public final void encodeChildren(FacesContext context, UIComponent component, boolean warnOnChildren) 166 throws IOException { 167 int childCount = component.getChildCount(); 168 boolean renderChildren = (0 < childCount); 169 170 if (warnOnChildren) { 171 Map<String, Object> attributes = component.getAttributes(); 172 boolean hasName = attributes.get("name") != null; 173 boolean hasSrc = attributes.get("src") != null; 174 // If we have no "name" attribute... 175 if (!hasName && !hasSrc) { 176 // and no child content... 177 if (0 == childCount) { 178 // this is user error, so put up a message if desired 179 if (context.isProjectStage(ProjectStage.Development)) { 180 FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_WARN, 181 "outputScript with no library, no name, and no body content", 182 "Is body content intended?"); 183 context.addMessage(component.getClientId(context), message); 184 } 185 // We have no children, but don't bother with the method 186 // invocation anyway. 187 renderChildren = false; 188 } 189 } else if (0 < childCount) { 190 // If we have a "name" and also have child content, ignore 191 // the child content and log a message. 192 if (logger.isLoggable(Level.INFO)) { 193 logger.info("outputScript with \"name\" attribute and nested content. Ignoring nested content."); 194 } 195 renderChildren = false; 196 } 197 } 198 if (renderChildren) { 199 ResponseWriter writer = context.getResponseWriter(); 200 startElement(writer, component); 201 super.encodeChildren(context, component); 202 endElement(writer); 203 } 204 205 } 206 207 // ------------------------------------------------------- Protected Methods 208 209 /** 210 * <p> 211 * Allow the subclass to customize the start element content 212 * </p> 213 */ 214 protected abstract void startElement(ResponseWriter writer, UIComponent component) throws IOException; 215 216 /** 217 * <p> 218 * Allow the subclass to customize the start element content 219 * </p> 220 */ 221 protected abstract void endElement(ResponseWriter writer) throws IOException; 222 223 /** 224 * <p> 225 * Allow a subclass to control what's a valid value for "target". 226 */ 227 protected String verifyTarget(String toVerify) { 228 if (StringUtils.isBlank(toVerify)) { 229 return null; 230 } 231 FacesContext context = FacesContext.getCurrentInstance(); 232 boolean ajaxRequest = context.getPartialViewContext().isAjaxRequest(); 233 if (ajaxRequest) { 234 // ease up ajax re-rendering in case of js scripts parsing defer 235 return null; 236 } 237 return toVerify; 238 } 239 240}