From ff5e337d78cf8d27f4fc0aee4e310a4b46006b3c 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 --- .../processor/HttpReportProcessorTest.java | 101 ++++++++++++++++++ .../report/processor/HttpReportProcessor.java | 89 +++++++++++++++ 2 files changed, 190 insertions(+) create mode 100644 api-tests/src/test/java/org/openmrs/module/reporting/report/processor/HttpReportProcessorTest.java create mode 100644 api/src/main/java/org/openmrs/module/reporting/report/processor/HttpReportProcessor.java diff --git a/api-tests/src/test/java/org/openmrs/module/reporting/report/processor/HttpReportProcessorTest.java b/api-tests/src/test/java/org/openmrs/module/reporting/report/processor/HttpReportProcessorTest.java new file mode 100644 index 0000000000..e84aca6cd1 --- /dev/null +++ b/api-tests/src/test/java/org/openmrs/module/reporting/report/processor/HttpReportProcessorTest.java @@ -0,0 +1,101 @@ + +/** + * 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 static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.net.HttpURLConnection; +import java.net.URL; + +import org.junit.Test; +import org.openmrs.api.context.Context; +import org.openmrs.module.reporting.cohort.definition.GenderCohortDefinition; +import org.openmrs.module.reporting.dataset.definition.CohortCrossTabDataSetDefinition; +import org.openmrs.module.reporting.dataset.definition.SimplePatientDataSetDefinition; +import org.openmrs.module.reporting.evaluation.parameter.Mapped; +import org.openmrs.module.reporting.report.Report; +import org.openmrs.module.reporting.report.ReportRequest; +import org.openmrs.module.reporting.report.ReportRequest.Priority; +import org.openmrs.module.reporting.report.definition.ReportDefinition; +import org.openmrs.module.reporting.report.renderer.CsvReportRenderer; +import org.openmrs.module.reporting.report.renderer.RenderingMode; +import org.openmrs.module.reporting.report.service.ReportService; +import org.openmrs.test.BaseModuleContextSensitiveTest; + + +public class HttpReportProcessorTest extends BaseModuleContextSensitiveTest { + + + @Test + public void testHttpUrlConnection() throws Exception { + + URL url = new URL("http://www.example.com/docs/resource1.html"); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + assertTrue(url.openConnection() instanceof HttpURLConnection); + } + + @Test + public void testHttPostConnection() throws Exception { + + URL url = new URL("http://www.example.com/docs/resource1.html"); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("POST"); + assertEquals(connection.getRequestMethod(),"POST"); + } + + @Test + public void testHttpReportContent() throws Exception{ + + // Generate a report definition to add in the data set definitions for + // generating a report + ReportDefinition reportDefinition = new ReportDefinition(); + reportDefinition.setName("Test processor Report"); + + SimplePatientDataSetDefinition allPatients = new SimplePatientDataSetDefinition("allPatients", ""); + allPatients.addPatientProperty("patientId"); + allPatients.addPatientProperty("gender"); + allPatients.addPatientProperty("birthdate"); + reportDefinition.addDataSetDefinition("allPatients", allPatients, null); + + GenderCohortDefinition males = new GenderCohortDefinition(); + males.setName("Males"); + males.setMaleIncluded(true); + + GenderCohortDefinition females = new GenderCohortDefinition(); + females.setName("Females"); + females.setFemaleIncluded(true); + + CohortCrossTabDataSetDefinition genderDsd = new CohortCrossTabDataSetDefinition(); + genderDsd.addColumn("males", males, null); + genderDsd.addColumn("females", females, null); + reportDefinition.addDataSetDefinition("genders", genderDsd, null); + + RenderingMode mode = new RenderingMode(new CsvReportRenderer(), "CSV", null, 50); + + ReportRequest request = new ReportRequest(new Mapped(reportDefinition, null), null, mode, + Priority.HIGHEST, null); + Report report = Context.getService(ReportService.class).runReport(request); + String addContent = new String(report.getRenderedOutput()); + + //content of a rendered report can be held via an httpconnection to a url + URL url = new URL("http://www.example.com/docs/resource1.html"); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestProperty("Content-Type","addContent"); + assertNotNull(connection.getRequestProperty("Content-Type")); + + + } + + +} 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..b8e08fc8e6 --- /dev/null +++ b/api/src/main/java/org/openmrs/module/reporting/report/processor/HttpReportProcessor.java @@ -0,0 +1,89 @@ +/** + * 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.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.io.IOUtils; +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()); + + public static final String CONNECTION_URL = "connectionUrl"; + public static final String SUBJECT = "subject"; + public static 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) { + OutputStreamWriter writer = null; + + try { + if (report.getRenderedOutput() != null && "true".equalsIgnoreCase(configuration.getProperty(ADD_REPORT))) { + URL url = new URL(configuration.getProperty(CONNECTION_URL)); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("POST"); + String outPutContentType = report.getOutputContentType(); + connection.setRequestProperty("Content-Type", "outPutContentType; charset=UTF-8"); + connection.setDoOutput(true); + connection.connect(); + //when the parameter ADD_REPORT is set to true then the rendered report is added to the url connection + //to be written with the respective SUBJECT + String addContent = configuration.getProperty(SUBJECT,""); + addContent = new String(report.getRenderedOutput(),"UTF-8"); + writer = new OutputStreamWriter( + connection.getOutputStream()); + writer.write(addContent); + writer.flush(); + } + + } catch (Exception e) { + throw new RuntimeException("Error occurred while sending report via http POST", e); + } + finally { + IOUtils.closeQuietly(writer); + } + + } + +}