Skip to content
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 #241

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
@@ -0,0 +1,102 @@
/**
* 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.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.openmrs.module.reporting.report.Report;
import org.springframework.stereotype.Component;

/**
* A ReportProcessor which sends the rendered report via POST
*/
@Component
public class HttpReportProcessor implements ReportProcessor {

public final String CONNECTION_URL = "url";
public final String SUBJECT = "subject";
public 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) {
// TODO Auto-generated method stub
HttpURLConnection connection = null;
try {
if (report.getRenderedOutput() != null && "true".equalsIgnoreCase(configuration.getProperty(SUBJECT))) {

URL url = new URL(configuration.getProperty(CONNECTION_URL));

connection = (HttpURLConnection) url.openConnection();

connection.setRequestMethod("POST");

connection.setRequestProperty("Content-Type", "application/json");

connection.setDoOutput(true);

String reportData = configuration.getProperty(ADD_REPORT, "");
reportData += new String(report.getRenderedOutput());

DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
outputStream.writeBytes(reportData);
outputStream.flush();
outputStream.close();

int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {

BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String responseData;
while ((responseData = reader.readLine()) != null) {
System.out.println(responseData);
}
reader.close();
} else {
System.out.println("HTTP POST request failed with response code: " + responseCode);
}

}

} catch(IOException e) {
e.getStackTrace();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the use of the above line?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is meant to handle the exception and print the error. Just realized printStackTrace() is a better code. Let me rectify that!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you get a chance to look at our openmrs java conventions?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. Let me try again following them. I have checked the docs for them.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @dkayiwa, I have instead thrown a RuntimeException with a message. Let me know what you think. Thank you.

} finally {
if(connection != null) {
connection.disconnect();
}
}
}

}