Jump to: navigation, search

Seam (BIRT) Recipe - DocumentStoreServlet

Seam (BIRT) Recipe
org.jboss.seam.pdf.birt.DocumentStoreServlet

package org.jboss.seam.pdf.birt;
 
import java.io.IOException;
import java.util.HashMap;
import java.util.Locale;
import java.util.logging.Level;
 
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.eclipse.birt.core.exception.BirtException;
import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.report.context.BirtContext;
import org.eclipse.birt.report.context.IContext;
import org.eclipse.birt.report.engine.api.EngineConfig;
import org.eclipse.birt.report.engine.api.EngineConstants;
import org.eclipse.birt.report.engine.api.EngineException;
import org.eclipse.birt.report.engine.api.HTMLRenderOption;
import org.eclipse.birt.report.engine.api.IRenderOption;
import org.eclipse.birt.report.engine.api.IReportEngine;
import org.eclipse.birt.report.engine.api.IReportEngineFactory;
import org.eclipse.birt.report.engine.api.IReportRunnable;
import org.eclipse.birt.report.engine.api.IRunAndRenderTask;
import org.eclipse.birt.report.engine.api.PDFRenderOption;
import org.eclipse.birt.report.engine.api.RenderOption;
import org.eclipse.birt.report.presentation.aggregation.IFragment;
import org.eclipse.birt.report.presentation.aggregation.layout.EngineFragment;
import org.eclipse.birt.report.presentation.aggregation.layout.RequesterFragment;
import org.eclipse.birt.report.resource.BirtResources;
import org.eclipse.birt.report.service.BirtReportServiceFactory;
import org.eclipse.birt.report.service.BirtViewerReportService;
import org.eclipse.birt.report.utility.ParameterAccessor;
import org.jboss.seam.servlet.ContextualHttpServletRequest;
 
public class DocumentStoreServlet extends HttpServlet {
	protected IReportEngine engine;
	protected IFragment requester;
 
	private static final long serialVersionUID = 519600274155718202L;
 
	public void init(ServletConfig servletconfig) throws ServletException {
		super.init(servletconfig);
		BirtEngine.initBirtConfig();
	}
 
	@Override
	protected void doGet(final HttpServletRequest request,
			final HttpServletResponse response) throws ServletException,
			IOException {
 
		new ContextualHttpServletRequest(request) {
			@Override
			public void process() throws ServletException, IOException {
				try {
					executeReport( request, response);
				} catch (EngineException e) {
					log("executeReport error", e);
				}
 
			}
		}.run();
 
	}
 
 
	void executeReport(HttpServletRequest request, HttpServletResponse response) throws EngineException {
 
        //resp.setContentType("text/html");
        response.setContentType( "application/pdf" ); 
        response.setHeader ("Content-Disposition","inline; filename=test.pdf");           
        String reportName = request.getServletPath();
        ServletContext sc = request.getSession().getServletContext();
        this.engine = BirtEngine.getBirtEngine(sc);
 
 
        IReportRunnable design;
        try
        {
                //Open report design
//            design = engine.openReportDesign( sc.getRealPath("/Reports")+reportName.substring(0, reportName.length() -4) + ".rptdesign" );
        		design = engine.openReportDesign( sc.getRealPath("./")+reportName.substring(0, reportName.length() -4) + ".xhtml" );
                //create task to run and render report
                IRunAndRenderTask task = engine.createRunAndRenderTask( design );             
                task.getAppContext().put(EngineConstants.APPCONTEXT_CLASSLOADER_KEY, DocumentStoreServlet.class.getClassLoader()); 
                //set output options
                //HTMLRenderOption options = new HTMLRenderOption();
                PDFRenderOption options = new PDFRenderOption();
                //options.setOutputFormat(HTMLRenderOption.OUTPUT_FORMAT_HTML);
                options.setOutputFormat(HTMLRenderOption.OUTPUT_FORMAT_PDF);
                options.setOutputStream(response.getOutputStream());
                //options.setBaseImageURL(req.getContextPath()+"/images");
                //options.setImageDirectory(sc.getRealPath("/images"));
                task.setRenderOption(options);
 
                //run report
                task.run();
                task.close();
        }catch (IOException e){
 
                e.printStackTrace();
                return;
        }
 
 
		System.out.println("Finished");
	}
 
	/**
	 * Destruction of the servlet. 
 
	 */
	public void destroy() {
	 super.destroy(); 
	 BirtEngine.destroyBirtEngine();
	}
}