/* * Copyright 2006 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * 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.apache.shale.dialog.faces; import javax.faces.application.NavigationHandler; import javax.faces.application.ViewHandler; import javax.faces.context.FacesContext; import javax.faces.event.PhaseEvent; import javax.faces.event.PhaseId; import javax.faces.event.PhaseListener; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** *
A JavaServer Faces PhaseListener that provides support for
* DialogNavigationHandler facility of Shale. This PhaseListener
* allows you to hook up the DialogManager without invoking on an UICommand
* class inside of your JSF page. For instance, when you are using j_security and willing
* to forward to a Dialog.
To use this PhaseListener configure it inside your faces-config.xml.
* Create a JavaServer Page, containing the jsp:forward-Tag. For the page
* attribute value use something like initdialog/dialog.faces, where dialog is
* the name of your Dialog, you'd like to invoke.
The benefit is you can process the standard Request Processing Lifecycle, without requiring explicit * configuration (for example, of a Servlet or a Filter).
*/ public class InitDialogPhaseListener implements PhaseListener { private transient Log log = LogFactory.getLog(InitDialogPhaseListener.class); /** * constant, which thisPhaseListner intercepts.
*/
public static String INIT_DIALOG = "/initdialog";
private static final long serialVersionUID = 1L;
// --------------------------------------------------- PhaseListener Methods
/**
* Perform interception processing activities after the * specified phase processing has been performed.
* * @param eventPhaseEvent to be processed
*/
public void afterPhase(PhaseEvent event) {
FacesContext facesContext = event.getFacesContext();
if(facesContext.getViewRoot().getViewId().startsWith(INIT_DIALOG))
invokeApplication(facesContext);
}
/**
* Perform interception processing activities before the * specified phase processing has been performed.
* * @param eventPhaseEvent to be processed
*/
public void beforePhase(PhaseEvent event) {
}
/**
* Return the identifier of the JavaServer Faces request processing * lifecycle phase(s) that we are interested in.
*/ public PhaseId getPhaseId() { return PhaseId.RESTORE_VIEW; } // --------------------------------------------------------- Support Methods /** *This method handles the invocation of the DialogNavigationHandler
* of Shale, by the given ViewId.
FacesContext for the current request
*/
private void invokeApplication(FacesContext facesContext) {
String incommingViewId = facesContext.getViewRoot().getViewId();
if(log.isTraceEnabled())
log.trace("Incomming view id: " + incommingViewId);
String viewId = incommingViewId.substring(INIT_DIALOG.length());
facesContext.getViewRoot().setViewId(viewId);
if(log.isTraceEnabled())
log.trace("new generated view id: " + viewId);
String dialog = extractDialogName(viewId);
NavigationHandler navigationHandler = facesContext.getApplication().getNavigationHandler();
if (navigationHandler instanceof DialogNavigationHandler) {
navigationHandler.handleNavigation(facesContext, null, DialogNavigationHandler.PREFIX+dialog);
} else {
if(log.isFatalEnabled())
log.fatal(navigationHandler + " is not instanceof " + DialogNavigationHandler.class);
facesContext.renderResponse();
}
}
/**
* Helper method to extract the name of the desired Dialog from the
* given ViewId.
* @param viewId the given ViewId for the request, we are processing
* @return name of the Dialog
*/
private String extractDialogName(String viewId) {
String extract = viewId.substring(1);
extract = extract.substring(0,extract.indexOf(ViewHandler.DEFAULT_SUFFIX));
return extract;
}
}