Read Context
This section provides developers with information on how to read entities from a context. Documentation for the REST protocol, as well as code samples for Java, PHP, and cURL, are provided below.
REST
The REST noun (and associated message content) for reading entities in a context is as follows:
- Relative Noun: AuthenticatedSession/$sessionID/Context. This is the relative path of the URL that should appear after the OTIS server application name. Below is an example of reading entities in a context:
AuthenticatedSession/78BD660E0E5F673378BD660E0E5F6733/Context
- HTTP Method: GET.
- Request/Response message details: <Context> element details.
- Description: Reads the entities from the context of the session.
Java
The following is example Java code that demonstrates how to read entities from the session:
//Get all entities in a context and read their attributes' values
Iterator iter = session.getEntities();
while (iter.hasNext())
{
IEntity example = (IEntity)iter.next();
System.out.println("\"" + example.getEntityID() + "\"s attribute values (first value only) are: ");
Iterator attributes = example.getAttributes();
while(attributes.hasNext()) {
IAttribute exampleAttribute = (IAttribute)attributes.next();
System.out.println("\t" + ((ISimpleAttrValue)exampleAttribute
.getValues().next()).getCanonical()); //only returns the first value in the Iterator
}
}
To view the complete Java sample code for Attribute/Entity nouns, see the Entity/Attribute Java Sample Code page.
The OTIS client Java library may be downloaded from the OTIS download page.
PHP
The following is example PHP code that demonstrates how to read entities from a session:
//Get the entity XML.
$entities = getRequest($sessionURL, $sessionSecret, "Context");
//Display the entities' xml.
echo $entities;
function getRequest($URL, $secret, $URI){
//Make a request header for the entity.
$getHeader =
"GET $URL/$URI HTTP/1.1\n".
"Host: wag.bandit-project.org\n".
(($secret) ? ("SessionSecret: " . $secret . "\n") : "") .
"Connection: Close\n\n";
$socket = fsockopen("wag.bandit-project.org", 80);
fputs($socket, $getHeader);
$xml = "";
while (!feof($socket))
{
$content = fgets($socket, 256);
//If the line contains an xml tag, add it to the xml var
if(strpos($content, ">")){
$xml .= $content;
}
}
//Return the xml var.
return($xml);
}
To view the complete PHP sample code for Attribute/Entity nouns, see the Entity/Attribute PHP Sample Code page.
cURL
The following command reads entities from the context of a session:
if [ -e secret.hdr ] ; then secret=`cat secret.hdr` ; else secret="nosecret: none" ; fi
curl -b cookies.txt -H "${secret}" https://wag.bandit-project.org/otis/Context
See the Entity Get Request documentation for further details including expected response codes and message body.