KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > renderkit > html > HtmlResponseStateManager


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.myfaces.renderkit.html;
17
18 import org.apache.myfaces.renderkit.MyfacesResponseStateManager;
19 import org.apache.myfaces.util.MyFacesObjectInputStream;
20
21 import org.apache.commons.codec.binary.Base64;
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24
25 import javax.faces.FacesException;
26 import javax.faces.application.StateManager;
27 import javax.faces.context.FacesContext;
28 import javax.faces.context.ResponseWriter;
29 import java.io.*;
30 import java.net.URLEncoder JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.zip.GZIPInputStream JavaDoc;
33 import java.util.zip.GZIPOutputStream JavaDoc;
34
35 /**
36  * @author Manfred Geiler (latest modification by $Author: matze $)
37  * @version $Revision: 1.6 $ $Date: 2004/10/13 11:51:00 $
38  * $Log: HtmlResponseStateManager.java,v $
39  * Revision 1.6 2004/10/13 11:51:00 matze
40  * renamed packages to org.apache
41  *
42  * Revision 1.5 2004/07/01 22:05:07 mwessendorf
43  * ASF switch
44  *
45  * Revision 1.4 2004/05/12 01:41:31 o_rossmueller
46  * fix #951896: added state params to link URLs for ALLOW_JAVASCRIPT=false
47  *
48  * Revision 1.3 2004/04/13 08:57:00 manolito
49  * commons codec
50  *
51  * Revision 1.3 2004/04/09 21:13:10 Sylvain Vieujot
52  * Replace oreilly's Base64 encoder and decoder with Jakarta Commons Codec
53  *
54  * Revision 1.2 2004/04/06 10:20:26 manolito
55  * no state restoring for different viewId
56  *
57  */

58 public class HtmlResponseStateManager
59         extends MyfacesResponseStateManager
60 {
61     private static final Log log = LogFactory.getLog(HtmlResponseStateManager.class);
62
63     private static final String JavaDoc TREE_PARAM = "jsf_tree";
64     private static final String JavaDoc STATE_PARAM = "jsf_state";
65     private static final String JavaDoc VIEWID_PARAM = "jsf_viewid";
66     private static final String JavaDoc BASE64_TREE_PARAM = "jsf_tree_64";
67     private static final String JavaDoc BASE64_STATE_PARAM = "jsf_state_64";
68     private static final String JavaDoc ZIP_CHARSET = "ISO-8859-1";
69
70     public void writeState(FacesContext facescontext,
71                            StateManager.SerializedView serializedview) throws IOException
72     {
73         ResponseWriter responseWriter = facescontext.getResponseWriter();
74         Object JavaDoc treeStruct = serializedview.getStructure();
75         Object JavaDoc compStates = serializedview.getState();
76
77         if (treeStruct != null)
78         {
79             if (treeStruct instanceof String JavaDoc)
80             {
81                 responseWriter.startElement(HTML.INPUT_ELEM, null);
82                 responseWriter.writeAttribute(HTML.TYPE_ATTR, HTML.INPUT_TYPE_HIDDEN, null);
83                 responseWriter.writeAttribute(HTML.NAME_ATTR, TREE_PARAM, null);
84                 responseWriter.writeAttribute(HTML.VALUE_ATTR, treeStruct, null);
85                 responseWriter.endElement(HTML.INPUT_ELEM);
86             }
87             else
88             {
89                 responseWriter.startElement(HTML.INPUT_ELEM, null);
90                 responseWriter.writeAttribute(HTML.TYPE_ATTR, HTML.INPUT_TYPE_HIDDEN, null);
91                 responseWriter.writeAttribute(HTML.NAME_ATTR, BASE64_TREE_PARAM, null);
92                 responseWriter.writeAttribute(HTML.VALUE_ATTR, encode64(treeStruct), null);
93                 responseWriter.endElement(HTML.INPUT_ELEM);
94             }
95         }
96         else
97         {
98             log.error("No tree structure to be saved in client response!");
99         }
100
101         if (compStates != null)
102         {
103             if (compStates instanceof String JavaDoc)
104             {
105                 responseWriter.startElement(HTML.INPUT_ELEM, null);
106                 responseWriter.writeAttribute(HTML.TYPE_ATTR, HTML.INPUT_TYPE_HIDDEN, null);
107                 responseWriter.writeAttribute(HTML.NAME_ATTR, STATE_PARAM, null);
108                 responseWriter.writeAttribute(HTML.VALUE_ATTR, compStates, null);
109                 responseWriter.endElement(HTML.INPUT_ELEM);
110             }
111             else
112             {
113                 responseWriter.startElement(HTML.INPUT_ELEM, null);
114                 responseWriter.writeAttribute(HTML.TYPE_ATTR, HTML.INPUT_TYPE_HIDDEN, null);
115                 responseWriter.writeAttribute(HTML.NAME_ATTR, BASE64_STATE_PARAM, null);
116                 responseWriter.writeAttribute(HTML.VALUE_ATTR, encode64(compStates), null);
117                 responseWriter.endElement(HTML.INPUT_ELEM);
118             }
119         }
120         else
121         {
122             log.error("No component states to be saved in client response!");
123         }
124
125         responseWriter.startElement(HTML.INPUT_ELEM, null);
126         responseWriter.writeAttribute(HTML.TYPE_ATTR, HTML.INPUT_TYPE_HIDDEN, null);
127         responseWriter.writeAttribute(HTML.NAME_ATTR, VIEWID_PARAM, null);
128         responseWriter.writeAttribute(HTML.VALUE_ATTR, facescontext.getViewRoot().getViewId(), null);
129         responseWriter.endElement(HTML.INPUT_ELEM);
130     }
131
132     /**
133      * MyFaces extension
134      * @param facescontext
135      * @param serializedview
136      * @throws IOException
137      */

138     public void writeStateAsUrlParams(FacesContext facescontext,
139                                       StateManager.SerializedView serializedview) throws IOException
140     {
141         ResponseWriter responseWriter = facescontext.getResponseWriter();
142         Object JavaDoc treeStruct = serializedview.getStructure();
143         Object JavaDoc compStates = serializedview.getState();
144
145         if (treeStruct != null)
146         {
147             if (treeStruct instanceof String JavaDoc)
148             {
149                 writeStateParam(responseWriter, TREE_PARAM, (String JavaDoc)treeStruct);
150             }
151             else
152             {
153                 writeStateParam(responseWriter, BASE64_TREE_PARAM, encode64(treeStruct));
154             }
155         }
156         else
157         {
158             log.error("No tree structure to be saved in client response!");
159         }
160
161         if (compStates != null)
162         {
163             if (treeStruct != null)
164             {
165                 responseWriter.write('&');
166             }
167             if (compStates instanceof String JavaDoc)
168             {
169                 writeStateParam(responseWriter, STATE_PARAM, (String JavaDoc)compStates);
170             }
171             else
172             {
173                 writeStateParam(responseWriter, BASE64_STATE_PARAM, encode64(compStates));
174             }
175         }
176         else
177         {
178             log.error("No component states to be saved in client response!");
179         }
180
181         if (treeStruct != null || compStates != null) {
182             responseWriter.write('&');
183         }
184         writeStateParam(responseWriter, VIEWID_PARAM, facescontext.getViewRoot().getViewId());
185     }
186
187
188     private String JavaDoc encode64(Object JavaDoc obj)
189     {
190         try
191         {
192             ByteArrayOutputStream baos = new ByteArrayOutputStream();
193             OutputStream JavaDoc zos = new GZIPOutputStream JavaDoc(baos);
194             ObjectOutputStream oos = new ObjectOutputStream(zos);
195             oos.writeObject(obj);
196             oos.close();
197             zos.close();
198             baos.close();
199             Base64 base64Codec = new Base64();
200             return new String JavaDoc(base64Codec.encode( baos.toByteArray() ), ZIP_CHARSET);
201         }
202         catch (IOException e)
203         {
204             log.fatal("Cannot encode Object with Base64", e);
205             throw new FacesException(e);
206         }
207     }
208
209
210     public Object JavaDoc getTreeStructureToRestore(FacesContext facescontext, String JavaDoc viewId)
211     {
212         Map JavaDoc reqParamMap = facescontext.getExternalContext().getRequestParameterMap();
213         Object JavaDoc param = reqParamMap.get(VIEWID_PARAM);
214         if (param == null || !param.equals(viewId))
215         {
216             //no saved state or state of different viewId
217
return null;
218         }
219
220         param = reqParamMap.get(TREE_PARAM);
221         if (param != null)
222         {
223             return param;
224         }
225
226         param = reqParamMap.get(BASE64_TREE_PARAM);
227         if (param != null)
228         {
229             return decode64((String JavaDoc)param);
230         }
231
232         return null;
233     }
234
235     public Object JavaDoc getComponentStateToRestore(FacesContext facescontext)
236     {
237         Map JavaDoc reqParamMap = facescontext.getExternalContext().getRequestParameterMap();
238         Object JavaDoc param = reqParamMap.get(STATE_PARAM);
239         if (param != null)
240         {
241             return param;
242         }
243
244         param = reqParamMap.get(BASE64_STATE_PARAM);
245         if (param != null)
246         {
247             return decode64((String JavaDoc)param);
248         }
249
250         return null;
251     }
252
253     private Object JavaDoc decode64(String JavaDoc s)
254     {
255         try
256         {
257             Base64 base64Codec = new Base64();
258             ByteArrayInputStream decodedStream = new ByteArrayInputStream( base64Codec.decode( s.getBytes(ZIP_CHARSET) ) );
259             InputStream unzippedStream = new GZIPInputStream JavaDoc(decodedStream);
260             ObjectInputStream ois = new MyFacesObjectInputStream(unzippedStream);
261             Object JavaDoc obj = ois.readObject();
262             ois.close();
263             unzippedStream.close();
264             decodedStream.close();
265             return obj;
266         }
267         catch (IOException e)
268         {
269             log.fatal("Cannot decode Object from Base64 String", e);
270             throw new FacesException(e);
271         }
272         catch (ClassNotFoundException JavaDoc e)
273         {
274             log.fatal("Cannot decode Object from Base64 String", e);
275             throw new FacesException(e);
276         }
277     }
278
279
280
281     private void writeStateParam(ResponseWriter writer, String JavaDoc name, String JavaDoc value)
282         throws IOException
283     {
284         writer.write(name);
285         writer.write('=');
286         writer.write(URLEncoder.encode(value, writer.getCharacterEncoding()));
287     }
288
289
290 }
291
292
Popular Tags