71
71
import no .digipost .api .datatypes .DataType ;
72
72
import no .digipost .api .datatypes .types .share .ShareDocumentsRequestSharingStopped ;
73
73
import org .apache .commons .io .output .ByteArrayOutputStream ;
74
- import org .apache .http .Header ;
75
- import org .apache .http .HttpEntity ;
76
- import org .apache .http .HttpHeaders ;
77
- import org .apache .http .HttpStatus ;
78
- import org .apache .http .client .methods .CloseableHttpResponse ;
79
- import org .apache .http .client .methods .HttpDelete ;
80
- import org .apache .http .client .methods .HttpGet ;
81
- import org .apache .http .client .methods .HttpPost ;
82
- import org .apache .http .client .methods .HttpPut ;
83
- import org .apache .http .client .methods .HttpRequestBase ;
84
- import org .apache .http .client .utils .URIBuilder ;
85
- import org .apache .http .entity .ByteArrayEntity ;
86
- import org .apache .http .entity .ContentType ;
87
- import org .apache .http .impl .client .CloseableHttpClient ;
88
- import org .apache .http .impl .client .HttpClientBuilder ;
89
- import org .apache .http .protocol .HttpContext ;
90
- import org .apache .http .protocol .HttpCoreContext ;
74
+ import org .apache .hc .client5 .http .classic .methods .HttpDelete ;
75
+ import org .apache .hc .client5 .http .classic .methods .HttpGet ;
76
+ import org .apache .hc .client5 .http .classic .methods .HttpPost ;
77
+ import org .apache .hc .client5 .http .classic .methods .HttpPut ;
78
+ import org .apache .hc .client5 .http .impl .classic .CloseableHttpClient ;
79
+ import org .apache .hc .client5 .http .impl .classic .CloseableHttpResponse ;
80
+ import org .apache .hc .client5 .http .impl .classic .HttpClientBuilder ;
81
+ import org .apache .hc .core5 .http .ClassicHttpRequest ;
82
+ import org .apache .hc .core5 .http .ContentType ;
83
+ import org .apache .hc .core5 .http .Header ;
84
+ import org .apache .hc .core5 .http .HttpEntity ;
85
+ import org .apache .hc .core5 .http .HttpHeaders ;
86
+ import org .apache .hc .core5 .http .HttpStatus ;
87
+ import org .apache .hc .core5 .http .io .HttpClientResponseHandler ;
88
+ import org .apache .hc .core5 .http .io .entity .ByteArrayEntity ;
89
+ import org .apache .hc .core5 .http .protocol .HttpContext ;
90
+ import org .apache .hc .core5 .http .protocol .HttpCoreContext ;
91
+ import org .apache .hc .core5 .net .URIBuilder ;
91
92
import org .slf4j .Logger ;
92
93
import org .slf4j .LoggerFactory ;
93
94
112
113
import static no .digipost .api .client .internal .http .UriUtils .withQueryParams ;
113
114
import static no .digipost .api .client .internal .http .response .HttpResponseUtils .checkResponse ;
114
115
import static no .digipost .api .client .internal .http .response .HttpResponseUtils .safelyOfferEntityStreamExternally ;
116
+ import static no .digipost .api .client .representations .MediaTypes .DIGIPOST_MEDIA_TYPE_V8 ;
115
117
import static no .digipost .api .client .util .JAXBContextUtils .jaxbContext ;
116
118
import static no .digipost .api .client .util .JAXBContextUtils .marshal ;
117
119
import static no .digipost .api .client .util .JAXBContextUtils .unmarshal ;
@@ -139,12 +141,12 @@ public ApiServiceImpl(DigipostClientConfig config, HttpClientBuilder httpClientB
139
141
140
142
this .cached = new Cached (() -> fetchEntryPoint (Optional .empty ()));
141
143
this .httpClient = httpClientBuilder
142
- .addInterceptorLast (new RequestDateInterceptor (config .eventLogger , config .clock ))
143
- .addInterceptorLast (new RequestUserAgentInterceptor ())
144
- .addInterceptorLast (new RequestSignatureInterceptor (signer , config .eventLogger , new RequestContentHashFilter (config .eventLogger , Digester .sha256 , Headers .X_Content_SHA256 )))
145
- .addInterceptorLast (new ResponseDateInterceptor (config .clock ))
146
- .addInterceptorLast (new ResponseContentSHA256Interceptor ())
147
- .addInterceptorLast (new ResponseSignatureInterceptor (this ::getEntryPoint ))
144
+ .addRequestInterceptorLast (new RequestDateInterceptor (config .eventLogger , config .clock ))
145
+ .addRequestInterceptorLast (new RequestUserAgentInterceptor ())
146
+ .addRequestInterceptorLast (new RequestSignatureInterceptor (signer , config .eventLogger , new RequestContentHashFilter (config .eventLogger , Digester .sha256 , Headers .X_Content_SHA256 )))
147
+ .addResponseInterceptorLast (new ResponseDateInterceptor (config .clock ))
148
+ .addResponseInterceptorLast (new ResponseContentSHA256Interceptor ())
149
+ .addResponseInterceptorLast (new ResponseSignatureInterceptor (this ::getEntryPoint ))
148
150
.build ();
149
151
this .eventLogger .log ("Initialiserte apache-klient mot " + config .digipostApiUri );
150
152
}
@@ -309,7 +311,7 @@ private EntryPoint fetchEntryPoint(Optional<SenderId> senderId) throws IOExcepti
309
311
httpCoreContext .setAttribute (ResponseSignatureInterceptor .NOT_SIGNED_RESPONSE , true );
310
312
try (CloseableHttpResponse response = send (httpGet , httpCoreContext )) {
311
313
312
- if (response .getStatusLine (). getStatusCode () == HttpStatus .SC_OK ) {
314
+ if (response .getCode () == HttpStatus .SC_OK ) {
313
315
return unmarshal (jaxbContext , response .getEntity ().getContent (), EntryPoint .class );
314
316
} else {
315
317
ErrorMessage errorMessage = unmarshal (jaxbContext , response .getEntity ().getContent (), ErrorMessage .class );
@@ -406,7 +408,7 @@ public ArchiveDocument saveArchiveDocument(ArchiveDocument archiveDocument, URI
406
408
httpPut .setHeader (Content_Type_DIGIPOST_MEDIA_TYPE_V8 );
407
409
ByteArrayOutputStream bao = new ByteArrayOutputStream ();
408
410
marshal (jaxbContext , archiveDocument , bao );
409
- httpPut .setEntity (new ByteArrayEntity (bao .toByteArray ()));
411
+ httpPut .setEntity (new ByteArrayEntity (bao .toByteArray (), ContentType . create ( DIGIPOST_MEDIA_TYPE_V8 ) ));
410
412
411
413
return requestEntity (httpPut , ArchiveDocument .class );
412
414
}
@@ -441,7 +443,7 @@ public UserAccount createOrActivateUserAccount(SenderId senderId, UserInformatio
441
443
httpPost .setHeader (Content_Type_DIGIPOST_MEDIA_TYPE_V8 );
442
444
ByteArrayOutputStream bao = new ByteArrayOutputStream ();
443
445
marshal (jaxbContext , user , bao );
444
- httpPost .setEntity (new ByteArrayEntity (bao .toByteArray ()));
446
+ httpPost .setEntity (new ByteArrayEntity (bao .toByteArray (), ContentType . create ( DIGIPOST_MEDIA_TYPE_V8 ) ));
445
447
return requestEntity (httpPost , UserAccount .class );
446
448
}
447
449
@@ -550,15 +552,15 @@ private <R> R getEntity(Class<R> entityType, String resourcePath, Map<String, ?>
550
552
return requestEntity (httpGet , entityType );
551
553
}
552
554
553
- private <R > InputStream requestStream (HttpRequestBase request ) {
555
+ private <R > InputStream requestStream (ClassicHttpRequest request ) {
554
556
return request (request , InputStream .class , new Header [0 ]);
555
557
}
556
558
557
- private <R > R requestEntity (HttpRequestBase request , Class <R > entityType ) {
559
+ private <R > R requestEntity (ClassicHttpRequest request , Class <R > entityType ) {
558
560
return request (request , entityType , Accept_DIGIPOST_MEDIA_TYPE_V8 );
559
561
}
560
562
561
- private <R > R request (HttpRequestBase request , Class <R > entityType , Header ... headers ) {
563
+ private <R > R request (ClassicHttpRequest request , Class <R > entityType , Header ... headers ) {
562
564
for (Header header : headers ) {
563
565
request .setHeader (header );
564
566
}
@@ -578,17 +580,17 @@ private <R> R request(HttpRequestBase request, Class<R> entityType, Header ... h
578
580
579
581
}
580
582
581
- private CloseableHttpResponse send (HttpRequestBase request ){
583
+ private CloseableHttpResponse send (ClassicHttpRequest request ){
582
584
return send (request , null );
583
585
}
584
586
585
- private CloseableHttpResponse send (HttpRequestBase request , HttpContext context ){
587
+ private CloseableHttpResponse send (ClassicHttpRequest request , HttpContext context ){
586
588
try {
587
589
request .setHeader (X_Digipost_UserId , brokerId .stringValue ());
588
590
if (context == null ) {
589
- return httpClient .execute (request );
591
+ return httpClient .execute (request , responseHandler () );
590
592
} else {
591
- return httpClient .execute (request , context );
593
+ return httpClient .execute (request , context , responseHandler () );
592
594
}
593
595
} catch (IOException e ) {
594
596
throw asUnchecked (e );
@@ -601,7 +603,23 @@ private CloseableHttpResponse sendDigipostMedia(Object data, String uri) {
601
603
httpPost .setHeader (Content_Type_DIGIPOST_MEDIA_TYPE_V8 );
602
604
ByteArrayOutputStream bao = new ByteArrayOutputStream ();
603
605
marshal (jaxbContext , data , bao );
604
- httpPost .setEntity (new ByteArrayEntity (bao .toByteArray ()));
606
+ httpPost .setEntity (new ByteArrayEntity (bao .toByteArray (), ContentType . create ( DIGIPOST_MEDIA_TYPE_V8 ) ));
605
607
return send (httpPost );
606
608
}
609
+
610
+ private HttpClientResponseHandler <CloseableHttpResponse > responseHandler () {
611
+ return response -> {
612
+ if (response .getCode () / 100 == 2 ) {
613
+ if (response instanceof CloseableHttpResponse ) {
614
+ return (CloseableHttpResponse ) response ;
615
+ } else {
616
+ throw new DigipostClientException (ErrorCode .GENERAL_ERROR ,
617
+ "Expected response to be instance of CloseableHttpResponse, but got " + response .getClass ().getName ());
618
+ }
619
+ } else {
620
+ ErrorMessage errorMessage = unmarshal (jaxbContext , response .getEntity ().getContent (), ErrorMessage .class );
621
+ throw new DigipostClientException (errorMessage );
622
+ }
623
+ };
624
+ }
607
625
}
0 commit comments