URIUtils APIs
URIUtils is a set of APIs for constructing relative and absolute URIs within an IBM® WebSphere® sMash 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 withgreeting.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>
| 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 |
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 thegetRelativeUri() 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>
getRelativeUri() is significant: - With a leading slash, the argument is considered relative to the application root without WebSphere sMash's context root. You could think of this as specifying a URI starting from the
publicfolder 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) { ... }