Tuesday, February 8, 2011

Spring 3 - DynamicJasper - Hibernate Tutorial: Using JRDataSource

In this tutorial we will build a simple Spring MVC 3 application with reporting capabilities. We will use DynamicJasper to generate the dynamic reports and Hibernate for the ORM framework. We will use Jasper's JRDataSource to wrap our Java List. We will use this wrapped object as our data source. The data will be retrieved from a MySQL database.

This tutorial is part of the following reporting tutorial series that uses Jasper, DynamicJasper, and Apache POI:

Spring 3 - Apache POI - Hibernate: Creating an Excel Report Tutorial
Spring 3 - DynamicJasper - Hibernate Tutorial: Concatenating a DynamicReport
Spring 3 - DynamicJasper - Hibernate Tutorial: Concatenating a Subreport
Spring 3 - DynamicJasper - Hibernate Tutorial: Using Plain List
Spring 3 - DynamicJasper - Hibernate Tutorial: Using JRDataSource
Spring 3 - DynamicJasper - Hibernate Tutorial: Using HQL Query

All of these tutorials produce the same document, and all of them demonstrate different ways of creating the same report.

What is DynamicJasper?
DynamicJasper (DJ) is an open source free library that hides the complexity of Jasper Reports, it helps developers to save time when designing simple/medium complexity reports generating the layout of the report elements automatically.

Source: http://dynamicjasper.com/

What is JasperReports?
JasperReports is the world's most popular open source reporting engine. It is entirely written in Java and it is able to use data coming from any kind of data source and produce pixel-perfect documents that can be viewed, printed or exported in a variety of document formats including HTML, PDF, Excel, OpenOffice and Word.

Source: http://jasperforge.org/projects/jasperreports

Background

Before we start our application, let's preview first the final print document:

Our document is a simple Excel document. It's a Sales Report for a list of power supplies. The data is retrieved from a MySQL database.

Domain

Notice that for each Power Supply entry there's a common set of properties:
id
brand
model
maximum power
price
efficiency

Development

Domain

We'll start our application by declaring the domain object PowerSupply

PowerSupply.java
package org.krams.tutorial.domain;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 * A simple POJO containing the common properties of a Power Supply
 * This is an annotated Hibernate entity. 
 * 
 * @author Krams at {@link http://krams915@blogspot.com}
 */
@Entity
@Table(name = "POWER_SUPPLY")
public class PowerSupply implements Serializable {

 private static final long serialVersionUID = 8634209606034270882L;

 @Id
 @Column(name = "ID")
 @GeneratedValue
 private Long id;
 
 @Column(name = "BRAND")
 private String brand;
 
 @Column(name = "MODEL")
 private String model;
 
 @Column(name = "MAXIMUM_POWER")
 private String maximumPower;
 
 @Column(name = "PRICE")
 private Double price;
 
 @Column(name = "EFFICIENCY")
 private Double efficiency;

 public Long getId() {
  return id;
 }

 public void setId(Long id) {
  this.id = id;
 }

 public String getBrand() {
  return brand;
 }

 public void setBrand(String brand) {
  this.brand = brand;
 }

 public String getModel() {
  return model;
 }

 public void setModel(String model) {
  this.model = model;
 }

 public String getMaximumPower() {
  return maximumPower;
 }

 public void setMaximumPower(String maximumPower) {
  this.maximumPower = maximumPower;
 }

 public Double getPrice() {
  return price;
 }

 public void setPrice(Double price) {
  this.price = price;
 }

 public Double getEfficiency() {
  return efficiency;
 }

 public void setEfficiency(Double efficiency) {
  this.efficiency = efficiency;
 }
 
}
PowerSupply is a simple POJO containing six private fields. Each of these fields have been annotated with @Column and assigned with corresponding database column names.
ID
BRAND
MODEL
MAXIMUM_POWER
PRICE
EFFICIENCY

Service

We'll be declaring a single service named DownloadService. This service is the heart of the application that will process and retrieve the report document.

The service will run the following steps:
1. Build the report layout
 2. Add the datasource to a HashMap parameter
 3. Compile the report layout
 4. Generate the JasperPrint object
 5. Export to a particular format, ie. XLS
 6. Set the HttpServletResponse properties
 7. Write to the output stream

DownloadService.java
package org.krams.tutorial.service;

import java.io.ByteArrayOutputStream;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.krams.tutorial.domain.PowerSupply;
import org.krams.tutorial.report.Exporter;
import org.krams.tutorial.report.Layouter;
import org.krams.tutorial.report.Writer;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import ar.com.fdvs.dj.core.DynamicJasperHelper;
import ar.com.fdvs.dj.core.layout.ClassicLayoutManager;
import ar.com.fdvs.dj.domain.DynamicReport;
import ar.com.fdvs.dj.domain.builders.ColumnBuilderException;
import javax.annotation.Resource;

/**
 * Service for processing DynamicJasper reports
 * 
 * @author Krams at {@link http://krams915@blogspot.com}
 */
@Service("downloadService")
@Transactional
public class DownloadService {

 private static Logger logger = Logger.getLogger("service");
 
 @Resource(name="sessionFactory")
 private SessionFactory sessionFactory;
 
 /**
  * Processes the download for Excel format.
  * It does the following steps:
  * <pre>1. Build the report layout
  * 2. Retrieve the datasource
  * 3. Compile the report layout
  * 4. Generate the JasperPrint object
  * 5. Export to a particular format, ie. XLS
  * 6. Set the HttpServletResponse properties
  * 7. Write to the output stream
  * </pre>
  */
 @SuppressWarnings("unchecked")
 public void downloadXLS(HttpServletResponse response) throws ColumnBuilderException, ClassNotFoundException, JRException {
  logger.debug("Downloading Excel report");

  // 1. Build the report layout
  DynamicReport dr = Layouter.buildReportLayout();

  // 2. Retrieve the datasource
  JRDataSource ds = getDatasource();
  
  // 3. Compile the report layout
  JasperReport jr = DynamicJasperHelper.generateJasperReport(dr, new ClassicLayoutManager(), null);
  
  // 4. Generate the JasperPrint object which also fills the report with data
  JasperPrint jp = JasperFillManager.fillReport(jr, null, ds);

  // We can also combine compilation (3) and generation (4) in a single line
  //JasperPrint jp = DynamicJasperHelper.generateJasperPrint(dr, new ClassicLayoutManager(), ds);
  
  // Create the output byte stream where the data will be written
  ByteArrayOutputStream baos = new ByteArrayOutputStream();

  // 5. Export to XLS format
  Exporter.exportToXLS(jp, baos);
  
  // 6. Set the response properties
  String fileName = "SalesReport.xls";
  response.setHeader("Content-Disposition", "inline; filename=" + fileName);
  // Make sure to set the correct content type
  response.setContentType("application/vnd.ms-excel");
  // Assign the byte stream's size
  response.setContentLength(baos.size());

  // 7. Write to response stream
  Writer.write(response, baos);
 }
 
 /**
  * Retrieves a JRDataSource from a Hibernate HQL query. 
  * This datasource is a Java List wrapper.
  * @return
  */
 @SuppressWarnings("unchecked")
 private JRDataSource getDatasource() {
  
      // Retrieve session
  Session session = sessionFactory.getCurrentSession();
  // Create query for retrieving products
  Query query = session.createQuery("FROM PowerSupply");
  // Execute query
  List<PowerSupply> result = query.list();
  
  // Wrap the collection in a JRBeanCollectionDataSource
  // This is one of the collections that Jasper understands
  JRDataSource ds = new JRBeanCollectionDataSource(result);
  
  // Return the datasource
  return ds;
 }
}
This service is our download service for generating the report document. It should be clear what each line of code is doing. Notice that in step 4 we declared the following:
JasperPrint jp = JasperFillManager.fillReport(jr, null, ds);
The first parameter jr pertains to the JasperReport object. The second parameter is normally a HashMap parameter, but it's empty here because it has no use. The third parameter ds is the datasource as declared in step 2.

Also, we've declared a getDatasource() method that retrieves a list of PowerSupply. The list is then wrapped with Jasper's JRDataSource.
JRDataSource ds = new JRBeanCollectionDataSource(result);

The service has been divided into separate classes to encapsulate specific jobs.

The Layouter

The purpose of the Layouter is to layout the design of the report. Here's where we declare the dynamic columns and special properties of the document. At the end of the class, we appended an HQL query

Layouter.java
package org.krams.tutorial.report;

import java.util.Date;

import org.apache.log4j.Logger;

import ar.com.fdvs.dj.domain.DynamicReport;
import ar.com.fdvs.dj.domain.builders.ColumnBuilderException;
import ar.com.fdvs.dj.domain.builders.FastReportBuilder;

/**
 * Everything under the package org.krams.tutorial.dynamicjasper are settings imposed by DynamicJasper (not Jasper)
 *<p>
 * Builds the report layout, the template, the design, the pattern or whatever synonym you may want to call it.
 * 
 * @author Krams at {@link http://krams915@blogspot.com}
 */
public class Layouter {

 private static Logger logger = Logger.getLogger("service");
 
 /**
  * Builds the report layout. This doesn't have any data yet. This is your template.
  * 
  * @return the layout
  */
 public static DynamicReport buildReportLayout() throws ColumnBuilderException, ClassNotFoundException {
  
  // Create an instance of FastReportBuilder
  FastReportBuilder drb = new FastReportBuilder();
  
  // Create columns
  // The column fields must match the name and type of the properties in your datasource
  drb.addColumn("Id", "id",  Long.class.getName(), 50)
        .addColumn("Brand", "brand", String.class.getName(), 50)
        .addColumn("Model", "model" , String.class.getName(), 50)
        .addColumn("Max Power", "maximumPower", String.class.getName(), 50)
        .addColumn("Price", "price", Double.class.getName(), 50)
        .addColumn("Efficiency", "efficiency", Double.class.getName(), 50)
        .setPrintColumnNames(true)
        
         // Disables pagination
        .setIgnorePagination(true)
        
        // Experiment with this numbers to see the effect
        .setMargins(0, 0, 0, 0) 
        
        // Set the title shown inside the Excel file
        .setTitle("Sales Report") 
        
        // Set the subtitle shown inside the Excel file
        .setSubtitle("This report was generated at " + new Date()) 
        
  // Set to true if you want to constrain your report within the page boundaries
  // For longer reports, set it to false
        .setUseFullPageWidth(true);

  // Set the name of the file
        //drb.setReportName("Sales Report");
        
        // Build the report layout. It doesn't have any data yet!
        DynamicReport dr = drb.build();
        
        // Return the layout
        return dr;
 }
}

The Exporter

The purpose of the Exporter is to export the JasperPrint object into different formats, like Excel, PDF, and CSV. Our current implementation exports the document as an Excel format.

Exporter.java
package org.krams.tutorial.report;

import java.io.ByteArrayOutputStream;

import org.apache.log4j.Logger;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRXlsAbstractExporterParameter;
import net.sf.jasperreports.engine.export.JRXlsExporter;

/**
 * Everything under the package org.krams.tutorial.jasper are settings imposed by Jasper (not DynamicJasper)
 * <p>
 * An exporter for exporting the report in various formats, i.e Excel, PDF, CSV. Here we declare a PDF exporter
 * 
 * @author Krams at {@link http://krams915@blogspot.com}
 */
public class Exporter {

 private static Logger logger = Logger.getLogger("service");
 
 /**
  * Exports a report to XLS (Excel) format. You can declare another export here, i.e PDF or CSV.
  * You don't really need to create a separate class or method for the exporter. You can call it
  * directly within your Service or Controller.
  * 
  * @param jp the JasperPrint object
  * @param baos the ByteArrayOutputStream where the report will be written
  */
 public static void exportToXLS(JasperPrint jp, ByteArrayOutputStream baos) throws JRException {
  // Create a JRXlsExporter instance
  JRXlsExporter exporter = new JRXlsExporter();
  
  // Here we assign the parameters jp and baos to the exporter
  exporter.setParameter(JRExporterParameter.JASPER_PRINT, jp);
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
  
        // Excel specific parameters
        // Check the Jasper (not DynamicJasper) docs for a description of these settings. Most are 
        // self-documenting
        exporter.setParameter(JRXlsAbstractExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
        exporter.setParameter(JRXlsAbstractExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
        exporter.setParameter(JRXlsAbstractExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
        
        // Retrieve the exported report in XLS format
        exporter.exportReport();
 }
}
It's worth mentioning that this class has nothing to do with DynamicJasper. Everything is Jasper-related configuration.

The Writer

The purpose of the Writer is to write the "exported" document to the output stream. Once the document has been written to the stream, the user will receive the document ready to be downloaded.

Writer.java
package org.krams.tutorial.report;

import java.io.ByteArrayOutputStream;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

/**
 * Writes the report to the output stream
 * 
 * @author Krams at {@link http://krams915@blogspot.com}
 */
public class Writer {

 private static Logger logger = Logger.getLogger("service");
 /**
  * Writes the report to the output stream
  */
 public static void write(HttpServletResponse response, ByteArrayOutputStream baos) {
  
  logger.debug("Writing report to the stream");
  try {
   // Retrieve the output stream
   ServletOutputStream outputStream = response.getOutputStream();
   // Write to the output stream
   baos.writeTo(outputStream);
   // Flush the stream
   outputStream.flush();

  } catch (Exception e) {
   logger.error("Unable to write report to the output stream");
  }
 }
}

Controller

We've completed the domain and service layer of the application. Since we're developing a Spring MVC web application, we're required to declare a controller that will handle the user's request.

DownloadController.java
package org.krams.tutorial.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JRException;
import org.apache.log4j.Logger;
import org.krams.tutorial.service.DownloadService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import ar.com.fdvs.dj.domain.builders.ColumnBuilderException;

/**
 * Handles download requests
 * 
 * @author Krams at {@link http://krams915@blogspot.com}
 */
@Controller
@RequestMapping("/download")
public class DownloadController {

 private static Logger logger = Logger.getLogger("controller");
 
 @Resource(name="downloadService")
 private DownloadService downloadService;

 /**
  * Downloads the report as an Excel format. 
  * 

* Make sure this method doesn't return any model. Otherwise, you'll get * an "IllegalStateException: getOutputStream() has already been called for this response" */ @RequestMapping(value = "/xls", method = RequestMethod.GET) public void getXLS(HttpServletResponse response, Model model) throws ColumnBuilderException, ClassNotFoundException, JRException { logger.debug("Received request to download report as an XLS"); // Delegate to downloadService. Make sure to pass an instance of HttpServletResponse downloadService.downloadXLS(response); } }

DownloadController is a simple controller that handles download requests. It delegates report generation to the DownloadService. Notice we're required to pass the HttpServletResponse to the service.

Database Configuration

We've completed the MVC module of the application. However we haven't created yet the Hibernate configuration and the MySQL database.

Our first task is to create an empty MySQL database.

Here are the steps:
1. Run MySQL
2. Open MySQL admin
3. Create a new database mydb

In this tutorial I've setup a local MySQL database and used phpmyadmin to administer it.

Next, we'll be declaring a hibernate-context.xml configuration file. Its purpose is to contain all of Spring-related configuration for Hibernate.

hibernate-context.xml

This configuration requires two external configurations further:

spring.properties
# database properties
app.jdbc.driverClassName=com.mysql.jdbc.Driver
app.jdbc.url=jdbc:mysql://localhost/mydb
app.jdbc.username=root
app.jdbc.password=

#hibernate properties
hibernate.config=/WEB-INF/hibernate.cfg.xml

hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
  "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  
<hibernate-configuration>
 <session-factory>
  <!-- We're using a MySQL database so the dialect needs to be MySQL as well -->
  <!-- Also we want to use MySQL's InnoDB engine -->
  <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
  
  <!-- Enable this to see the Hibernate generated SQL statements in the logs -->
  <property name="show_sql">false</property>
  
  <!-- Setting this to 'create' will drop our existing database and re-create a new one.
    This is only good for testing. In production, this is a bad idea! -->
  <property name="hbm2ddl.auto">create</property>
 </session-factory>
</hibernate-configuration>

The Import.SQL

After declaring all the Hibernate-related configuration, let's now declare a SQL script that will populate our database with a sample data automatically.

import.sql
insert into POWER_SUPPLY (ID, BRAND, MODEL, MAXIMUM_POWER, PRICE, EFFICIENCY) values (null, 'Corsair', 'CMPSU-750TX', '750W', '109.99', '0.80')
insert into POWER_SUPPLY (ID, BRAND, MODEL, MAXIMUM_POWER, PRICE, EFFICIENCY) values (null, 'Antec', 'NEO ECO 620C', '620W', '69.99', '0.80')
insert into POWER_SUPPLY (ID, BRAND, MODEL, MAXIMUM_POWER, PRICE, EFFICIENCY) values (null, 'OCZ', 'OCZ700MXSP', '700W', '89.99', '0.86')
insert into POWER_SUPPLY (ID, BRAND, MODEL, MAXIMUM_POWER, PRICE, EFFICIENCY) values (null, 'Thermaltake', 'W0070RUC', '430W', '43.99', '0.65')
insert into POWER_SUPPLY (ID, BRAND, MODEL, MAXIMUM_POWER, PRICE, EFFICIENCY) values (null, 'COOLER MASTER', 'RS-460-PSAR-J3', '460W', '29.99', '0.70')
insert into POWER_SUPPLY (ID, BRAND, MODEL, MAXIMUM_POWER, PRICE, EFFICIENCY) values (null, 'Rosewill', 'RG530-S12', '530W', '54.99', '0.80')
Make sure to place this document under the classpath. Hibernate will automatically import the contents of this document everytime your start the application. This is dictated by the hbm2ddl.auto setting we declared in the hibernate.cfg.xml earlier.

We're not required to create this import.sql file. We could of course create a MySQL SQL script and import it directly to the database, or add the data manually in the database. I just believe this is convenient for development purposes.

Spring MVC Configuration

We've declared all the necessary classes and Hibernate-related configuration of the application. However, we haven't created yet the required Spring MVC configuration.

Let's begin with the web.xml
web.xml
<servlet>
  <servlet-name>spring</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>
 
 <servlet-mapping>
  <servlet-name>spring</servlet-name>
  <url-pattern>/krams/*</url-pattern>
 </servlet-mapping>

 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
Take note of the URL pattern. When accessing any pages in our MVC application, the host name must be appended with
/krams
In the web.xml we declared a servlet-name spring. By convention, we must declare a spring-servlet.xml.

spring-servlet.xml
<!-- Declare a view resolver for resolving JSPs -->
 <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
      p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
By convention, we must declare an applicationContext.xml as well.

applicationContext.xml
<!-- Activates various annotations to be detected in bean classes -->
 <context:annotation-config />
 
 <!-- Scans the classpath for annotated components that will be auto-registered as Spring beans.
  For example @Controller and @Service. Make sure to set the correct base-package-->
 <context:component-scan base-package="org.krams.tutorial" />
 
 <!-- Configures the annotation-driven Spring MVC Controller programming model.
 Note that, with Spring 3.0, this tag works in Servlet MVC only!  -->
 <mvc:annotation-driven /> 
 
 <!-- Loads Hibernate related configuration -->
 <import resource="hibernate-context.xml" />

Run the Application

We've completed the application. Our last task is to run the application and download the report.

To run the application, open your browser and enter the following URL:
http://localhost:8080/spring-djasper-hibernate-jrdatasource/krams/download/xls
This will automatically download the report document. Again, here's the final screenshot of the document:

Conclusion

That's it. We've managed to build a simple Spring MVC 3 application with reporting capabilities. We used DynamicJasper to generate the dynamic reports and Hibernate for the ORM framework. Lastly, we used Jasper's JRDataSource to wrap our Java List and used the wrapper object as the data source. Remember the data is still retrieved from a MySQL database.

Download the project
You can access the project site at Google's Project Hosting at http://code.google.com/p/spring-mvc-dynamicjasper-integration-tutorial/

You can download the project as a Maven build. Look for the spring-djasper-hibernate-jrdatasource.zip in the Download sections.

You can run the project directly using an embedded server via Maven.
For Tomcat: mvn tomcat:run
For Jetty: mvn jetty:run

If you want to learn more about Spring MVC and integration with other technologies, feel free to read my other tutorials in the Tutorials section.
StumpleUpon DiggIt! Del.icio.us Blinklist Yahoo Furl Technorati Simpy Spurl Reddit Google I'm reading: Spring 3 - DynamicJasper - Hibernate Tutorial: Using JRDataSource ~ Twitter FaceBook

Subscribe by reader Subscribe by email Share

Spring 3 - DynamicJasper - Hibernate Tutorial: Using HQL Query

In this tutorial we will build a simple Spring MVC 3 application with reporting capabilities. We will use DynamicJasper to generate the dynamic reports and Hibernate for the ORM framework. We will use a custom HQL query to retrieve our data source from a MySQL database.

This tutorial is part of the following reporting tutorial series that uses Jasper, DynamicJasper, and Apache POI:

Spring 3 - Apache POI - Hibernate: Creating an Excel Report Tutorial
Spring 3 - DynamicJasper - Hibernate Tutorial: Concatenating a DynamicReport
Spring 3 - DynamicJasper - Hibernate Tutorial: Concatenating a Subreport
Spring 3 - DynamicJasper - Hibernate Tutorial: Using Plain List
Spring 3 - DynamicJasper - Hibernate Tutorial: Using JRDataSource
Spring 3 - DynamicJasper - Hibernate Tutorial: Using HQL Query

All of these tutorials produce the same document, and all of them demonstrate different ways of creating the same report.

What is DynamicJasper?
DynamicJasper (DJ) is an open source free library that hides the complexity of Jasper Reports, it helps developers to save time when designing simple/medium complexity reports generating the layout of the report elements automatically.

Source: http://dynamicjasper.com/

What is JasperReports?
JasperReports is the world's most popular open source reporting engine. It is entirely written in Java and it is able to use data coming from any kind of data source and produce pixel-perfect documents that can be viewed, printed or exported in a variety of document formats including HTML, PDF, Excel, OpenOffice and Word.

Source: http://jasperforge.org/projects/jasperreports

Background

Before we start our application, let's preview first the final print document:

Our document is a simple Excel document. It's a Sales Report for a list of power supplies. The data is retrieved from a MySQL database.

Domain

Notice that for each Power Supply entry there's a common set of properties:
id
brand
model
maximum power
price
efficiency

Development

Domain

We'll start our application by declaring the domain object PowerSupply

PowerSupply.java
package org.krams.tutorial.domain;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 * A simple POJO containing the common properties of a Power Supply
 * This is an annotated Hibernate entity. 
 * 
 * @author Krams at {@link http://krams915@blogspot.com}
 */
@Entity
@Table(name = "POWER_SUPPLY")
public class PowerSupply implements Serializable {

 private static final long serialVersionUID = 8634209606034270882L;

 @Id
 @Column(name = "ID")
 @GeneratedValue
 private Long id;
 
 @Column(name = "BRAND")
 private String brand;
 
 @Column(name = "MODEL")
 private String model;
 
 @Column(name = "MAXIMUM_POWER")
 private String maximumPower;
 
 @Column(name = "PRICE")
 private Double price;
 
 @Column(name = "EFFICIENCY")
 private Double efficiency;

 public Long getId() {
  return id;
 }

 public void setId(Long id) {
  this.id = id;
 }

 public String getBrand() {
  return brand;
 }

 public void setBrand(String brand) {
  this.brand = brand;
 }

 public String getModel() {
  return model;
 }

 public void setModel(String model) {
  this.model = model;
 }

 public String getMaximumPower() {
  return maximumPower;
 }

 public void setMaximumPower(String maximumPower) {
  this.maximumPower = maximumPower;
 }

 public Double getPrice() {
  return price;
 }

 public void setPrice(Double price) {
  this.price = price;
 }

 public Double getEfficiency() {
  return efficiency;
 }

 public void setEfficiency(Double efficiency) {
  this.efficiency = efficiency;
 }
 
}
PowerSupply is a simple POJO containing six private fields. Each of these fields have been annotated with @Column and assigned with corresponding database column names.
ID
BRAND
MODEL
MAXIMUM_POWER
PRICE
EFFICIENCY

Service

We'll be declaring a single service named DownloadService. This service is the heart of the application that will process and retrieve the report document.

The service will run the following steps:
1. Build the report layout
 2. Add the datasource to a HashMap parameter
 3. Compile the report layout
 4. Generate the JasperPrint object
 5. Export to a particular format, ie. XLS
 6. Set the HttpServletResponse properties
 7. Write to the output stream

DownloadService.java
package org.krams.tutorial.service;

import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.query.JRHibernateQueryExecuterFactory;

import org.apache.log4j.Logger;
import org.hibernate.SessionFactory;
import org.krams.tutorial.report.Exporter;
import org.krams.tutorial.report.Layouter;
import org.krams.tutorial.report.Writer;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import ar.com.fdvs.dj.core.DynamicJasperHelper;
import ar.com.fdvs.dj.core.layout.ClassicLayoutManager;
import ar.com.fdvs.dj.domain.DynamicReport;
import ar.com.fdvs.dj.domain.builders.ColumnBuilderException;
import javax.annotation.Resource;

/**
 * Service for processing DynamicJasper reports
 * 
 * @author Krams at {@link http://krams915@blogspot.com}
 */
@Service("downloadService")
@Transactional
public class DownloadService {

 private static Logger logger = Logger.getLogger("service");
 
 @Resource(name="sessionFactory")
 private SessionFactory sessionFactory;
 
 /**
  * Processes the download for Excel format.
  * It does the following steps:
  * <pre> 1. Build the report layout
  * 2. Add the datasource to a HashMap parameter
  * 3. Compile the report layout
  * 4. Generate the JasperPrint object
  * 5. Export to a particular format, ie. XLS
  * 6. Set the HttpServletResponse properties
  * 7. Write to the output stream
  * </pre>
  */
 @SuppressWarnings("unchecked")
 public void downloadXLS(HttpServletResponse response) throws ColumnBuilderException, ClassNotFoundException, JRException {
  logger.debug("Downloading Excel report");

  // 1. Build the report layout
  DynamicReport dr = Layouter.buildReportLayout();

  // 2. Add the datasource to a HashMap parameter
  HashMap params = new HashMap(); 
  // Here we're adding Hibernate session as the datasource
  params.put(JRHibernateQueryExecuterFactory.PARAMETER_HIBERNATE_SESSION, sessionFactory.getCurrentSession());
  
  // 3. Compile the report layout
  JasperReport jr = DynamicJasperHelper.generateJasperReport(dr, new ClassicLayoutManager(), params);
  
  // 4. Generate the JasperPrint object which also fills the report with data
  JasperPrint jp = JasperFillManager.fillReport(jr, params);

  // We can also combine compilation (3) and generation (4) in a single line
  //JasperPrint jp = DynamicJasperHelper.generateJasperPrint(dr, new ClassicLayoutManager(), params);
  
  // Create the output byte stream where the data will be written
  ByteArrayOutputStream baos = new ByteArrayOutputStream();

  // 5. Export to XLS format
  Exporter.exportToXLS(jp, baos);
  
  // 6. Set the response properties
  String fileName = "SalesReport.xls";
  response.setHeader("Content-Disposition", "inline; filename=" + fileName);
  // Make sure to set the correct content type
  response.setContentType("application/vnd.ms-excel");
  // Assign the byte stream's size
  response.setContentLength(baos.size());

  // 7. Write to response stream
  Writer.write(response, baos);
 }
 

}
This service is our download service for generating the report document. It should be clear what each line of code is doing. Notice that steps 3 and 4 can be combined in one line of code:
JasperPrint jp = DynamicJasperHelper.generateJasperPrint(dr, new ClassicLayoutManager(), params);

Also we've added the Hibernate session as HashMap parameter:
HashMap params = new HashMap(); 
params.put(JRHibernateQueryExecuterFactory.PARAMETER_HIBERNATE_SESSION, sessionFactory.getCurrentSession());

The service has been divided into separate classes to encapsulate specific jobs.

The Layouter

The purpose of the Layouter is to layout the design of the report. Here's where we declare the dynamic columns and special properties of the document. At the end of the class, we appended an HQL query

Layouter.java
package org.krams.tutorial.report;

import java.util.Date;

import org.apache.log4j.Logger;

import ar.com.fdvs.dj.core.DJConstants;
import ar.com.fdvs.dj.domain.DynamicReport;
import ar.com.fdvs.dj.domain.builders.ColumnBuilderException;
import ar.com.fdvs.dj.domain.builders.FastReportBuilder;

/**
 * Everything under the package org.krams.tutorial.dynamicjasper are settings imposed by DynamicJasper (not Jasper)
 *<p>
 * Builds the report layout, the template, the design, the pattern or whatever synonym you may want to call it.
 * 
 * @author Krams at {@link http://krams915@blogspot.com}
 */
public class Layouter {

 private static Logger logger = Logger.getLogger("service");
 
 /**
  * Builds the report layout. This doesn't have any data yet. This is your template.
  * 
  * @return the layout
  */
 public static DynamicReport buildReportLayout() throws ColumnBuilderException, ClassNotFoundException {
  
  // Create an instance of FastReportBuilder
  FastReportBuilder drb = new FastReportBuilder();
  
  // Create columns
  // The column fields must match the name and type of the properties in your datasource
  drb.addColumn("Id", "id",  Long.class.getName(), 50)
        .addColumn("Brand", "brand", String.class.getName(), 50)
        .addColumn("Model", "model" , String.class.getName(), 50)
        .addColumn("Max Power", "maximumPower", String.class.getName(), 50)
        .addColumn("Price", "price", Double.class.getName(), 50)
        .addColumn("Efficiency", "efficiency", Double.class.getName(), 50)
        .setPrintColumnNames(true)
        
         // Disables pagination
        .setIgnorePagination(true)
        
        // Experiment with this numbers to see the effect
        .setMargins(0, 0, 0, 0) 
        
        // Set the title shown inside the Excel file
        .setTitle("Sales Report") 
        
        // Set the subtitle shown inside the Excel file
        .setSubtitle("This report was generated at " + new Date()) 
        
  // Set to true if you want to constrain your report within the page boundaries
  // For longer reports, set it to false
        .setUseFullPageWidth(true);

  // Set the name of the file
        //drb.setReportName("Sales Report");
        
        // Create a Hibernate HQL query
        StringBuilder hqlQuery = new StringBuilder();
      hqlQuery.append("SELECT p.id as id, ");
      hqlQuery.append("p.brand as brand, ");
      hqlQuery.append("p.model as model, ");
      hqlQuery.append("p.maximumPower as maximumPower, ");
      hqlQuery.append("p.price as price, ");
      hqlQuery.append("p.efficiency as efficiency ");
      hqlQuery.append("FROM PowerSupply p");
      
      // Assign the HQL query
        drb.setQuery(hqlQuery.toString(), DJConstants.QUERY_LANGUAGE_HQL);
        
        // Build the report layout. It doesn't have any data yet!
        DynamicReport dr = drb.build();
        
        // Return the layout
        return dr;
 }
}
Notice at the end of the buildReportLayout() method, we appended a custom Hibernate HQL query:
// Create a Hibernate HQL query
        StringBuilder hqlQuery = new StringBuilder();
      hqlQuery.append("SELECT p.id as id, ");
      hqlQuery.append("p.brand as brand, ");
      hqlQuery.append("p.model as model, ");
      hqlQuery.append("p.maximumPower as maximumPower, ");
      hqlQuery.append("p.price as price, ");
      hqlQuery.append("p.efficiency as efficiency ");
      hqlQuery.append("FROM PowerSupply p");
      
      // Assign the HQL query
        drb.setQuery(hqlQuery.toString(), DJConstants.QUERY_LANGUAGE_HQL);
The most notable setting here is the adding the setQuery(hqlQuery.toString(), DJConstants.QUERY_LANGUAGE_HQL);. This setting enables us to use a custom HQL query.

The Exporter

The purpose of the Exporter is to export the JasperPrint object into different formats, like Excel, PDF, and CSV. Our current implementation exports the document as an Excel format.

Exporter.java
package org.krams.tutorial.report;

import java.io.ByteArrayOutputStream;

import org.apache.log4j.Logger;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRXlsAbstractExporterParameter;
import net.sf.jasperreports.engine.export.JRXlsExporter;

/**
 * Everything under the package org.krams.tutorial.jasper are settings imposed by Jasper (not DynamicJasper)
 * <p>
 * An exporter for exporting the report in various formats, i.e Excel, PDF, CSV. Here we declare a PDF exporter
 * 
 * @author Krams at {@link http://krams915@blogspot.com}
 */
public class Exporter {

 private static Logger logger = Logger.getLogger("service");
 
 /**
  * Exports a report to XLS (Excel) format. You can declare another export here, i.e PDF or CSV.
  * You don't really need to create a separate class or method for the exporter. You can call it
  * directly within your Service or Controller.
  * 
  * @param jp the JasperPrint object
  * @param baos the ByteArrayOutputStream where the report will be written
  */
 public static void exportToXLS(JasperPrint jp, ByteArrayOutputStream baos) throws JRException {
  // Create a JRXlsExporter instance
  JRXlsExporter exporter = new JRXlsExporter();
  
  // Here we assign the parameters jp and baos to the exporter
  exporter.setParameter(JRExporterParameter.JASPER_PRINT, jp);
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
  
        // Excel specific parameters
        // Check the Jasper (not DynamicJasper) docs for a description of these settings. Most are 
        // self-documenting
        exporter.setParameter(JRXlsAbstractExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
        exporter.setParameter(JRXlsAbstractExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
        exporter.setParameter(JRXlsAbstractExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
        
        // Retrieve the exported report in XLS format
        exporter.exportReport();
 }
}
It's worth mentioning that this class has nothing to do with DynamicJasper. Everything is Jasper-related configuration.

The Writer

The purpose of the Writer is to write the "exported" document to the output stream. Once the document has been written to the stream, the user will receive the document ready to be downloaded.

Writer.java
package org.krams.tutorial.report;

import java.io.ByteArrayOutputStream;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

/**
 * Writes the report to the output stream
 * 
 * @author Krams at {@link http://krams915@blogspot.com}
 */
public class Writer {

 private static Logger logger = Logger.getLogger("service");
 /**
  * Writes the report to the output stream
  */
 public static void write(HttpServletResponse response, ByteArrayOutputStream baos) {
  
  logger.debug("Writing report to the stream");
  try {
   // Retrieve the output stream
   ServletOutputStream outputStream = response.getOutputStream();
   // Write to the output stream
   baos.writeTo(outputStream);
   // Flush the stream
   outputStream.flush();

  } catch (Exception e) {
   logger.error("Unable to write report to the output stream");
  }
 }
}

Controller

We've completed the domain and service layer of the application. Since we're developing a Spring MVC web application, we're required to declare a controller that will handle the user's request.

DownloadController.java
package org.krams.tutorial.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JRException;
import org.apache.log4j.Logger;
import org.krams.tutorial.service.DownloadService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import ar.com.fdvs.dj.domain.builders.ColumnBuilderException;

/**
 * Handles download requests
 * 
 * @author Krams at {@link http://krams915@blogspot.com}
 */
@Controller
@RequestMapping("/download")
public class DownloadController {

 private static Logger logger = Logger.getLogger("controller");
 
 @Resource(name="downloadService")
 private DownloadService downloadService;

 /**
  * Downloads the report as an Excel format. 
  * 

* Make sure this method doesn't return any model. Otherwise, you'll get * an "IllegalStateException: getOutputStream() has already been called for this response" */ @RequestMapping(value = "/xls", method = RequestMethod.GET) public void getXLS(HttpServletResponse response, Model model) throws ColumnBuilderException, ClassNotFoundException, JRException { logger.debug("Received request to download report as an XLS"); // Delegate to downloadService. Make sure to pass an instance of HttpServletResponse downloadService.downloadXLS(response); } }

DownloadController is a simple controller that handles download requests. It delegates report generation to the DownloadService. Notice we're required to pass the HttpServletResponse to the service.

Database Configuration

We've completed the MVC module of the application. However we haven't created yet the Hibernate configuration and the MySQL database.

Our first task is to create an empty MySQL database.

Here are the steps:
1. Run MySQL
2. Open MySQL admin
3. Create a new database mydb

In this tutorial I've setup a local MySQL database and used phpmyadmin to administer it.

Next, we'll be declaring a hibernate-context.xml configuration file. Its purpose is to contain all of Spring-related configuration for Hibernate.

hibernate-context.xml

This configuration requires two external configurations further:

spring.properties
# database properties
app.jdbc.driverClassName=com.mysql.jdbc.Driver
app.jdbc.url=jdbc:mysql://localhost/mydb
app.jdbc.username=root
app.jdbc.password=

#hibernate properties
hibernate.config=/WEB-INF/hibernate.cfg.xml

hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
  "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  
<hibernate-configuration>
 <session-factory>
  <!-- We're using a MySQL database so the dialect needs to be MySQL as well -->
  <!-- Also we want to use MySQL's InnoDB engine -->
  <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
  
  <!-- Enable this to see the Hibernate generated SQL statements in the logs -->
  <property name="show_sql">false</property>
  
  <!-- Setting this to 'create' will drop our existing database and re-create a new one.
    This is only good for testing. In production, this is a bad idea! -->
  <property name="hbm2ddl.auto">create</property>
 </session-factory>
</hibernate-configuration>

The Import.SQL

After declaring all the Hibernate-related configuration, let's now declare a SQL script that will populate our database with a sample data automatically.

import.sql
insert into POWER_SUPPLY (ID, BRAND, MODEL, MAXIMUM_POWER, PRICE, EFFICIENCY) values (null, 'Corsair', 'CMPSU-750TX', '750W', '109.99', '0.80')
insert into POWER_SUPPLY (ID, BRAND, MODEL, MAXIMUM_POWER, PRICE, EFFICIENCY) values (null, 'Antec', 'NEO ECO 620C', '620W', '69.99', '0.80')
insert into POWER_SUPPLY (ID, BRAND, MODEL, MAXIMUM_POWER, PRICE, EFFICIENCY) values (null, 'OCZ', 'OCZ700MXSP', '700W', '89.99', '0.86')
insert into POWER_SUPPLY (ID, BRAND, MODEL, MAXIMUM_POWER, PRICE, EFFICIENCY) values (null, 'Thermaltake', 'W0070RUC', '430W', '43.99', '0.65')
insert into POWER_SUPPLY (ID, BRAND, MODEL, MAXIMUM_POWER, PRICE, EFFICIENCY) values (null, 'COOLER MASTER', 'RS-460-PSAR-J3', '460W', '29.99', '0.70')
insert into POWER_SUPPLY (ID, BRAND, MODEL, MAXIMUM_POWER, PRICE, EFFICIENCY) values (null, 'Rosewill', 'RG530-S12', '530W', '54.99', '0.80')
Make sure to place this document under the classpath. Hibernate will automatically import the contents of this document everytime your start the application. This is dictated by the hbm2ddl.auto setting we declared in the hibernate.cfg.xml earlier.

We're not required to create this import.sql file. We could of course create a MySQL SQL script and import it directly to the database, or add the data manually in the database. I just believe this is convenient for development purposes.

Spring MVC Configuration

We've declared all the necessary classes and Hibernate-related configuration of the application. However, we haven't created yet the required Spring MVC configuration.

Let's begin with the web.xml
web.xml
<servlet>
  <servlet-name>spring</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>
 
 <servlet-mapping>
  <servlet-name>spring</servlet-name>
  <url-pattern>/krams/*</url-pattern>
 </servlet-mapping>

 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
Take note of the URL pattern. When accessing any pages in our MVC application, the host name must be appended with
/krams
In the web.xml we declared a servlet-name spring. By convention, we must declare a spring-servlet.xml.

spring-servlet.xml
<!-- Declare a view resolver for resolving JSPs -->
 <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
      p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
By convention, we must declare an applicationContext.xml as well.

applicationContext.xml
<!-- Activates various annotations to be detected in bean classes -->
 <context:annotation-config />
 
 <!-- Scans the classpath for annotated components that will be auto-registered as Spring beans.
  For example @Controller and @Service. Make sure to set the correct base-package-->
 <context:component-scan base-package="org.krams.tutorial" />
 
 <!-- Configures the annotation-driven Spring MVC Controller programming model.
 Note that, with Spring 3.0, this tag works in Servlet MVC only!  -->
 <mvc:annotation-driven /> 
 
 <!-- Loads Hibernate related configuration -->
 <import resource="hibernate-context.xml" />

Run the Application

We've completed the application. Our last task is to run the application and download the report.

To run the application, open your browser and enter the following URL:
http://localhost:8080/spring-djasper-hibernate-hql/krams/download/xls
This will automatically download the report document. Again, here's the final screenshot of the document:

Conclusion

That's it. We've managed to build a simple Spring MVC 3 application with reporting capabilities. We used DynamicJasper to generate the dynamic reports and Hibernate for the ORM framework. Lastly, we used a custom HQL query to retrieve the data source from a MySQL database.

Download the project
You can access the project site at Google's Project Hosting at http://code.google.com/p/spring-mvc-dynamicjasper-integration-tutorial/

You can download the project as a Maven build. Look for the spring-djasper-hibernate-hql.zip in the Download sections.

You can run the project directly using an embedded server via Maven.
For Tomcat: mvn tomcat:run
For Jetty: mvn jetty:run

If you want to learn more about Spring MVC and integration with other technologies, feel free to read my other tutorials in the Tutorials section.
StumpleUpon DiggIt! Del.icio.us Blinklist Yahoo Furl Technorati Simpy Spurl Reddit Google I'm reading: Spring 3 - DynamicJasper - Hibernate Tutorial: Using HQL Query ~ Twitter FaceBook

Subscribe by reader Subscribe by email Share

Sunday, January 30, 2011

Spring Security 3: Full ACL Tutorial (Part 4)

In Part 1 of this tutorial we've completed setting up the ACL and Bulletin databases. In Part 2 we've completed the Spring Security configuration. In Part 3 we've developed the Spring MVC module of the application. In Part 4 we'll be testing and running the application. We'll also cover some of the unexpected issues we've encountered while developing the system.

Part 1: Functional Specs and the Application Database
Part 2: Spring Security Configuration
Part 3: Spring MVC Module
Part 4: Running the Application

Run the Application

We've completed all the necessary elements of the application. It's time to run it and check the results.

The Admin

We'll login first as an admin and check which actions are allowed to us.

Here are the steps:
1. Visit the login page at http://localhost:8080/spring-security-full-acl/krams/auth/login

2. Enter the following username and password:
username: john
password: admin

3. After a successful login, we should see the following:

Notice the current user is john with the following roles: ROLE_ADMIN, ROLE_USER, and ROLE_VISITOR. As an admin, we're allowed to see all posts in the Bulletin application.

4. Now let's try editing a post. Select post #2 and click Edit. We should see the following page:

Notice we're allowed to edit the post. That's because in the ACL database we've declared a permission for AdminPost object with object_identity_id of 2 that corresponds to the primary id of the admin_post table in the Bulletin database.

5. Edit the message then click Save.

We should see a success message.

6. Go back to the main page either by pressing the back button twice in your browser or typing the following url:
http://localhost:8080/spring-security-full-acl/krams/bulletin/view

7. Again on the Admin Posts, click on any of the Add links. We should see the following page:

8. Type a new message. Then click Save.

Notice the new posts has been added successfully.

9. Go back to the main page.

10. Again on the Admin Posts, select the post #1 and click on Delete. The post will be deleted automatically with the following confirmation page:

So far everything works as expected. Now try viewing the main page. You'll notice that we've managed to delete the first post. Edit the second post. But the fourth post we added is nowhere to be found!

Let's check the Bulletin database if the new posts has been added really.

The database shows that the fourth post has been added. But how come it doesn't show on the Bulletin application? That's because we haven't declared it yet in the ACL database. We also haven't configured the permissions for this record. We'll discuss this again in the Unexpected Problems section

11. Now let's check the Personal Post. Try adding, editing, or deleting any of the posts. Notice in all actions you will be denied with the following message:

It works as expected.

12. Now let's check the Public Post. Try adding, editing, or deleting any of the posts. Notice it works exactly the same as with the Admin Post, and also shows the same behavior when adding a new posts.

The User

After logging-in as an admin, we'll login next as a regular user and check which actions are allowed to us.

Here are the steps:
1. Visit the login page at http://localhost:8080/spring-security-full-acl/krams/auth/login

2. Enter the following username and password:
username: jane
password: user

3. After a successful login, we should see the following:

Notice the current user is jane with the following roles: ROLE_USER and ROLE_VISITOR. As a regular user, we're allowed to see all posts, except the Admin Posts, in the Bulletin application.

The Visitor

Lastly we'll login as a visitor and check which actions are allowed to us.

Here are the steps:
1. Visit the login page at http://localhost:8080/spring-security-full-acl/krams/auth/login

2. Enter the following username and password:
username: mike
password: visitor

3. After a successful login, we should see the following:

Notice the current user is mike with the following roles: ROLE_VISITOR. As a visitor, we're allowed to see only the visitor posts in the Bulletin application.

Unexpected Problems

If you'd been following the whole implementations closely, you'll discover the following issues:

Issue #1: Bit mask permission doesn't work as expected
When declaring permissions in the database, we're required to declare permissions as bit masks.
- Bit mask "1" (0001 in binary) is interpreted as "READ" access
- Bit mask "2" (0010 in binary) is interpreted as "WRITE" access.

Naturally declaring bit mask "3" (0011) should be interpreted as READ and WRITE access. But it's not. Why? Because AclImpl compares the values by normal equality comparison instead of performing an actual bitwise comparison!

To solve this issue, we can implement our own AclImpl. We'll cover this in future tutorials.

Issue #2: We're stuck with the default READ and WRITE permissions
When declaring Expression-based access control we're forced with the default READ and WRITE access. What if we would like to declare our own permission like READWRITE?

The reason for this behavior is that the by default the AclPermissionEvaluator relies on the DefaultPermissionFactory which by default uses the BasePermission implementation.

Let's examine the BasePermission class:
public class BasePermission extends AbstractPermission {
    public static final Permission READ = new BasePermission(1 << 0, 'R'); // 1
    public static final Permission WRITE = new BasePermission(1 << 1, 'W'); // 2
    public static final Permission CREATE = new BasePermission(1 << 2, 'C'); // 4
    public static final Permission DELETE = new BasePermission(1 << 3, 'D'); // 8
    public static final Permission ADMINISTRATION = new BasePermission(1 << 4, 'A'); // 16

...
}
Notice by default it declares a READ, WRITE, CREATE, DELETE, ADMINISTRATION permissions.

To solve this issue, we can extend the BasePermission. Again, we'll cover this in future tutorials.

Issue #3: New posts are not shown on the View all page
When we create a new post, the system will only allow us if we have the correct permission. However, we don't see our new posts in the View page. That's because the new post objects weren't recorded in the ACL database. If the objects do not exist, we don't get any permission.

To solve this issue, we need to incorporate the ACL service within the posts services. And again, we'll cover this in future tutorials.

Conclusion

That's it. We've completed our Bulletin application. We've successfully implemented an ACL system using Spring Security 3. We've explored how to apply expression-based access controls and explain in detail all the important elements in the system.

The best way to learn further is to try the actual application.

Download the project
You can access the project site at Google's Project Hosting at http://code.google.com/p/spring-security-acl-expression/

You can download the project as a Maven build. Look for the spring-security-full-acl.zip in the Download sections.

You can run the project directly using an embedded server via Maven.
For Tomcat: mvn tomcat:run
For Jetty: mvn jetty:run
StumpleUpon DiggIt! Del.icio.us Blinklist Yahoo Furl Technorati Simpy Spurl Reddit Google I'm reading: Spring Security 3: Full ACL Tutorial (Part 4) ~ Twitter FaceBook

Subscribe by reader Subscribe by email Share