Goals
System Administrators or developers may have a need to upload arbitrary reports via REST to JasperReports Server. Using a simple script to do that rather than import/export, iReport or JSS can be useful in number of situations:
- Development process - new report version of the report uploaded via SVN or GIT hook. This keeps both reports versioned and deployed to the server.
- System Administration process - when using an export is not feasible
- Bulk upload via command-line
- Embedding report upload functionality in other applications
- Various test utilities
Documentation
- JasperReports Server Web Services Guide - see Docs.
- Python 2.7 official documentation.
- Requests library for Python
Short description
Web Services Guide, 2.2.4 Creating a Resource describes how to create a request.
In a short form this should be:
- PUT http://<host>:<port>/jasperserver[-pro]/rest/resource/path/to/resource/
- form-data: valid ResourceDescriptor
- application/octet-stream for attached JRXML files.
ResourceDescriptor syntax is out of the scope of this article. I would suggest exisiting ResourceDescriptors. You can check them via iReport or REST GET to resources service. A sample resourcedescriptor_0.xml is attached for reference.
Example request
Looking through requests documentation this should look something like this:
#!/usr/bin/python import requests #open resource descriptor and jrxml file for reading try: resource_descriptor = open('resource_descriptor.xml', 'rb') report_jrxml = open('test.jrxml', 'rb') except: raise #Path to resource rest service: url = 'http://loclahost:8080/jasperserver-pro/rest/resource' #Authorisation credentials: auth = ('jasperadmin', 'jasperadmin') #initialize data. Requests will handle encoding. data = {"ResourceDescriptor": resource_descriptor.read() } #initialize files with report_name and data tuple. Requests will handle encoding. #Note that "name" in the PUT request should equal report_files path, #i.e. for report in /repopathtoreport/report the path would be /repopathtoreport/report_files files = {'/repopathtoreport/report_files': ('report_name', upload)} #making a put request straight from documentation r = requests.put(url=url, data=data, files=files, auth=auth) #to see request status code do #print r.status_code #will raise HTTP error if there is one: r.raise_for_status() #Done! Good practice would be to close files when they are not required - both can be quite big.
Example implementation: command-line utility to upload reports
Example uses jinja2 to render valid ResourceDescriptor.
Templates should be located in 'templates' directory (see res_descr.xml)
See jasperrest_0.zipfor jasperrest.py. Libraries required:
import requests import argparse import logging import httplib import jinja2 import os
To see help, run jasperrest.py -h :
usage: jasperrest.py [-h] -r REPO_PATH -j REPORT_JRXML -d REPORT_DATASOURCE [-l JASPER_URL] [-u USER] [-p PASSWORD] [-t TEMPLATE] Upload jrxml file as a report to JasperReports server optional arguments: -h, --help show this help message and exit -r REPO_PATH, --repo_path REPO_PATH New report repository path (with report name) -j REPORT_JRXML, --report_jrxml REPORT_JRXML jrxml_file for upload -d REPORT_DATASOURCE, --report_datasource REPORT_DATASOURCE datasource repo path -l JASPER_URL, --jasper_url JASPER_URL JasperReports Server URL, default http://localhost:8080/jasperserver-pro -u USER, --user USER JasperReports Server user, default superuser -p PASSWORD, --password PASSWORD JasperReports Server password, default superuser -t TEMPLATE, --template TEMPLATE ResourceDescriptor template name in templates dir, default res_descr.xml
Sample syntax:
jasperrest -r /public/new07 -j test.jrxml -d /organizations/organization_1/datasources/repositoryDS -l http://localhost:8080/jasperserver-pro
Notes
In this article I am assuming the PRO version of JasperReports Server is used. However, the same should work for CE version if you use correct uri, repo path and user.
Recommended Comments
There are no comments to display.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now