Broadcast on Broadcast off
The Documentation for Project Zero has moved. Please update your bookmarks to: http://www.projectzero.org/documentation/
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

 

URIUtils

URIUtils is a set of APIs for constructing relative and absolute URIs within a Zero application. These APIs are helpful when constructing URIs for many purposes, including hyperlinks, images, and CSS links.

Why page-relative URIs are recommended

In general, page-relative URIs are recommended since they are independent of reverse-proxy URI mappings and mash-up scenarios. However, one must take care in constructing these URIs.

A "broken" page

Consider a simple illustration with greeting.gt, which returns an HTML page containing three images:

<!-- greeting.groovy:  Version 1 -->
<html><body>
Hello world.
<img src="http://host/images/absolute.jpg">
<img src="/images/root.jpg">
<img src="images/page.jpg">
</body></html>

Now look at what happens when the page is requested under three different scenarios:

Scenario Client request Client tries to access images at...
No context root;
exact request
http://host/greeting.gt http://host/images/absolute.jpg
http://host/images/root.jpg
http://host/images/page.jpg
No context root;
request with path info
http://host/greeting.gt/more http://host/images/absolute.jpg
http://host/images/root.jpg
http://host/greeting.gt/images/page.jpg
Context root (/sample) http://host/sample/greeting.gt http://host/images/absolute.jpg
http://host/images/root.jpg

http://host/sample/images/page.jpg

The first scenario works fine, but the other two scenarios fail with "broken" image references.

In the second scenario, the page-relative link (page.jpg) fails because the user agent resolves it with the request URI, including path info.

In the final scenario, the absolute URI (absolute.jpg) and root-relative URI (root.jpg) fail because they are sensitive to changes in deployment.

A robust page

A more robust page will be one that uses page-relative links that are adjusted for path info. URIUtils provides a means to construct these links with the getRelativeUri() API:

<!-- greeting.groovy:  Version 2 -->
<html><body>
Hello world.
<img src="<%= getRelativeUri("/images/absolute.jpg") %>">
<img src="<%= getRelativeUri("/images/root.jpg") %>">
<img src="<%= getRelativeUri("images/page.jpg") %>">
</body></html>

Note that the leading slash in the argument to getRelativeUri() is significant:

  • With a leading slash, the argument is considered relative to the application root without Zero's context root. You could think of this as specifying a URI starting from the public folder of your application.
  • Without a leading slash, the argument is relative to the calling script.

API reference

URIUtils includes the following utility APIs:

public static String getRelativeUri(String path) { ... }
public static String getRelativeUri(String path, Map<String,String> queryParms) { ... }
public static String getRelativeUri(String path, Map<String,String> queryParms, String fragment) { ... }
public static String getAbsoluteUri(String path) { ... }
public static String getAbsoluteUri(String path, Map<String,String> queryParms) { ... }
public static String getAbsoluteUri(String path, Map<String,String> queryParms, String fragment) { ... }
public static String getRequestedUri(boolean includeQueryString) { ... }

Usage details may be found in the Javadoc.

r5 - 26 Oct 2007 - 13:52:45 - steveims
Syndicate this site RSS ATOM
Copyright 2007 © IBM Corporation | Privacy | Terms of Use | About this site