The Session Manager manages user sessions on the server side. It is a persistent Session Manager. It allows to save attributes in the session and retrieve them. This Session Manager is especially useful for Mobile/Device Apps which want to do session management.
In order to use the various functions available under a particular API, a developer will have to create an instance of ServiceAPI by passing the apiKey and the secretKey which has been received while creating an app.
ServiceAPI api = new ServiceAPI("<API_KEY>","<SECRET_KEY>");
After initialization, developer needs to call the buildXXXService method on ServiceAPI instance to get the instance of the particular API that you wish to build. For example, To build an instance of SessionService, buildSessionService() method needs to be called.
SessionService sessionService = api.buildSessionService();
Once the API instance has been retrieved, You are ready to use the functions available for that particular API.
The methods available under a particular API will return the domain object (like Session in this case) as a response which will have the accessor / mutator to access the available properties for that object. You can get the response in the form of JSON as well. We have provided the JSON response with every function detail which can be retrieved by calling the toString() on the returned object.
String userName = "Nick";
Session session = sessionService.getSession(userName); /* returns the Session object. */
System.out.println("userName is" + session.getUserName());
System.out.println("sessionId is" + session.getSessionId());
System.out.println("createdOn is" + session.getCreatedOn());
String jsonResponse = session.toString(); /* returns the response in JSON format. */
The functions available under Session Management API can throw some exceptions in abnormal conditions. Example of the same has been given below.
E.g. If App developer is trying to invalidate a Session which is already in invalidated, the function will throw the App42Exception (as shown below) with message as "Not Found" and the appErrorCode as "2202" and the details as "Session with the id '<sessionId>' does not exist".
String sessionId = "48dacb76-7f5e-4011-ac24-e09623bdc70f";
try
{
App42Response response = sessionService.invalidate(sessionId);
}
catch(App42Exception ex)
{
int appErrorCode = ex.getAppErrorCode();
int httpErrorCode = ex.getHttpErrorCode();
if(appErrorCode == 2202)
{
// Handle here for Not Found (Session with the id '<sessionId>' does not exist.)
}
else if(appErrorCode == 2203)
{
// Handle here for Bad Request (The request parameters are invalid. Session with the Id '<sessionId>' is already invalidated.)
}
else if(appErrorCode == 1401)
{
// handle here for Client is not authorized
}
else if(appErrorCode == 1500)
{
// handle here for Internal Server Error
}
String jsonText = ex.getMessage(); /* returns the Exception text in JSON format. (as shown below)*/
}
{
"app42Fault": {
"httpErrorCode": 400,
"appErrorCode": 2202,
"message": "Not Found",
"details": "Session with the id '48dacb76-7f5e-4011-ac24-e09623bdc70f' does not exist"
}
}
Below are the HTTP Error Codes and their description, the function under the Session Management API can throw.
/* 1400 - BAD REQUEST - The Request parameters are invalid 1401 - UNAUTHORIZED - Client is not authorized 1500 - INTERNAL SERVER ERROR - Internal Server Error. Please try again */
Below are the Application Error Codes and their description, the function under the Session Management API can throw.
/* 2200 - NOT FOUND - User by the name '<userName>' does not exist. 2201 - NOT FOUND - Session for the user '<userName>' does not exist. 2202 - NOT FOUND - Session with the id '<sessionId>' does not exist. 2203 - BAD REQUEST - The request parameters are invalid. Session with the Id '<sessionId>' is already invalidated. 2204 - NOT FOUND - Attribute with the name '<name>' for session id '<sessionId>' does not exist. 2205 - NOT FOUND - There are no attributes for session id '<sessionId>'. 2206 - NOT FOUND - Attributes for session id '<sessionId>' do not exist. */
Various functions available under Session Management API has been explained below.
Create Session for the User. If the session does not exist it will create a new session
Parameters:
Returns:
Response: Session Object
String userName = "Nick";
Session session = sessionService.getSession(userName); /* returns the Session object. */
System.out.println("userName is" + session.getUserName());
System.out.println("sessionId is" + session.getSessionId());
System.out.println("createdOn is" + session.getCreatedOn());
String jsonResponse = session.toString(); /* returns the response in JSON format. (as shown below)*/
{
"app42": {
"response": {
"success": true,
"session": {
"userName": "Nick",
"sessionId": "74f9b379-60f7-4d93-89f2-4aca536ba407",
"createdOn": "2012-05-09T05:34:55.000Z"
}
}
}
}
Create User Session based on the isCreate boolean parameter. If isCreate is true and there is an existing session for the user, the existing session is returned. If there is no existing session for the user a new one is created. If isCreate is false and there is an existing session, the existing session is returned if there is no existing session new one is not created.
Parameters:
Returns:
Response: Session Object
Exception:
/* 2201 - NOT FOUND - Session for the user '<userName>' does not exist. */
String userName = "Nick";
boolean isCreate = true;
Session session = sessionService.getSession(userName, isCreate); /* returns the Session object. */
System.out.println("userName is" + session.getUserName());
System.out.println("sessionId is" + session.getSessionId());
System.out.println("createdOn is" + session.getCreatedOn());
String jsonResponse = session.toString(); /* returns the response in JSON format. (as shown below)*/
{
"app42": {
"response": {
"success": true,
"session": {
"userName": "Nick",
"sessionId": "48dacb76-7f5e-4011-ac24-e09623bdc70f",
"createdOn": "2012-05-09T05:38:43.000Z"
}
}
}
}
Invalidate a session based on the session id. All the attributes store in the session will be lost.
Parameters:
Returns:
Response: App42Response Object
Exception:
/* 2202 - NOT FOUND - Session with the id '<sessionId>' does not exist. 2203 - BAD REQUEST - The request parameters are invalid. Session with the Id '<sessionId>' is already invalidated. */
String sessionId = "48dacb76-7f5e-4011-ac24-e09623bdc70f"; App42Response response = sessionService.invalidate(sessionId); /* returns the App42Response object. */ boolean success = response.isResponseSuccess(); String jsonResponse = response.toString(); /* returns the response in JSON format. (as shown below)*/
{
"app42": {
"response": {
"success": true,
"session": {
"userName": "Nick",
"sessionId": "48dacb76-7f5e-4011-ac24-e09623bdc70f",
"createdOn": "2012-05-09T05:34:55.000Z",
"invalidatedOn": "2012-05-09T05:38:42.000Z"
}
}
}
}
Sets attribute in a session whose session id is provided. Attributes are stored in a key value pair.
Parameters:
Returns:
Response: Session Object
Exception:
/* 2202 - NOT FOUND - Session with the id '<sessionId>' does not exist. 2203 - BAD REQUEST - The request parameters are invalid. Session with the Id '<sessionId>' is already invalidated. */
String sessionId = "48dacb76-7f5e-4011-ac24-e09623bdc70f";
String attributeName = "attributeName01";
String attributeValue = "attributeValue01";
Session session = sessionService.setAttribute(sessionId, attributeName, attributeValue); /* returns the Session object. */
System.out.println("sessionId is" + session.getSessionId());
ArrayList<Session.Attribute> attributeList = session.getAttributeList();
for(Session.Attribute attribute :attributeList )
{
System.out.println("name is" + attribute.getName());
System.out.println("value is" + attribute.getValue());
}
String jsonResponse = session.toString(); /* returns the response in JSON format. (as shown below)*/
{
"app42": {
"response": {
"success": true,
"session": {
"sessionId": "48dacb76-7f5e-4011-ac24-e09623bdc70f",
"attributes": {
"attribute": {
"name": "attributeName01",
"value": "attributeValue01"
}
}
}
}
}
}
Gets the attribute value in a session whose session id is provided.
Parameters:
Returns:
Response: Session Object
Exception:
/* 2202 - NOT FOUND - Session with the id '<sessionId>' does not exist. 2203 - BAD REQUEST - The request parameters are invalid. Session with the Id '<sessionId>' is already invalidated. 2204 - NOT FOUND - Attribute with the name '<name>' for session id '<sessionId>' does not exist. */
String sessionId = "48dacb76-7f5e-4011-ac24-e09623bdc70f";
String attributeName = "attributeName01";
Session session = sessionService.getAttribute(sessionId, attributeName); /* returns the Session object. */
System.out.println("sessionId is" + session.getSessionId());
ArrayList<Session.Attribute> attributeList = session.getAttributeList();
for(Session.Attribute attribute :attributeList )
{
System.out.println("name is" + attribute.getName());
System.out.println("value is" + attribute.getValue());
}
String jsonResponse = session.toString(); /* returns the response in JSON format. (as shown below)*/
{
"app42": {
"response": {
"success": true,
"session": {
"sessionId": "48dacb76-7f5e-4011-ac24-e09623bdc70f",
"attributes": {
"attribute": {
"name": "attributeName01",
"value": "attributeValue01"
}
}
}
}
}
}
Gets all the attributes for a given session id.
Parameters:
Returns:
Response: Session Object
Exception:
/* 2202 - NOT FOUND - Session with the id '<sessionId>' does not exist. 2203 - BAD REQUEST - The request parameters are invalid. Session with the Id '<sessionId>' is already invalidated. 2205 - NOT FOUND - There are no attributes for session id '<sessionId>'. */
String sessionId = "48dacb76-7f5e-4011-ac24-e09623bdc70f";
Session session = sessionService.getAllAttributes(sessionId); /* returns the Session object. */
System.out.println("sessionId is" + session.getSessionId());
ArrayList<Session.Attribute> attributeList = session.getAttributeList();
for(Session.Attribute attribute :attributeList )
{
System.out.println("name is" + attribute.getName());
System.out.println("value is" + attribute.getValue());
}
String jsonResponse = session.toString(); /* returns the response in JSON format. (as shown below)*/
{
"app42": {
"response": {
"success": true,
"session": {
"sessionId": "48dacb76-7f5e-4011-ac24-e09623bdc70f",
"attributes": {
"attribute": [
{
"name": "attributeName01",
"value": "attributeValue01"
},
{
"name": "attributeName02",
"value": "attributeValue02"
}
]
}
}
}
}
}
Removes the attribute from a session whose session id is provided.
Parameters:
Returns:
Response: App42Response Object
Exception:
/* 2202 - NOT FOUND - Session with the id '<sessionId>' does not exist. 2203 - BAD REQUEST - The request parameters are invalid. Session with the Id '<sessionId>' is already invalidated. */
String sessionId = "48dacb76-7f5e-4011-ac24-e09623bdc70f"; String attributeName = "attributeName01"; App42Response response = sessionService.removeAttribute(sessionId, attributeName); /* returns the App42Response object. */ boolean success = response.isResponseSuccess(); String jsonResponse = response.toString(); /* returns the response in JSON format. (as shown below)*/
{
"app42": {
"response": {
"success": true,
"session": {
"sessionId": "48dacb76-7f5e-4011-ac24-e09623bdc70f",
"attributes": {
"attribute": {
"name": "attributeName01"
}
}
}
}
}
}
Removes all the attributes for a given session id
Parameters:
Returns:
Response: App42Response Object
Exception:
/* 2202 - NOT FOUND - Session with the id '<sessionId>' does not exist. 2203 - BAD REQUEST - The request parameters are invalid. Session with the Id '<sessionId>' is already invalidated. 2206 - NOT FOUND - Attributes for session id '<sessionId>' do not exist. */
String sessionId = "48dacb76-7f5e-4011-ac24-e09623bdc70f"; App42Response response = sessionService.removeAllAttributes(sessionId); /* returns the App42Response object. */ boolean success = response.isResponseSuccess(); String jsonResponse = response.toString(); /* returns the response in JSON format. (as shown below)*/
{
"app42": {
"response": {
"success": true,
"session": {
"sessionId": "48dacb76-7f5e-4011-ac24-e09623bdc70f",
"attributes": {
"attribute": [
{
"name": "attributeName01"
},
{
"name": "attributeName02"
}
]
}
}
}
}
}