1 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 ; 31 import java.util.Map ; 32 import java.util.zip.GZIPInputStream ; 33 import java.util.zip.GZIPOutputStream ; 34 35 58 public class HtmlResponseStateManager 59 extends MyfacesResponseStateManager 60 { 61 private static final Log log = LogFactory.getLog(HtmlResponseStateManager.class); 62 63 private static final String TREE_PARAM = "jsf_tree"; 64 private static final String STATE_PARAM = "jsf_state"; 65 private static final String VIEWID_PARAM = "jsf_viewid"; 66 private static final String BASE64_TREE_PARAM = "jsf_tree_64"; 67 private static final String BASE64_STATE_PARAM = "jsf_state_64"; 68 private static final String 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 treeStruct = serializedview.getStructure(); 75 Object compStates = serializedview.getState(); 76 77 if (treeStruct != null) 78 { 79 if (treeStruct instanceof String ) 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 ) 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 138 public void writeStateAsUrlParams(FacesContext facescontext, 139 StateManager.SerializedView serializedview) throws IOException 140 { 141 ResponseWriter responseWriter = facescontext.getResponseWriter(); 142 Object treeStruct = serializedview.getStructure(); 143 Object compStates = serializedview.getState(); 144 145 if (treeStruct != null) 146 { 147 if (treeStruct instanceof String ) 148 { 149 writeStateParam(responseWriter, TREE_PARAM, (String )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 ) 168 { 169 writeStateParam(responseWriter, STATE_PARAM, (String )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 encode64(Object obj) 189 { 190 try 191 { 192 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 193 OutputStream zos = new GZIPOutputStream (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 (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 getTreeStructureToRestore(FacesContext facescontext, String viewId) 211 { 212 Map reqParamMap = facescontext.getExternalContext().getRequestParameterMap(); 213 Object param = reqParamMap.get(VIEWID_PARAM); 214 if (param == null || !param.equals(viewId)) 215 { 216 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 )param); 230 } 231 232 return null; 233 } 234 235 public Object getComponentStateToRestore(FacesContext facescontext) 236 { 237 Map reqParamMap = facescontext.getExternalContext().getRequestParameterMap(); 238 Object 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 )param); 248 } 249 250 return null; 251 } 252 253 private Object decode64(String 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 (decodedStream); 260 ObjectInputStream ois = new MyFacesObjectInputStream(unzippedStream); 261 Object 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 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 name, String 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 |