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.HtmlConstants;
023    import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
024    
025    import javax.faces.render.ResponseStateManager;
026    import javax.faces.context.FacesContext;
027    import javax.faces.context.ResponseWriter;
028    import javax.faces.application.StateManager;
029    import java.util.Map;
030    import java.io.IOException;
031    
032    /*
033     * Created by IntelliJ IDEA.
034     * User: bommel
035     * Date: 21.05.2006
036     * Time: 10:59:19
037     */
038    public class TobagoResponseStateManager extends ResponseStateManager {
039      private static final Log LOG = LogFactory.getLog(TobagoResponseStateManager.class);
040    
041      private static final String TREE_PARAM = "jsf_tree";
042      private static final String STATE_PARAM = "jsf_state";
043      private static final String VIEWID_PARAM = "jsf_viewid";
044    
045      public Object getTreeStructureToRestore(FacesContext facescontext, String viewId) {
046        Map requestMap = facescontext.getExternalContext().getRequestParameterMap();
047        Object requestViewId = requestMap.get(VIEWID_PARAM);
048        if (requestViewId == null || !requestViewId.equals(viewId)) {
049          //no saved state or state of different viewId
050          return null;
051        }
052    
053        return requestMap.get(TREE_PARAM);
054    
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    
106    }