简体   繁体   English

以编程方式从Web-Viewer生成和检索BIRT报告

[英]Programmatically generate and retrieve BIRT Report from Web-Viewer

I've installed BIRT Web-Viewer on my server and am able to build the report with this URL: 我已经在服务器上安装了BIRT Web-Viewer,并能够使用以下URL构建报告:

http://hostname:port/birt/run?__report=test.rptdesign

Now I need to programmatically call this URL from my Java Code and retrieve the result as stream or file. 现在,我需要从Java代码中以编程方式调用此URL,并以流或文件的形式检索结果。

Is there any API for the Web-Viewer? Web-Viewer是否有任何API?
If not, could I just call the URL like this and extract the PDF?: 如果没有,我可以这样调用URL并提取PDF吗?:

HttpClient httpClient = HttpClients.createDefault();
HttpGet postRequest = new HttpPost("http://hostname:port/birt/run");
List<NameValuePair> formData = new ArrayList<>();
formData.add(new BasicNameValuePair("__report", "test.rptdesign"));
HttpEntity entity = new UrlEncodedFormEntity(formData);
HttpResponse response = httpClient.execute(postRequest);

I found out, if I use the __format parameter with the value pdf , the response to the request is the PDF content, which is exactly what I wanted. 我发现,如果我将__format参数与pdf值一起使用,则对请求的响应就是PDF内容,这正是我想要的。

The standard response is a HTML, which will be returned with a second request. 标准响应是HTML,它将与第二个请求一起返回。 I'm pretty sure that response has to be retrieved with sessions. 我非常确定必须通过会话来检索响应。

Edit: As requested I will post my request code. 编辑:根据要求,我将发布我的请求代码。 I modified it a bit, because I used some custom classes to hold configuration and the report. 我做了一些修改,因为我使用了一些自定义类来保存配置和报告。

public InputStream getReport() throws Exception {
  StringBuilder urlBuilder = new StringBuilder()
      .append("http://example.com:9080/contextRoot/run")
      .append("?__report=ReportDesign.rptdesign&__format=pdf");

  if (reportParameters != null) {
    for (Map.Entry<String, String> parameter : reportParameters.entrySet()) {
      String key = StringEscapeUtils.escapeHtml(parameter.getKey());
      String value = StringEscapeUtils.escapeHtml(parameter.getValue());

      urlBuilder.append('&')
          .append(key);
          .append('=');
          .append(value);
    }
  }

  URL requestUrl = new URL(burlBuilder.toString());
  HttpURLConnection connection = (HttpURLConnection) requestUrl.openConnection();
  connection.setRequestMethod("GET");
  connection.setDoInput(true);
  connection.connect();

  return connection.getInputStream();
}

I also had another method write the used data as XML to the file system before I called requestUrl.openConnection() , but I think this is only necessary if you use very dynamic data like I did. 在调用requestUrl.openConnection()之前,我还有另一种方法将使用过的数据作为XML写入文件系统,但是我认为只有在像我一样使用非常动态的数据时,才需requestUrl.openConnection()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM