From ecd3ed66a2d5ccab5ecd682dbf8fd600777a777c Mon Sep 17 00:00:00 2001 From: gita cliff Date: Tue, 7 Apr 2020 11:18:34 +0300 Subject: [PATCH] REPORT-862:Create an HttpReportProcessor --- .../report/processor/HttpReportProcessor.java | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 api/src/main/java/org/openmrs/module/reporting/report/processor/HttpReportProcessor.java diff --git a/api/src/main/java/org/openmrs/module/reporting/report/processor/HttpReportProcessor.java b/api/src/main/java/org/openmrs/module/reporting/report/processor/HttpReportProcessor.java new file mode 100644 index 0000000000..bfa5c61fe7 --- /dev/null +++ b/api/src/main/java/org/openmrs/module/reporting/report/processor/HttpReportProcessor.java @@ -0,0 +1,64 @@ +package org.openmrs.module.reporting.report.processor; + +import java.io.OutputStreamWriter; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.openmrs.module.reporting.report.Report; +import org.springframework.stereotype.Component; + +@Component +public class HttpReportProcessor implements ReportProcessor { + + protected Log log = LogFactory.getLog(this.getClass()); + + /** + * @see ReportProcessor#getConfigurationPropertyNames() + */ + @Override + public List getConfigurationPropertyNames() { + List ret = new ArrayList(); + ret.add("connection url"); + ret.add("subject"); + ret.add("Add report"); + return ret; + } + + /** + * Performs some action on the given report + * + * @param report the Report to process + */ + @Override + public void process(Report report, Properties configuration) { + try { + URL url = new URL(configuration.getProperty("connection url")); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("POST"); + connection.setRequestProperty("Content-Type", "text/html; charset=UTF-8"); + connection.setDoOutput(true); + connection.connect(); + + if (report.getRenderedOutput() != null && "true".equalsIgnoreCase(configuration.getProperty("Add report"))) { + String addcontent = configuration.getProperty(("Add report") + configuration.getProperty("subject")); + if (report.getOutputContentType().contains("text") || report.getOutputContentType().contains("html")) { + addcontent = new String(report.getRenderedOutput(),"UTF-8"); + } + OutputStreamWriter writer = new OutputStreamWriter( + connection.getOutputStream()); + writer.write(addcontent); + writer.flush(); + writer.close(); + } + } catch (Exception e) { + throw new RuntimeException("Error occurred while sending report via http POST", e); + } + + } + +}