001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.myfaces.portlet;
020:
021: import java.io.IOException;
022:
023: import javax.faces.FacesException;
024: import javax.faces.application.Application;
025: import javax.faces.application.ViewHandler;
026: import javax.faces.application.ViewHandlerWrapper;
027: import javax.faces.component.UIViewRoot;
028: import javax.faces.context.FacesContext;
029: import javax.portlet.PortletRequest;
030: import javax.portlet.PortletURL;
031: import javax.portlet.RenderResponse;
032:
033: import org.apache.commons.logging.Log;
034: import org.apache.commons.logging.LogFactory;
035:
036: /**
037: * TODO: test this portlet view handler in portlet environment
038: *
039: * @author Mathias Broekelmann (latest modification by $Author: mbr $)
040: * @version $Revision: 517404 $ $Date: 2007-03-12 22:17:24 +0100 (Mo, 12 Mrz 2007) $
041: */
042: public class PortletViewHandler extends ViewHandlerWrapper {
043: private final ViewHandler _viewHandler;
044:
045: private static final Log log = LogFactory
046: .getLog(PortletViewHandler.class);
047:
048: public PortletViewHandler(ViewHandler viewHandler) {
049: _viewHandler = viewHandler;
050: }
051:
052: @Override
053: protected ViewHandler getWrapped() {
054: return _viewHandler;
055: }
056:
057: @Override
058: public UIViewRoot restoreView(FacesContext context, String viewId) {
059: if (PortletUtil.isPortletRequest(context)) {
060: PortletRequest request = (PortletRequest) context
061: .getExternalContext().getRequest();
062: String portletViewId = request
063: .getParameter(MyFacesGenericPortlet.VIEW_ID);
064: Application application = context.getApplication();
065: ViewHandler applicationViewHandler = application
066: .getViewHandler();
067: String renderKitId = applicationViewHandler
068: .calculateRenderKitId(context);
069: UIViewRoot viewRoot = application.getStateManager()
070: .restoreView(context, portletViewId, renderKitId);
071: return viewRoot;
072: }
073: return super .restoreView(context, viewId);
074: }
075:
076: @Override
077: public UIViewRoot createView(FacesContext context, String viewId) {
078: UIViewRoot viewRoot = super .createView(context, viewId);
079: if (PortletUtil.isPortletRequest(context)) {
080: PortletRequest request = (PortletRequest) context
081: .getExternalContext().getRequest();
082: viewRoot.setViewId(request
083: .getParameter(MyFacesGenericPortlet.VIEW_ID));
084: }
085: return viewRoot;
086: }
087:
088: @Override
089: public String getActionURL(FacesContext context, String viewId) {
090: if (PortletUtil.isRenderResponse(context)) {
091: RenderResponse response = (RenderResponse) context
092: .getExternalContext().getResponse();
093: PortletURL url = response.createActionURL();
094: url.setParameter(MyFacesGenericPortlet.VIEW_ID, viewId);
095: return url.toString();
096: }
097: return super .getActionURL(context, viewId);
098: }
099:
100: @Override
101: public void renderView(FacesContext context, UIViewRoot viewToRender)
102: throws IOException, FacesException {
103: if (PortletUtil.isPortletRequest(context)) {
104: if (viewToRender.isRendered()) {
105: if (log.isTraceEnabled())
106: log
107: .trace("It is a portlet request. Dispatching to view");
108: context.getExternalContext().dispatch(
109: viewToRender.getViewId());
110: }
111: } else {
112: super.renderView(context, viewToRender);
113: }
114: }
115: }
|