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  public class TobagoResponseStateManager extends ResponseStateManager {
36    private static final Logger LOG = LoggerFactory.getLogger(TobagoResponseStateManager.class);
37  
38    public static final String TREE_PARAM = "jsf_tree";
39    public static final String VIEW_STATE_PARAM = "javax.faces.ViewState";
40    private static final String VIEWID_PARAM = "jsf_viewid";
41  
42    public Object getState(FacesContext context, String viewId) {
43      Object treeStructure = getTreeStructureToRestore(context, viewId);
44      Object componentStateToRestore = getComponentStateToRestore(context);
45      if (treeStructure != null && componentStateToRestore != null) {
46          return new Object[] {treeStructure, componentStateToRestore};
47      }
48      return null;
49    }
50  
51    public Object getTreeStructureToRestore(FacesContext facescontext, String viewId) {
52      Map requestMap = facescontext.getExternalContext().getRequestParameterMap();
53      Object requestViewId = requestMap.get(VIEWID_PARAM);
54      if (requestViewId == null || !requestViewId.equals(viewId)) {
55        //no saved state or state of different viewId
56        return null;
57      }
58  
59      return requestMap.get(TREE_PARAM);
60  
61    }
62  
63    public Object getComponentStateToRestore(FacesContext facesContext) {
64      Map requestMap = facesContext.getExternalContext().getRequestParameterMap();
65      return requestMap.get(VIEW_STATE_PARAM);
66    }
67  
68    public void writeState(FacesContext facesContext,
69        StateManager.SerializedView serializedview) throws IOException {
70      ResponseWriter responseWriter = facesContext.getResponseWriter();
71      Object treeStruct = serializedview.getStructure();
72      Object compStates = serializedview.getState();
73  
74      if (treeStruct != null) {
75        if (treeStruct instanceof String) {
76          responseWriter.startElement(HtmlElements.INPUT, null);
77          responseWriter.writeAttribute(HtmlAttributes.TYPE, HtmlInputTypes.HIDDEN, null);
78          responseWriter.writeAttribute(HtmlAttributes.NAME, TREE_PARAM, null);
79          responseWriter.writeAttribute(HtmlAttributes.ID, TREE_PARAM, null);
80          responseWriter.writeAttribute(HtmlAttributes.VALUE, treeStruct, null);
81          responseWriter.endElement(HtmlElements.INPUT);
82        }
83      } else {
84        if (LOG.isDebugEnabled()) {
85          LOG.debug("No tree structure to be saved in client response!");
86        }
87      }
88  
89      responseWriter.startElement(HtmlElements.INPUT, null);
90      responseWriter.writeAttribute(HtmlAttributes.TYPE, HtmlInputTypes.HIDDEN, null);
91      responseWriter.writeAttribute(HtmlAttributes.NAME, VIEW_STATE_PARAM, null);
92      responseWriter.writeAttribute(HtmlAttributes.ID, VIEW_STATE_PARAM, null);
93      if (compStates != null) {
94        if (compStates instanceof String) {
95          responseWriter.writeAttribute(HtmlAttributes.VALUE, compStates, null);
96        } else {
97          LOG.error("No component states written in response! Unknown instance of " + compStates.getClass().getName());
98        }
99      } else {
100       responseWriter.writeAttribute(HtmlAttributes.VALUE, "", null);
101       if (LOG.isDebugEnabled()) {
102         LOG.debug("No component states to be saved in client response!");
103       }
104     }
105     responseWriter.endElement(HtmlElements.INPUT);
106 
107     responseWriter.startElement(HtmlElements.INPUT, null);
108     responseWriter.writeAttribute(HtmlAttributes.TYPE, HtmlInputTypes.HIDDEN, null);
109     responseWriter.writeAttribute(HtmlAttributes.NAME, VIEWID_PARAM, null);
110     responseWriter.writeAttribute(HtmlAttributes.ID, VIEWID_PARAM, null);
111     responseWriter.writeAttribute(HtmlAttributes.VALUE, facesContext.getViewRoot().getViewId(), null);
112     responseWriter.endElement(HtmlElements.INPUT);
113   }
114 
115 }