-
Notifications
You must be signed in to change notification settings - Fork 237
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
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
License Header
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Javadoc
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i have fixed it
There was a problem hiding this comment.
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"); |
There was a problem hiding this comment.
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")); |
There was a problem hiding this comment.
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"); | ||
} |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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);
}
There was a problem hiding this comment.
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
There was a problem hiding this 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"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo in outPutContentType?
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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
link:https://issues.openmrs.org/browse/REPORT-862?src=confmacro