001: /*
002: * $Id: ViewHandlerImpl.java,v 1.5 2006/06/13 09:45:55 dg154973 Exp $
003: */
004:
005: /*
006: * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
007: * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
008: */
009:
010: package com.sun.faces.portlet;
011:
012: import java.io.IOException;
013: import java.util.Locale;
014:
015: import javax.faces.application.ViewHandler;
016: import javax.faces.component.UIViewRoot;
017: import javax.faces.context.FacesContext;
018: import javax.faces.context.ResponseWriter;
019: import javax.faces.FacesException;
020:
021: import javax.portlet.PortletURL;
022: import javax.portlet.RenderResponse;
023:
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026:
027: /**
028: * <p>Concrete implementation of <code>ViewHandler</code> for use in
029: * a portlet environment. This implementation delegates to the
030: * standard <codeViewHandler</code> instance provided to our constructor,
031: * and only implements portlet-specific behavior where necessary.</p>
032: */
033:
034: public final class ViewHandlerImpl extends ViewHandler {
035:
036: // ------------------------------------------------------------- Constructor
037:
038: /**
039: * <p>Construct a new <code>ViewHandler</code> instance that delegates
040: * all non-portlet-specific behavior to the specified implementation.
041: *
042: * @param handler The <code>ViewHandler</code> instance to whom
043: * we can delegate
044: *
045: * @exception NullPointerException if <code>handler</code>
046: * is <code>null</code>
047: */
048: public ViewHandlerImpl(ViewHandler handler) {
049:
050: if (handler == null) {
051: throw new NullPointerException();
052: }
053: if (log.isDebugEnabled()) {
054: log.debug("Delegating to '" + handler + "'");
055: }
056: this .handler = handler;
057:
058: }
059:
060: // -------------------------------------------------------- Static Variables
061:
062: /**
063: * <p>The URL parameter we use to pass the view identifier of the
064: * requested view.</p>
065: */
066: public static final String VIEW_ID_PARAMETER = "com.sun.faces.portlet.VIEW_ID";
067:
068: /**
069: * <p>The URL parameter we use to pass the namespace of the portlet.</p>
070: */
071: public static final String NAME_SPACE_PARAMETER = "com.sun.faces.portlet.NAME_SPACE";
072:
073: // The Log instance for this class
074: private static final Log log = LogFactory
075: .getLog(ViewHandlerImpl.class);
076:
077: // ------------------------------------------------------ Instance Variables
078:
079: // The ViewHandler we delegate to
080: private ViewHandler handler;
081:
082: // ----------------------------------------------------- ViewHandler Methods
083:
084: public Locale calculateLocale(FacesContext context) {
085: return (handler.calculateLocale(context));
086: }
087:
088: public String calculateRenderKitId(FacesContext context) {
089: return (handler.calculateRenderKitId(context));
090: }
091:
092: public UIViewRoot createView(FacesContext context, String viewId) {
093: return (handler.createView(context, viewId));
094: }
095:
096: public String getActionURL(FacesContext context, String viewId) {
097: Object r = context.getExternalContext().getResponse();
098: if (!(r instanceof RenderResponse)) {
099: if (log.isDebugEnabled()) {
100: log.debug("Must be a RenderResponse");
101: }
102: throw new IllegalStateException("Must be a RenderResponse");
103: }
104: RenderResponse response = (RenderResponse) r;
105: PortletURL actionURL = response.createActionURL();
106: actionURL.setParameter(VIEW_ID_PARAMETER, viewId);
107: if (log.isTraceEnabled()) {
108: log.trace("Action URL:" + actionURL.toString());
109: }
110: return (actionURL.toString());
111: }
112:
113: public String getResourceURL(FacesContext context, String path) {
114: return (handler.getResourceURL(context, path));
115: }
116:
117: public void renderView(FacesContext context, UIViewRoot viewToRender)
118: throws IOException, FacesException {
119: handler.renderView(context, viewToRender);
120: }
121:
122: public UIViewRoot restoreView(FacesContext context, String viewId) {
123: return (handler.restoreView(context, viewId));
124: }
125:
126: public void writeState(FacesContext context) throws IOException {
127: handler.writeState(context);
128: // A unique value is needed while saving values in the session.
129: // As the rendee response namespace is unique, we write the namespace obtainer
130: // from the render response as hidden field and send it with the page.
131: ResponseWriter writer = context.getResponseWriter();
132: RenderResponse response = (RenderResponse) context
133: .getExternalContext().getResponse();
134: writer.startElement("input", context.getViewRoot());
135: writer.writeAttribute("type", "hidden", null);
136: writer.writeAttribute("name", NAME_SPACE_PARAMETER, null);
137: writer.writeAttribute("id", NAME_SPACE_PARAMETER, null);
138: writer.writeAttribute("value", response.getNamespace(), null);
139: }
140: }
|