org.ajax4jsf.framework.renderer.AjaxPhaseListener Maven / Gradle / Ivy
/**
* Licensed under the Common Development and Distribution License,
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.sun.com/cddl/
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package org.ajax4jsf.framework.renderer;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import javax.faces.FacesException;
import javax.faces.component.UIComponent;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
import javax.servlet.ServletResponse;
import org.ajax4jsf.framework.ajax.AjaxContainer;
import org.ajax4jsf.framework.ajax.AjaxContext;
import org.ajax4jsf.framework.skin.SkinFactory;
import org.ajax4jsf.framework.util.message.Messages;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Listener for act before Render phase to set RenderKit Id for current skin.
*
* @author shura (latest modification by $Author: alexsmirnov $)
* @version $Revision: 1.6 $ $Date: 2006/11/30 15:42:14 $
*
*/
public class AjaxPhaseListener implements PhaseListener {
/**
*
*/
private static final long serialVersionUID = -4087936963051339868L;
public static final String VIEW_BEAN_PREFIX = "org.ajax4jsf.viewbean:";
public static final String VIEW_STATE_SAVED_PARAM = "org.ajax4jsf.VIEW_STATE_SAVED";
private static final Log log = LogFactory
.getLog(AjaxPhaseListener.class);
public static final String AJAX_BEAN_PREFIX = "org.ajax4jsf.ajaxviewbean:";
/*
* (non-Javadoc)
*
* @see javax.faces.event.PhaseListener#afterPhase(javax.faces.event.PhaseEvent)
*/
public void afterPhase(PhaseEvent event) {
PhaseId phaseId = event.getPhaseId();
if(log.isDebugEnabled()){
log.debug("Process after phase "+phaseId.toString());
}
FacesContext context = event.getFacesContext();
Map requestMap = context.getExternalContext().getRequestMap();
AjaxContext ajaxContext = AjaxContext.getCurrentInstance(context);
if (phaseId == PhaseId.RENDER_RESPONSE) {
if (ajaxContext.isAjaxRequest()) {
/* if (null == requestMap.get(
AjaxRendererUtils.AJAX_AREAS_RENDERED)) {
// HACK for MyFaces ( tag not call renderers )
if (log.isDebugEnabled()) {
log
.debug(Messages
.getMessage(Messages.AJAX_RESPONSE_NOT_RENDERED_INFO));
}
AjaxContainer ajax = AjaxRendererUtils
.getSubmittedAjaxContainer(context, null);
// if (root instanceof AjaxContainer) {
// ajax = (AjaxContainer) root;
// } else {
// ajax =
// AjaxRendererUtils.getSubmittedAjaxContainer(context,null);
// }
if (null != ajax) {
ajaxContext.setSelfRender(true);
ServletResponse response = (ServletResponse) context
.getExternalContext().getResponse();
try {
response.reset();
} catch (Exception e) {
// Do nothing - we will use directly and reset
// wrapper
}
ajaxContext.renderAjaxRegion(context,
(UIComponent) ajax, true);
return;
}
}
*/ // JSF RI 1.1 hack - view state not saved in tag.
if(null == requestMap.get(VIEW_STATE_SAVED_PARAM)){
try {
ajaxContext.saveViewState(context);
} catch (IOException e) {
throw new FacesException(e);
}
}
}
// ajaxContext.processHeadResources(context);
} else if (phaseId == PhaseId.RESTORE_VIEW) {
UIViewRoot viewRoot = context.getViewRoot();
if(null != viewRoot){
boolean isAjax = ajaxContext.isAjaxRequest();
Map attributes = viewRoot.getAttributes();
for (Iterator it = attributes.keySet().iterator(); it.hasNext();) {
Object key = it.next();
if (key instanceof String) {
String stringKey = (String) key;
if(stringKey.startsWith(VIEW_BEAN_PREFIX)){
requestMap.put(stringKey.substring(VIEW_BEAN_PREFIX.length()), attributes.get(key));
} else if(isAjax && stringKey.startsWith(AJAX_BEAN_PREFIX)){
requestMap.put(stringKey.substring(AJAX_BEAN_PREFIX.length()), attributes.get(key));
}
}
}
}
}
}
/*
* (non-Javadoc)
*
* @see javax.faces.event.PhaseListener#beforePhase(javax.faces.event.PhaseEvent)
*/
public void beforePhase(PhaseEvent event) {
PhaseId phaseId = event.getPhaseId();
if(log.isDebugEnabled()){
log.debug("Process before phase "+phaseId.toString());
}
FacesContext context = event.getFacesContext();
if (phaseId == PhaseId.RENDER_RESPONSE) {
// Clear ViewId replacement, to avoid incorrect rendering of forms URI.
AjaxContext.getCurrentInstance(context).setViewIdHolder(null);
UIViewRoot root = context.getViewRoot();
log.debug(Messages.getMessage(
Messages.ENTER_BEFORE_RENDER_VIEW_PHASE, root.getViewId(),
root.getRenderKitId()));
// TODO - create special skin-config.xml configuration.
String renderKitId = null;
try {
renderKitId = SkinFactory.getInstance().getSkin(context)
.getRenderKitId(context);
} catch (Exception e) {
log.error("Exception on get current Skin ", e);
}
if (null != renderKitId) {
log.debug(Messages.getMessage(Messages.SET_RENDER_KIT_ID_INFO,
renderKitId));
root.setRenderKitId(renderKitId);
}
} else if (phaseId == PhaseId.RESTORE_VIEW) {
}
}
/*
* (non-Javadoc)
*
* @see javax.faces.event.PhaseListener#getPhaseId()
*/
public PhaseId getPhaseId() {
return PhaseId.ANY_PHASE;
}
protected boolean isValueReference(String value) {
if (value == null)
throw new NullPointerException("value");
int start = value.indexOf("#{");
if (start < 0)
return false;
int end = value.lastIndexOf('}');
return (end >= 0 && start < end);
}
}