Just like with Google Home devices and Amazon Alexa devices, SUSI.AI Users should have a way to add and store the information of their devices (Smart Speakers) on SUSI Server, so that it could be displayed to them on various clients. Hence, we needed a Servlet which could add and store the User data on the Server.
Implementation of Servlets in SUSI.AI
All servlets in SUSI extend AbstractAPIHandler class and implement APIHandler. All servlets have 4 methods, which we overwrite depending on what we want the servlet to do. They are as follows :
How these 4 methods work together?
- First method is getAPIPath(). It returns the endpoint of the servlet.
- The second method is getMinimalBaseUserRole(). It returns the minimum privilege level required to access the endpoint.
- The third method is getDefaultPermissions(). It gets the Default Permissions of a UserRole in SUSI Server. Different UserRoles have different permissions defined in SUSI Server.
- Whenever the endpoint defined in the getAPIPath() method is called properly, it responds with whatever is defined in the fourth method, which is serviceImpl().
How is User data stored on SUSI Server?
Before we move on to the actual implementation of the API required to store the device information, we should get a brief idea of how exactly User data is stored on SUSI Server.
Every SUSI User has an Accounting Object. It is a JSONObject which stores the Settings of the particular User on the Server. For example, every time you change some setting on the Web Client https://chat.susi.ai, an API call is made to aaa/changeUserSettings.json with appropriate parameters with information of the changed setting, and the User settings are stored to the Server in the Accounting Object of the User.
Implementation of a Servlet to store Device information on Server
The task of this servlet is to store the information of a new device (Smart speaker) whenever it is initially set up using the Android/iOS app.
This is the implementation of the 4 methods of a servlet which is used to store information of connected devices:
As it can be seen from the above code, the endpoint for this servlet is /aaa/addNewDevice.json and it accepts 3 parameters –
- macid : Mac Address of the device
- name : Name of the device
- device : Additional information which you want to send about the device (subject to changes)
As the main task of this servlet is user specific, and should only be accessible to the particular user, hence we returned UserRole as USER in the getMinimalUserRole() method.
In the serviceImpl() method, first we extract the value of the URL query parameters and store them in variables. If any of the parameters is missing, we display an error response code of 400 with error message “Bad service call, missing arguments”. If query parameters are fine, we store the name and device values in a new JSONObject, value in this case.
An if-else statement then checks for whether the User is logged in or not, using the authorization.getIdentity(), which is a function which returns the identity of the User. The implementation of this function is in Accounting.java file, and is as follows :
If the User is logged in, getAccounting() function of DAO.java file is called which returns an Accounting object according to the following implementation of the function :
Our device information is then stored in this Accounting object in the following format :