From 3175c5ba60c4dfa0cbf18500a737afd9e508fce9 Mon Sep 17 00:00:00 2001 From: Gift Mungule Date: Tue, 26 Mar 2024 14:24:08 +0200 Subject: [PATCH 1/5] added http report processor --- .../report/processor/HttpReportProcessor.java | 91 +++++++++++++++++++ 1 file changed, 91 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..28ff8cf692 --- /dev/null +++ b/api/src/main/java/org/openmrs/module/reporting/report/processor/HttpReportProcessor.java @@ -0,0 +1,91 @@ +package org.openmrs.module.reporting.report.processor; + +import java.io.OutputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; + +import org.json.simple.JSONObject; +import org.openmrs.module.reporting.report.Report; + +public class HttpReportProcessor implements ReportProcessor { + private String connectionUrl; + + + public HttpReportProcessor(String connectionUrl) { + this.connectionUrl = connectionUrl; + } + + @Override + public List getConfigurationPropertyNames() { + List ret = new ArrayList<>(); + ret.add("connectionUrl"); + ret.add("exportType"); + ret.add("reportName"); + ret.add("description"); + ret.add("dateFrom"); + ret.add("dateTo"); + return ret; + } + + + @Override + public void process(Report report, Properties configuration) { + this.connectionUrl = configuration.getProperty("connectionUrl"); + + try { + // Establish connection to the URL + URL url = new URL(connectionUrl); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("POST"); + connection.setRequestProperty("Content-Type", "application/json"); + connection.setDoOutput(true); + + // Write report data to the request body + String reportData = generateReportData(report, configuration); + + try (OutputStream os = connection.getOutputStream()) { + byte[] input = reportData.getBytes(StandardCharsets.UTF_8); + os.write(input, 0, input.length); + } + + // Check response code + int responseCode = connection.getResponseCode(); + if (responseCode == HttpURLConnection.HTTP_OK) { + // Connection is successful + System.out.println("Report sent successfully via HTTP POST."); + } else { + // Handle other response codes if needed + System.out.println("Failed to send report. Response code: " + responseCode); + } + } catch (Exception e) { + // Handle connection errors + System.out.println("Error sending report via HTTP POST: " + e.getMessage()); + } + } + + private String generateReportData(Report report, Properties configuration) { + // Example method to generate report data + String exportType = configuration.getProperty("exportType"); + String reportName = configuration.getProperty("reportName"); + String description = configuration.getProperty("description"); + String dateFrom = configuration.getProperty("dateFrom"); + String dateTo = configuration.getProperty("dateTo"); + + // Construct JSON object with report properties + JSONObject data = new JSONObject(); + data.put("exportType", exportType); + data.put("reportName", reportName); + data.put("description", description); + data.put("dateFrom", dateFrom); + data.put("dateTo", dateTo); + + // Replace this with your actual report data if needed + data.put("reportData", "Sample report data"); + + return data.toString(); + } +} From 92d00a4f74810b6749d6be5d21ae7cc5e88693a9 Mon Sep 17 00:00:00 2001 From: Gift Mungule Date: Wed, 27 Mar 2024 22:01:03 +0200 Subject: [PATCH 2/5] Adding http report processor to report loader and changes to the http report processor class --- .../module/reporting/config/ReportLoader.java | 3 +++ .../report/processor/HttpReportProcessor.java | 14 +++++--------- 2 files changed, 8 insertions(+), 9 deletions(-) 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 index 28ff8cf692..f1a6d01430 100644 --- 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 @@ -52,23 +52,22 @@ public void process(Report report, Properties configuration) { os.write(input, 0, input.length); } - // Check response code + // Check connection response int responseCode = connection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { - // Connection is successful + System.out.println("Report sent successfully via HTTP POST."); } else { - // Handle other response codes if needed + System.out.println("Failed to send report. Response code: " + responseCode); } } catch (Exception e) { - // Handle connection errors System.out.println("Error sending report via HTTP POST: " + e.getMessage()); } } private String generateReportData(Report report, Properties configuration) { - // Example method to generate report data + String exportType = configuration.getProperty("exportType"); String reportName = configuration.getProperty("reportName"); String description = configuration.getProperty("description"); @@ -77,15 +76,12 @@ private String generateReportData(Report report, Properties configuration) { // Construct JSON object with report properties JSONObject data = new JSONObject(); - data.put("exportType", exportType); + data.put("exportType", exportType); data.put("reportName", reportName); data.put("description", description); data.put("dateFrom", dateFrom); data.put("dateTo", dateTo); - // Replace this with your actual report data if needed - data.put("reportData", "Sample report data"); - return data.toString(); } } From b2ea989704bfc52676e8db825c4f74e1a5d56f59 Mon Sep 17 00:00:00 2001 From: Gift Mungule Date: Wed, 27 Mar 2024 22:18:16 +0200 Subject: [PATCH 3/5] REPORT-862 --- .../report/processor/HttpReportProcessor.java | 99 +++++++++---------- 1 file changed, 46 insertions(+), 53 deletions(-) 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 index f1a6d01430..50820ef1a1 100644 --- 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 @@ -3,85 +3,78 @@ import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; -import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; import java.util.Properties; -import org.json.simple.JSONObject; +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 { - private String connectionUrl; + protected Log log = LogFactory.getLog(this.getClass()); - public HttpReportProcessor(String connectionUrl) { - this.connectionUrl = connectionUrl; - } - - @Override + /** + * @see ReportProcessor#getConfigurationPropertyNames() + */ public List getConfigurationPropertyNames() { List ret = new ArrayList<>(); - ret.add("connectionUrl"); - ret.add("exportType"); - ret.add("reportName"); - ret.add("description"); + ret.add("url"); + ret.add("contentType"); ret.add("dateFrom"); ret.add("dateTo"); return ret; } - - @Override + /** + * Performs some action on the given report + * @param report the Report to process + */ public void process(Report report, Properties configuration) { - this.connectionUrl = configuration.getProperty("connectionUrl"); try { - // Establish connection to the URL - URL url = new URL(connectionUrl); - HttpURLConnection connection = (HttpURLConnection) url.openConnection(); - connection.setRequestMethod("POST"); - connection.setRequestProperty("Content-Type", "application/json"); - connection.setDoOutput(true); - - // Write report data to the request body - String reportData = generateReportData(report, configuration); - - try (OutputStream os = connection.getOutputStream()) { - byte[] input = reportData.getBytes(StandardCharsets.UTF_8); - os.write(input, 0, input.length); + String urlString = configuration.getProperty("url"); + if (StringUtils.isBlank(urlString)) { + throw new IllegalArgumentException("URL cannot be blank"); } - // Check connection response - int responseCode = connection.getResponseCode(); - if (responseCode == HttpURLConnection.HTTP_OK) { + String contentType = configuration.getProperty("contentType", "text/plain"); + String dateFrom = configuration.getProperty("dateFrom"); + String dateTo = configuration.getProperty("dateTo"); - System.out.println("Report sent successfully via HTTP POST."); - } else { + URL url = new URL(urlString); + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setDoOutput(true); + conn.setRequestMethod("POST"); + conn.setRequestProperty("Content-Type", contentType); - System.out.println("Failed to send report. Response code: " + responseCode); - } - } catch (Exception e) { - System.out.println("Error sending report via HTTP POST: " + e.getMessage()); - } - } + byte[] reportData = report.getRenderedOutput(); - private String generateReportData(Report report, Properties configuration) { + conn.setRequestProperty("Date-From", dateFrom); + conn.setRequestProperty("Date-To", dateTo); - String exportType = configuration.getProperty("exportType"); - String reportName = configuration.getProperty("reportName"); - String description = configuration.getProperty("description"); - String dateFrom = configuration.getProperty("dateFrom"); - String dateTo = configuration.getProperty("dateTo"); + try (OutputStream os = conn.getOutputStream()) { + os.write(reportData); + } - // Construct JSON object with report properties - JSONObject data = new JSONObject(); - data.put("exportType", exportType); - data.put("reportName", reportName); - data.put("description", description); - data.put("dateFrom", dateFrom); - data.put("dateTo", dateTo); + 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); + } - return data.toString(); + conn.disconnect(); + } + catch (Exception e) { + throw new RuntimeException("Error occurred while sending report via HTTP", e); + } } } From 3abef28c2b756f78f133578c6f9b5c7df4859ab9 Mon Sep 17 00:00:00 2001 From: Gift Mungule Date: Thu, 28 Mar 2024 09:14:38 +0200 Subject: [PATCH 4/5] REPORT-862-CREATION-OF-HTTP-REPORT-PROCESSOR --- api/pom.xml | 12 ++++++++++++ .../report/processor/HttpReportProcessor.java | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/api/pom.xml b/api/pom.xml index 517596edc4..f4aab253ec 100644 --- a/api/pom.xml +++ b/api/pom.xml @@ -1,5 +1,17 @@ 4.0.0 + + + + org.apache.maven.plugins + maven-compiler-plugin + + 7 + 7 + + + + org.openmrs.module reporting 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 index 50820ef1a1..158bd74d80 100644 --- 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 @@ -25,7 +25,7 @@ public class HttpReportProcessor implements ReportProcessor { * @see ReportProcessor#getConfigurationPropertyNames() */ public List getConfigurationPropertyNames() { - List ret = new ArrayList<>(); + List ret = new ArrayList(); ret.add("url"); ret.add("contentType"); ret.add("dateFrom"); From 036e956d474760630091b399ceb032738b3c7e29 Mon Sep 17 00:00:00 2001 From: Gift Mungule Date: Thu, 28 Mar 2024 12:47:52 +0200 Subject: [PATCH 5/5] reverting pom file due to build check failure --- api/pom.xml | 12 ------------ .../report/processor/HttpReportProcessor.java | 4 ++-- 2 files changed, 2 insertions(+), 14 deletions(-) diff --git a/api/pom.xml b/api/pom.xml index f4aab253ec..517596edc4 100644 --- a/api/pom.xml +++ b/api/pom.xml @@ -1,17 +1,5 @@ 4.0.0 - - - - org.apache.maven.plugins - maven-compiler-plugin - - 7 - 7 - - - - org.openmrs.module reporting 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 index 158bd74d80..7c401cdea9 100644 --- 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 @@ -60,9 +60,9 @@ public void process(Report report, Properties configuration) { conn.setRequestProperty("Date-From", dateFrom); conn.setRequestProperty("Date-To", dateTo); - try (OutputStream os = conn.getOutputStream()) { + OutputStream os = conn.getOutputStream(); os.write(reportData); - } + int responseCode = conn.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) {