App42 Cloud API

Java and Groovy Cloud SDK

Session Management

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.

public Session getSession(String userName) throws App42Exception

Create Session for the User. If the session does not exist it will create a new session

Parameters:

userName - Username for which the session has to be created

Returns:

Session object containing the session id of the created session. This id has to be used for storing or retrieving attributes.

Response: Session Object

For more details click Exception
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"
            }
        }
    }
}
public Session getSession(String userName, boolean isCreate) throws App42Exception

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:

userName - Username for which the session has to be created
isCreate - A boolean value for specifying if an existing session is not there, should a new one is to be created or not

Returns:

Session object containing the session id of the created session. This id has to be used for storing or retrieving attributes.

Response: Session Object

Exception:

The method throws the following exception with the error code.
/*
2201 - NOT FOUND - Session for the user '<userName>' does not exist.
*/        
For more details click Exception
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"
            }
        }
    }
}
public App42Response invalidate(String sessionId) throws App42Exception

Invalidate a session based on the session id. All the attributes store in the session will be lost.

Parameters:

sessionId - The session id for which the session has to be invalidated

Returns:

App42Response if invalidated successfully

Response: App42Response Object

Exception:

The method throws the following exception with the error code.
/*
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.
*/ 	        
For more details click Exception
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"
            }
        }
    }
}
public Session setAttribute(String sessionId, String attributeName, String attributeValue) throws App42Exception

Sets attribute in a session whose session id is provided. Attributes are stored in a key value pair.

Parameters:

sessionId - Session id for which the attribute has to be saved
attributeName - The attribute key that needs to be stored
attributeValue - The attribute value that needs to be stored

Returns:

Session object containing the attribute and value which is stored

Response: Session Object

Exception:

The method throws the following exception with the error code.
/*
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.
*/ 		        
For more details click Exception
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"
                    }
                }
            }
        }
    }
}
public Session getAttribute(String sessionId, String attributeName) throws App42Exception

Gets the attribute value in a session whose session id is provided.

Parameters:

sessionId - The session id for which the attribute has to be fetched
attributeName - The attribute key that has to be fetched

Returns:

Session object containing the attribute and value which is stored for the session id and attribute name

Response: Session Object

Exception:

The method throws the following exception with the error code.
/*
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.
*/ 	        
For more details click Exception
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"
                    }
                }
            }
        }
    }
}
public Session getAllAttributes(String sessionId) throws App42Exception

Gets all the attributes for a given session id.

Parameters:

sessionId - The session id for which the attribute has to be fetched

Returns:

Session object containing all the attributes and values which are stored for the session id

Response: Session Object

Exception:

The method throws the following exception with the error code.
/*
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>'.
*/ 	        
For more details click Exception
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"
                        }
                    ]
                }
            }
        }
    }
}
public App42Response removeAttribute(String sessionId, String attributeName) throws App42Exception

Removes the attribute from a session whose session id is provided.

Parameters:

sessionId - The session id for which the attribute has to be removed
attributeName - The attribute key that has to be removed

Returns:

App42Response if removed successfully

Response: App42Response Object

Exception:

The method throws the following exception with the error code.
/*
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)*/     
For more details click Exception
{
    "app42": {
        "response": {
            "success": true,
            "session": {
                "sessionId": "48dacb76-7f5e-4011-ac24-e09623bdc70f",
                "attributes": {
                    "attribute": {
                        "name": "attributeName01"
                    }
                }
            }
        }
    }
}
public App42Response removeAllAttributes(String sessionId) throws App42Exception

Removes all the attributes for a given session id

Parameters:

sessionId - The session id for which the attributes has to be removed

Returns:

App42Response if removed successfully

Response: App42Response Object

Exception:

The method throws the following exception with the error code.
/*
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.
*/ 		        
For more details click Exception
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"
                        }
                    ]
                }
            }
        }
    }
}