diff --git a/api/src/main/java/org/openmrs/module/reporting/config/ReportLoader.java b/api/src/main/java/org/openmrs/module/reporting/config/ReportLoader.java index d8fedb69ad..da0ce6ba47 100644 --- a/api/src/main/java/org/openmrs/module/reporting/config/ReportLoader.java +++ b/api/src/main/java/org/openmrs/module/reporting/config/ReportLoader.java @@ -27,6 +27,7 @@ import org.openmrs.module.reporting.report.definition.service.ReportDefinitionService; import org.openmrs.module.reporting.report.processor.DiskReportProcessor; import org.openmrs.module.reporting.report.processor.EmailReportProcessor; +import org.openmrs.module.reporting.report.processor.HttpReportProcessor; import org.openmrs.module.reporting.report.processor.LoggingReportProcessor; import org.openmrs.module.reporting.report.renderer.CsvReportRenderer; import org.openmrs.module.reporting.report.renderer.ReportDesignRenderer; @@ -287,6 +288,8 @@ else if ("email".equalsIgnoreCase(type)) { } else if ("logging".equalsIgnoreCase(type)) { type = LoggingReportProcessor.class.getName(); + } else if ("http".equalsIgnoreCase(type)) { + type = HttpReportProcessor.class.getName(); } c.setProcessorType(type); c.setRunOnSuccess(processorDescriptor.getRunOnSuccess()); 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..7c401cdea9 --- /dev/null +++ b/api/src/main/java/org/openmrs/module/reporting/report/processor/HttpReportProcessor.java @@ -0,0 +1,80 @@ +package org.openmrs.module.reporting.report.processor; + +import java.io.OutputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; + +import org.apache.commons.lang.StringUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.openmrs.module.reporting.report.Report; +import org.springframework.stereotype.Component; + +/** + * A ReportProcessor which sends the rendered report via HTTP POST + */ +@Component +public class HttpReportProcessor implements ReportProcessor { + + protected Log log = LogFactory.getLog(this.getClass()); + + /** + * @see ReportProcessor#getConfigurationPropertyNames() + */ + public List getConfigurationPropertyNames() { + List ret = new ArrayList(); + ret.add("url"); + ret.add("contentType"); + ret.add("dateFrom"); + ret.add("dateTo"); + return ret; + } + + /** + * Performs some action on the given report + * @param report the Report to process + */ + public void process(Report report, Properties configuration) { + + try { + String urlString = configuration.getProperty("url"); + if (StringUtils.isBlank(urlString)) { + throw new IllegalArgumentException("URL cannot be blank"); + } + + String contentType = configuration.getProperty("contentType", "text/plain"); + String dateFrom = configuration.getProperty("dateFrom"); + String dateTo = configuration.getProperty("dateTo"); + + URL url = new URL(urlString); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setDoOutput(true); + conn.setRequestMethod("POST"); + conn.setRequestProperty("Content-Type", contentType); + + byte[] reportData = report.getRenderedOutput(); + + conn.setRequestProperty("Date-From", dateFrom); + conn.setRequestProperty("Date-To", dateTo); + + OutputStream os = conn.getOutputStream(); + os.write(reportData); + + + int responseCode = conn.getResponseCode(); + if (responseCode == HttpURLConnection.HTTP_OK) { + log.info("Report sent successfully to " + urlString); + } else { + log.error("Failed to send report to " + urlString + ", HTTP error code: " + responseCode); + } + + conn.disconnect(); + } + catch (Exception e) { + throw new RuntimeException("Error occurred while sending report via HTTP", e); + } + } +}