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

Add utf-8 encoding on dhis2 endpoint: this resolve bad request error on win platform #10

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
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
Expand Up @@ -28,6 +28,7 @@
import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -212,7 +213,7 @@ private void saveToBackUp(String path, String jsonResponse) {
}

String directoryStructure = OpenmrsUtil.getApplicationDataDirectory() + DHISCONNECTOR_DHIS2BACKUP_FOLDER
+ path.substring(0, path.lastIndexOf(File.separator));
+ path.substring(0, path.lastIndexOf(File.separator) == -1? path.length() - 1 :path.lastIndexOf(File.separator));
Copy link

Choose a reason for hiding this comment

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

@ahammi maybe this?

import java.nio.file.Paths;

Paths.get(OpenmrsUtil.getApplicationDataDirectory(), DHISCONNECTOR_DHIS2BACKUP_FOLDER, path)
.toString()

Copy link
Contributor

Choose a reason for hiding this comment

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

looks like that looks better if tested to work

Copy link

Choose a reason for hiding this comment

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

@kaweesi yes this a vetted Java lib that is working fine, please try it out :-)
Example:

System.out.println( Paths.get("/path/to", "some", "/place", "somewhere/").toString() );


File directory = new File(directoryStructure);

Expand Down Expand Up @@ -248,25 +249,35 @@ public String getDataFromDHISEndpoint(String endpoint) {
String user = Context.getAdministrationService().getGlobalProperty("dhisconnector.user");
String pass = Context.getAdministrationService().getGlobalProperty("dhisconnector.pass");



DefaultHttpClient client = null;
String payload = "";

if (StringUtils.isNotBlank(endpoint)) {
try {
if( endpoint.contains("fields=") ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

i see some trailing spaces, rightly format the code to remove such spaces like after 'if(' and before ') {'

String fieldsJsonFormat = endpoint.split("fields=")[1];
if (StringUtils.isNotBlank(fieldsJsonFormat) ) {
fieldsJsonFormat = URLEncoder.encode(fieldsJsonFormat, "UTF-8");
endpoint = endpoint.replace(endpoint.split("fields=")[1], fieldsJsonFormat);
}
}

URL dhisURL = new URL(url);
String host = dhisURL.getHost();
int port = dhisURL.getPort();

HttpHost targetHost = new HttpHost(host, port, dhisURL.getProtocol());
client = new DefaultHttpClient();
BasicHttpContext localcontext = new BasicHttpContext();

HttpGet httpGet = new HttpGet(dhisURL.getPath() + endpoint);
HttpGet httpGet = new HttpGet(dhisURL.getPath() + endpoint );
Credentials creds = new UsernamePasswordCredentials(user, pass);
Header bs = new BasicScheme().authenticate(creds, httpGet, localcontext);
httpGet.addHeader("Authorization", bs.getValue());
httpGet.addHeader("Content-Type", "application/json");
httpGet.addHeader("Accept", "application/json");

HttpResponse response = client.execute(targetHost, httpGet, localcontext);
HttpEntity entity = response.getEntity();

Expand Down