dev@jsf-extensions.java.net

[JSF-EXT] Proposal for serverside handling of xjson

From: Norbert von Truchsess <norbert.truchsess_at_t-online.de>
Date: Sat, 03 Mar 2007 23:00:27 +0100

modification of xjson-header on serverside now can only be archived by
direct manipulation of HttpServletResponse headers. This is problematic
if several independent components try to access and modify this header
(there is no getter-method to retrive previously set headers from
HttpServletResponse and reading the original header from
RequestHeaderMap does not give information whether this header was
previously modified or not).

To ease modification of the X-JSON header on the serverside I propose to
parse the header into an JSONObject (see
http://www.json.org/java/index.html) and store this in
RequestParameterMap. This way the structure is easily accessible (and
modifiable) by any component interested.

see attached files.

regards,

Norbert von Truchsess


Index: C:/localdata/projects/JSF/jsf-extensions-HEAD/code/run-time/avatar/src/main/java/com/sun/faces/extensions/avatar/lifecycle/AsyncResponse.java
===================================================================
--- C:/localdata/projects/JSF/jsf-extensions-HEAD/code/run-time/avatar/src/main/java/com/sun/faces/extensions/avatar/lifecycle/AsyncResponse.java (revision 413)
+++ C:/localdata/projects/JSF/jsf-extensions-HEAD/code/run-time/avatar/src/main/java/com/sun/faces/extensions/avatar/lifecycle/AsyncResponse.java (working copy)
@@ -485,6 +485,7 @@
     private static final String RENDER_ALL = FACES_PREFIX + "RenderAll";
     
     public static final String FACES_EVENT_CONTEXT_PARAM = "com.sun.faces.extensions.avatar.FacesEvents";
+ public static final String XJSON_CONTEXT_PARAM = "com.sun.faces.extensions.avatar.XJSON";
     
      private static class StateCapture extends ResponseWriterWrapper {
         
Index: C:/localdata/projects/JSF/jsf-extensions-HEAD/code/run-time/avatar/src/main/java/com/sun/faces/extensions/avatar/components/PartialTraversalViewRootHelper.java
===================================================================
--- C:/localdata/projects/JSF/jsf-extensions-HEAD/code/run-time/avatar/src/main/java/com/sun/faces/extensions/avatar/components/PartialTraversalViewRootHelper.java (revision 413)
+++ C:/localdata/projects/JSF/jsf-extensions-HEAD/code/run-time/avatar/src/main/java/com/sun/faces/extensions/avatar/components/PartialTraversalViewRootHelper.java (working copy)
@@ -51,6 +51,8 @@
 import javax.faces.convert.ConverterException;
 import javax.faces.event.PhaseId;
 import javax.servlet.http.HttpServletResponse;
+import org.json.JSONObject;
+import org.json.JSONException;
 
 /**
  *
@@ -448,8 +450,15 @@
                 String xjson =
                         extContext.getRequestHeaderMap().get(AsyncResponse.XJSON_HEADER);
                 if (null != xjson) {
- servletResponse.setHeader(AsyncResponse.XJSON_HEADER,
- xjson);
+ JSONObject xjsonObject = null;
+ try {
+ xjsonObject = new JSONObject(xjson);
+ extContext.getRequestMap().put(
+ AsyncResponse.XJSON_CONTEXT_PARAM,
+ xjsonObject);
+ } catch (JSONException je) {
+ throw new IOException(je.toString());
+ }
                 }
 
                 UIComponent root = (UIComponent) getUIViewRoot();
@@ -475,12 +484,22 @@
         if (!async.isRenderNone()) {
             writer.endElement("components");
         }
-
+ ExternalContext extContext = context.getExternalContext();
+ JSONObject xjsonObject = (JSONObject) extContext.getRequestMap().get(
+ AsyncResponse.XJSON_CONTEXT_PARAM);
+ if (xjsonObject != null) {
+ if (extContext.getResponse() instanceof HttpServletResponse) {
+ HttpServletResponse servletResponse = (HttpServletResponse) extContext
+ .getResponse();
+ servletResponse.setHeader(AsyncResponse.XJSON_HEADER,
+ xjsonObject.toString());
+ }
+ }
     }
 
     /**
- *
- */
+ *
+ */
     protected void broadcastEvents(FacesContext ctx, PhaseId phaseId) {
         String preViewId = ctx.getViewRoot().getViewId();