From 6c2cf29a1fe349a68f107ff70bfe5dc74232c7ee Mon Sep 17 00:00:00 2001 From: Seremba Patrick Date: Thu, 23 Mar 2023 12:12:51 +0300 Subject: [PATCH 1/2] REPORT-862:create an HttpReportProcessor --- .../report/processor/HttpReportProcessor.java | 102 ++++++++++++++++++ 1 file changed, 102 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..1416b413e7 --- /dev/null +++ b/api/src/main/java/org/openmrs/module/reporting/report/processor/HttpReportProcessor.java @@ -0,0 +1,102 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under + * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. + * + * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS + * graphic logo is a trademark of OpenMRS Inc. + */ + +package org.openmrs.module.reporting.report.processor; + +import java.io.BufferedReader; +import java.io.DataOutputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; + +import org.openmrs.module.reporting.report.Report; +import org.springframework.stereotype.Component; + +/** + * A ReportProcessor which sends the rendered report via POST + */ +@Component +public class HttpReportProcessor implements ReportProcessor { + + public final String CONNECTION_URL = "url"; + public final String SUBJECT = "subject"; + public final String ADD_REPORT = "addReport"; + + /** + * @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) { + // TODO Auto-generated method stub + HttpURLConnection connection = null; + try { + if (report.getRenderedOutput() != null && "true".equalsIgnoreCase(configuration.getProperty(SUBJECT))) { + + URL url = new URL(configuration.getProperty(CONNECTION_URL)); + + connection = (HttpURLConnection) url.openConnection(); + + connection.setRequestMethod("POST"); + + connection.setRequestProperty("Content-Type", "application/json"); + + connection.setDoOutput(true); + + String reportData = configuration.getProperty(ADD_REPORT, ""); + reportData += new String(report.getRenderedOutput()); + + DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream()); + outputStream.writeBytes(reportData); + outputStream.flush(); + outputStream.close(); + + int responseCode = connection.getResponseCode(); + if (responseCode == HttpURLConnection.HTTP_OK) { + + BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); + String responseData; + while ((responseData = reader.readLine()) != null) { + System.out.println(responseData); + } + reader.close(); + } else { + System.out.println("HTTP POST request failed with response code: " + responseCode); + } + + } + + } catch(IOException e) { + e.getStackTrace(); + } finally { + if(connection != null) { + connection.disconnect(); + } + } + } + +} From 10efadc70f5fc4486b592b835e198d0b326b9913 Mon Sep 17 00:00:00 2001 From: Seremba Patrick Date: Fri, 24 Mar 2023 09:29:11 +0300 Subject: [PATCH 2/2] throw RunTimeExemption when an error occurs while processing the processor --- .../reporting/report/processor/HttpReportProcessor.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) 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 1416b413e7..ee57ef4faa 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 @@ -20,6 +20,8 @@ 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; @@ -29,6 +31,8 @@ @Component public class HttpReportProcessor implements ReportProcessor { + protected Log log = LogFactory.getLog(this.getClass()); + public final String CONNECTION_URL = "url"; public final String SUBJECT = "subject"; public final String ADD_REPORT = "addReport"; @@ -36,6 +40,7 @@ public class HttpReportProcessor implements ReportProcessor { /** * @see ReportProcessor#getConfigurationPropertyNames() */ + @Override public List getConfigurationPropertyNames() { @@ -50,6 +55,7 @@ public List getConfigurationPropertyNames() { * Performs some action on the given report * @param report the Report to process */ + @Override public void process(Report report, Properties configuration) { // TODO Auto-generated method stub @@ -91,7 +97,7 @@ public void process(Report report, Properties configuration) { } } catch(IOException e) { - e.getStackTrace(); + throw new RuntimeException("Error occured while sending report via HTTP POST", e); } finally { if(connection != null) { connection.disconnect();