Skip to content

Commit

Permalink
REPORT-862:Create an HttpReportProcessor
Browse files Browse the repository at this point in the history
  • Loading branch information
gitcliff committed Apr 7, 2020
1 parent ffeb6c1 commit ecd3ed6
Showing 1 changed file with 64 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
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.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openmrs.module.reporting.report.Report;
import org.springframework.stereotype.Component;

@Component
public class HttpReportProcessor implements ReportProcessor {

protected Log log = LogFactory.getLog(this.getClass());

/**
* @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) {
try {
URL url = new URL(configuration.getProperty("connection url"));
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "text/html; charset=UTF-8");
connection.setDoOutput(true);
connection.connect();

if (report.getRenderedOutput() != null && "true".equalsIgnoreCase(configuration.getProperty("Add report"))) {
String addcontent = configuration.getProperty(("Add report") + configuration.getProperty("subject"));
if (report.getOutputContentType().contains("text") || report.getOutputContentType().contains("html")) {
addcontent = new String(report.getRenderedOutput(),"UTF-8");
}
OutputStreamWriter writer = new OutputStreamWriter(
connection.getOutputStream());
writer.write(addcontent);
writer.flush();
writer.close();
}
} catch (Exception e) {
throw new RuntimeException("Error occurred while sending report via http POST", e);
}

}

}

0 comments on commit ecd3ed6

Please sign in to comment.