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 #196

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

gitcliff
Copy link

@gitcliff gitcliff commented Apr 7, 2020

Copy link
Member

@mseaton mseaton left a comment

Choose a reason for hiding this comment

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

Thanks for getting this started @gitcliff . Still some work to do, please see my comments below.

@@ -0,0 +1,64 @@
package org.openmrs.module.reporting.report.processor;
Copy link
Member

Choose a reason for hiding this comment

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

License Header

Copy link
Author

Choose a reason for hiding this comment

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

i have fixed it

import org.springframework.stereotype.Component;

@Component
public class HttpReportProcessor implements ReportProcessor {
Copy link
Member

Choose a reason for hiding this comment

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

Javadoc

Copy link
Author

Choose a reason for hiding this comment

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

i have added it

ret.add("connection url");
ret.add("subject");
ret.add("Add report");
return ret;
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Author

Choose a reason for hiding this comment

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

i have fixed it

Copy link
Author

Choose a reason for hiding this comment

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

@mseaton kindly review ..Thanks

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");
Copy link
Member

Choose a reason for hiding this comment

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

Why limit this to text/html? Wouldn't it be better to get the report.getOutputContentType(), and set this content type?

connection.connect();

if (report.getRenderedOutput() != null && "true".equalsIgnoreCase(configuration.getProperty("Add report"))) {
String addcontent = configuration.getProperty(("Add report") + configuration.getProperty("subject"));
Copy link
Member

Choose a reason for hiding this comment

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

I don't really understand what all of this is doing with the "Add report" and "subject" properties. Is this meant to be like saying "enabled=true/false"?

There is no javadoc, no examples, and no unit tests showing what is expected here. All of that needs to be added.

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");
}
Copy link
Member

Choose a reason for hiding this comment

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

Per my comment above, there is no reason to limit this to text/html, is there?

connection.getOutputStream());
writer.write(addcontent);
writer.flush();
writer.close();
Copy link
Member

Choose a reason for hiding this comment

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

Does this need to be in a finally block?

Copy link
Author

Choose a reason for hiding this comment

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

No because the finally blocks can't be inserted in try-catch blocks

Copy link
Member

Choose a reason for hiding this comment

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

With I/O, typically it's a good idea to close the connection in a finally block. You can have nested try/catch blocks if you need to. In this case, I would declare "HttpURLConnection" outside of the try block, assign it and use it inside the try block, and close it in the finally block:

HttpURLConnection connection;
OutputStreamWriter writer;
try {
  connection = (HttpURLConnection) url.openConnection();
  ...
  writer = new OutputStreamWriter(connection.getOutputStream());
  ...
}
catch (Exception e) {
  throw new RuntimeException("Error occurred while sending report via HTTP POST", e);
}
finally {
  IOUtils.closeQuietly(writer);
  IOUtils.closeQuietly(connection);
}

Copy link
Author

Choose a reason for hiding this comment

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

@mseaton the connection variable is as a result of casting the url variable to HttpURLConnection of which if am to declare "HttpURLConnection" outside of the try block then i will need to declare URL outside to, which requires also a try block to catch the MalformedURLException and if this is done,then the scope of the url variable used here url.openConnection() will be limited

Copy link
Member

@mseaton mseaton left a comment

Choose a reason for hiding this comment

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

See additional comments.

HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
String outPutContentType = report.getOutputContentType();
connection.setRequestProperty("Content-Type", "outPutContentType; charset=UTF-8");
Copy link
Member

Choose a reason for hiding this comment

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

Typo in outPutContentType?

Copy link
Author

Choose a reason for hiding this comment

The 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.getOutputStream());
writer.write(addcontent);
writer.flush();
writer.close();
Copy link
Member

Choose a reason for hiding this comment

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

With I/O, typically it's a good idea to close the connection in a finally block. You can have nested try/catch blocks if you need to. In this case, I would declare "HttpURLConnection" outside of the try block, assign it and use it inside the try block, and close it in the finally block:

HttpURLConnection connection;
OutputStreamWriter writer;
try {
  connection = (HttpURLConnection) url.openConnection();
  ...
  writer = new OutputStreamWriter(connection.getOutputStream());
  ...
}
catch (Exception e) {
  throw new RuntimeException("Error occurred while sending report via HTTP POST", e);
}
finally {
  IOUtils.closeQuietly(writer);
  IOUtils.closeQuietly(connection);
}

connection.setRequestProperty("Content-Type", "outPutContentType; charset=UTF-8");
connection.setDoOutput(true);
connection.connect();
//when the parameter ADD_REPORT is set to true then the rendered report is added to the url connection
Copy link
Member

Choose a reason for hiding this comment

The 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...

Copy link
Author

@gitcliff gitcliff Apr 10, 2020

Choose a reason for hiding this comment

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

Sir the intention to send the subject to the report.
Am going to add the ADD_REPORT to the very top because my intention is that when its not set or false then the processor is disabled.
i used connection.connect() because it opens a connection link to a resource referenced by the URL but i will go ahead remove it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants