/*
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the License). You may not use this file except in
* compliance with the License.
*
* You can obtain a copy of the license at
* https://jsftemplating.dev.java.net/cddl1.html or
* jsftemplating/cddl1.txt.
* See the License for the specific language governing
* permissions and limitations under the License.
*
* When distributing Covered Code, include this CDDL
* Header Notice in each file and include the License file
* at jsftemplating/cddl1.txt.
* If applicable, add the following below the CDDL Header,
* with the fields enclosed by brackets [] replaced by
* you own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
*/
/*
* NavigationHandlers.java
*
* Created on December 6, 2004, 11:06 PM
*/
package com.sun.jsftemplating.handlers;
import com.sun.jsftemplating.annotation.Handler;
import com.sun.jsftemplating.annotation.HandlerInput;
import com.sun.jsftemplating.annotation.HandlerOutput;
import com.sun.jsftemplating.layout.descriptors.handler.HandlerContext;
import java.io.IOException;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.context.ExternalContext;
import javax.servlet.http.HttpServletResponse;
/**
*
This class contains
* {@link com.sun.jsftemplating.layout.descriptors.handler.Handler}
* methods that perform navigation oriented actions.
*
* @author Ken Paulsen (ken.paulsen@sun.com)
*/
public class NavigationHandlers {
/**
* Default Constructor.
*/
public NavigationHandlers() {
}
/**
* This handler returns a UIViewRoot
. If the
* id
parameter is supplied it will return the requested
* UIViewRoot
(this may fail and cause an exception). If
* the id
is not supplied, it will return the
* current UIViewRoot
. The result will be returned in
* an output parameter named viewRoot
.
*/
@Handler(id="getUIViewRoot",
input={
@HandlerInput(name="id", type=String.class)
},
output={
@HandlerOutput(name="viewRoot", type=UIViewRoot.class)
})
public void getUIViewRoot(HandlerContext context) {
String pageName = (String) context.getInputValue("id");
FacesContext ctx = context.getFacesContext();
UIViewRoot root = null;
if (pageName == null) {
root = ctx.getViewRoot();
} else {
if (pageName.charAt(0) != '/') {
// Ensure we start w/ a '/'
pageName = "/" + pageName;
}
root = ctx.getApplication().getViewHandler().
createView(ctx, pageName);
}
context.setOutputValue("viewRoot", root);
}
/**
* This method gives you a "resource URL" as defined by the
* ViewHandler
's getActionURL(String
* url)
method.
*
* @param handlerCtx The {@link HandlerContext}.
*/
@Handler(id="getActionURL",
input={
@HandlerInput(name="url", type=String.class, required=true)
},
output={
@HandlerOutput(name="url", type=String.class)
})
public static void getActionURL(HandlerContext handlerCtx) {
String url = (String) handlerCtx.getInputValue("url");
FacesContext ctx = handlerCtx.getFacesContext();
handlerCtx.setOutputValue("url", ctx.getApplication().getViewHandler().
getActionURL(ctx, url));
}
/**
* This method gives you a "resource URL" as defined by the
* ViewHandler
's getResourceURL(String
* url)
method.
*
* @param handlerCtx The {@link HandlerContext}.
*/
@Handler(id="getResourceURL",
input={
@HandlerInput(name="url", type=String.class, required=true)
},
output={
@HandlerOutput(name="url", type=String.class)
})
public static void getResourceURL(HandlerContext handlerCtx) {
String url = (String) handlerCtx.getInputValue("url");
FacesContext ctx = handlerCtx.getFacesContext();
handlerCtx.setOutputValue("url", ctx.getApplication().getViewHandler().
getResourceURL(ctx, url));
}
/**
* This handler navigates to the given page. page
may
* either be a UIViewRoot
or a String
* representing a UIViewRoot
. Passing in a
* String
name of a UIViewRoot
will always
* create a new UIViewRoot
. Passing in the
* UIViewRoot
provides an opportunity to customize the
* UIComponent
tree that will be displayed.
*
* The {@link #getUIViewRoot(HandlerContext)} handler provides a way
* to obtain a UIViewRoot
.
*
* Input value: "page" -- Type: Object
(should be a
* String
or a UIViewRoot
).
*
* @param context The {@link HandlerContext}.
*/
@Handler(id="navigate",
input={
@HandlerInput(name="page", type=Object.class, required=true)
})
public static void navigate(HandlerContext context) {
Object page = context.getInputValue("page");
UIViewRoot root = null;
FacesContext ctx = context.getFacesContext();
if (page instanceof String) {
// Create a new UIViewRoot with the given id
String strPage = (String) page;
if (strPage.charAt(0) != '/') {
// Ensure we start w/ a '/'
strPage = "/" + strPage;
}
root = ctx.getApplication().getViewHandler().
createView(ctx, strPage);
} else if (page instanceof UIViewRoot) {
// We recieved a UIViewRoot, use it...
root = (UIViewRoot) page;
} else {
throw new IllegalArgumentException("Type '"
+ page.getClass().getName()
+ "' is not valid. It must be a String or UIViewRoot.");
}
// Set the UIViewRoot so that it will be displayed
ctx.setViewRoot(root);
}
/**
* This handler redirects to the given page.
*
* Input value: "page" -- Type: String
*
* @param context The {@link HandlerContext}.
*/
@Handler(id="redirect",
input={
@HandlerInput(name="page", type=String.class, required=true)
})
public static void redirect(HandlerContext context) {
String page = (String) context.getInputValue("page");
FacesContext ctx = context.getFacesContext();
try {
ctx.getExternalContext().redirect(page);
ctx.responseComplete();
} catch (IOException ex) {
throw new RuntimeException(
"Unable to navigate to page '" + page + "'!", ex);
}
}
/**
* This handler encodes URL, and redirects to the given page.
*
* Input value: "page" -- Type: String
*
* @param context The {@link HandlerContext}.
*/
@Handler(id="encoderedirect",
input={
@HandlerInput(name="page", type=String.class, required=true)
})
public static void encoderedirect(HandlerContext context) {
String page = (String) context.getInputValue("page");
ExternalContext ctx = context.getFacesContext().getExternalContext();
HttpServletResponse response = (HttpServletResponse)ctx.getResponse();
page = response.encodeRedirectURL(page);
try {
ctx.redirect(page);
context.getFacesContext().responseComplete();
} catch (IOException ex) {
throw new RuntimeException(
"Unable to navigate to page '" + page + "'!", ex);
}
}
/**
* This handler forwards to the given page. Normally you will want
* to do {@link #navigate} as that follows JSF patterns. This uses
* the raw dispatcher forward mechanism (via the ExternalContext).
*
* Input value: "url" -- Type: String
*
* @param context The {@link HandlerContext}.
*/
@Handler(id="dispatch",
input={
@HandlerInput(name="path", type=String.class, required=true)
})
public static void dispatch(HandlerContext context) {
String path = (String) context.getInputValue("path");
FacesContext ctx = context.getFacesContext();
try {
ctx.getExternalContext().dispatch(path);
ctx.responseComplete();
} catch (IOException ex) {
throw new RuntimeException(
"Unable to navigate to path '" + path + "'!", ex);
}
}
}