2222
2323package eu .webeid .security .validator .ocsp ;
2424
25- import okhttp3 .MediaType ;
26- import okhttp3 .OkHttpClient ;
27- import okhttp3 .Request ;
28- import okhttp3 .RequestBody ;
29- import okhttp3 .Response ;
30- import okhttp3 .ResponseBody ;
3125import org .bouncycastle .cert .ocsp .OCSPReq ;
3226import org .bouncycastle .cert .ocsp .OCSPResp ;
3327import org .slf4j .Logger ;
3428import org .slf4j .LoggerFactory ;
3529
3630import java .io .IOException ;
3731import java .net .URI ;
32+ import java .net .http .HttpClient ;
33+ import java .net .http .HttpRequest ;
34+ import java .net .http .HttpResponse ;
3835import java .time .Duration ;
39- import java .util .Objects ;
4036
41- public class OkHttpOcspClient implements OcspClient {
37+ public class OcspClientImpl implements OcspClient {
4238
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" ;
4643
47- private final OkHttpClient httpClient ;
44+ private final HttpClient httpClient ;
45+ private final Duration ocspRequestTimeout ;
4846
4947 public static OcspClient build (Duration ocspRequestTimeout ) {
50- return new OkHttpOcspClient (
51- new OkHttpClient . Builder ()
48+ return new OcspClientImpl (
49+ HttpClient . newBuilder ()
5250 .connectTimeout (ocspRequestTimeout )
53- .callTimeout (ocspRequestTimeout )
54- .build ()
55- );
51+ .build (),
52+ ocspRequestTimeout );
5653 }
5754
5855 /**
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.
6057 *
6158 * @param uri OCSP server URL
6259 * @param ocspReq OCSP request
@@ -66,31 +63,36 @@ public static OcspClient build(Duration ocspRequestTimeout) {
6663 */
6764 @ Override
6865 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 )
7371 .build ();
7472
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 );
8979 }
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 ());
9091 }
9192
92- public OkHttpOcspClient ( OkHttpClient httpClient ) {
93+ public OcspClientImpl ( HttpClient httpClient , Duration ocspRequestTimeout ) {
9394 this .httpClient = httpClient ;
95+ this .ocspRequestTimeout = ocspRequestTimeout ;
9496 }
9597
9698}
0 commit comments