quinta-feira, 21 de agosto de 2014

JavaEE, JavaFX and RFID - Part 4: The Client

Finally! It's the last post of the series about a JavaFX application that reads a RFID tag and talks to a JEE application that is in the "cloud".

Part 1: The Application
Part 2: Reading RFID from Java 
Part 3: REST API and Security
Part 4: The Client

The JavaFX application

This application aims to allow the user to input data from their RFID Tag and using a high level client(JAX-RS 2), access the data somewhere, which, in our case, is in Openshift.


The initial screen of our application


Application Structure

Basically we have FXMLs, for each FXML we have a controller that interacts with the PersonService class:




Brief explanation:
  • In model package we simply have simply Java that represent our business, in this case information about people, so we have a Person class.
  • *.fxml files are the view part of our application. These files can be opened in SceneBuilder for edition in a visual way. You can edit the fxml files directly as well...
  • FXMLs have a controller. The *Controller classes are responsible to handle user input and update the view with Person information;
  • Some basic styling was done using the appStyle.css file
  • The point of access to Person information is the PersonService class. It allow us to interact with the Person objects. We have two PersonService implementation: Mock and REST. The Mock represents a fake service, storing the data in memory, the REST service does the actual job of talking to Openshift to send and receive information using JAX-RS 2 client API
  • The rfid package contains the RFID pane, that when opened will wait the user pass a tag on the reader and tell our program the RFID of that tag;
  • AppTask is something I created to do a task, like query the server, and that the app is busy. I should have added a cancel button, but usually the REST client will send a timeout error if the server is not available; It has a Boolean property to indicate that the application is loading something and navigation binds this  property to an overlay progress indicator:

  • Finally we have the class AppNavigation that allow us to easily navigate between the application screens without having to spread FXML loads through the application. The main advantage of this class is that I can control the screens in a centralized point, so I could add later animation to the screen navigation and an overlay pane to shows a progress indicator while we are doing something.
That's basically it. The source code is on github and any suggestion is welcome.

Demo


Here's a short video with the app in action!




Conclusion

In these series the main goal was to have fun, the second was to show a PoC about using JavaFX and JEE technologies. Since this application had to access local resources(RFID reader), it makes sense to use JavaFX.
For future I want to run the client application on a raspberry pi with a touch screen!

quinta-feira, 14 de agosto de 2014

JavaEE, JavaFX and RFID - Part 3: REST API and Security (+ Openshift)

Continuing our series of post about RFID and JavaFX, we are going to show today how to we expose the application database using a REST interface and how we secure it.

Part 1: The Application
Part 2: Reading RFID from Java 
Part 3: REST API and Security
Part 4: The Client


For this part, we will use Wildfly(The JBoss community application server) and we will deploy a REST application to it that access our database.

 

Why REST?

RESTful APIs allow us to access information in a stateless manner. It also allow us to access this information in a remote centralized way, so any application can access it.
RESTful APIs also makes easy to integrate your system. In our blog post we are talking about a simple and small application, now imagine we have a big database of employees or products that we wants to integrate with a RFID system and we want to add thousands of data to our RFID system. It's much easier when the system is exposed using REST.

 

REST and Java

REST Web Services can be created using the JavaEE JAX-RS API, where we can simply annotate Java classes to add HTTP information to it, and deploy it on an Application Server, that will expose our classes methods to be accessed from HTTP.
If we want to activate JAX-RS on our WEB Application that is deployed in an application server that implements JEE 6, we need to use an application that extends Application and uses an annotation ApplicationPath, where we indicate the context for all the JAX-RS resources:

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("rest")
public class JaxRSActivator extends Application {

}

 

 The REST API

To access our database, we use CDI to inject the service class we talked about in the last post. Then we just need to create a class that contains the appropriate JAX-RS annotations, see:





It means that access to the database will be done as summarized in the following lines:

Getting all registered people
GET on /rest/person URI will return a list of the people in the DB in JSON format;
Adding a new Person
POST on /rest/person sending a person representation in JSON format will add it to the DB;
Removing an existing person
DELETE on /rest/person/{person id} will remove it from the DB;
Getting a person by RFID
GET on /rest/person/rfid/{person rfid} the person from DB that contains the given RFID.

 

Securing the REST API

To secure our app, we uses JAAS integrated with Wildfly (see more about security on JEE). That was really simple since we used a security domain that is already available with Wildfly, it's named "other". To add an user to this security domain, we use the add-user.sh script which is located at the bin directory of a Wildfly installation:



Now, we configure our application to use that security domain to authenticate our REST WS. It simply done by declaring the security that will be used in jboss-web.xml then declaring security on web.xml! 



Now our REST methods are secured, see above that all the stuff under rest context will require basic authentication. Of course we could improve the security a lot by using SSL and other authentication ways, however, in this app we will simply use basic authentication...

 

The WAR application

We used Maven to build our application application. See the project structure below:






We use mvn clean package to create a war file in the project target's directory. This file will be ready to be deployed on a JBoss AS 7.1, EAP 6.x or Wildfly application server.

 

Application on cloud

I put my project on Openshift, so anyone can have access to it! The first thing I did was setup my JBDS to use Openshift.  I also had to change the security I just described since it seems that I don't have acess to add-users.sh on Openshift.

What I did was:

  • Edited .openshift/conf/standalone.xml to add files to contains the user information which will be used on the authentication process:

  • Then I had to ssh the openshift server using rhc ssh people to create the files that contains the user information! (rest-users.properties and rest-roles.properties)
I could create another security domain for my application specifically, but I noticed other was empty, so I decided to use it. The other change was that Openshift was doing something with the default JBoss AS database, so I decided to move to MySQL. And it was REALLY easy, I just had to:

  • Added a MySQL cartridge to my application in Openshift administration;
  • JBoss AS is pre-configured with a MySQL DS! I just had to modify persistence.xml to point to the java:jboss/datasources/MySQLDS datasource instead the one I was deploying. The pre-configured DS includes everything using environments variable...

The application on OpenShift is here. Access person resource to see the data we have on DB (restadmin/restadmin123!)

Conclusion

So far what we have is a REST WEB Service to server people information stored in the default filesystem based database. Now we need to add a way to humans interact with it. On next post, we will show a JavaFX client we created to connect to the cloud and send RFID information!

domingo, 10 de agosto de 2014

JavaEE, JavaFX and RFID - Part 2: Reading RFID from Java

Hello, continuing our series of posts about RFID and JavaFX, today we are going to show you how we read the RFID tag information from the receptor using Java. Remember we are using the RFID USB Starter Kit, the information of this post is specific for this kit, however, I believe most of the information applies to others RFID readers.

Part 1: The Application
Part 2: Reading RFID from Java 
Part 3: REST API and Security
Part 4: The Client


The RFID reader

RFID is simply an identification that will be read using a Radio Frequency. The ID is in RFID tags which can be read using a RFID Reader. My purpose is only read the tag, so we won't go deep in the writing part, we will focus on reading a RFID tag.

RFID Diagram

I used the simplest RFID Reader I could find since I was avoiding unnecessary trouble. The reader is connected by USB and we just have to connect it to a PC so we can start hearing the buzz when we approach a RFID tag to the reader.



Notice that the reader and the tags have some conditions such as frequency of operation. I won't go deep on these details on this post.
The reader I bought is integrated with the FTDI chip and it has total support for installation in the popular operating systems we have. Notice that the main duty of this chip was to interface the old RS232 interface with USB, so we need a driver.

Installing the library(driver)

Initially I found some issues to install the driver so I can perform the communication with the chip and simply read the incoming data. In another scenarios, it's usual to also write data, but today we will only read.
First thing I did was to download the driver on D2XX web page, then I followed the instructions on the installation guide for linux, which was basically was run the following commands:

sudo cp /releases/build/arch/lib* /usr/local/lib
cd /usr/local/lib 
sudo ln –s libftd2xx.so.1.1.12
libftd2xx.so 
sudo chmod 0755 libftd2xx.so.1.1.12 

* arch is the architecture of my operating system.

After this, I could see that a new directory was created under /dev when I connected my RFID reader using the USB cable:


So I opened the file(well, actually a symlink) under serial/by-id directory and I was able to read the content of my RFID when I approached the RFID tag to the reader:

After setting native stuff, it was time to find a way to read it from Java!

Reading FTDI from Java

In D2XX page we can find two libraries to read it from Java, however, I tried to use these and I had issues. The main issue was related to the native part and, as I said, I didn't want to waste time on this...
Well, everything is a file in Linux, remember we were reading data using cat command. Why not read the content of that file from Java? Having this in mind, I created a simple program to read the content of that file, see the code:



Notice that it keeps the file open and busy(see method read), so it's not recommended to continuously read the file as did for test. A better implementation would be using the "new" WatchService, but it seems it doesn't work for symbolic links!

A reader from JavaFX

I tried to create a control that will perform the read for me. It's specific to the conditions I showed above, so I think it's not too reusable...
What I have it's a dialog and it lists the devices and then I can choose a device. After I choose it, it will accumulate the RFIDs that were read into a List. When you click a button, the dialog will dispose and a list of the read RFID will be available to the caller... See the source here and a screenshot:







Conclusion

Read RFID from Java is not a hard task on Linux. It could be, however, simpler if we had a library to make the operations for us.

Source Code on github

sexta-feira, 1 de agosto de 2014

JavaEE, JavaFX and RFID - Part 1: The Application


Hello! Today I'm going to show how is easy to create a JavaFX application that integrates with a RFID reader. Specifically with the RFID Starter Kit.

It's a complete application that reads a RFID tag and then search information about it on a remote database accessed through a REST interface. To avoid an extensive blog post, I divided it in 4 parts:

Part 1: The Application
Part 2: Reading RFID from Java
Part 3: REST API and Security
Part 4: The Client

The application

We will demonstrate the use of RFID with JavaFX using a simple Application where a person with  will pass a card on a RFID reader and our application will give more information about the person, or if the person is in the database.
The application also allows you to register a new person, delete existing people and modify information.
The app will basically work as demonstrated on the following diagram:


 The Application Database


The database contains person information and also the RFID related to it and we give roles to this person, so we can authorize the person(if needed).









We also have a table to add administrators that can access this database information:










Our application is responsible to guarantee that only authorized users can access and change the information in the database. As you can see, the table person has a column for the RFID tag and the client application will be simple responsible to read it and access the person information using a REST API.

Accessing the data from Java using JPA

We use Java to access the database. Our final application will be in a WAR deployed in an application server (I'll talk more about it on the next post) that implements JEE 6 specification, and one of the famous JEE specification is JPA, that allow us to handle database information.
To set the application persistence we need to have the persistence.xml file, which is the descriptor  responsible to declare general JPA configuration. See persistence.xml.

Notice it refers to a Datasource, the application server we will use is Wildfly and datasources can be declared using *-ds.xml. Deployable datasources can be placed in the WEB-INF directory of our WAR. In our application we have the  people-ds.xml, that we configure to access the default filesystem based database that comes with Wildfly. Code of both XMLs:

Now our application is ready to access a database and to have access to it from Java, we created model objects and we use JPA annotations in order to declare the database correspondence with our Java class. See Person.java source code.
Finally, to handle this information, we use a class named EntityManager in our PersonService Stateless EJB class.

Now we are ready to expose our DB information through a REST interface. But it's a subject for the next post ;)

See Complete Source code of the WEB module

Conclusion

We presented our application modeling and the database access. It's a simple application, however, we will briefly cover all technologies used to create it. Next part we will talk about the REST interface and how we secure it.