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.

quinta-feira, 10 de julho de 2014

JavaFX apps using Javascript and Gainda

In my last posts I've worked the with Nashorn Javascript engine to create JavaFX application.

It's great to work with JavaFX and Javascript, however, we can't forget that a boring part of the javascript code is that we have to import Java classes do be used and this can be a tedious things because you just want to script the view... See some examples of imports I had to use in my last projects

var Scene = Java.type('javafx.scene.Scene')
var VBox = Java.type('javafx.scene.layout.VBox')
var Label = Java.type('javafx.scene.control.Label')
var TextField = Java.type('javafx.scene.control.TextField')
var PieChart = Java.type('javafx.scene.chart.PieChart')
var TableView = Java.type('javafx.scene.control.TableView')
var TableColumn = Java.type('javafx.scene.control.TableColumn')
var PropertyValueFactory = Java.type('javafx.scene.control.cell.PropertyValueFactory')
var SearchService = Java.type('org.jugvale.sentiments.service.SearchService')
var TextSentimentService = Java.type('org.jugvale.sentiments.service.TextSentimentService')
var TableView = Java.type('javafx.scene.control.TableView')
var FXCollections = Java.type('javafx.collections.FXCollections')


Well, it can be improved and that's the goal of the Gainda project. Gainda means Nashorn in Hindi (गैंडा) and Nashorn is the name of the new Javascript engine that comes with Java 8.
With Gainda, we have like a DSL for JavaFX based on Javascript. See how a Hello World application looks like with Gainda:

load('./dist/gainda.js');

Gainda.run([ 'base', 'graphics', 'controls' ], function (stage) {

    var button = new Button();
    var root = new StackPane();

    stage.title = "Hello World!";
    
    button.text = "Say 'Hello World'";
    button.onAction = function() print("Hello World!");
    
    root.children.add(button);

    stage.scene = new Scene(root, 300, 250);
    stage.show();

});


Awesome! The project was created by Rajmahendra, the leader of the Chennai JUG.

My first application using Gainda

Here's the step-by-step to create an application using this framework:

1) Download the gainda.core.js file from github;
2) load it in your application: load('./gainda.core.js');
3) everything happens on run function, you must call this function and pass the modules you want to load, then you are ready to write JavaFX app using Gainda
4) Run it using jjs that can be found in JDK 8 bin folder, use the following command to run it:

$ jjs -fx myApp.js

My first application is a simple app that blinks a text when we click on it:

load('./gainda.core.js');
Gainda.run([ 'base', 'graphics', 'controls' ], function (stage) {
    var txt = new Text("Hello, Gainda!");
    var blink = new FadeTransition(Duration.millis(200), txt);
    txt.effect = new Reflection();
    txt.font = new Font(50);
    txt.fill = Color.RED
    blink.fromValue = 1;
    blink.toValue = 0;
    blink.autoReverse = true;
    blink.cycleCount = 2;
    txt.onMousePressed = function(e){
            blink.playFromStart();    
    };  
    stage.title = "Hello Gainda!";
    stage.scene = new Scene(new StackPane(txt), 350, 150);
    stage.show();
});

Conclusion

Gainda is a new utility for who likes to use another language to create user interface. The project is still starting and if you want to contribute, please contact Rajmahendra.