1 package org.ajaxanywhere.jsf; 2 3 import org.ajaxanywhere.PreSendHandler; 4 import org.ajaxanywhere.BufferResponseWrapper; 5 import org.ajaxanywhere.AAUtils; 6 7 import javax.servlet.http.HttpServletRequest ; 8 import java.io.IOException ; 9 10 public class MyFacesClientStateSavingPreSendHandler implements PreSendHandler { 11 private static final String TREE_KEY = "<input type=\"hidden\" name=\"jsf_tree_64\" id=\"jsf_tree_64\" value=\""; 12 private static final String STATE_KEY = "<input type=\"hidden\" name=\"jsf_state_64\" id=\"jsf_state_64\" value=\""; 13 private static final String SEQUENCE_KEY = "<input type=\"hidden\" name=\"jsf_sequence\" value=\""; 14 private static final int TREE_KEY_LEN = TREE_KEY.length(); 15 private static final int STATE_KEY_LEN = STATE_KEY.length(); 16 private static final int SEQUENCE_KEY_LEN = SEQUENCE_KEY.length(); 17 private static final String ZONE_NAME = "stateSavingScript"; 18 19 public BufferResponseWrapper handle(HttpServletRequest request, BufferResponseWrapper responseWrapper) { 20 21 String content = responseWrapper.getBuffer(); 22 String tree = null; 23 String state = null; 24 String jsf_sequence = null; 25 int pos1 = content.indexOf(TREE_KEY); 26 if (pos1 != -1) { 27 int pos2 = content.indexOf('"', pos1 + TREE_KEY_LEN); 28 if (pos2 != -1) 29 tree = content.substring(pos1 + TREE_KEY_LEN, pos2); 30 } 31 pos1 = content.indexOf(STATE_KEY); 32 if (pos1 != -1) { 33 int pos2 = content.indexOf('"', pos1 + STATE_KEY_LEN); 34 if (pos2 != -1) 35 state = content.substring(pos1 + STATE_KEY_LEN, pos2); 36 } 37 38 pos1 = content.indexOf(SEQUENCE_KEY); 39 if (pos1 != -1) { 40 int pos2 = content.indexOf('"', pos1 + SEQUENCE_KEY_LEN); 41 if (pos2 != -1) 42 jsf_sequence = content.substring(pos1 + SEQUENCE_KEY_LEN, pos2); 43 } 44 45 try { 46 if (tree != null || state != null || jsf_sequence != null) { 47 responseWrapper.output(AAUtils.getZoneStartDelimiter(ZONE_NAME) + "<script type=\"text/javascript\">\n"); 48 if (state != null) responseWrapper.output(" var state = AjaxAnywhere.findInstance(aaInstanceId).findForm().elements[\"jsf_state_64\"]\n" + 49 " if (state!=null && state.tagName.toLowerCase() == \"input\")\n" + 50 " state.value=\"" + state + "\";" 51 ); 52 if (tree != null) responseWrapper.output(" var tree = AjaxAnywhere.findInstance(aaInstanceId).findForm().elements[\"jsf_tree_64\"]\n" + 53 " if (tree!=null && tree.tagName.toLowerCase() == \"input\")\n" + 54 " tree.value=\"" + tree + "\";" 55 ); 56 if (jsf_sequence != null) responseWrapper.output( 57 " var jsf_sequence = AjaxAnywhere.findInstance(aaInstanceId).findForm().elements[\"jsf_sequence\"]\n" + 58 " if (jsf_sequence!=null && jsf_sequence.tagName.toLowerCase() == \"input\")\n" + 59 " jsf_sequence.value=\"" + jsf_sequence + "\";"); 60 responseWrapper.output("</script>" + AAUtils.getZoneEndDelimiter(ZONE_NAME)); 61 AAUtils.addZonesToRefresh(request, ZONE_NAME); 62 } 63 } catch (IOException e) { 64 e.printStackTrace(); 65 } 66 67 return responseWrapper; 68 } 69 } 70 | Popular Tags |