samaxes

Icon

on technology

Deploying BIRT Report Engine API with Stripes

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.

BIRT WebReport Example

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*
 * $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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
 * $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.
     * 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.
     * 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.
     * 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.

1
2
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.

1
deploy.dir=E:/Work/jboss/server/default/deploy

Resources

Related Posts

One Response

  1. [...] on Technology has several blog posts on BIRT and some very nice examples on deploying BIRT with Stripes or [...]

Leave a Reply

Archives