Validating JAX-RS resource data with Bean Validation in Java EE 7 and WildFly

April 1st, 2014 by Samuel Santos 7 comments »

I have already approached this subject twice in the past. First, on my post Integrating Bean Validation with JAX-RS in Java EE 6, describing how to use Bean Validation with JAX-RS in JBoss AS 7, even before this was defined in the Java EE Platform Specification. And later, on an article written for JAX Magazine and posteriorly posted on JAXenter, using the new standard way defined in Java EE 7 with Glassfish 4 server (the first Java EE 7 certified server).
Now that WildFly 8, previously know as JBoss Application Server, has finally reached the final version and has joined the Java EE 7 certified servers club, it’s time for a new post highlighting the specificities and differences between these two application servers, GlassFish 4 and WildFly 8.

Specs and APIs

Java EE 7 is the long-awaited major overhaul of Java EE 6. With each release of Java EE, new features are added and existing specifications are enhanced. Java EE 7 builds on top of the success of Java EE 6 and continues to focus on increasing developer productivity.

JAX-RS, the Java API for RESTful Web Services, is one of the fastest-evolving APIs in the Java EE landscape. This is, of course, due to the massive adoption of REST-based Web services and the increasing number of applications that consume those services.

This post will go through the steps required to configure REST endpoints to support a JavaScript client and to handle validation exceptions to send localized error messages to the client in addition to HTTP error status codes.

Source code

The source code accompanying this article is available on GitHub.

» Read more: Validating JAX-RS resource data with Bean Validation in Java EE 7 and WildFly

Migrating from a Subversion repository to GitHub

November 7th, 2013 by Samuel Santos No comments »

One of greatest attractions of GitHub is the community and the tooling that allows this community to share code. Each contributor can clone the repository, make their changes and then send you a pull request. As the project maintainer your job is now a whole lot easier and more manageable. No more patch files to worry about.

Follow the recipe bellow to move your SVN repo to Git.
Best of all, you get to keep the entire commit history of your project.

» Read more: Migrating from a Subversion repository to GitHub

PT.JUG at OSDOC 2013

July 12th, 2013 by Samuel Santos No comments »

Portugal Java User Group (PT.JUG) introduction at OSDOC 2013.

Minify Maven Plugin adds support for Google Closure Compiler

February 18th, 2013 by Samuel Santos No comments »

I’ve recently implemented some new improvements to Minify Maven Plugin.
Version 1.6 added support for Google Closure Compiler and version 1.7 offers several performance improvements and more detailed logs (e.g. compression benefits from minification).

A complete list of changes can be found in the changelog file.
For next releases I’m planning to add Source Maps support and improve the documentation.

Integrating Bean Validation with JAX-RS in Java EE 6

January 10th, 2013 by Samuel Santos 9 comments »

This article was first published on Java Advent Calendar.

Introduction to Bean Validation

JavaBeans Validation (Bean Validation) is a new validation model available as part of Java EE 6 platform. The Bean Validation model is supported by constraints in the form of annotations placed on a field, method, or class of a JavaBeans component, such as a managed bean.

Several built-in constraints are available in the javax.validation.constraints package. The Java EE 6 Tutorial lists all the built-in constraints.

Constraints in Bean Validation are expressed via Java annotations:

public class Person {
    @NotNull
    @Size(min = 2, max = 50)
    private String name;
    // ...
}

Bean Validation and RESTful web services

JAX-RS 1.0 provides great support for extracting request values and binding them into Java fields, properties and parameters using annotations such as @HeaderParam, @QueryParam, etc. It also supports binding of request entity bodies into Java objects via non-annotated parameters (i.e., parameters that are not annotated with any of the JAX-RS annotations). Currently, any additional validation on these values in a resource class must be performed programmatically.

The next release, JAX-RS 2.0, includes a proposal to enable validation annotations to be combined with JAX-RS annotations. For example, given the validation annotation @Pattern, the following example shows how form parameters could be validated.

» Read more: Integrating Bean Validation with JAX-RS in Java EE 6

Running Drools 5.4.0 Final as a JBoss AS 7 module

November 6th, 2012 by Samuel Santos No comments »

Drools 5 introduces the Business Logic integration Platform which provides a unified and integrated platform for Rules, Workflow and Event Processing. It’s been designed from the ground up so that each aspect is a first class citizen, with no compromises.

Drools 5 has splitted up into 4 main sub projects:

  • Drools Guvnor (BRMS/BPMS)
  • Drools Expert (rule engine)
  • Drools Flow (process/workflow)
  • Drools Fusion (cep/temporal reasoning)

In this example we will focus on how we can use Drools Expert inside JBoss Application Server 7.

» Read more: Running Drools 5.4.0 Final as a JBoss AS 7 module

Comparing Device Description Repositories

October 2nd, 2012 by Samuel Santos 11 comments »

Update: OpenDDR and 51Degrees.mobi dependencies have been updated on 2014-01-20.

Web content delivered to mobile devices can benefit from being tailored to take into account a range of factors such as screen size, markup language support and image format support. Such information is stored in “Device Description Repositories” (DDRs).

Until recently WURFL was the de facto DDR standard for mobile capabilities, but its license changed to AGPL (Affero GPL) v3, meaning it is not free to be used commercially anymore. Consequently some free open source alternatives to WURFL have recently started to show up and are improving quickly.

OpenDDR and 51Degrees.mobi are candidate substitutes to WURFL that also provide an API to access DDRs.

These tools ease and promote the development of Web content that adapts to its delivery context. This post summarizes the installation and configuration of these tools and analyzes how they compare in terms of image adaptation.

The source code used for this post is also available on GitHub.

» Read more: Comparing Device Description Repositories

Java EE 6 Testing Part II – Introduction to Arquillian and ShrinkWrap

May 3rd, 2012 by Samuel Santos 8 comments »

In Java EE 6 Testing Part I I briefly introduced the EJB 3.1 Embeddable API using Glassfish embedded container to demonstrate how to start the container, lookup a bean in the project classpath and run a very simple integration test.

This post focus on Arquillian and ShrinkWrap and why they are awesome tools for integration testing of enterprise Java applications.

The source code used for this post is available on GitHub under the folder arquillian-shrinkwrap.

The tools

Arquillian

Arquillian brings test execution to the target runtime, alleviating the burden on the developer of managing the runtime from within the test or project build. To invert this control, Arquillian wraps a lifecycle around test execution that does the following:

  • Manages the lifecycle of one or more containers
  • Bundles the test case, dependent classes and resources as ShrinkWrap archives
  • Deploys the archives to the containers
  • Enriches the test case with dependency injection and other declarative services
  • Executes the tests inside (or against) the containers
  • Returns the results to the test runner for reporting
ShrinkWrap

ShrinkWrap, a central component of Arquillian, provides a simple mechanism to assemble archives like JARs, WARs, and EARs with a friendly, fluent API.

One of the major benefits of using Arquillian is that you run the tests in a remote container (i.e. application server). That means you’ll be testing the real deal. No mocks. Not even embedded runtimes!

Agenda

The following topics will be covered on this post:

  • Configure the Arquillian infrastructure in a Maven-based Java project
  • Inject EJBs and Managed Beans (CDI) directly in test instances
  • Test Java Persistence API (JPA) layer
  • Run Arquillian in client mode
  • Run and debug Arquillian tests inside your IDE

» Read more: Java EE 6 Testing Part II – Introduction to Arquillian and ShrinkWrap

Java EE 6 Testing Part I – EJB 3.1 Embeddable API

December 6th, 2011 by Samuel Santos 7 comments »

One of the most common requests we hear from Enterprise JavaBeans developers is for improved unit/integration testing support.
EJB 3.1 Specification introduced the EJB 3.1 Embeddable API for executing EJB components within a Java SE environment.

Unlike traditional Java EE server-based execution, embeddable usage allows client code and its corresponding enterprise beans to run within the same JVM and class loader. This provides better support for testing, offline processing (e.g. batch), and the use of the EJB programming model in desktop applications.
[...]
The embeddable EJB container provides a managed environment with support for the same basic services that exist within a Java EE runtime: injection, access to a component environment, container-managed transactions, etc. In general, enterprise bean components are unaware of the kind of managed environment in which they are running. This allows maximum reusability of enterprise components across a wide range of testing and deployment scenarios without significant rework.

» Read more: Java EE 6 Testing Part I – EJB 3.1 Embeddable API

Changing URL parameters with jQuery

September 20th, 2011 by Samuel Santos 7 comments »

You can find plenty of resources about this topic just by googling the web, most of which will point to jQuery plugins.
But the fact is that it’s so easy to achieve this by simply using jQuery that you do not need a plugin.

The code is pretty much self explanatory:

/*
 * queryParameters -> handles the query string parameters
 * queryString -> the query string without the fist '?' character
 * re -> the regular expression
 * m -> holds the string matching the regular expression
 */
var queryParameters = {}, queryString = location.search.substring(1),
    re = /([^&=]+)=([^&]*)/g, m;

// Creates a map with the query string parameters
while (m = re.exec(queryString)) {
    queryParameters[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
}

// Add new parameters or update existing ones
queryParameters['newParameter'] = 'new parameter';
queryParameters['existingParameter'] = 'new value';

/*
 * Replace the query portion of the URL.
 * jQuery.param() -> create a serialized representation of an array or
 *     object, suitable for use in a URL query string or Ajax request.
 */
location.search = $.param(queryParameters); // Causes page to reload

You can clearly improve the regular expression, but the one above meet my needs.