001    package org.apache.myfaces.tobago.renderkit;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one or more
005     * contributor license agreements.  See the NOTICE file distributed with
006     * this work for additional information regarding copyright ownership.
007     * The ASF licenses this file to You under the Apache License, Version 2.0
008     * (the "License"); you may not use this file except in compliance with
009     * the License.  You may obtain a copy of the License at
010     *
011     *      http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing, software
014     * distributed under the License is distributed on an "AS IS" BASIS,
015     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016     * See the License for the specific language governing permissions and
017     * limitations under the License.
018     */
019    
020    import org.apache.commons.logging.Log;
021    import org.apache.commons.logging.LogFactory;
022    import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
023    import org.apache.myfaces.tobago.renderkit.html.HtmlConstants;
024    
025    import javax.faces.application.StateManager;
026    import javax.faces.context.FacesContext;
027    import javax.faces.context.ResponseWriter;
028    import javax.faces.render.ResponseStateManager;
029    import java.io.IOException;
030    import java.util.Map;
031    
032    /*
033     * Date: 21.05.2006
034     * Time: 10:59:19
035     */
036    public class TobagoResponseStateManager extends ResponseStateManager {
037    
038      private static final Log LOG = LogFactory.getLog(TobagoResponseStateManager.class);
039    
040      public static final String TREE_PARAM = "jsf_tree";
041      private static final String STATE_PARAM = "jsf_state";
042      private static final String VIEWID_PARAM = "jsf_viewid";
043    
044      public Object getTreeStructureToRestore(FacesContext facescontext, String viewId) {
045        Map requestMap = facescontext.getExternalContext().getRequestParameterMap();
046        Object requestViewId = requestMap.get(VIEWID_PARAM);
047        if (requestViewId == null || !requestViewId.equals(viewId)) {
048          //no saved state or state of different viewId
049          return null;
050        }
051    
052        return requestMap.get(TREE_PARAM);
053      }
054    
055      public Object getComponentStateToRestore(FacesContext facesContext) {
056        Map requestMap = facesContext.getExternalContext().getRequestParameterMap();
057        return requestMap.get(STATE_PARAM);
058      }
059    
060      public void writeState(FacesContext facesContext,
061          StateManager.SerializedView serializedview) throws IOException {
062        ResponseWriter responseWriter = facesContext.getResponseWriter();
063        Object treeStruct = serializedview.getStructure();
064        Object compStates = serializedview.getState();
065    
066        if (treeStruct != null) {
067          if (treeStruct instanceof String) {
068            responseWriter.startElement(HtmlConstants.INPUT, null);
069            responseWriter.writeAttribute(HtmlAttributes.TYPE, "hidden", null);
070            responseWriter.writeAttribute(HtmlAttributes.NAME, TREE_PARAM, null);
071            responseWriter.writeAttribute(HtmlAttributes.ID, TREE_PARAM, null);
072            responseWriter.writeAttribute(HtmlAttributes.VALUE, treeStruct, null);
073            responseWriter.endElement(HtmlConstants.INPUT);
074          }
075        } else {
076          if (LOG.isDebugEnabled()) {
077            LOG.debug("No tree structure to be saved in client response!");
078          }
079        }
080    
081        if (compStates != null) {
082          if (compStates instanceof String) {
083            responseWriter.startElement(HtmlConstants.INPUT, null);
084            responseWriter.writeAttribute(HtmlAttributes.TYPE, "hidden", null);
085            responseWriter.writeAttribute(HtmlAttributes.NAME, STATE_PARAM, null);
086            responseWriter.writeAttribute(HtmlAttributes.ID, STATE_PARAM, null);
087            responseWriter.writeAttribute(HtmlAttributes.VALUE, compStates, null);
088            responseWriter.endElement(HtmlConstants.INPUT);
089          }
090        } else {
091          if (LOG.isDebugEnabled()) {
092            LOG.debug("No component states to be saved in client response!");
093          }
094        }
095    
096        responseWriter.startElement(HtmlConstants.INPUT, null);
097        responseWriter.writeAttribute(HtmlAttributes.TYPE, "hidden", null);
098        responseWriter.writeAttribute(HtmlAttributes.NAME, VIEWID_PARAM, null);
099        responseWriter.writeAttribute(HtmlAttributes.ID, VIEWID_PARAM, null);
100        responseWriter.writeAttribute(HtmlAttributes.VALUE, facesContext.getViewRoot().getViewId(), null);
101        responseWriter.endElement(HtmlConstants.INPUT);
102      }
103    }