-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathFormRequestClient.java
More file actions
84 lines (72 loc) · 3.22 KB
/
FormRequestClient.java
File metadata and controls
84 lines (72 loc) · 3.22 KB
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
package com.yoti.auth;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.stream.Collectors;
import com.yoti.api.client.spi.remote.call.ResourceException;
import com.yoti.api.client.spi.remote.util.QuietCloseable;
/**
* Internal use only.
* <p>
* The {@link FormRequestClient} is used for performing an application/x-www-form-urlencoded
* HTTP request using base Java libraries only.
*/
final class FormRequestClient {
byte[] performRequest(URL url, String method, Map<String, String> formParams) throws IOException, ResourceException {
byte[] postData = getData(formParams);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod(method);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", Integer.toString(postData.length));
connection.setRequestProperty("charset", StandardCharsets.UTF_8.toString());
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(false);
try (DataOutputStream wr = new DataOutputStream(connection.getOutputStream())) {
wr.write(postData);
}
return parseResponse(connection);
}
private byte[] getData(Map<String, String> params) {
return params.entrySet().stream()
.map(entry -> encode(entry.getKey()) + "=" + encode(entry.getValue()))
.collect(Collectors.joining("&"))
.getBytes(StandardCharsets.UTF_8);
}
private static String encode(String v) {
try {
return URLEncoder.encode(v, StandardCharsets.UTF_8.toString());
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
private byte[] parseResponse(HttpURLConnection httpUrlConnection) throws ResourceException, IOException {
int responseCode = httpUrlConnection.getResponseCode();
if (responseCode >= HttpURLConnection.HTTP_BAD_REQUEST) {
byte[] responseBody = readBody(httpUrlConnection.getErrorStream());
throw new ResourceException(responseCode, httpUrlConnection.getResponseMessage(), new String(responseBody));
}
return readBody(httpUrlConnection.getInputStream());
}
private byte[] readBody(InputStream httpInputStream) throws IOException {
try (QuietCloseable<InputStream> inputStream = new QuietCloseable<>(httpInputStream)) {
return readChunked(inputStream.get());
}
}
private byte[] readChunked(InputStream inputStream) throws IOException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] byteChunk = new byte[4096];
int n;
while ((n = inputStream.read(byteChunk)) > 0) {
byteArrayOutputStream.write(byteChunk, 0, n);
}
return byteArrayOutputStream.toByteArray();
}
}