-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dhis.java
138 lines (110 loc) · 4.34 KB
/
Dhis.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package com.non.jom;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.commons.codec.binary.Base64;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.http.HttpMethod;
import com.mysql.jdbc.StringUtils;
public class Dhis {
/**
* @param args
* @throws JSONException
*/
public static void main(String[] args) throws JSONException {
JSONObject personObj = new JSONObject();
JSONArray nameArray = new JSONArray();
JSONObject nameAttrObj = new JSONObject();
nameAttrObj.put("dataElement", "HYDA5S2jFz1");
nameAttrObj.put("period", "201601");
nameAttrObj.put("orgUnit", "Na2vHKtaYKL");
nameAttrObj.put("value", "260");
JSONObject nameAttrObj1 = new JSONObject();
nameAttrObj1.put("dataElement", "GCwJmDGRN9a");
nameAttrObj1.put("period", "201601");
nameAttrObj1.put("orgUnit", "Na2vHKtaYKL");
nameAttrObj1.put("value", "400");
nameArray.put(nameAttrObj);
nameArray.put(nameAttrObj1);
personObj.put("dataValues", nameArray);
System.out.println(personObj.toString());
TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
}
};
// Install the all-trusting trust manager
SSLContext sc = null;
try {
sc = SSLContext.getInstance("SSL");
} catch (NoSuchAlgorithmException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
sc.init(null, trustAllCerts, new java.security.SecureRandom());
} catch (KeyManagementException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
// Create all-trusting host name verifier
HostnameVerifier allHostsValid = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
// Install the all-trusting host verifier
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
try{
String url = "https://192.168.22.152/dhis/api/24/dataValueSets";
String payload ="";
url = (url+(StringUtils.isEmptyOrWhitespaceOnly(payload)?"":("?"+payload))).replaceAll(" ", "%20");
URL urlo = new URL(url);
HttpURLConnection urlc = (HttpURLConnection) urlo.openConnection();
urlc.setRequestProperty("Content-Type", "application/json");
String charset = "UTF-8";
urlc.setRequestProperty("Accept-Charset", charset);
String encoded = new String(Base64.encodeBase64(("dghs:Dghs@123").getBytes()));
urlc.setRequestProperty("Authorization", "Basic "+encoded);
urlc.setRequestMethod(HttpMethod.POST.name());
urlc.setFixedLengthStreamingMode(
personObj.toString().getBytes().length);
urlc.setDoOutput(true);
PrintWriter writer = new PrintWriter(new OutputStreamWriter(urlc.getOutputStream(), charset ), true); // true = autoFlush, important!
writer.print(personObj.toString());
if (writer != null) writer.close();
int statusCode = urlc.getResponseCode();
if (statusCode != HttpURLConnection.HTTP_OK) {
}
BufferedReader br = new BufferedReader(new InputStreamReader((urlc.getInputStream())));
StringBuilder sb = new StringBuilder();
String output;
while ((output = br.readLine()) != null) {
sb.append(output);
}
System.out.println(sb.toString());
} catch (Exception e){
e.printStackTrace();
}
}
}