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

Programmatically generate and retrieve BIRT Report from Web-Viewer

我已经在我的服务器上安装了 BIRT Web-Viewer,并且能够使用这个 URL:

来构建报告
http://hostname:port/birt/run?__report=test.rptdesign

现在我需要从我的 Java 代码中以编程方式调用此 URL 并将结果作为流或文件检索。

Web-Viewer 有 API 吗?
如果没有,我可以像这样调用 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);

我发现,如果我使用值为pdf__format参数,对请求的响应是PDF内容,这正是我想要的。

标准响应是 HTML,它将与第二个请求一起返回。我很确定必须通过会话检索响应。

编辑: 根据要求,我将 post 我的请求代码。我稍微修改了一下,因为我使用了一些自定义 类 来保存配置和报告。

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();
}

在我调用 requestUrl.openConnection() 之前,我还有另一种方法将使用的数据作为 XML 写入文件系统,但我认为只有当您像我一样使用非常动态的数据时才需要这样做。