dev@jsf-extensions.java.net

Re: [JSF-EXT] writing to the xjson object server-side

From: Ed Burns <ed.burns_at_sun.com>
Date: Tue, 31 Oct 2006 13:02:14 -0800

>>>>> On Mon, 30 Oct 2006 16:46:51 -0500, Jeff Bond <Jeff.Bond_at_cnet.com> said:

JB> Hi,
JB> Per the subject line, I'm having trouble writing to the xjson object
JB> that DynaFaces sends up to the server via the "X-JSON" header. I'm able
JB> to access it within a ValueChangeListener fine, getting the
JB> RequestHeaderMap from ExternalContext, then calling
JB> .get(AsyncResponse.XJSON_HEADER). I can then convert that String into a
JB> JSONObject and manipulate it all I want, but cannot "put it back" in the
JB> header, as the RequestHeaderMap is immutable. I tried getting the
JB> FacesContext and setting the "X-JSON" header to the JSONObject's string
JB> value, but this gets overwritten as soon as PartialTraversalViewRoot
JB> begins rendering the view:

JB> HttpServletResponse servletResponse = (HttpServletResponse)

JB> extContext.getResponse();

JB> servletResponse.setContentType("text/xml");

JB> servletResponse.setHeader("Cache-Control", "no-cache");

JB> String xjson =

JB> extContext.getRequestHeaderMap().get(AsyncResponse.XJSON_HEADER);

JB> if (null != xjson) {

JB> servletResponse.setHeader(AsyncResponse.XJSON_HEADER,

JB> xjson);

JB> }

JB> writer.startElement("partial-response", this);

JB> writer.startElement("components", this);

 
JB> As you can see here in bold, the xjson object is read directly from the
JB> (immutable) header map, then immediately placed into the
JB> servletResponse. So the question is, where do you have the opportunity
JB> to alter the xjson string? I'm not able to do it in a
JB> ValueChangeListener because it's fired before the Rendering phase. I
JB> haven't tried writing to it during the render or post-render phase b/c
JB> that requires messing with the actual component code (or writing a phase
JB> listener), and I'm not even 100% sure that once you've begun writing to
JB> the body of a response, you can change the headers at all.

JB> Has anyone had success changing the xjson object on the server? If so,
JB> where did you do it?

Thanks for catching this Jeff, you're right, it's currently impossible
to mutate the XJSON_HEADER so that the mutated value is returned to the
client.

How about this:

If there is a value in the request scope under the key
AsyncResponse.XJSON_HEADER, that value is assumed to be intended for the
XJSON_HEADER. If no such value exists, the value is copied directly
from the request headers.

Ed