-
Notifications
You must be signed in to change notification settings - Fork 237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
REPORT-862:Create an HttpReportProcessor #196
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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")); | ||
|
||
|
||
} | ||
|
||
|
||
} |
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Javadoc There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i have added it |
||
|
||
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These should not contain spaces in the names. Please make these camel case. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since you refer to these names below, you should also make them into Constants and then refer to the constants in each place. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i have fixed it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mseaton kindly review ..Thanks |
||
} | ||
|
||
/** | ||
* 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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo in outPutContentType? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mseaton i don't understand clearly what you mean here..please elaborate |
||
connection.setDoOutput(true); | ||
connection.connect(); | ||
//when the parameter ADD_REPORT is set to true then the rendered report is added to the url connection | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I still don't quite understand this. What happens if ADD_REPORT is set to false or left empty? Why are you opening a connection above at all in this case if you are not actually writing anything to it? If the intention is to send the subject to that URL but not the report, then this code isn't achieving that. If the intention is to just disable this processor altogether, then put this check at the very top before you do anything with a connection at all... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sir the intention to send the subject to the report. |
||
//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); | ||
} | ||
|
||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
License Header
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i have fixed it