-
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
1 changed file
with
64 additions
and
0 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
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,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); | ||
} | ||
|
||
} | ||
|
||
} |