Accessing global context

The global context is available from all parts of a IBM® WebSphere® sMash application. How you access the global context varies depending on the language of the artifact.

Global context APIs

Values in the global context are addressed with a key reference, which has the following syntax: /<zone>/<path>[#<value_path>] .

The global context APIs are summarized with examples based on the following global context contents:

/config/foo/a = b
/config/foo/x = y
/config/map = ["a" : "b", "x" : "y"]
Syntax Comments and examples
T zget(String uri [, T defaultValue]) Returns the value stored at uri. If none, then null or defaultValue is returned.
zget("/config/foo/a") == "b" zget("/config/foo") == null
boolean zput(String uri, V value) Create and replace the stored value with value. Elements of a stored value can be replaced with Value pathing appended to the uri.
boolean zpost(String uri, V value) Create and append value to the stored value. Appends are handled by a TypeHandler. Details are summarized in the Value pathing section.
boolean zdelete(String uri [, boolean deleteChildren]) Deletes the specified key, and optionally removes all keys that start with uri.
boolean zcontains(String uri) Indicates whether a value is stored at uri.

zcontains("/config") == true
zcontains("/config/map#a") == true

if (zcontains("/config")) { echo "OK"; } //PHP
List<String> zlist(String uriPrefix [, boolean includePrefix]) Returns a list of children of uriPrefix

zlist("/") == ["/config"]
zlist("/config/foo") == ["/config/foo/a", "/config/foo/b"]
zlist("/config", false) == ["foo", "map"]

$arr = zlist("/config/foo",false); //PHP
List<String> zlistAll(String uriPrefix [, boolean includePrefix]) Returns a list of all global context URIs that start with uriPrefix

zlistAll("/") == ["/config/foo/a", "/config/foo/x", "/config/map"]
String zdump(String uri) Renders the content of all keys that start with uri using toString()

zdump("/config/foo"); //PHP
Map zputs(String uriPrefix, Map payload) Convenience method for creating global context entries from key/value pairs in the payload.

map.put("w", "z")
zputs("/config/bar", map)
zget("/config/bar/w") == "z"

PHP information

Use the global context to share information between the various event handlers, which you can use WebSphere sMash to build. To enable the many language environments to access the information, there are certain restrictions on the types of information that can be placed in the global context. From the PHP perspective, objects and resources are not readily sharable, and objects in particular contain behaviors that might be dependent on the PHP runtime instance, for example, their own class definition.

Objects and resources cannot be stored in the global context. To share objects, save the values of the objects' fields by casting the object to an array.

A PHP code sample using the global context

<?php
    // Get the URI from the request.
    $uri    = zget("/request/uri");

    echo "<br/>$uri";

    // Get the user agent
    $header = zget("/request/headers/in/User-Agent");
    echo "<br/>$header";

    // List all the parameters.
    $params = zlist("/request/params",false);
    echo "<br/>The parameters<br/>";
    var_dump($params);

     // Put the status.
    zput("/request/status",200);

    // Delete a key.
    zdelete("/request/some_unwanted_key");

    // Add a header value 'no-cache'
    zpost("/request/headers/out/Cache-Control","no-cache");

    // secured pages only.
    $remoteU = zget("/request/subject#remoteUser");
    $group   = zget("/request/subject#groups");
    $roles   = zget("/request/subject#roles");
    if ($remoteU != null) {
        var_dump($remoteU);
    }
    if ($group != null) {
        var_dump($group);
    }
    if ($roles != null) {
        var_dump($roles);
    }
    echo "<br/> Page Complete";

?>

Value pathing

Value pathing is a means of referring to elements within a stored value. Specify value paths by appending fragment identifiers to the key. Details about the fragment, such as syntax and meaning, are dependent on the type handler associated with the value.

The following information summarizes the support for zput and zpost within the global context APIs:

  • zput() means create, if not defined, or replace otherwise.
  • zpost() means create, if not defined, or append otherwise.

Exception conditions

The global context APIs throw an IllegalArgumentException if the uri is incorrectly formed. A valid global context uri contains the following characteristics:

Description Valid example Invalid example (throws IllegalArgumentException)
Starts with a slash zget("/") zget("config")
Path does not end with a slash zget("/") is valid zget("/config/")
zget("/config/#any")
Not null zget("/config") zget(null)
Not empty zget("/") zget("")
No value paths with zlist() zlist("/config") zlist("/app/foo#bar)

Maps

The Map type handler supports the #<key> fragment, and refers to the value mapped to <key>.

Java API Groovy API PHP API Description Can create entry
zput("/app/myMap", map) app.myMap = map zput("/app/myMap", $arr) Create/replace existing map yes
zput("/app/myMap#foo", "bar") app.myMap['foo'] = 'bar' zput("/app/myMap#foo", "bar") Add or replace entry; map must exist  
zpost("/app/myMap", map) zpost("/app/myMap", map) zpost("/app/myMap", $arr) Create/merge into existing map; replaces existing keys yes
zget("/app/myMap") x = app.myMap[] $arr = zget("/app/myMap") Returns the map  
zget("/app/myMap#foo") x = app.myMap['foo'] $key = zget("/app/myMap#foo") Returns entry at key foo  
zdelete("/app/myMap") zdelete("/app/myMap") zdelete("/app/myMap") Deletes map  
zdelete("/app/myMap#foo") zdelete('/app/myMap#foo') zdelete("/app/myMap#foo") Removes entry  
Posts to a map entry are not supported, such as zpost("/app/myMap#foo", "bar").

Lists

The List type handler supports the #<N> fragment, and refers to the N-th element in the list.

Java API Groovy API PHP API Description Can create entry
zput("/app/myList", list) app.myList = list zput("/app/myList", $list) Create and replace an existing list. yes
zput("/app/myList#0", "bar") app.myList[0] zput("/app/myList#0", "bar") Set/replace list element; list must exist  
zpost("/app/myList", list) zpost("/app/myList", list) zpost("/app/myList", $list) yes
zpost("/app/myList", "bar") zpost("/app/myList", "bar") zpost("/app/myList", "bar") Append to list; list must exist  
zget("/app/myList") x = app.myList[] $list = zget("/app/myList") Returns the list  
zget("/app/myList#0") x = app.myList[0] $val = zget("/app/myList#0") Returns 0th element  
zdelete("/app/myList") zdelete("/app/myList") zdelete("/app/myList") Deletes list  
zdelete("/app/myList#0") zdelete("/app/myList#0") zdelete("/app/myList#0") Removes first entry  

FirstElementList

FirstElementList is a variant of List that supports access to the first element as a simple object without specifying the index. FirstElementList simplifies the handling of values, such as query parameters, that are properly stored as lists, but are most commonly retrieved as single values. Within the default zones, only /request/params/<parmName> and /request/headers/in/<headerName> are instances of FirstElementList.

Because FirstElementList is used to simplify access to these particular type of lists, the most commonly used API invocations are zget("/app/someList") and zput("/app/someList", "bar"). However, the following table features the full API of FirstElementType:

Java API Groovy API PHP API Description Can create entry
zput("/app/myFeList#*", feList) app.myFeList['*'] = feList zput("/app/myFeList#*", feList) Replace existing list. yes
zput("/app/myFeList#N", "bar") app.myFeList[N]="bar" zput("/app/myFeList#N", "bar") Replace Nth element of list; Nth element must exist.  
zput("/app/myFeList", "bar") app.myFeList="bar" zput("/app/myFeList", "bar") Replace first element of list.  
zpost("/app/myFeList#*", myList) zpost("/app/myFeList#*", myList) zpost("/app/myFeList#*", myList) Create list or append all list elements to existing list. yes
zpost("/app/myFeList#*", "bar") zpost("/app/myFeList#*", "bar") zpost("/app/myFeList#*", "bar") Append to list; list must exist.  
zpost("/app/myFeList#N", myObj) zpost("/app/myFeList#N", myObj) zpost("/app/myFeList#N", $arr) Append to collection at Nth element of list; Nth element must be an existing collection.  
zpost("/app/myFeList", myObj) zpost("/app/myFeList", myObj) zpost("/app/myFeList", $arr) Create list or append to collection at first element of list. yes
zget("/app/myFeList#*") x = app.myFeList['*'] $list = zget("/app/myFeList#*") Returns the list.  
zget("/app/myFeList#N") x = app.myFeList[N] $item = zget("/app/myFeList#N") Returns Nth element of list.  
zget("/app/myFeList") x = app.myFeList[] $list = zget("/app/myFeList") Returns first element of list.  
zdelete("/app/myFeList#*) zdelete("/app/myFeList#*") zdelete("/app/myFeList#*) Deletes list.  
zdelete("/app/myFeList#N") zdelete("/app/myFeList#N") zdelete("/app/myFeList#N") Removes Nth element from list.  
zdelete("/app/myFeList") zdelete("/app/myFeList") zdelete("/app/myFeList") Removes first element from list.  
When accessing FirstElementList data, "/app/someList#N" refers to the same location as "/app/someList#*/N". For example, zget("/app/someList#N") is equivalent to zget("/app/someList#*/N").

Objects (default)

Java API Groovy API PHP API Description Can create entry
zput("/app/myObj", o) app.myObj = o zput("/app/myObj", (array)$obj) Create/replace object yes
zget("/app/myObj") x = app.myObj[] $array = zget("/app/myObj") Returns object (as an array of field values in PHP).  
zdelete("/app/myObj") zdelete("/app/myObj") zdelete("/app/myObj") Deletes object  
post is not a valid operation on Objects.

Nested

Value manipulation can be nested for zget() zcontains(). All other operations support up to one level of value manipulation. Parameters for value manipulation are delimited by a slash (/), as shown in the following example:

Map<String, String> innerMap0 = new HashMap<String, String>();
innerMap0.put("im0a", "a");
innerMap0.put("im0b", "b");
      
Map<String, String> innerMap1 = new HashMap<String, String>();
innerMap1.put("im1c", "c");
innerMap1.put("im1d", "d");

List<Map<String, String>> list = new ArrayList<Map<String, String>>();
list.add(innerMap0);
list.add(innerMap1);
      
Map<String, List<Map<String, String>>> map
    = new HashMap<String, List<Map<String, String>>>();
map.put("x", list);
      
zput("/config/nested", map);

      
assertEquals("b", zget("/config/nested#x/0/im0b"));
assertEquals("c", zget("/config/nested#x/1/im1c"));

Version 1.0.0.3.25591