-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
REPORT-862:Create an HttpReportProcessor
- Loading branch information
Showing
2 changed files
with
190 additions
and
0 deletions.
There are no files selected for viewing
101 changes: 101 additions & 0 deletions
101
.../src/test/java/org/openmrs/module/reporting/report/processor/HttpReportProcessorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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>(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")); | ||
|
||
|
||
} | ||
|
||
|
||
} |
89 changes: 89 additions & 0 deletions
89
api/src/main/java/org/openmrs/module/reporting/report/processor/HttpReportProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String> getConfigurationPropertyNames() { | ||
List<String> ret = new ArrayList<String>(); | ||
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); | ||
} | ||
|
||
} | ||
|
||
} |