Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.openmrs.module.reporting.report.definition.service.ReportDefinitionService;
import org.openmrs.module.reporting.report.processor.DiskReportProcessor;
import org.openmrs.module.reporting.report.processor.EmailReportProcessor;
import org.openmrs.module.reporting.report.processor.HttpReportProcessor;
import org.openmrs.module.reporting.report.processor.LoggingReportProcessor;
import org.openmrs.module.reporting.report.renderer.CsvReportRenderer;
import org.openmrs.module.reporting.report.renderer.ReportDesignRenderer;
Expand Down Expand Up @@ -287,6 +288,8 @@ else if ("email".equalsIgnoreCase(type)) {
}
else if ("logging".equalsIgnoreCase(type)) {
type = LoggingReportProcessor.class.getName();
} else if ("http".equalsIgnoreCase(type)) {
type = HttpReportProcessor.class.getName();
}
c.setProcessorType(type);
c.setRunOnSuccess(processorDescriptor.getRunOnSuccess());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package org.openmrs.module.reporting.report.processor;

import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;
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());

/**
* @see ReportProcessor#getConfigurationPropertyNames()
*/
public List<String> getConfigurationPropertyNames() {
List<String> ret = new ArrayList<String>();
ret.add("url");
ret.add("contentType");
ret.add("dateFrom");
ret.add("dateTo");
return ret;
}

/**
* Performs some action on the given report
* @param report the Report to process
*/
public void process(Report report, Properties configuration) {

try {
String urlString = configuration.getProperty("url");
if (StringUtils.isBlank(urlString)) {
throw new IllegalArgumentException("URL cannot be blank");
}

String contentType = configuration.getProperty("contentType", "text/plain");
String dateFrom = configuration.getProperty("dateFrom");
String dateTo = configuration.getProperty("dateTo");

URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", contentType);

byte[] reportData = report.getRenderedOutput();

conn.setRequestProperty("Date-From", dateFrom);
conn.setRequestProperty("Date-To", dateTo);

OutputStream os = conn.getOutputStream();
os.write(reportData);


int responseCode = conn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
log.info("Report sent successfully to " + urlString);
} else {
log.error("Failed to send report to " + urlString + ", HTTP error code: " + responseCode);
}

conn.disconnect();
}
catch (Exception e) {
throw new RuntimeException("Error occurred while sending report via HTTP", e);
}
}
}