22
22
23
23
package eu .webeid .security .validator .ocsp ;
24
24
25
- import okhttp3 .MediaType ;
26
- import okhttp3 .OkHttpClient ;
27
- import okhttp3 .Request ;
28
- import okhttp3 .RequestBody ;
29
- import okhttp3 .Response ;
30
- import okhttp3 .ResponseBody ;
31
25
import org .bouncycastle .cert .ocsp .OCSPReq ;
32
26
import org .bouncycastle .cert .ocsp .OCSPResp ;
33
27
import org .slf4j .Logger ;
34
28
import org .slf4j .LoggerFactory ;
35
29
36
30
import java .io .IOException ;
37
31
import java .net .URI ;
32
+ import java .net .http .HttpClient ;
33
+ import java .net .http .HttpRequest ;
34
+ import java .net .http .HttpResponse ;
38
35
import java .time .Duration ;
39
- import java .util .Objects ;
40
36
41
- public class OkHttpOcspClient implements OcspClient {
37
+ public class OcspClientImpl implements OcspClient {
42
38
43
- private static final Logger LOG = LoggerFactory .getLogger (OkHttpOcspClient .class );
44
- private static final MediaType OCSP_REQUEST_TYPE = MediaType .get ("application/ocsp-request" );
45
- private static final MediaType OCSP_RESPONSE_TYPE = MediaType .get ("application/ocsp-response" );
39
+ private static final Logger LOG = LoggerFactory .getLogger (OcspClientImpl .class );
40
+ private static final String OCSP_REQUEST_TYPE = "application/ocsp-request" ;
41
+ private static final String OCSP_RESPONSE_TYPE = "application/ocsp-response" ;
42
+ public static final String CONTENT_TYPE = "Content-Type" ;
46
43
47
- private final OkHttpClient httpClient ;
44
+ private final HttpClient httpClient ;
45
+ private final Duration ocspRequestTimeout ;
48
46
49
47
public static OcspClient build (Duration ocspRequestTimeout ) {
50
- return new OkHttpOcspClient (
51
- new OkHttpClient . Builder ()
48
+ return new OcspClientImpl (
49
+ HttpClient . newBuilder ()
52
50
.connectTimeout (ocspRequestTimeout )
53
- .callTimeout (ocspRequestTimeout )
54
- .build ()
55
- );
51
+ .build (),
52
+ ocspRequestTimeout );
56
53
}
57
54
58
55
/**
59
- * Use OkHttpClient to fetch the OCSP response from the OCSP responder service.
56
+ * Use the built-in HttpClient to fetch the OCSP response from the OCSP responder service.
60
57
*
61
58
* @param uri OCSP server URL
62
59
* @param ocspReq OCSP request
@@ -66,31 +63,36 @@ public static OcspClient build(Duration ocspRequestTimeout) {
66
63
*/
67
64
@ Override
68
65
public OCSPResp request (URI uri , OCSPReq ocspReq ) throws IOException {
69
- final RequestBody requestBody = RequestBody .create (ocspReq .getEncoded (), OCSP_REQUEST_TYPE );
70
- final Request request = new Request .Builder ()
71
- .url (uri .toURL ())
72
- .post (requestBody )
66
+ final HttpRequest request = HttpRequest .newBuilder ()
67
+ .uri (uri )
68
+ .header (CONTENT_TYPE , OCSP_REQUEST_TYPE )
69
+ .POST (HttpRequest .BodyPublishers .ofByteArray (ocspReq .getEncoded ()))
70
+ .timeout (ocspRequestTimeout )
73
71
.build ();
74
72
75
- try (final Response response = httpClient .newCall (request ).execute ()) {
76
- if (!response .isSuccessful ()) {
77
- throw new IOException ("OCSP request was not successful, response: " + response );
78
- } else {
79
- LOG .debug ("OCSP response: {}" , response );
80
- }
81
- try (final ResponseBody responseBody = Objects .requireNonNull (response .body (), "response body" )) {
82
- Objects .requireNonNull (responseBody .contentType (), "response content type" );
83
- if (!OCSP_RESPONSE_TYPE .type ().equals (responseBody .contentType ().type ()) ||
84
- !OCSP_RESPONSE_TYPE .subtype ().equals (responseBody .contentType ().subtype ())) {
85
- throw new IOException ("OCSP response content type is not " + OCSP_RESPONSE_TYPE );
86
- }
87
- return new OCSPResp (responseBody .bytes ());
88
- }
73
+ final HttpResponse <byte []> response ;
74
+ try {
75
+ response = httpClient .send (request , HttpResponse .BodyHandlers .ofByteArray ());
76
+ } catch (InterruptedException e ) {
77
+ Thread .currentThread ().interrupt ();
78
+ throw new IOException ("Interrupted while sending OCSP request" , e );
89
79
}
80
+
81
+ if (response .statusCode () != 200 ) {
82
+ throw new IOException ("OCSP request was not successful, response: " + response );
83
+ } else {
84
+ LOG .debug ("OCSP response: {}" , response );
85
+ }
86
+ final String contentType = response .headers ().firstValue (CONTENT_TYPE ).orElse ("" );
87
+ if (!contentType .startsWith (OCSP_RESPONSE_TYPE )) {
88
+ throw new IOException ("OCSP response content type is not " + OCSP_RESPONSE_TYPE );
89
+ }
90
+ return new OCSPResp (response .body ());
90
91
}
91
92
92
- public OkHttpOcspClient ( OkHttpClient httpClient ) {
93
+ public OcspClientImpl ( HttpClient httpClient , Duration ocspRequestTimeout ) {
93
94
this .httpClient = httpClient ;
95
+ this .ocspRequestTimeout = ocspRequestTimeout ;
94
96
}
95
97
96
98
}
0 commit comments