RESTEasy Interceptors tutorial

RESTEasy is a great JAX-RS 1.1 implementation that comes with JBoss AS 7. However, JAX-RS 1.1 lacks some important features such as Interceptors. As you may have concluded, interceptors will allow you to perform some action around the JAX-RS method execution. It let you do a few interesting things:

  • Logging;
  • Security;
  • Validation;
  • Complement the HTTP Request;
  • Complement the HTTP Response;
  • Add some additional business logic for a set of existing services.

Luckily RESTeasy brings a rich set of features which you can use in your application, one of these feature is a rich interceptor model. In this article we will show you how to create and register interceptors with a very simple example.

A service for Trigonometry basic calcs

Most of the readers should know about basic trigonometry, if not, it should not be  a problem. Our idea is to have working a simple REST Web Service which will allow you to calculate all basic trigonometric functions and keep a history of the recent calculations performed. So our application requirements are simple calculate, keep history and allow the history clean.

For this we created a simple model object:

package org.mastertheboss.resteasy.model;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class RightTriangle {
    private double adjacent;
    private double opposite;
    private double hypotenusa;

    private double sine;
    private double cosine;
    private double tangent;

    // gets and sets omitted
}


And then a JAX-RS resource to allow us to expose these requirements as a REST Web Service:
package org.mastertheboss.resteasy.resources;

import java.util.List;

import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;

import org.mastertheboss.resteasy.controller.TrigonometryController;
import org.mastertheboss.resteasy.model.RightTriangle;

@Path("trigonometry")
public class TrigonometryService {

    /**
     * A RPC function to calculate the basic trigonometric functions for the
     * given parameters
     * 
     * @param hypotenusa
     * @param adjacent
     * @param opposite
     * @return
     */
    @Path("calculate")
    @POST
    public RightTriangle calculateAllBasicTrigonometricFunctions(
            @FormParam("hypotenusa") double hypotenusa,
            @FormParam("adjacent") double adjacent,
            @FormParam("opposite") double opposite) {
        return TrigonometryController.calculate(hypotenusa, adjacent, opposite);
    }

    @Path("history")
    @GET
    public List<RightTriangle> history() {
        return TrigonometryController.getHistory();
    }

    @Path("history")
    // @DELETE it could be delete, but we are using post for ease the WEB page
    @POST
    public String clearAll() {
        TrigonometryController.clearHistory();
        // will return 200
        return "History cleaned with success";
    }
}


Our Resource is doing what it was supposed to do and it is working great. The problem is that after a few days “in production” the requirements for the application increased, we now have to add the following features to our existing service:

  • Logging of all requests to make easy the error troubleshooting if we have some trouble in production;
  • Validation to avoid misuse of the service
  • Security check to avoid unauthorized users to delete the history

As the service is already working and it presents no problems, it was decided to use something more appropriate than change the REST services code. The solution fits very well for interceptors because the goal of these new requirement are not related with the main goal of our service, and we want to maintain these new features in an independent way, without having to change the code of the service all the time.

 

Search our tutorials