forked from Hakky54/mutual-tls-ssl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OldJdkHttpClientService.java
75 lines (59 loc) 路 2.73 KB
/
OldJdkHttpClientService.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
package nl.altindag.client.service;
import static nl.altindag.client.ClientType.OLD_JDK_HTTP_CLIENT;
import static nl.altindag.client.Constants.HEADER_KEY_CLIENT_TYPE;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import javax.net.ssl.HttpsURLConnection;
import org.apache.commons.io.IOUtils;
import org.apache.http.client.methods.HttpGet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.google.common.annotations.VisibleForTesting;
import nl.altindag.client.ClientException;
import nl.altindag.client.ClientType;
import nl.altindag.client.model.ClientResponse;
import nl.altindag.ssl.SSLFactory;
@Service
public class OldJdkHttpClientService implements RequestService {
private static final String HTTP_REQUEST = "http:";
private static final String HTTPS_REQUEST = "https:";
private final SSLFactory sslFactory;
public OldJdkHttpClientService(@Autowired(required = false) SSLFactory sslFactory) {
this.sslFactory = sslFactory;
}
@Override
public ClientResponse executeRequest(String url) throws IOException {
HttpURLConnection connection;
if (url.contains(HTTP_REQUEST)) {
connection = createHttpURLConnection(url);
} else if (url.contains(HTTPS_REQUEST)) {
HttpsURLConnection httpsURLConnection = createHttpsURLConnection(url);
httpsURLConnection.setHostnameVerifier(sslFactory.getHostnameVerifier());
httpsURLConnection.setSSLSocketFactory(sslFactory.getSslSocketFactory());
connection = httpsURLConnection;
} else {
throw new ClientException("Could not create a http client for one of these reasons: "
+ "invalid url, "
+ "security is enable while using an url with http or "
+ "security is disable while using an url with https");
}
connection.setRequestMethod(HttpGet.METHOD_NAME);
connection.setRequestProperty(HEADER_KEY_CLIENT_TYPE, getClientType().getValue());
String responseBody = IOUtils.toString(connection.getInputStream(), StandardCharsets.UTF_8);
return new ClientResponse(responseBody, connection.getResponseCode());
}
@VisibleForTesting
HttpURLConnection createHttpURLConnection(String url) throws IOException {
return (HttpURLConnection) new URL(url).openConnection();
}
@VisibleForTesting
HttpsURLConnection createHttpsURLConnection(String url) throws IOException {
return (HttpsURLConnection) new URL(url).openConnection();
}
@Override
public ClientType getClientType() {
return OLD_JDK_HTTP_CLIENT;
}
}