-
Notifications
You must be signed in to change notification settings - Fork 21
Server
This application runs inside an Apache Tomcat server. There are two types of Java files within the application, controllers, and models. They are stored in the Java packages for each type.
The web files are located under the /WebContent folder. This folder could include .html, .jsp, or any other kind of web browser file. Another folder of importance within /WebContent is the /WEB-INF folder. This folder contains the web.xml file which is important because it defines the public facing URLs for your java classes.
Finally, the last folder of note is the lib folder. This is where packaged .jar files are stored to be used as extensions within java classes and .jsp files. Examples could include a JSON, MySQL, or Java Standard Tag Library, more commonly known as JSTL.
The beginning of this file contains many import statements. These lines import code from other files in the application and from public Java classes. In this class there are import statements for a JSON library, HTTP servlet (a java class) classes, and a custom made Java class stored within the models package. There is only one method in this class, and that is the doPost method. It is important to note that the name of this method is important because it signifies that the request posts information along with its request that can be used to determine what information to send back to the user. In this case the information posted is a beacon’s uuid, major, and minor values. The beginning of this method creates an array of strings for the resulting information returned from the database. Then it defines three important variables. These are the uuid, major, and minor values that were posted with the URL request. The next line instantiates the Java class for dispatching the response to the request. This will be used after the information to be returned has been determined. Then the beacon is authenticated against the database using a custom BeaconAuthenticator class defined below. After authenticating the beacon, the uuid, major, and minor values are compared with the returned values to be sure the correct beacon was returned. If it is the correct beacon, the request dispatcher is created and set to the beacon_success.jsp file and the request attributes are set to the returned values. If the correct information is not returned then an error page is used instead. Finally, the dispatcher is told to forward the request and response back.
This controller class starts very similar to the the BeaconController, however its imports are slightly different and it only has one variable, a boolean used to determine if a device is being turned off or on. This method is also a doPost because the iOS app is going to post a key value pair of “onoroff” : value. Based on this parameter the boolean is set to true or false. Next the method to run the script is called. First a string is created. This string will be processed and run as a command line command and is composed of three parts. The first part is the path to the script being called, the second is the IP address of the Wemo switch on the network, and finally the last part is either the text on or off to tell the switch to turn itself on or off. The rest of this method might seem somewhat more confusing however it is all in order to run the wemo.sh script. The ProcessBuilder class is a Java class that is used to run commands that would normally be run through a terminal or command prompt from the server code.
Unlike the BeaconController class, this class only needs to import the SQL classes so that mysql statements can be executed through Java. This class only has a few variables defined at the beginning and these are holders for the posted uuid, major, and minor, as well as the returned name and usecase. In addition there is a boolean for the success of the authenticator and a Connection that will create the link to the SQL database.
The first method of this class attempts to connect to a mysql instance given its URL and login credentials.
The next method is what is of real consequence. The authenticateBeacon class is a return type of String[] noted by its designation after public and before its name. This means that running this method returns an array of strings, in this case, to the BeaconController class.
First this method creates an empty array of 5 strings that will be returned after they are populated. Then the method opens a connection to the database, creates the SQL query as a string, then creates a PreparedStatement object using the connection and the SQL string, and then creates a ResultSet for entries returned from the query. Next the result set is iterated through using a while that checks if there is a next entry in the resultSet. This chunk of code assigns the results to the beacon variables created at the beginning, specifically the uuid, major, minor, name, and usecase variables. Then if the posted uuid, major, and minor values still equal the returned values for those variables, the success boolean is set to true and the response array is filled with the uuid, major, minor, name, and usecase. It is important to note that before the response is returned and the method is finished, the resultSet, PreparedStatement, and Connection all call their close() method. This is important because it is bad to leave these open as they could be compromised. The best method is to close each as soon as it isn’t needed, but it is still acceptable as long as they are all closed before the end of the method. Finally, the response created is returned.
WebContent
WEB-INF
/lib
JSON - a library imported to format JSON output in a .jsp file.
JSTL - a library to utilize java standard library tags
MySQL - a library to connect to and query a mysql database
web.xml
.jsp, .html, and other web files
This xml file is where the outward facing URLs to execute tyour Java controller classes are formed. Each controller or URL has two xml parts a servlet and a servlet-mapping.
Within the servlet there are four inner parts, the description, display-name, servlet-name, and servlet-class. The first two are for the user, a description and the name to display.
The second two are important as the servlet-name is the name of the servlet class, and the servlet-class is the full path to the controller, i.e. project_name.controller_package_name.java_class_name.
Within the servlet-mapping there are only two inner parts. The servlet-name is repeated as in the above part. The important part is the url-pattern part. This is always started with a / and followed by the URL name. A template full path would be domain_name or IP address: 8080/project_name/url-pattern or company.com:8080/project/Controller.
This is a short .jsp page that is used to return beacon uuid, major, minor, name, and usecase values to the caller (in this case the iOS application). First, the contentType is set to application/json and the JSON object class is imported in order to create a JSON object. Finally, the JSON object is created and the attributes listed above are put into the JSONObject.