如何使用节点js通过JasperServer api上传jrxml文件

时间:2016-10-13 07:25:38

标签: node.js jasperserver

我正在尝试通过putRequest api将jrxml文件上传到Jasper服务器。使用以下xml输入

在服务器MyReports路径中添加报告条目

XML

<?xml version="1.0" encoding="UTF-8"?>
<request operationName="put" locale="en">
<argument name="CREATE_REPORTUNIT_BOOLEAN">true</argument>
<!--MainReportunit-->
<resourceDescriptor name="TestReport1" wsType="reportUnit"     uriString="/reports/Myreports/TestReport1" isNew="true">
  <label><![CDATA[TestReport1]]></label>
  <description><![CDATA[TestReport1]]></description>
  <resourceProperty name="PROP_RESOURCE_TYPE">
     <value><![CDATA[com.jaspersoft.jasperserver.api.metadata.jasperreports.domain.ReportUnit]]></value>
  </resourceProperty>
  <resourceProperty name="PROP_PARENT_FOLDER">
     <value><![CDATA[/reports/Myreports]]></value>
  </resourceProperty>
  <resourceProperty name="PROP_RU_ALWAYS_PROPMT_CONTROLS">
     <value><![CDATA[true]]></value>
  </resourceProperty>
  <resourceProperty name="PROP_RU_CONTROLS_LAYOUT">
     <value><![CDATA[1]]></value>
  </resourceProperty>
  <resourceDescriptor wsType="datasource" isNew="false">
     <resourceProperty name="PROP_REFERENCE_URI">
        <value><![CDATA[/datasources/{DATASOURCE_NAME}]]></value>
     </resourceProperty>
     <resourceProperty name="PROP_IS_REFERENCE">
        <value><![CDATA[true]]></value>
     </resourceProperty>
  </resourceDescriptor>
  <resourceDescriptor name="TestReport1" wsType="jrxml" uriString="/reports/Myreports/TestReport1" isNew="true">
     <label><![CDATA[TestReport1]]></label>
     <description><![CDATA[TestReport1]]></description>
     <resourceProperty name="PROP_RESOURCE_TYPE">
        <value><![CDATA[com.jaspersoft.jasperserver.api.metadata.common.domain.FileResource]]></value>
     </resourceProperty>
     <resourceProperty name="PROP_HAS_DATA">
        <value><![CDATA[true]]></value>
     </resourceProperty>
     <resourceProperty name="PROP_RU_IS_MAIN_REPORT">
        <value><![CDATA[true]]></value>
     </resourceProperty>
  </resourceDescriptor>
</resourceDescriptor>
</request>

node js code

var parameters = {
        putRequest: RequestXmlStr
    }
var soap = require('soap');
soap.createClient(url, function (err, client) {
 client.put(parameters, function (err, result) {
        if (err) console.error(err);
    });

});

没有依恋,工作正常。但我不知道,在这个xml中包含jrxml数据或base64字符串的位置。

1 个答案:

答案 0 :(得分:0)

由于SOAP不是必需的,因此以下是我使用REST_v2服务的方法:

var request = require("request"),
  fs = require("fs"),
  reportUnitDescriptor = {
    "label": "My Report Unit",
    "description": "My First Report Unit",
    "alwaysPromptControls": "true",
    "controlsLayout": "popupScreen",
    "dataSource": {
        "dataSourceReference": {
            "uri": "/analysis/datasources/FoodmartDataSourceJNDI"
        }
    },
    "jrxml": {
        "jrxmlFile": {
            "label": "Main JRXML",
            "type": "jrxml"
        }
    }   
};

request.post({
  url: "http://localhost:8080/jasperserver/rest_v2/resources/public",
  auth: { user: "jasperadmin", password: "jasperadmin" },
  formData: {
    resource: {
      value: JSON.stringify(reportUnitDescriptor),
      options: {
        contentType: "application/repository.reportUnit+json"
      }
    },
    jrxml: fs.createReadStream(__dirname + '/MainReport.jrxml'),
  }
}, function(err, response, body) {
  console.log("status: " + response.statusCode + "; message: " + response.statusMessage);
});

一些注意事项:

  • reportUnit将在Public文件夹中创建。您可以在/resources部分之后的URL中指定。
  • MainReport.jrxml与此脚本
  • 位于同一目录中
  • 有关REST_v2 API的更多信息here