Goals
System Administrators or developers may have a need to download arbitrary report contents 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:
- Embedding report download and viewing in other applications
- Development process - download report content without tying up to repository structure. Could be useful to initilize a new repository with new structure.
- System Administration process - when using an export is not feasible
- Bulk download via command-line
- Various test utilities
This article is related to uploading reports with python 2.7 and requests
Documentation
- JasperReports Server Web Services Guide - see Docs.
- Python 2.7 official documentation.
- Requests library for Python
Short description
Web Services Guide, 2.2.1 and 2.2.2 Requesting the Contents of a JasperReport and Requesting the Contents of a File Resource
This is a bit more complex than uploading a report (see uploading reports with python 2.7 and requests )
- GET http://<host>:<port>/jasperserver[-pro]/rest/resource/path/to/resource/
- We will get a valid ResourceDescriptor with all attachements listed
- Now we need to parse this ResourceDescriptor and download required attachements
- Nw we need to download each file, for example: GET /jasperserver/rest/resource/reports/samples/AllAccounts_files/AllAccounts_Res3?fileData=true
- You HAVE to use fileData=true
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.
Example request
#!/usr/bin/env import requests import xml.etree.ElementTree as ET # Path to resource rest service: url = 'http://loclahost:8080/jasperserver-pro/rest/resource' # Report to process: report = '/somereport' # Authorisation credentials: auth = ('jasperadmin', 'jasperadmin') # file types to download: file_types = ['jrxml', 'img', 'jar' ] # Init session so we have no need to auth again and again: s = requests.Session() r = s.get(url=url+report, auth=auth) # init empty dictionary files = {} # creating tree from ResourceDescripotor we have in Response tree = ET.fromstring(r.content) # searching tree. Could be more efficient with Xpath # You may also implement testing for reportUnit wsType for speedsup for i in tree.iter('resourceDescriptor'): if 'wsType' in i.attrib: # we do not need all wsTypes, checking against file_types set previously if i.attrib['wsType'] in file_types: #Storing uriString and wsType files[i.attrib['name']] = [i.attrib['uriString'], i.attrib['wsType']] # This could have been done while searching the tree. for filename in files: #each file has a bit different url file_url = url + files[filename][0] using session and parameters to get file content r = s.get(url=url, params=params) # Files in repository # can be stored without extension, so # writing to filename with extension of wsType. i.e: # jrxml, jar, img. This has implication of all images # having ".img" file extension, etc. # Note that file is opened in binary mode with open(filename + "." + files[filename][1], "wb") as f: # we can have binary data, so using r.content f.write(r.content) # Done!
Example implementation: command-line utility to download report files
See report_download.zip.
Required libraries (in most cases only requests needs to be installed):
import requests import argparse import xml.etree.ElementTree as ET import logging import httplib import os import sys
To see help, run report_download.py -h:
usage: report_download.py [-h] -r REPO_PATH [-f FOLDER] [-l JASPER_URL] [-u USER] [-p PASSWORD] 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 Report repository path (with report name) -f FOLDER, --folder FOLDER Folder to download report -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
Example usage:
report_download.py -r /organizations/organization_1/reports/samples/Cascading_multi_select_report -l http://localhost/jasperserver-pro-51 -f cascading
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.