7171import no .digipost .api .datatypes .DataType ;
7272import no .digipost .api .datatypes .types .share .ShareDocumentsRequestSharingStopped ;
7373import 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 ;
9192import org .slf4j .Logger ;
9293import org .slf4j .LoggerFactory ;
9394
112113import static no .digipost .api .client .internal .http .UriUtils .withQueryParams ;
113114import static no .digipost .api .client .internal .http .response .HttpResponseUtils .checkResponse ;
114115import static no .digipost .api .client .internal .http .response .HttpResponseUtils .safelyOfferEntityStreamExternally ;
116+ import static no .digipost .api .client .representations .MediaTypes .DIGIPOST_MEDIA_TYPE_V8 ;
115117import static no .digipost .api .client .util .JAXBContextUtils .jaxbContext ;
116118import static no .digipost .api .client .util .JAXBContextUtils .marshal ;
117119import static no .digipost .api .client .util .JAXBContextUtils .unmarshal ;
@@ -139,12 +141,12 @@ public ApiServiceImpl(DigipostClientConfig config, HttpClientBuilder httpClientB
139141
140142 this .cached = new Cached (() -> fetchEntryPoint (Optional .empty ()));
141143 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 ))
148150 .build ();
149151 this .eventLogger .log ("Initialiserte apache-klient mot " + config .digipostApiUri );
150152 }
@@ -309,7 +311,7 @@ private EntryPoint fetchEntryPoint(Optional<SenderId> senderId) throws IOExcepti
309311 httpCoreContext .setAttribute (ResponseSignatureInterceptor .NOT_SIGNED_RESPONSE , true );
310312 try (CloseableHttpResponse response = send (httpGet , httpCoreContext )) {
311313
312- if (response .getStatusLine (). getStatusCode () == HttpStatus .SC_OK ) {
314+ if (response .getCode () == HttpStatus .SC_OK ) {
313315 return unmarshal (jaxbContext , response .getEntity ().getContent (), EntryPoint .class );
314316 } else {
315317 ErrorMessage errorMessage = unmarshal (jaxbContext , response .getEntity ().getContent (), ErrorMessage .class );
@@ -406,7 +408,7 @@ public ArchiveDocument saveArchiveDocument(ArchiveDocument archiveDocument, URI
406408 httpPut .setHeader (Content_Type_DIGIPOST_MEDIA_TYPE_V8 );
407409 ByteArrayOutputStream bao = new ByteArrayOutputStream ();
408410 marshal (jaxbContext , archiveDocument , bao );
409- httpPut .setEntity (new ByteArrayEntity (bao .toByteArray ()));
411+ httpPut .setEntity (new ByteArrayEntity (bao .toByteArray (), ContentType . create ( DIGIPOST_MEDIA_TYPE_V8 ) ));
410412
411413 return requestEntity (httpPut , ArchiveDocument .class );
412414 }
@@ -441,7 +443,7 @@ public UserAccount createOrActivateUserAccount(SenderId senderId, UserInformatio
441443 httpPost .setHeader (Content_Type_DIGIPOST_MEDIA_TYPE_V8 );
442444 ByteArrayOutputStream bao = new ByteArrayOutputStream ();
443445 marshal (jaxbContext , user , bao );
444- httpPost .setEntity (new ByteArrayEntity (bao .toByteArray ()));
446+ httpPost .setEntity (new ByteArrayEntity (bao .toByteArray (), ContentType . create ( DIGIPOST_MEDIA_TYPE_V8 ) ));
445447 return requestEntity (httpPost , UserAccount .class );
446448 }
447449
@@ -550,15 +552,15 @@ private <R> R getEntity(Class<R> entityType, String resourcePath, Map<String, ?>
550552 return requestEntity (httpGet , entityType );
551553 }
552554
553- private <R > InputStream requestStream (HttpRequestBase request ) {
555+ private <R > InputStream requestStream (ClassicHttpRequest request ) {
554556 return request (request , InputStream .class , new Header [0 ]);
555557 }
556558
557- private <R > R requestEntity (HttpRequestBase request , Class <R > entityType ) {
559+ private <R > R requestEntity (ClassicHttpRequest request , Class <R > entityType ) {
558560 return request (request , entityType , Accept_DIGIPOST_MEDIA_TYPE_V8 );
559561 }
560562
561- private <R > R request (HttpRequestBase request , Class <R > entityType , Header ... headers ) {
563+ private <R > R request (ClassicHttpRequest request , Class <R > entityType , Header ... headers ) {
562564 for (Header header : headers ) {
563565 request .setHeader (header );
564566 }
@@ -578,17 +580,17 @@ private <R> R request(HttpRequestBase request, Class<R> entityType, Header ... h
578580
579581 }
580582
581- private CloseableHttpResponse send (HttpRequestBase request ){
583+ private CloseableHttpResponse send (ClassicHttpRequest request ){
582584 return send (request , null );
583585 }
584586
585- private CloseableHttpResponse send (HttpRequestBase request , HttpContext context ){
587+ private CloseableHttpResponse send (ClassicHttpRequest request , HttpContext context ){
586588 try {
587589 request .setHeader (X_Digipost_UserId , brokerId .stringValue ());
588590 if (context == null ) {
589- return httpClient .execute (request );
591+ return httpClient .execute (request , responseHandler () );
590592 } else {
591- return httpClient .execute (request , context );
593+ return httpClient .execute (request , context , responseHandler () );
592594 }
593595 } catch (IOException e ) {
594596 throw asUnchecked (e );
@@ -601,7 +603,23 @@ private CloseableHttpResponse sendDigipostMedia(Object data, String uri) {
601603 httpPost .setHeader (Content_Type_DIGIPOST_MEDIA_TYPE_V8 );
602604 ByteArrayOutputStream bao = new ByteArrayOutputStream ();
603605 marshal (jaxbContext , data , bao );
604- httpPost .setEntity (new ByteArrayEntity (bao .toByteArray ()));
606+ httpPost .setEntity (new ByteArrayEntity (bao .toByteArray (), ContentType . create ( DIGIPOST_MEDIA_TYPE_V8 ) ));
605607 return send (httpPost );
606608 }
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+ }
607625}
0 commit comments