org.apache.tapestry.services.impl
Class JSONResponseBuilder

java.lang.Object
  extended by org.apache.tapestry.services.impl.JSONResponseBuilder
All Implemented Interfaces:
ResponseBuilder

public class JSONResponseBuilder
extends Object
implements ResponseBuilder

Class that implements JSON responses in tapestry.

Author:
jkuhnert
See Also:
json.org

Field Summary
protected  RequestLocaleManager _localeManager
           
protected  MarkupWriterSource _markupWriterSource
           
protected  IMarkupWriter _nullWriter
          Passed in to bypass normal rendering.
protected  List _parts
          Parts that will be updated.
protected  WebResponse _webResponse
           
protected  IJSONWriter _writer
          Writer that creates JSON output response.
 
Fields inherited from interface org.apache.tapestry.services.ResponseBuilder
BODY_SCRIPT, CONTENT_TYPE, ELEMENT_TYPE, ENCODING_KEY, EXCEPTION_TYPE, INCLUDE_SCRIPT, INITIALIZATION_SCRIPT, SCRIPT_TYPE
 
Constructor Summary
JSONResponseBuilder(IRequestCycle cycle, RequestLocaleManager localeManager, MarkupWriterSource markupWriterSource, WebResponse webResponse)
          Creates a new response builder with the required services it needs to render the response when renderResponse(IRequestCycle) is called.
 
Method Summary
 void beginBodyScript(IMarkupWriter writer, IRequestCycle cycle)
          Marks the beginning of the core body script.
 boolean contains(IComponent target)
          Determines if the specified component is contained in the responses requested update parts.
 void endBodyScript(IMarkupWriter writer, IRequestCycle cycle)
          Marks the end of the body block being called.
 IMarkupWriter getWriter()
          Returns the IMarkupWriter associated with this response, it may or may not be a NullWriter instance depending on the response type or stage of the render cycle.
 IMarkupWriter getWriter(String id, String type)
          Gets a write that will output its content in a response element with the given id and type.
 boolean isBodyScriptAllowed(IComponent target)
          Determines if the specified component should have its javascript body added to the response.
 boolean isDynamic()
          Implementors that manage content writes dynamically (ie DojoAjaxResponseBuilder) should return true to denote that dynamic behaviour is on for a particular response.
 boolean isExternalScriptAllowed(IComponent target)
          Determines if the specified component should have its javascript external resource scripts added to the response.
 boolean isImageInitializationAllowed(IComponent target)
          Determines if the specified component should have any asset image URL references embedded in the response.
 boolean isInitializationScriptAllowed(IComponent target)
          Determines if the specified component should have its javascript initialization added to the response.
protected  void parseParameters(IRequestCycle cycle)
          Grabs the incoming parameters needed for json responses, most notable the ServiceConstants.UPDATE_PARTS parameter.
 void render(IMarkupWriter writer, IRender render, IRequestCycle cycle)
          Invoked to render a renderable object.
 void renderResponse(IRequestCycle cycle)
          Renders the response to a client.
 void updateComponent(String id)
          If the component identified by the specified id isn't already set to be updated, will add it to the response for updating.
 void writeBodyScript(IMarkupWriter writer, String script, IRequestCycle cycle)
          Called after ResponseBuilder.beginBodyScript(IMarkupWriter, IRequestCycle) to write the containing body script.
 void writeExternalScript(IMarkupWriter normalWriter, String url, IRequestCycle cycle)
          Invoked by PageRenderSupport to write external js package includes.
 void writeImageInitializations(IMarkupWriter writer, String script, String preloadName, IRequestCycle cycle)
          Intended to be written within the confines of the body script, should be invoked once just after ResponseBuilder.beginBodyScript(IMarkupWriter, IRequestCycle) is called to include any image initializations.
 void writeInitializationScript(IMarkupWriter writer, String script)
          Writes any javascript that should only execute after all other items on a page have completed rendering.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_writer

protected IJSONWriter _writer
Writer that creates JSON output response.


_nullWriter

protected IMarkupWriter _nullWriter
Passed in to bypass normal rendering.


_parts

protected List _parts
Parts that will be updated.


_localeManager

protected RequestLocaleManager _localeManager

_markupWriterSource

protected MarkupWriterSource _markupWriterSource

_webResponse

protected WebResponse _webResponse
Constructor Detail

JSONResponseBuilder

public JSONResponseBuilder(IRequestCycle cycle,
                           RequestLocaleManager localeManager,
                           MarkupWriterSource markupWriterSource,
                           WebResponse webResponse)
Creates a new response builder with the required services it needs to render the response when renderResponse(IRequestCycle) is called.

Parameters:
localeManager - Used to set the locale on the response.
markupWriterSource - Creates IJSONWriter instance to be used.
webResponse - Web response for output stream.
Method Detail

isDynamic

public boolean isDynamic()
Implementors that manage content writes dynamically (ie DojoAjaxResponseBuilder) should return true to denote that dynamic behaviour is on for a particular response.

Specified by:
isDynamic in interface ResponseBuilder
Returns:

renderResponse

public void renderResponse(IRequestCycle cycle)
                    throws IOException
Renders the response to a client. Handles transitioning logic for setting up page and associated components for response.

Specified by:
renderResponse in interface ResponseBuilder
Parameters:
cycle - The main request cycle object for this request.
Throws:
IOException

parseParameters

protected void parseParameters(IRequestCycle cycle)
Grabs the incoming parameters needed for json responses, most notable the ServiceConstants.UPDATE_PARTS parameter.

Parameters:
cycle - The request cycle to parse from

render

public void render(IMarkupWriter writer,
                   IRender render,
                   IRequestCycle cycle)
Invoked to render a renderable object. Performs any necessary under the hood type logic involving ajax/json/normal responses, where needed.

Specified by:
render in interface ResponseBuilder
Parameters:
writer - The markup writer to use, this may be ignored or swapped out for a different writer depending on the implementation being used.
render - The renderable object to render
cycle - Render request cycle

updateComponent

public void updateComponent(String id)
If the component identified by the specified id isn't already set to be updated, will add it to the response for updating. (Only applicable in dynamic responses such as XHR/JSON ).

Specified by:
updateComponent in interface ResponseBuilder
Parameters:
id - The IComponent id to update.

contains

public boolean contains(IComponent target)
Determines if the specified component is contained in the responses requested update parts.

Specified by:
contains in interface ResponseBuilder
Parameters:
target - The component to check for.
Returns:
True if the request should capture the components output.

getWriter

public IMarkupWriter getWriter()
Returns the IMarkupWriter associated with this response, it may or may not be a NullWriter instance depending on the response type or stage of the render cycle. (specifically during rewind)

Specified by:
getWriter in interface ResponseBuilder
Returns:
A validly writable markup writer, even if the content is sometimes ignored.

getWriter

public IMarkupWriter getWriter(String id,
                               String type)
Gets a write that will output its content in a response element with the given id and type.

Specified by:
getWriter in interface ResponseBuilder
Parameters:
id - The response element id to give writer.
type - Optional - If specified will give the response element a type attribute.
Returns:
A valid IMarkupWriter instance to write content to.

isBodyScriptAllowed

public boolean isBodyScriptAllowed(IComponent target)
Determines if the specified component should have its javascript body added to the response.

Specified by:
isBodyScriptAllowed in interface ResponseBuilder
Parameters:
target - The component to allow/disallow body script content from.
Returns:
True if the component script should be allowed.

isExternalScriptAllowed

public boolean isExternalScriptAllowed(IComponent target)
Determines if the specified component should have its javascript external resource scripts added to the response.

Specified by:
isExternalScriptAllowed in interface ResponseBuilder
Parameters:
target - The component to check for inclusion/exclusion.
Returns:
True if external scripts from this component should be added to the response.

isInitializationScriptAllowed

public boolean isInitializationScriptAllowed(IComponent target)
Determines if the specified component should have its javascript initialization added to the response.

Specified by:
isInitializationScriptAllowed in interface ResponseBuilder
Parameters:
target - The component to allow/disallow initialization script content from.
Returns:
True if the component script should be allowed.

isImageInitializationAllowed

public boolean isImageInitializationAllowed(IComponent target)
Determines if the specified component should have any asset image URL references embedded in the response.

Specified by:
isImageInitializationAllowed in interface ResponseBuilder
Parameters:
target - The component to allow/disallow image initialization script content from.
Returns:
True if the component script should be allowed.

beginBodyScript

public void beginBodyScript(IMarkupWriter writer,
                            IRequestCycle cycle)
Marks the beginning of the core body script.

Specified by:
beginBodyScript in interface ResponseBuilder
Parameters:
writer - The markup writer to use, this may be ignored or swapped out for a different writer depending on the implementation being used.
cycle - The associated request.

endBodyScript

public void endBodyScript(IMarkupWriter writer,
                          IRequestCycle cycle)
Marks the end of the body block being called. This method will always be called if ResponseBuilder.beginBodyScript(IMarkupWriter, IRequestCycle) was previously called.

Specified by:
endBodyScript in interface ResponseBuilder
Parameters:
writer - The markup writer to use, this may be ignored or swapped out for a different writer depending on the implementation being used.
cycle - The associated request.

writeBodyScript

public void writeBodyScript(IMarkupWriter writer,
                            String script,
                            IRequestCycle cycle)
Called after ResponseBuilder.beginBodyScript(IMarkupWriter, IRequestCycle) to write the containing body script. This method may not be called at all if there is no js body to write into the response.

Specified by:
writeBodyScript in interface ResponseBuilder
Parameters:
writer - The markup writer to use, this may be ignored or swapped out for a different writer depending on the implementation being used.
script - The script to write into the body response.
cycle - The associated request.

writeExternalScript

public void writeExternalScript(IMarkupWriter normalWriter,
                                String url,
                                IRequestCycle cycle)
Invoked by PageRenderSupport to write external js package includes. This method will be invoked for each external script requesting inclusion in the response. These will typically be written out as .

Specified by:
writeExternalScript in interface ResponseBuilder
Parameters:
normalWriter - The markup writer to use, this may be ignored or swapped out for a different writer depending on the implementation being used.
url - The absolute url to the .js package to be included.
cycle - The associated request.

writeImageInitializations

public void writeImageInitializations(IMarkupWriter writer,
                                      String script,
                                      String preloadName,
                                      IRequestCycle cycle)
Intended to be written within the confines of the body script, should be invoked once just after ResponseBuilder.beginBodyScript(IMarkupWriter, IRequestCycle) is called to include any image initializations. This method should only be called if there are actually images that need pre-initialization. Ie in many instances it will not be called at all.

Specified by:
writeImageInitializations in interface ResponseBuilder
Parameters:
writer - The markup writer to use, this may be ignored or swapped out for a different writer depending on the implementation being used.
script - The non null value of the script images to include.
preloadName - The global variable name to give to the preloaded images array.
cycle - The associated request.

writeInitializationScript

public void writeInitializationScript(IMarkupWriter writer,
                                      String script)
Writes any javascript that should only execute after all other items on a page have completed rendering. This is typically implemented via wrapping the executing of the code to some sort of window.onload event, but will vary depending on the implementation of the builder being used. This method will ~only~ be called if there is any queued intialization script to write.

Specified by:
writeInitializationScript in interface ResponseBuilder
Parameters:
writer - The markup writer to use, this may be ignored or swapped out for a different writer depending on the implementation being used.
script - The initialzation script to write.


Copyright © 2006 Apache Software Foundation. All Rights Reserved.