# ----------------------------------------------------------------------- # Copyright © 2012 ShepHertz Technologies Pvt Ltd. All rights reserved. # ----------------------------------------------------------------------- require 'rubygems' require 'connection/RESTConnection.rb' require 'util/util.rb' require 'json/pure' require 'App42Response.rb' require 'session/SessionResponseBuilder.rb' require 'App42Exception.rb' require 'session/Session.rb' module App42 module Session # # 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. # # @see Session # class SessionService # # this is a constructor that takes # # @param apiKey # @param secretKey # @param baseURL # def initialize(api_key, secret_key, base_url) puts "Session Service->initialize" @api_key = api_key @secret_key = secret_key @base_url = base_url @resource = "session" @version = "1.0" end # # Create Session for the User. If the session does not exist it will create a new session # # @param uName # - Username for which the session has to be created. # # @return Session object containing the session id of the created session. This id has to be used for storing or # retrieving attributes. # # @raise App42Exception # def get_session(uName) puts "Get Session Called " puts "Base url #{@base_url}" response = nil sessionObj = nil sessionObj = Session.new() util = Util.new util.throwExceptionIfNullOrBlank(uName, "User Name"); begin connection = App42::Connection::RESTConnection.new(@base_url) body = {'app42' => {"session"=> { "userName" => uName }}}.to_json puts "Body #{body}" query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("body", body) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}" response = connection.post(signature, resource_url, query_params, body) session = SessionResponseBuilder.new sessionObj = session.buildSessionResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return sessionObj end # # 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 # # @param uName # - Username for which the session has to be created. # @param isCreate # - A boolean value for specifying if an existing session is not there, should # a new one is to be created or not. # # @return Session object containing the session id of the created session. This id has to be used for storing or # retrieving attributes. # # @raise App42Exception # def get_session_boolean(uName, isCreate) puts "Get Session Boolean Called " puts "Base url #{@base_url}" response = nil sessionObj = nil sessionObj = Session.new() util = Util.new util.throwExceptionIfNullOrBlank(uName, "User Name"); util.throwExceptionIfNullOrBlank(isCreate, "Is Create"); begin connection = App42::Connection::RESTConnection.new(@base_url) body = {'app42' => {"session"=> { "userName" => uName }}}.to_json puts "Body #{body}" query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("body", body) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/#{isCreate}" response = connection.post(signature, resource_url, query_params, body) session = SessionResponseBuilder.new sessionObj = session.buildSessionResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return sessionObj end # # Invalidate a session based on the session id. All the attributes store in the session will be lost. # # @param sessionId # - The session id for which the session has to be invalidated. # # @return App42Response if invalidated successfully # # @raise App42Exception # def invalidate(sessionId) puts "In Validate Called " puts "Base url #{@base_url}" response = nil sessionObj = nil sessionObj = Session.new() util = Util.new util.throwExceptionIfNullOrBlank(sessionId, "Session Id"); begin connection = App42::Connection::RESTConnection.new(@base_url) body = {'app42' => {"session"=> { "id" => sessionId }}}.to_json puts "Body #{body}" query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("body", body) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}" response = connection.put(signature, resource_url, query_params, body) session = SessionResponseBuilder.new sessionObj = session.buildSessionResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return sessionObj end # # Sets attribute in a session whose session id is provided. Attributes are stored in a key value pair. # # @param sessionId # - Session id for which the attribute has to be saved. # @param attributeName # - The attribute key that needs to be stored # @param attributeValue # - The attribute value that needs to be stored # # @return Session object containing the attribute and value which is stored # # @raise App42Exception # def set_attribute(sessionId,attributeName,attributeValue) puts "Set Attribute Called " puts "Base url #{@base_url}" response = nil sessionObj = nil sessionObj = Session.new() util = Util.new util.throwExceptionIfNullOrBlank(sessionId, "Session Id"); util.throwExceptionIfNullOrBlank(attributeName, "Attribute Name"); util.throwExceptionIfNullOrBlank(attributeValue, "Attribute Value"); begin connection = App42::Connection::RESTConnection.new(@base_url) body = {'app42' => {"session"=> { "name" => attributeName, "value" => attributeValue }}}.to_json puts "Body #{body}" query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, 'sessionId'=> sessionId } query_params = params.clone params.store("body", body) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/id/#{sessionId}" response = connection.post(signature, resource_url, query_params, body) session = SessionResponseBuilder.new sessionObj = session.buildSessionResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return sessionObj end # # Gets the attribute value in a session whose session id is provided. # # @param sessionId # - The session id for which the attribute has to be fetched # @param attributeName # - The attribute key that has to be fetched # # @return Session object containing the attribute and value which is stored for the session id and attribute name # # @raise App42Exception # def get_attribute(sessionId,attributeName) puts "Get Attribute Called " puts "Base url #{@base_url}" response = nil sessionObj = nil sessionObj = Session.new() util = Util.new util.throwExceptionIfNullOrBlank(sessionId, "Session Id"); util.throwExceptionIfNullOrBlank(attributeName, "Attribute Name"); begin connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("sessionId", sessionId); params.store("name", attributeName) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/id/#{sessionId}/#{attributeName}" response = connection.get(signature, resource_url, query_params) session = SessionResponseBuilder.new sessionObj = session.buildSessionResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return sessionObj end # # Gets all the attributes for a given session id # # @param sessionId # - The session id for which the attribute has to be fetched # # @return Session object containing all the attributes and values which are stored for the session id # # @raise App42Exception # def get_all_attribute(sessionId) puts "Get All Attributes Called " puts "Base url #{@base_url}" response = nil sessionObj = nil sessionObj = Session.new() util = Util.new util.throwExceptionIfNullOrBlank(sessionId,"Session Id") begin connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("sessionId", sessionId); puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/id/#{sessionId}" response = connection.get(signature, resource_url, query_params) session = SessionResponseBuilder.new sessionObj = session.buildSessionResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return sessionObj end # # Removes the attribute from a session whose session id is provided. # # @param sessionId # - The session id for which the attribute has to be removed # @param attributeName # - The attribute key that has to be removed # # @return App42Response if removed successfully # # @raise App42Exception # def remove_attribute(sessionId,attributeName) puts "Remove Attribute Called " puts "Base url #{@base_url}" response = nil responseObj = App42Response.new(); util = Util.new util.throwExceptionIfNullOrBlank(sessionId, "Session Id"); util.throwExceptionIfNullOrBlank(attributeName, "Attribute Name"); begin connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("sessionId", sessionId) params.store("name", attributeName) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/id/#{sessionId}/#{attributeName}" response = connection.delete(signature, resource_url, query_params) responseObj.strResponse=(response) responseObj.isResponseSuccess=(true) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return responseObj end # # Removes all the attributes for a given session id # # @param sessionId # - The session id for which the attributes has to be removed # # @return App42Response if removed successfully # # @raise App42Exception # def remove_all_attribute(sessionId) puts "Remove All Attributes Called " puts "Base url #{@base_url}" response = nil responseObj = App42Response.new(); util = Util.new util.throwExceptionIfNullOrBlank(sessionId, "Session Id"); begin connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("sessionId", sessionId) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/id/#{sessionId}" response = connection.delete(signature, resource_url, query_params) responseObj.strResponse=(response) responseObj.isResponseSuccess=(true) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return responseObj end end end end