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.lifecycle;
020:
021: import java.util.Iterator;
022: import java.util.Map;
023:
024: import javax.el.ValueExpression;
025: import javax.faces.FacesException;
026: import javax.faces.application.ViewHandler;
027: import javax.faces.component.UIComponent;
028: import javax.faces.context.ExternalContext;
029: import javax.faces.context.FacesContext;
030: import javax.faces.render.ResponseStateManager;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034: import org.apache.myfaces.shared_impl.renderkit.RendererUtils;
035: import org.apache.myfaces.shared_impl.util.Assert;
036:
037: /**
038: * @author Mathias Broekelmann (latest modification by $Author: mbr $)
039: * @version $Revision: 517403 $ $Date: 2007-03-12 22:17:00 +0100 (Mo, 12 Mrz 2007) $
040: */
041: public class DefaultRestoreViewSupport implements RestoreViewSupport {
042: private static final String JAVAX_SERVLET_INCLUDE_SERVLET_PATH = "javax.servlet.include.servlet_path";
043:
044: private static final String JAVAX_SERVLET_INCLUDE_PATH_INFO = "javax.servlet.include.path_info";
045:
046: private final Log log = LogFactory
047: .getLog(DefaultRestoreViewSupport.class);
048:
049: public void processComponentBinding(FacesContext facesContext,
050: UIComponent component) {
051: ValueExpression binding = component
052: .getValueExpression("binding");
053: if (binding != null) {
054: binding.setValue(facesContext.getELContext(), component);
055: }
056:
057: for (Iterator<UIComponent> iter = component
058: .getFacetsAndChildren(); iter.hasNext();) {
059: processComponentBinding(facesContext, iter.next());
060: }
061: }
062:
063: public String calculateViewId(FacesContext facesContext) {
064: Assert.notNull(facesContext);
065: ExternalContext externalContext = facesContext
066: .getExternalContext();
067: Map requestMap = externalContext.getRequestMap();
068:
069: String viewId = (String) requestMap
070: .get(JAVAX_SERVLET_INCLUDE_PATH_INFO);
071: boolean traceEnabled = log.isTraceEnabled();
072: if (viewId != null) {
073: if (traceEnabled) {
074: log.trace("Calculated viewId '" + viewId
075: + "' from request param '"
076: + JAVAX_SERVLET_INCLUDE_PATH_INFO + "'");
077: }
078: } else {
079: viewId = externalContext.getRequestPathInfo();
080: if (viewId != null && traceEnabled) {
081: log.trace("Calculated viewId '" + viewId
082: + "' from request path info");
083: }
084: }
085:
086: if (viewId == null) {
087: viewId = (String) requestMap
088: .get(JAVAX_SERVLET_INCLUDE_SERVLET_PATH);
089: if (viewId != null && traceEnabled) {
090: log.trace("Calculated viewId '" + viewId
091: + "' from request param '"
092: + JAVAX_SERVLET_INCLUDE_SERVLET_PATH + "'");
093: }
094: }
095:
096: if (viewId == null) {
097: viewId = externalContext.getRequestServletPath();
098: if (viewId != null && traceEnabled) {
099: log.trace("Calculated viewId '" + viewId
100: + "' from request servlet path");
101: }
102: }
103:
104: if (viewId == null) {
105: throw new FacesException("Could not determine view id.");
106: }
107:
108: return viewId;
109: }
110:
111: public boolean isPostback(FacesContext facesContext) {
112: ViewHandler viewHandler = facesContext.getApplication()
113: .getViewHandler();
114: String renderkitId = viewHandler
115: .calculateRenderKitId(facesContext);
116: ResponseStateManager rsm = RendererUtils
117: .getResponseStateManager(facesContext, renderkitId);
118: return rsm.isPostback(facesContext);
119: }
120: }
|