Generating a Custom HTML Report
Introduction
This sample demonstrates how to create a HTML Report for REST Services using a Jasper Report template in WSO2 Governance Registry. The content of the report is a list of all REST Services that are stored in WSO2 G-Reg, including their name and version along with a brief description. If you need to customize it, change the key of the GenericArtifactManager
class, and modify the ReportBean
class in the source of the sample, according to the attributes you need to list in the report.
This sample converts the content of the image file to Base64 representation. Therefore, it can serve a single HTML file without having links to image files.
Prerequisites
Set up the general prerequisites required for WSO2 G-Reg, before you start.
This sample requires Apache Maven. For information on how to install it, see Installation Prerequisites.
Building the sample
Follow the steps below to build the sample.
Navigate to the
<G-REG_HOME>/samples/handler/src/
directory, using the Command Line Interface (CLI).Execute the following command, to compile the source code:
mvn clean install
This requires you to have Apache Maven installed.
This triggers an Apache Maven Build in your CLI. You view the following output in the CLI, when the build is successful.
Copy the
<G-REG_HOME>/samples/handler/src/target/org.wso2.carbon.registry.samples.handler-5.1.1.jar
file to the<G-REG_HOME>/repository/components/dropins/
directory.- Start the WSO2 Governance Registry. F or more instructions, see Running the Product.
Executing the sample
Follow the steps below to execute the sample.
Creating the resource
Follow the steps below to create a new collection.
- Log in to the G-Reg management console using the following URL and admin/admin credentials: https://<G-REG_HOST>:<G-REG_PORT>/carbon/
- Click Main, and then click Browse in the Resources menu as shown below.
- Navigate to the following location in the Tree view as shown below:
/_system/governance/repository/components/org.wso2.carbon.governance/
- Click the org.wso2.carbon.goveranance link, and then click Add Collection as shown below.
- Enter
templates
for Name, and click Add as show below.
- Click OK in the message that pops-up upon successful creation of the collection.
- Click Add Resource, to add a new resource to the
/_system/governance/repository/components/org.wso2.carbon.governance/
collection. - Enter the following details as shown below.
- Method: Select Create Text content
- Name: Enter
service_template.jrxml
- Media Type: Enter application/xml
Content: Select Plain Text Editor, and add the following content
Below is the Jasper Report template (.jrxml) that you use as the template file.
- Set the
isLazy
property to true, to display images in the report. - You can add an image to the report by editing the
${image path}
variable in this .jrxml file. Either use the URL of the image, or use its directory path if you save it locally into your machine. For example:- <![CDATA[
http://dab1nmslvvntp.cloudfront.net/wp-content/uploads/2015/07/1437059531JasperReports_Logo.png]]>
- <![CDATA[file:///Users/Desktop/download.png]]
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="Service List Report" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20"> <property name="ireport.zoom" value="1.3310000000000068"/> <property name="ireport.x" value="0"/> <property name="ireport.y" value="0"/> <field name="overview_name" class="java.lang.String"/> <field name="overview_version" class="java.lang.String"/> <field name="overview_description" class="java.lang.String"/> <background> <band height="802" splitType="Stretch"/> </background> <title> <band height="52" splitType="Stretch"> <staticText> <reportElement x="0" y="9" width="555" height="27"/> <textElement textAlignment="Center" verticalAlignment="Middle"> <font fontName="SansSerif" size="20"/> </textElement> <text><![CDATA[Service List Report]]></text> </staticText> <image isLazy="true"> <reportElement x="0" y="0" width="104" height="29"/> <imageExpression class="java.lang.String"> <![CDATA[file:///Users/Desktop/download.png]]> </imageExpression> </image> </band> </title> <pageHeader> <band splitType="Stretch"/> </pageHeader> <columnHeader> <band height="29" splitType="Stretch"> <staticText> <reportElement x="0" y="0" width="150" height="21"/> <textElement textAlignment="Left" verticalAlignment="Middle"> <font fontName="SansSerif" size="12" isBold="true"/> </textElement> <text><![CDATA[Name]]></text> </staticText> <staticText> <reportElement x="150" y="0" width="305" height="21"/> <textElement textAlignment="Center" verticalAlignment="Middle"> <font fontName="SansSerif" size="12" isBold="true"/> </textElement> <text><![CDATA[Version]]></text> </staticText> <line> <reportElement x="0" y="21" width="555" height="1"/> </line> <staticText> <reportElement x="455" y="0" width="100" height="21"/> <textElement textAlignment="Center" verticalAlignment="Middle"> <font fontName="SansSerif" size="12" isBold="true"/> </textElement> <text><![CDATA[Description]]></text> </staticText> </band> </columnHeader> <detail> <band height="30" splitType="Stretch"> <textField> <reportElement x="0" y="4" width="305" height="20"/> <textElement textAlignment="Center" verticalAlignment="Middle"/> <textFieldExpression class="java.lang.String"><![CDATA[$F{overview_name}]]></textFieldExpression> </textField> <textField> <reportElement x="305" y="4" width="150" height="20"/> <textElement textAlignment="Left" verticalAlignment="Middle"/> <textFieldExpression class="java.lang.String"><![CDATA[$F{overview_version}]]></textFieldExpression> </textField> <textField> <reportElement x="455" y="4" width="100" height="20"/> <textElement textAlignment="Center" verticalAlignment="Middle"/> <textFieldExpression class="java.lang.String"><![CDATA[$F{overview_description}]]></textFieldExpression> </textField> </band> </detail> <columnFooter> <band splitType="Stretch"/> </columnFooter> <pageFooter> <band height="29" splitType="Stretch"> <staticText> <reportElement x="0" y="3" width="204" height="18"/> <textElement verticalAlignment="Top"> <font size="8"/> </textElement> <text><![CDATA[Created with WSO2 Governanace Registry]]></text> </staticText> <line> <reportElement x="0" y="1" width="555" height="1"/> </line> <textField> <reportElement x="246" y="2" width="80" height="20"/> <textElement textAlignment="Right"> <font size="8"/> </textElement> <textFieldExpression class="java.lang.String"><![CDATA["Page "+$V{PAGE_NUMBER}+" of"]]></textFieldExpression> </textField> <textField evaluationTime="Report"> <reportElement x="327" y="2" width="69" height="20"/> <textElement> <font size="8"/> </textElement> <textFieldExpression class="java.lang.String"><![CDATA[" " + $V{PAGE_NUMBER}]]></textFieldExpression> </textField> <textField pattern="dd/MM/yyyy HH.mm.ss"> <reportElement x="456" y="3" width="99" height="18"/> <textElement textAlignment="Center"> <font size="8"/> </textElement> <textFieldExpression class="java.util.Date"><![CDATA[new java.util.Date()]]> </textFieldExpression> </textField> </band> </pageFooter> <summary> <band splitType="Stretch"/> </summary> </jasperReport>
- Click OK in the message that pops-up upon successful creation of the resource.
Creating the report
Follow the steps below to create the report in WSO2 G-Reg.
- Log in to the G-Reg management console, if you are not already logged in.
- Click Main, and then click Reports in the Resources menu.
- Click Add Report.
- Enter the following details as shown below.
- Report Name: Enter
Service Report
- Template: Browse, and select the
/_system/governance/repository/components/org.wso2.carbon.governance/templates/service_template.jrxml
template, and click OK - Report Type: Seelct HTML
Report Class: Enter
org.wso2.carbon.registry.samples.reporting.ServiceReportGenerator
- Report Name: Enter
- Click Add. You view the new report added to the list of available reports as shown below.
Viewing the output
Follow the steps below to view the output of this sample.
- Click the corresponding Generate button of the
Service Report
. - Click Generate as shown below.
- This downloads the HTML report you generated. You can view it using your web browser as shown below.