Session & session attributes are newly introduced in OAM 11g. OAM 11g maintains a session at server side the sessionIds are managed by Session Management Engine SME. How to retrieve session IDs from OAM server are explained in post. So this post will give a sample code snippet to set and retrieve session attributes from session.

OAM session ID is around 20 characters and will be of the format d331ab34-2e17-4c5c-8ba1-a7cdf695150c. Session IDs can also be seen in OAM database schema by using sql command select * from oam_session;

The above sql command output will have results similar as shown below.

SESSIONID
——————————————————————————–
USERID
——————————————————————————–
IDENTITY_DOMAIN
——————————————————————————–
SESSION_INDEX
——————————————————————————–
CREATE_TIME
—————————————————————————
LAST_ACCESS_TIME
—————————————————————————
LAST_UPDATE_TIME
—————————————————————————
EXPIRY_TIME
4c2cdff7-0cbf-4723-aaab-489c06366d96
LN-Admin
NewOVDStore
-3226491390739477568
04-MAY-12 03.28.39.124000 AM
04-MAY-12 03.28.39.124000 AM
04-MAY-12 03.28.41.536000 AM
04-MAY-12 11.28.39.124000 AM

So it is merely easy to identify the session ID associated with which user IDs. The same behavior can also be seen in OAM console in Session Management section.

So the below snippet can be used to set and retrieve the session attributes from session.

Hashtable credentials = new Hashtable ();
credentials.put(“userid”, user_login);
credentials.put(“password”, user_passwd);
try{
AccessClient ac=null;
ResourceRequest req ;
ac = AccessClient.createDefaultInstance(Location,AccessClient.CompatibilityMode.OAM_10G);
req = new ResourceRequest(protocol, resource,method_way);
System.out.println(“Location–>”+Location);
UserSession session = new UserSession(req,credentials);
String sessionToken=session.getSessionToken();
System.out.println(sessionToken);
Set set=session.getSessionIds(user_login);

Iterator it = set.iterator();

String sessionID = “”;

while (it.hasNext()){

sessionID = it.next();

}
System.out.println(“Sessionattr—>”+set);

Hashtable sessionAttributes= new Hashtable ();
sessionAttributes.put(“userid”, “mahendra”);
sessionAttributes.put(“mobile”, “999123412″);

sessionAttributes.put(“email”, “mahendra@abcd.com”);

UserSession.setSessionAttributes(sessionID, sessionAttributes);

Hashtable sessionAttrs = UserSession.getSessionAttributes(sessionID);

System.out.println(“userid from session Attributes ===>”+sessionAttrs.get(“userid”));
System.out.println(“mobile from session Attributes ===>”+sessionAttrs.get(“mobile”));
System.out.println(“email from session Attributes ===>”+sessionAttrs.get(“email”));

}catch(Exception e){}

In OAM 11g user can have multiple sessions and based on this, the attributes can be retrieved in a loop.