Broadcast on Broadcast off
The Documentation for Project Zero has moved. Please update your bookmarks to: http://www.projectzero.org/documentation/
Advanced Wiki Search
Table of
Contents...
Hide

Project Zero Developer’s Guide?

Concepts and components
Basic concepts overview
Event processing
Writing Java handlers
Writing Groovy handlers
Firing events
Global Context
Global Context reference
Application directory layout?
Virtualized directories
Assemble
PHP
Features and configuration
Configuration
Debugging?
Dependencies?
Packaging?
Application classpath
Logging and tracing
RESTful resources?
RESTful documentation?
File serving
Response rendering
Validators and validation
HTTP error handling
Calling a remote resource
Using the Connection API
Sending an email using EmailConnection
Configuring destinations
Configuring protocols
Configuring connection handlers
Creating a connection handler
Creating a custom protocol transport
Simple logging connection handlers
Protocol reference
Client programming with Dojo
Runtime options
Deployment modifications
HTTP configuration
SSL configuration
Proxy configuration
Extending the CLI
Security considerations
Authentication
OpenID authentication
Extending security
Security tokens
CSRF prevention support
Extending token support
Leveraging TAI
User service
File based user service
LDAP user service
Extending user service
Security Utilities
Leveraging XOREncoder
Extensions
Atom support
RSS support
JSON support
XMLEncoder
REST to SOAP extension
URIUtils
Developer Web tools?
Database setup tools
Configuring data access
Common query patterns
Advanced query patterns
Update patterns
Local database transactions
Extending data access
Configuration vendor differences
PHP data access
Resource model
Configuring ZRM
Resource model declaration
Programmatic model API
HTTP REST API
A ZRM mini tutorial
Active content filtering support
Default filters
Custom filters
Runtime management
Management commands
Zero socket opener
Other extension modules
Amazon E-commerce service
Flickr service
WeatherZero forecast service
Wikipedia service
Reference
Zero command line interface
JavaDoc - Public API
JavaDoc - Public SPI
JavaDoc - All Classes

 

Response rendering

An HTTP response body can be generated using direct APIs or indirect rendering. In either case, you should set status and headers before writing any output. The following sections of this article provide information about response rendering:

Direct APIs

Application code and event handlers can write directly to the response using OutputStream (/request/outputstream) and PrintWriter (/request/writer) objects.

The following is a Java example:

// Content of a Java class
PrintWriter writer = (PrintWriter) zget("/request/writer");
writer.println("Hello World.");

The default output for all Groovy scripts (including templates) is the response writer. This allows application developers to write directly to the response with the println method, as shown in the following Groovy example:

println "Hello World."

Indirect rendering

Zero provides a library of renderers for common response handling. Invoking a renderer generally involves the following steps:

  1. Set the value of /request/view in the Global Context to the corresponding renderer
  2. Set additional renderer-specific values into the GlobalContext, where appropriate
  3. Invoke zero.core.views.ViewEngine.render()

zero.core includes View, Error, JSON, and XML renderers.

ALERT! NOTE: The JSON and XML renderers use reflection when serializing Objects. Using String, Number, Boolean, or primitive types as keys, values and element names will result in faster rendering.

View renderer

The View renderer serves a file from the app/views virtualized directory. Files are processed in a similar fashion to standard file serving in Zero, meaning executable files are "invoked" and static files are served directly.

To use this renderer, set /request/view to the file path relative to app/views.

The following is a Groovy example, specifying app/views/x/hello.gt as the view script:

request.view="x/hello.gt"
render()

The following is the equivalent PHP example, specifying app/views/x/hello.php as the view script:

<?php
zput('/request/view', 'x/hello.php');
render_view();
?>

Error renderer

The Error renderer is tailored for error reporting and uses scripts from the app/errors virtualized directory. zero.core includes a set of basic error pages; zero.core.webtools includes error pages that are more descriptive for developers.

To use this renderer, set /request/view to "error" and set /request/error/page to the path to the script relative to app/errors.

Groovy example:

request.view = "error"
request.error.page = "errorError"
render()

See the HTTP error handling documentation for more details.

JSON renderer

The JSON renderer serializes Java objects to the outputstream as JSON representations. Details about this serialization may be found on the JSON support documentation.

To use this renderer, set /request/view to "JSON" and set /request/json/output to the object to be serialized. The renderer will set the Content-Type response header to application/json, if not already set.

Groovy example:

Customer customer = new Customer();
customer.name = "John Smith"

request.view="JSON"
request.json.output=customer
render()

JSON rendering can be performed in PHP in one of two ways :

a) The following code sample shows how this can done using the ViewEngine

<?php
$customer = array('name' => 'John Smith');

zput('/request/view', 'JSON');
zput('/request/json/output', $customer);
render_view();
?>

b) Alternately, you can use the JSON extension to render the data directly

<?php

$customer = array('name' => 'John Smith');
echo json_encode($customer);

?>

XML renderer

The Extensible Markup Language (XML) renderer serializes Java objects to the outputstream as XML.

To use this renderer, set /request/view to "XML" and set /request/xml/output to the object to be serialized. It should be noted that if the object to be serialized is a subtype of org.w3c.dom.Node interface (such as Document or Element), the resulting XML will be the contents of the Node object (which is what you want), not a XML rendering of the Node Java object.

You can optionally set /request/xml/rootElement to the name of the XML tag that will form the root element of the produced XML document. If the /request/xml/rootElement value is not specified then the lower cased simple name of the Java Object will be used as the root element name.

You can optionally set /request/xml/stylesheet to the name of an XSL stylesheet located in the app/views folder of the project (or its dependencies). This will cause the stylesheet to be applied to the resulting XML document and the XSL output to be sent to the client. The value of the /request/xml/stylesheet attribute is a String relative path to the XSL document within the app/views folder.

The renderer will set the Content-Type response header to application/xml if not already set. Be careful when using the stylesheet attribute to set the Content-Type to something appropriate to the XSL output as the value of application/xml may be incorrect.

Groovy example (Object to XML):

Customer customer = new Customer();
customer.name = "John Smith"

request.view="XML"
request.xml.output=customer
// the root element name is optional
request.xml.rootElement='customer'
render()

Expected output:

<?xml version="1.0" encoding="UTF-8"?>
  <customer>
    <name>John Smith</name>
  </customer>

Groovy example (Using a XSL stylesheet):

Customer customer = new Customer();
customer.name = "John Smith"

request.view="XML"
request.xml.output=customer
// the root element name is optional
request.xml.rootElement='customer'
request.xml.stylesheet='viewCustomer.xsl'
render()

Java Example (Object to XML):

Customer customer = new Customer();
customer.setName("John Smith");

zput("/request/view","XML");
zput("/request/xml/output", customer);
zput("/request/xml/rootElement","customer");
zero.core.views.ViewEngine.render();

Also, if you may have circular references in your resulting XML document, you may want to consider setting /request/xml/idRefs to true. This will set the idref attribute to the respective id value. If a circular reference is detected and /request/xml/idRefs is null or set to anything other than true, then the idref value will be set to CIRCULAR_RFERENCE.

Groovy example (Object to XML):

Customer customer = new Customer();
customer.name = "John Smith"

request.view="XML"
request.xml.output=customer
// the root element name is optional
request.xml.rootElement='customer'
request.xml.idRefs = true
render()

Expected output:

<?xml version="1.0" encoding="UTF-8"?>
  <customer id="1">
    <name id="2">John Smith</name>
  </customer>


HELP Advanced Collection and Array item elements will contain an index attribute.

XML rendering can be performed in PHP in one of two ways:

a) The following code sample shows how this can done using the ViewEngine

<?php
$customer = array('name' => 'John Smith');

zput('/request/view', 'XML');
zput('/request/xml/output', $customer);
zput('/request/xml/rootElement', 'customer');
zput('/request/xml/idRefs', false);
render_view();
?>

b) Alternately, you can use the XML extension to render the data directly

<?php

$customer = array('name' => 'John Smith');
echo xml_encode($customer, false, 'customer');

?>

r24 - 14 Dec 2007 - 19:13:03 - madhu
Syndicate this site RSS ATOM
Copyright 2007 © IBM Corporation | Privacy | Terms of Use | About this site