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.myfaces.tobago.renderkit.html.HtmlAttributes;
021    import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
022    import org.slf4j.Logger;
023    import org.slf4j.LoggerFactory;
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      private static final Logger LOG = LoggerFactory.getLogger(TobagoResponseStateManager.class);
038    
039      public static final String TREE_PARAM = "jsf_tree";
040      private static final String STATE_PARAM = "javax.faces.ViewState";
041      private static final String VIEWID_PARAM = "jsf_viewid";
042    
043      public Object getTreeStructureToRestore(FacesContext facescontext, String viewId) {
044        Map requestMap = facescontext.getExternalContext().getRequestParameterMap();
045        Object requestViewId = requestMap.get(VIEWID_PARAM);
046        if (requestViewId == null || !requestViewId.equals(viewId)) {
047          //no saved state or state of different viewId
048          return null;
049        }
050    
051        return requestMap.get(TREE_PARAM);
052    
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(HtmlElements.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(HtmlElements.INPUT);
074          }
075        } else {
076          if (LOG.isDebugEnabled()) {
077            LOG.debug("No tree structure to be saved in client response!");
078          }
079        }
080    
081        responseWriter.startElement(HtmlElements.INPUT, null);
082        responseWriter.writeAttribute(HtmlAttributes.TYPE, "hidden", null);
083        responseWriter.writeAttribute(HtmlAttributes.NAME, STATE_PARAM, null);
084        responseWriter.writeAttribute(HtmlAttributes.ID, STATE_PARAM, null);
085        if (compStates != null) {
086          if (compStates instanceof String) {
087            responseWriter.writeAttribute(HtmlAttributes.VALUE, compStates, null);
088          }
089        } else {
090          responseWriter.writeAttribute(HtmlAttributes.VALUE, "", null);
091          if (LOG.isDebugEnabled()) {
092            LOG.debug("No component states to be saved in client response!");
093          }
094        }
095        responseWriter.endElement(HtmlElements.INPUT);
096    
097        responseWriter.startElement(HtmlElements.INPUT, null);
098        responseWriter.writeAttribute(HtmlAttributes.TYPE, "hidden", null);
099        responseWriter.writeAttribute(HtmlAttributes.NAME, VIEWID_PARAM, null);
100        responseWriter.writeAttribute(HtmlAttributes.ID, VIEWID_PARAM, null);
101        responseWriter.writeAttribute(HtmlAttributes.VALUE, facesContext.getViewRoot().getViewId(), null);
102        responseWriter.endElement(HtmlElements.INPUT);
103      }
104    
105    }