/
HttpClient.java
108 lines (92 loc) · 2.94 KB
/
HttpClient.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
/**
* 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.util;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URLEncoder;
import java.util.Map;
/**
* This class supports doing an HTTP post to a URL. (It replaces the OpenmrsUtil.postToUrl method, allowing us to
* mock http calls in unit tests.)
*/
public class HttpClient {
private static Log log = LogFactory.getLog(HttpClient.class);
private HttpUrl url;
public HttpClient(String url) throws MalformedURLException {
this(new HttpUrl(url));
}
public HttpClient(HttpUrl url) {
this.url = url;
}
public String post(Map<String, String> parameters) {
OutputStreamWriter wr = null;
BufferedReader rd = null;
String response = "";
StringBuilder data = new StringBuilder();
try {
// Construct data
for (Map.Entry<String, String> entry : parameters.entrySet()) {
// skip over invalid post variables
if (entry.getKey() == null || entry.getValue() == null) {
continue;
}
data.append("&"); // only append this if its _not_ the first
// datum
// finally, setup the actual post string
data.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
data.append("=");
data.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
}
// Send the data
HttpURLConnection connection = url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Length", String.valueOf(data.length()));
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
wr = new OutputStreamWriter(connection.getOutputStream());
wr.write(data.toString());
wr.flush();
wr.close();
// Get the response
rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
response = String.format("%s%s%n", response, line);
}
}
catch (Exception e) {
log.warn("Exception while posting to : " + this.url, e);
log.warn("Reponse from server was: " + response);
}
finally {
if (wr != null) {
try {
wr.close();
}
catch (Exception e) { /* pass */
}
}
if (rd != null) {
try {
rd.close();
}
catch (Exception e) { /* pass */
}
}
}
return response;
}
}