October 07, 2007
Contributed by Ashish Sarin, Pentaho Community Member
Pentaho reports are normally accessed using the web application that comes alongwith the Pentaho PCI. There are scenarios when its required to access the reports over the web from other applications. For example, if there is an application which is developed using JSF/Portlets and it needs to use Pentaho reporting for generating and showing reports. One of the widely used approach is (assuming that reports are generated based on parameters passed by the user):
User enters the report filtering criterias on the screen. The application generates a hyperlink to the ViewAction servlet that generates and returns the report. The report filtering criterias (along with path to solutions directory and the name of xaction) are passed to ViewAction in the query string of the URL.
There are a number of problems with this approach:
1. It allows users to see the URL of the Pentaho server
2. There is no authentication involved.
3. The parameters are passed in the URL, therefore, its not possible to restrict the user from entering invalid filtering criterias, solution directory name, xactio name in the URL.
4. The application itself doesn't know what parameters were passed to the ViewAction and what was the result of executing the xaction. In some cases the application would like to maintain a log in case the report generation fails for some reason.
This tech tip attempts to provide a simpler solution to the above mentioned problems using Apache HttpClient for sending HTTP POST (you can also use HTTPS request) request to the ViewAction servlet and then showing how to access the generated report from a JSF/Portlet web application.
In Pentaho, a report is generated as a result of execution of an 'Action Sequence'. The action sequence implementation decides whether the report needs be generated and shown to the user or it is generated in the background and then e-mailed to the user as an attachment.
The following information is needed by the ViewAction servlet to generate the report :
1. Type of report to be generated: The report type could be PDF, XLS, HTML, RTF, etc
2. Solutions directory name: The name of the solutions directory which contains Pentaho solutions.
3. Action Sequence name: The name of the action sequence that needs to be executed
4. Path to the action sequence: The path to the directory (inside the solutions directory) which contains the action sequence
5. Run in background: Show report to the user or generate the report in background and e-mail it to the user as attachment
6. Filtering criteria: Report specific filtering criteria that is entered by the user.
If you go to the Pentaho 'Solutions' that come with PCI and access the 'Detailed Inventory Report' inside Steel Wheel Reporting Samples, you will see that the URL in the browser looks like:
The following code snippet shows the ReportParam class that contains all the information that Pentaho ViewAction servlet needs to generate a report:
The following code snippet shows the Constants class that contains all the standard names/values ViewAction servlet expects in the request:
The web application which is going to access the Pentaho report will be responsible for creating a ReportParam object and setting the appropriate values for all its attributes. In case of a JSF/Portlet application, when a user selects the filtering criteria, report type, etc the web application will be responsible for validating the inputs from the user. This way the user can be asked to fill the mandatory filtering criteria which the application expects.
The following figure shows a report lookup screen based on the similar lines as Detailed Inventory Report:
When the user clicks the 'Generate Report' button then the JSF/Portlet application ( or any other web application ) will validate that the mandatory fields are selected. The 'View Report' link is not shown to the user unless the validation is successful. The following code snippet shows how this can be done in a JSF action defined in a managed bean:
The validateAndShowReportLink method does the following:
- Validates that the user has selected the mandatory options
- Creates a ReportParam object and populates it with the parameters that needs to be passed to the Action Sequence
- Adds the ReportParam object to the PortletSession.
- Adds an attribute 'viewReportLink' to the session. The value of this attributes decides whether the 'View Report' link needs to be shown to the user or no
Portlets don't allow the content type of the response to be set to anything except text/html, therefore, an HttpServlet must be used to show the generated report from Pentaho. The 'View Report' link will invoke this HttpServlet and show the generated report to the user. If a portlet application needs to share session attributes with a servlet (in the same portal web application) then that session attribute must be set in the PortletSession's APPLICATION_SCOPE. This is the reason why the ReportParam object has been added to the PortletSession.APPLICATION_SCOPE. The Servlet will make use of the ReportParam object and generate the parameters to be passed to the Pentaho server.
The Servlet that will access the Pentaho server needs to address the following points:
1. It should make use of some validation mechanism to access the server
2. It should be able to send an HTTP request to the server as a POST request instead of GET request
3. It should pass obtain the report from the Pentaho and pass it to the response of the web application which invoked the servlet
Sending HTTP request using HttpClient
Step 1. Obtain the ReportParam object from the session and create the request parameters that needs to be passed to the Pentaho server
Step 2. Create the PostMethod object and add all the parameters to it.
Step 3. Set the default credentials
Step 4. Invoke the POST method on the Pentaho server and add the response obtained to the current reponse.