How to deploy a report (jrxml
file) through the Jasper PHP/REST API
to the Jasper Reports Server?
To upload a jrxml file, create a ResourceDescriptor
with PROP_HAS_DATA
= true
and insert the jrxml content in a multipart PUT
request.
After a while of researching and investigating I got it running and developed a PHP class that's easy to use.
http://blog.flowl.info/2013/jasper-php-library-on-github/
To upload a jrxml file this code does the job:
// Init the Jasper connection
require_once('Jasper/Jasper.php');
$jasper = new \Jasper\Jasper();
$jasper->login('jasperadmin', 'jasperadmin', 'jasper.host.com:8080');
// Create a Resource Descriptor object for the jrxml file
$jrxml = new \Jasper\JasperJrxml('/reports/test.jrxml');
// Upload the Resource Descriptor object with content
$jasper->createContent($jrxml, file_get_contents('templates/test.jrxml'));
To create a report unit, go on with the following lines:
// Datasource Resource Descriptor
$mongo = new \Jasper\JasperDatasource();
$mongo->setPropIsReference('true');
$mongo->setPropReferenceUri('/datasources/mongo_local_test');
// Put everything together and deploy the report
$report->addChildResource($mongo);
$report->addChildResource($jrxml);
// Want to see the Resource Descriptor of the Report Unit?
// true = pretty print
print_r($report->getXml(true));
// Create a the Report Unit
$jasper->createResource($report);
For deploying Jasper reports on Jasper Server using Rest-API you can use following method:
JRXML_DATA=$(cat $PATHTOJRXMLFILE/$JRXML_FILE)
curl -X POST $JASPER_REST_HOST/jasperserver/rest_v2/resources/reports \
-H "Content-Type:application/jrxml" \
-H "Content-Disposition:attachment; filename=test" \
-H "Content-Description:test file" \
-d "$JRXML_DATA" \
--user $JASPER_USERNAME:$JASPER_USERNAME
RESOURCEDESCRIPTOR_JSON=$(cat $REPORT/deployable/reportunit.json)
curl -X POST $JASPER_REST_HOST/jasperserver/rest_v2/resources/reports \
-H "Content-Type:application/repository.reportUnit+json" \
-d "$RESOURCEDESCRIPTOR_JSON" \
--user $JASPER_USERNAME:$JASPER_PASSWORD
{
"uri": "/reports/test_report",
"label": "test_report",
"description": "description",
"permissionMask": "0",
"version": "0" ,
"alwaysPromptControls": "true",
"controlsLayout": "popupScreen",
"jrxml": {
"jrxmlFileReference": {
"uri": "/reports/test"
}
}
}
http://community.jaspersoft.com/documentation/jasperreports-server-web-services-guide/v56/v2-resource-descriptor-types