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    
020    package org.apache.myfaces.tobago.renderkit;
021    
022    import org.apache.commons.logging.Log;
023    import org.apache.commons.logging.LogFactory;
024    import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
025    import org.apache.myfaces.tobago.renderkit.html.HtmlConstants;
026    
027    import javax.faces.application.StateManager;
028    import javax.faces.context.FacesContext;
029    import javax.faces.context.ResponseWriter;
030    import javax.faces.render.ResponseStateManager;
031    import java.io.IOException;
032    import java.util.Map;
033    
034    /*
035     * Date: 21.05.2006
036     * Time: 10:59:19
037     */
038    public class TobagoResponseStateManager extends ResponseStateManager {
039    
040      private static final Log LOG = LogFactory.getLog(TobagoResponseStateManager.class);
041    
042      public static final String TREE_PARAM = "jsf_tree";
043      private static final String STATE_PARAM = "jsf_state";
044      private static final String VIEWID_PARAM = "jsf_viewid";
045    
046      public Object getTreeStructureToRestore(FacesContext facescontext, String viewId) {
047        Map requestMap = facescontext.getExternalContext().getRequestParameterMap();
048        Object requestViewId = requestMap.get(VIEWID_PARAM);
049        if (requestViewId == null || !requestViewId.equals(viewId)) {
050          //no saved state or state of different viewId
051          return null;
052        }
053    
054        return requestMap.get(TREE_PARAM);
055      }
056    
057      public Object getComponentStateToRestore(FacesContext facesContext) {
058        Map requestMap = facesContext.getExternalContext().getRequestParameterMap();
059        return requestMap.get(STATE_PARAM);
060      }
061    
062      public void writeState(FacesContext facesContext,
063          StateManager.SerializedView serializedview) throws IOException {
064        ResponseWriter responseWriter = facesContext.getResponseWriter();
065        Object treeStruct = serializedview.getStructure();
066        Object compStates = serializedview.getState();
067    
068        if (treeStruct != null) {
069          if (treeStruct instanceof String) {
070            responseWriter.startElement(HtmlConstants.INPUT, null);
071            responseWriter.writeAttribute(HtmlAttributes.TYPE, "hidden", null);
072            responseWriter.writeAttribute(HtmlAttributes.NAME, TREE_PARAM, null);
073            responseWriter.writeAttribute(HtmlAttributes.ID, TREE_PARAM, null);
074            responseWriter.writeAttribute(HtmlAttributes.VALUE, treeStruct, null);
075            responseWriter.endElement(HtmlConstants.INPUT);
076          }
077        } else {
078          if (LOG.isDebugEnabled()) {
079            LOG.debug("No tree structure to be saved in client response!");
080          }
081        }
082    
083        if (compStates != null) {
084          if (compStates instanceof String) {
085            responseWriter.startElement(HtmlConstants.INPUT, null);
086            responseWriter.writeAttribute(HtmlAttributes.TYPE, "hidden", null);
087            responseWriter.writeAttribute(HtmlAttributes.NAME, STATE_PARAM, null);
088            responseWriter.writeAttribute(HtmlAttributes.ID, STATE_PARAM, null);
089            responseWriter.writeAttribute(HtmlAttributes.VALUE, compStates, null);
090            responseWriter.endElement(HtmlConstants.INPUT);
091          }
092        } else {
093          if (LOG.isDebugEnabled()) {
094            LOG.debug("No component states to be saved in client response!");
095          }
096        }
097    
098        responseWriter.startElement(HtmlConstants.INPUT, null);
099        responseWriter.writeAttribute(HtmlAttributes.TYPE, "hidden", null);
100        responseWriter.writeAttribute(HtmlAttributes.NAME, VIEWID_PARAM, null);
101        responseWriter.writeAttribute(HtmlAttributes.ID, VIEWID_PARAM, null);
102        responseWriter.writeAttribute(HtmlAttributes.VALUE, facesContext.getViewRoot().getViewId(), null);
103        responseWriter.endElement(HtmlConstants.INPUT);
104      }
105    }