Now that I use preferably Stripes over Struts Framework, I’ve decided to port my example “Deploying BIRT Report Engine API with Jakarta Struts” to this Framework.

For this example I’ve used Stripes 1.4.2, BIRT Runtime 2.1.2, and Tribix 2.1.2.
Go to the Resources section to get the instructions on how to get this example source code.
ContextListener.java
Listener to BIRT Report Engine initialization and shutdown.
/*
* $Id$
*
* Last changed on : $Date$
* Last changed by : $Author$
*/
package com.samaxes.webreport.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import com.samaxes.webreport.birt.BirtEngine;
/**
* BIRT Report Engine initialization and shutdown.
*
* @author : samaxes
* @version : $Revision$
*/
public class ContextListener implements ServletContextListener {
/**
* Initialization of the BIRT Engine.
*
* @param event ServletContextEvent
*/
public void contextInitialized(ServletContextEvent event) {
BirtEngine.initBirtConfig();
}
/**
* Destruction of the BIRT Engine.
*
* @param event ServletContextEvent
*/
public void contextDestroyed(ServletContextEvent event) {
BirtEngine.destroyBirtEngine();
}
}
WebReportActionBean.java
Actions related to the reports generation.
/*
* $Id$
*
* Last changed on : $Date$
* Last changed by : $Author$
*/
package com.samaxes.webreport.action;
import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.HandlesEvent;
import net.sourceforge.stripes.action.Resolution;
import org.eclipse.birt.report.engine.api.EngineConstants;
import org.eclipse.birt.report.engine.api.HTMLRenderContext;
import org.eclipse.birt.report.engine.api.HTMLRenderOption;
import org.eclipse.birt.report.engine.api.IReportEngine;
import org.eclipse.birt.report.engine.api.IReportRunnable;
import org.eclipse.birt.report.engine.api.IRunAndRenderTask;
import com.samaxes.webreport.birt.BirtEngine;
import com.samaxes.webreport.common.Constants;
/**
* Actions related to the reports generation.
*
* @author : samaxes
* @version : $Revision$
*/
public class WebReportActionBean implements ActionBean {
protected static Logger logger = Logger.getLogger("org.eclipse.birt");
private IReportEngine birtReportEngine;
private ActionBeanContext context;
public ActionBeanContext getContext() {
return context;
}
public void setContext(ActionBeanContext context) {
this.context = context;
}
/**
* The html report action. <br />
* Gets the report's html output stream.
*
* @return Resolution
*/
@DefaultHandler
public Resolution htmlReport() {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
out = renderReportPage(context.getRequest());
} catch (ServletException e) {
logger.log(Level.SEVERE, e.getMessage(), e);
}
context.getRequest().setAttribute("reportHTML", out);
return new ForwardResolution("/WEB-INF/jsps/viewReport.jsp");
}
/**
* The pdf report action. <br />
* Generates the pdf report.
*
* @throws ServletException
*/
@HandlesEvent("pdfReport")
public void pdfReport() throws ServletException {
// get report name and launch the engine
context.getResponse().setContentType("application/pdf");
context.getResponse().setHeader("Content-Disposition", "inline; filename=WebReport.pdf"); // inline,
// attachment
String reportName = context.getRequest().getParameter("ReportName");
ServletContext sc = context.getRequest().getSession().getServletContext();
this.birtReportEngine = BirtEngine.getBirtEngine(sc);
// setup image directory
HTMLRenderContext renderContext = new HTMLRenderContext();
renderContext.setBaseImageURL(context.getRequest().getContextPath() + "/images");
renderContext.setImageDirectory(sc.getRealPath("/images"));
logger.log(Level.FINE, "image directory " + sc.getRealPath("/images"));
HashMap<String, HTMLRenderContext> contextMap = new HashMap<String, HTMLRenderContext>();
contextMap.put(EngineConstants.APPCONTEXT_HTML_RENDER_CONTEXT, renderContext);
IReportRunnable design;
try {
// Open report design
design = birtReportEngine.openReportDesign(sc.getRealPath("/reports") + "/" + reportName);
// create task to run and render report
IRunAndRenderTask task = birtReportEngine.createRunAndRenderTask(design);
task.setAppContext(contextMap);
// set output options
HTMLRenderOption options = new HTMLRenderOption();
options.setOutputFormat(HTMLRenderOption.OUTPUT_FORMAT_PDF);
options.setOutputStream(context.getResponse().getOutputStream());
task.setRenderOption(options);
// run report
task.run();
task.close();
} catch (Exception e) {
logger.log(Level.SEVERE, e.getMessage(), e);
throw new ServletException(e);
}
}
/**
* The excel report action. <br />
* Generates the excel report.
*
* @throws ServletException
*/
@HandlesEvent("xlsReport")
public void xlsReport() throws ServletException {
// get report name and launch the engine
context.getResponse().setContentType("application/vnd.ms-excel");
context.getResponse().setHeader("Content-Disposition", "inline; filename=WebReport.xls"); // inline,
// attachment
String reportName = context.getRequest().getParameter("ReportName");
ServletContext sc = context.getRequest().getSession().getServletContext();
this.birtReportEngine = BirtEngine.getBirtEngine(sc);
// setup image directory
HTMLRenderContext renderContext = new HTMLRenderContext();
renderContext.setBaseImageURL(context.getRequest().getContextPath() + "/images");
renderContext.setImageDirectory(sc.getRealPath("/images"));
logger.log(Level.FINE, "image directory " + sc.getRealPath("/images"));
HashMap<String, HTMLRenderContext> contextMap = new HashMap<String, HTMLRenderContext>();
contextMap.put(EngineConstants.APPCONTEXT_HTML_RENDER_CONTEXT, renderContext);
IReportRunnable design;
try {
// Open report design
design = birtReportEngine.openReportDesign(sc.getRealPath("/reports") + "/" + reportName);
// create task to run and render report
IRunAndRenderTask task = birtReportEngine.createRunAndRenderTask(design);
task.setAppContext(contextMap);
// set output options
HTMLRenderOption options = new HTMLRenderOption();
options.setOutputFormat(Constants.XLS_FORMAT);
options.setOutputStream(context.getResponse().getOutputStream());
task.setRenderOption(options);
// run report
task.run();
task.close();
} catch (Exception e) {
logger.log(Level.SEVERE, e.getMessage(), e);
throw new ServletException(e);
}
}
/**
* Generates the html report.
*
* @param request HttpServletRequest
* @return ByteArrayOutputStreamwith the report generated
* @throws ServletException
*/
private ByteArrayOutputStream renderReportPage(HttpServletRequest request) throws ServletException {
// get report name and launch the engine
ByteArrayOutputStream out = new ByteArrayOutputStream();
String reportName = request.getParameter("ReportName");
ServletContext sc = request.getSession().getServletContext();
this.birtReportEngine = BirtEngine.getBirtEngine(sc);
// setup image directory
HTMLRenderContext renderContext = new HTMLRenderContext();
renderContext.setBaseImageURL(request.getContextPath() + "/images");
renderContext.setImageDirectory(sc.getRealPath("/images"));
logger.log(Level.FINE, "image directory " + sc.getRealPath("/images"));
HashMap<String, HTMLRenderContext> contextMap = new HashMap<String, HTMLRenderContext>();
contextMap.put(EngineConstants.APPCONTEXT_HTML_RENDER_CONTEXT, renderContext);
IReportRunnable design;
try {
// Open report design
design = birtReportEngine.openReportDesign(sc.getRealPath("/reports") + "/" + reportName);
// create task to run and render report
IRunAndRenderTask task = birtReportEngine.createRunAndRenderTask(design);
task.setAppContext(contextMap);
// set output options
HTMLRenderOption options = new HTMLRenderOption();
options.setOutputFormat(HTMLRenderOption.OUTPUT_FORMAT_HTML);
options.setEmbeddable(true);
options.setOutputStream(out);
task.setRenderOption(options);
// run report
task.run();
task.close();
} catch (Exception e) {
logger.log(Level.SEVERE, e.getMessage(), e);
throw new ServletException(e);
}
return out;
}
}
You also need to follow the same steps as in the previous example (Deploying BIRT Report Engine API with Jakarta Struts):
Copy the WAR file to your server deploy dir and configure the BIRT Report Engine logging in BirtConfig.properties file.
logDirectory=e:/Work/logs/birt logLevel=CONFIG
If you are building the application from the source code you must also configure the application server deployment directory, rename the file build.properties-example to build.properties and edit the following line.
deploy.dir=E:/Work/jboss/server/default/deploy
Resources
- Stripes BIRT WebReport Project Download the WAR file or get the source code from the SVN repository.
- Report Engine API Contains further information on BIRT RE API.
[…] on Technology has several blog posts on BIRT and some very nice examples on deploying BIRT with Stripes or […]