-
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 #241
Open
Seremba
wants to merge
2
commits into
openmrs:master
Choose a base branch
from
Seremba:REPORT-862
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
102 changes: 102 additions & 0 deletions
102
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,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(); | ||
} finally { | ||
if(connection != null) { | ||
connection.disconnect(); | ||
} | ||
} | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
What is the use of the above line?
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.
It is meant to handle the exception and print the error. Just realized
printStackTrace()
is a better code. Let me rectify that!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.
Did you get a chance to look at our openmrs java conventions?
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. Let me try again following them. I have checked the docs for them.
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.
Hi @dkayiwa, I have instead thrown a
RuntimeException
with a message. Let me know what you think. Thank you.