Author: alexsmirnov
Date: 2008-07-25 19:06:19 -0400 (Fri, 25 Jul 2008)
New Revision: 9792
Modified:
trunk/framework/impl/src/main/java/org/ajax4jsf/application/AjaxStateManager.java
Log:
Capture writed view-state param into request-scope variable.
Modified:
trunk/framework/impl/src/main/java/org/ajax4jsf/application/AjaxStateManager.java
===================================================================
---
trunk/framework/impl/src/main/java/org/ajax4jsf/application/AjaxStateManager.java 2008-07-25
15:50:52 UTC (rev 9791)
+++
trunk/framework/impl/src/main/java/org/ajax4jsf/application/AjaxStateManager.java 2008-07-25
23:06:19 UTC (rev 9792)
@@ -22,12 +22,15 @@
package org.ajax4jsf.application;
import java.io.IOException;
+import java.io.StringWriter;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import javax.faces.FacesException;
import javax.faces.FactoryFinder;
@@ -37,6 +40,7 @@
import javax.faces.component.UIViewRoot;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
+import javax.faces.context.ResponseWriter;
import javax.faces.render.RenderKit;
import javax.faces.render.RenderKitFactory;
import javax.faces.render.ResponseStateManager;
@@ -53,6 +57,8 @@
public class AjaxStateManager extends StateManager {
+ public static final String CAPTURED_VIEW_STATE =
"org.ajax4jsf.captured_view_state";
+
private final class SeamStateManagerWrapper extends StateManager {
protected Object getComponentStateToSave(FacesContext arg0) {
// do nothing
@@ -217,7 +223,7 @@
// Myfaces
https://issues.apache.org/jira/browse/MYFACES-1753 hack.
stateArray=new Object[]{getLogicalViewId(context),null};
}
- responseStateManager.writeState(context, stateArray);
+ writeState(context, responseStateManager, stateArray);
if (_log.isDebugEnabled()) {
_log.debug("Write view state to the response");
}
@@ -245,6 +251,51 @@
}
}
+ private void writeState(FacesContext context,
+ ResponseStateManager responseStateManager, Object[] stateArray)
+ throws IOException {
+ // Capture writed state into string.
+ ResponseWriter originalWriter = context.getResponseWriter();
+ StringWriter buff = new StringWriter(128);
+ try {
+ ResponseWriter stateResponseWriter = originalWriter
+ .cloneWithWriter(buff);
+ context.setResponseWriter(stateResponseWriter);
+ responseStateManager.writeState(context, stateArray);
+ stateResponseWriter.flush();
+ String stateString = buff.toString();
+ originalWriter.write(stateString);
+ String stateValue = getStateValue(stateString);
+ context.getExternalContext().getRequestMap().put(CAPTURED_VIEW_STATE, stateValue);
+ if (null != stateValue) {
+ } else {
+ }
+ } finally {
+ context.setResponseWriter(originalWriter);
+ }
+ }
+
+ private static final Pattern PATTERN =
Pattern.compile(".*<input.*(?:\\svalue=[\"\'](.*)[\"\']\\s).*name=[\"']"+ResponseStateManager.VIEW_STATE_PARAM+"[\"'].*>");
+
+ private static final Pattern PATTERN2 = Pattern.compile(".*<input
.*name=[\"']"+ResponseStateManager.VIEW_STATE_PARAM+"[\"'].*(?:\\svalue=[\"\'](.*)[\"\']\\s).*>");
+
+
+ /**
+ * Parse content of the writed viewState hidden input field for a state value.
+ * @param input
+ * @return
+ */
+ private String getStateValue(String input) {
+ Matcher matcher = PATTERN.matcher(input);
+ if(!matcher.matches()){
+ matcher = PATTERN2.matcher(input);
+ if(!matcher.matches()){
+ return null;
+ }
+ }
+ return matcher.group(1);
+}
+
/*
* (non-Javadoc)
*
@@ -265,7 +316,7 @@
stateArray = new Object[] {
state.getStructure(),state.getState() };
}
- responseStateManager.writeState(context, stateArray);
+ writeState(context, responseStateManager, stateArray);
if (_log.isDebugEnabled()) {
_log.debug("Write view state to the response");
}