This is a follow up post from the last post ‘How to use BI Publisher Web Service with JDeveloper 11G’. Today, I want to talk about how to run a BI Publisher report with the Web Service within a Java Servlet. This post is assuming that you have already generated Java Web Service Proxy with JDeveloper, which creates a set of Java classes/methods that are required to call BI Publisher Web Service APIs. If you haven’t please take a look at ‘How to use BI Publisher Web Service with JDeveloper 11G’ first.
Which Method to run Report?
You can use a runReport() or runReportInSession() method to have BI Publisher Enterprise server to run a report and get the report output. The report output will be returned in binary format so you need to take care of handling the binary format appropriately in order to display or locate the report output.
Here are the definitions of runReport() and runReportInSession().
- ReportResponse runReport(Request request, String username, String password);
- ReportResponse runReport(Request request, String sessionToken);
While with runReport() method you need to always pass username and password, with runReportInSession() you can just pass a session token after you get with login() method. (See ‘How to use BI Publisher Web Service with JDeveloper 11G’ for the detail of the login() method.)
I’m going to use the runReportInSession() for my example below.
How to Call runReportInSession()?
First of all you need to create a ReportRequest object, with which you can set a report that you want to run. You can also set a report layout template name, report output format type such as ‘pdf, ‘html’, etc, and locale (language and territory) information. Once the ReportRequest is set then you can pass it to the runReportInSession() method along with the session token. The runReportInSession() willl return a ReportResponse object, which contains the report output in a binary format along with the report meta-data information such as content type, language information.
Here is a visualized process flow of the runReport() method.
So basically there are three steps.
- Create ReportRequest object
- Call runReportInSession() method
- Get a report from ReportResponse object
Create ReportRequest Object
You can set the following information when you create the ReportRequest object
- Absolute Path to the Report
- Report output format
- Locale (Language and Country/Territory)
- Template name
| Function | Method | Description |
| Output format | void setAttributeFormat() | You can set a desired report output format such as ‘pdf’, ‘rtf’, ‘excel’, etc. |
| Locale | void setAttributeLocale() | You can set a user’s preferred report locale such as ‘en-US’ for English/America, ‘fr-FR’ for French/France. Based on this setting BI Publisher Enterprise will decide which language translation to use for the specified template if available. |
| Template Names | void setAttributeTemplate() | You can set a desired template among available templates that are registered for the specified report. |
| Absolute Path to the Report | void setReportAbsolutePath() | You can set an absolute path for the report that you want to run. |
Example:
| ReportRequest req = new ReportRequest(); //Instantiate ReportRequest object req.setAttributeFormat("pdf"); //Set a report output format |
Call runReportInSession()
You can call the runReportInSession() method through the publicReportService Java Web proxy client object. It returns a ReportResponse object.
Example:
| ReportResponse res = new ReportResponse(); //Instantiate ReportResponse object res = publicReportService.runReportInSession(req, sid); |
Get a Reprot from ReportResponse Object
Here is a list of the methods that you can use to retrieve information from the ReportResponse object. And the most you care would be the getReprotBytes(), which returns a report output in a binary format.
| Function | Method | Description |
| Report Content | byte[] getReportBytes() | You can get the report content itself. It returns the report in a form of binary so you need to take care of a proper encoding to retrieve the data and handle it appropriately. |
| Report Content Type | String getReportContentType() | You can get the report’s content type. For example if the report output is generated in a PDF format then it will return ‘application/pdf’. |
| Report Locale | String getReportLocale() | You can get a locale information of the generated report. |
Example:
| response.setContentType(res.getReportContentType()); // Set content type to HttpServletResponse byte[] binaryBytes = res.getReportBytes(); // Get report content from ReportResponse |
Troubleshoot
You might not get any report content from the ReportResponse object. If that’s the case you want to add the following code to set a data chunk size to ‘-1’. I had to set this before in order to get the report output successfully, but now it’s working fine without the setting with my latest BI Publisher Server environment. (March ‘10 patchset).
| ReportRequest req = new ReportRequest(); //Instantiate ReportRequest object req. setSizeOfDataChunkDownload (-1); //download all |
Conclusion
Now you know how to call BI Publisher Web Service to run a report. My example is trying to get the report output and display it in a browser, but of course you can display it as part of your application UI or locate it on a file system.
Here is my whole source code. Again, I’m using a Java Servlet to exercise my code with BI Publisher Web Service.
| package project1; import com.oracle.xmlns.oxp.service.publicreportservice.AccessDeniedException; import com.oracle.xmlns.oxp.service.publicreportservice.PublicReportServiceService; import com.oracle.xmlns.oxp.service.publicreportservice.ReportResponse; import com.oracle.xmlns.oxp.service.publicreportservice.ScheduleRequest; import java.io.IOException; import java.net.URL; import javax.servlet.*; import javax.xml.namespace.QName; public class BipServletTest extends HttpServlet { public void init(ServletConfig config) throws ServletException { public void doGet(HttpServletRequest request, } public String getSessionID(String username, String password, PublicReportService publicReportService) throws AccessDeniedException_Exception { public ReportResponse getReportInSession(PublicReportService publicReportService, ReportRequest req = new ReportRequest(); req.setAttributeFormat("pdf"); try { } catch (Exception e) { return res; } |
I’ll talk about how to schedule a BI Publisher report with the Web Service tomorrow, so stay tuned!