View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.renderkit;
21  
22  import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
23  import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
24  import org.apache.myfaces.tobago.renderkit.html.HtmlInputTypes;
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  
28  import javax.faces.application.StateManager;
29  import javax.faces.context.FacesContext;
30  import javax.faces.context.ResponseWriter;
31  import javax.faces.render.ResponseStateManager;
32  import java.io.IOException;
33  import java.util.Map;
34  
35  /**
36   * @deprecated since 2.0.0 No longer needed for JSF 2.0
37   */
38  @Deprecated
39  public class TobagoResponseStateManager extends ResponseStateManager {
40    private static final Logger LOG = LoggerFactory.getLogger(TobagoResponseStateManager.class);
41  
42    public static final String TREE_PARAM = "jsf_tree";
43    public static final String VIEW_STATE_PARAM = "javax.faces.ViewState";
44    private static final String VIEWID_PARAM = "jsf_viewid";
45  
46    public Object getState(FacesContext context, String viewId) {
47      Object treeStructure = getTreeStructureToRestore(context, viewId);
48      Object componentStateToRestore = getComponentStateToRestore(context);
49      if (treeStructure != null && componentStateToRestore != null) {
50          return new Object[] {treeStructure, componentStateToRestore};
51      }
52      return null;
53    }
54  
55    public Object getTreeStructureToRestore(FacesContext facescontext, String viewId) {
56      Map requestMap = facescontext.getExternalContext().getRequestParameterMap();
57      Object requestViewId = requestMap.get(VIEWID_PARAM);
58      if (requestViewId == null || !requestViewId.equals(viewId)) {
59        //no saved state or state of different viewId
60        return null;
61      }
62  
63      return requestMap.get(TREE_PARAM);
64  
65    }
66  
67    public Object getComponentStateToRestore(FacesContext facesContext) {
68      Map requestMap = facesContext.getExternalContext().getRequestParameterMap();
69      return requestMap.get(VIEW_STATE_PARAM);
70    }
71  
72    public void writeState(FacesContext facesContext,
73        StateManager.SerializedView serializedview) throws IOException {
74      ResponseWriter responseWriter = facesContext.getResponseWriter();
75      Object treeStruct = serializedview.getStructure();
76      Object compStates = serializedview.getState();
77  
78      if (treeStruct != null) {
79        if (treeStruct instanceof String) {
80          responseWriter.startElement(HtmlElements.INPUT, null);
81          responseWriter.writeAttribute(HtmlAttributes.TYPE, HtmlInputTypes.HIDDEN, null);
82          responseWriter.writeAttribute(HtmlAttributes.NAME, TREE_PARAM, null);
83          responseWriter.writeAttribute(HtmlAttributes.ID, TREE_PARAM, null);
84          responseWriter.writeAttribute(HtmlAttributes.VALUE, treeStruct, null);
85          responseWriter.endElement(HtmlElements.INPUT);
86        }
87      } else {
88        if (LOG.isDebugEnabled()) {
89          LOG.debug("No tree structure to be saved in client response!");
90        }
91      }
92  
93      responseWriter.startElement(HtmlElements.INPUT, null);
94      responseWriter.writeAttribute(HtmlAttributes.TYPE, HtmlInputTypes.HIDDEN, null);
95      responseWriter.writeAttribute(HtmlAttributes.NAME, VIEW_STATE_PARAM, null);
96      responseWriter.writeAttribute(HtmlAttributes.ID, VIEW_STATE_PARAM, null);
97      if (compStates != null) {
98        if (compStates instanceof String) {
99          responseWriter.writeAttribute(HtmlAttributes.VALUE, compStates, null);
100       } else {
101         LOG.error("No component states written in response! Unknown instance of " + compStates.getClass().getName());
102       }
103     } else {
104       responseWriter.writeAttribute(HtmlAttributes.VALUE, "", null);
105       if (LOG.isDebugEnabled()) {
106         LOG.debug("No component states to be saved in client response!");
107       }
108     }
109     responseWriter.endElement(HtmlElements.INPUT);
110 
111     responseWriter.startElement(HtmlElements.INPUT, null);
112     responseWriter.writeAttribute(HtmlAttributes.TYPE, HtmlInputTypes.HIDDEN, null);
113     responseWriter.writeAttribute(HtmlAttributes.NAME, VIEWID_PARAM, null);
114     responseWriter.writeAttribute(HtmlAttributes.ID, VIEWID_PARAM, null);
115     responseWriter.writeAttribute(HtmlAttributes.VALUE, facesContext.getViewRoot().getViewId(), null);
116     responseWriter.endElement(HtmlElements.INPUT);
117   }
118 
119 }