Skip to content

Commit a8a17e9

Browse files
authored
Merge pull request #201 from digipost/httpclientbuilder5
Oppgrader httpclientbuilder fra v3 til v5
2 parents 295f37e + 972a7ff commit a8a17e9

34 files changed

+336
-392
lines changed

NOTICE

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@ Licensed under Apache 2 - http://www.apache.org/licenses/LICENSE-2.0.html
99
This software includes third party software subject to the following licenses:
1010

1111
Angus Activation Registries under EDL 1.0
12-
Apache Commons Codec under Apache-2.0
1312
Apache Commons IO under Apache-2.0
1413
Apache Commons Lang under Apache-2.0
1514
Apache FontBox under Apache-2.0
1615
Apache HttpClient under Apache License, Version 2.0
17-
Apache HttpClient Mime under Apache License, Version 2.0
18-
Apache HttpCore under Apache License, Version 2.0
16+
Apache HttpComponents Core HTTP/1.1 under Apache License, Version 2.0
1917
Apache PDFBox under Apache-2.0
2018
Apache PDFBox io under Apache-2.0
2119
Bouncy Castle ASN.1 Extension and Utility APIs under Bouncy Castle Licence

pom.xml

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -68,30 +68,29 @@
6868
<scope>test</scope>
6969
</dependency>
7070
<dependency>
71-
<groupId>org.apache.httpcomponents</groupId>
72-
<artifactId>httpclient</artifactId>
73-
<version>4.5.14</version>
71+
<groupId>org.apache.httpcomponents.client5</groupId>
72+
<artifactId>httpclient5</artifactId>
73+
<version>5.4.4</version>
7474
<exclusions>
7575
<exclusion>
7676
<groupId>commons-logging</groupId>
7777
<artifactId>commons-logging</artifactId>
7878
</exclusion>
79+
<exclusion>
80+
<groupId>org.apache.httpcomponents.core5</groupId>
81+
<artifactId>httpcore5-h2</artifactId>
82+
</exclusion>
7983
</exclusions>
8084
</dependency>
8185
<dependency>
82-
<groupId>org.apache.httpcomponents</groupId>
83-
<artifactId>httpcore</artifactId>
84-
<version>4.4.16</version>
85-
</dependency>
86-
<dependency>
87-
<groupId>org.apache.httpcomponents</groupId>
88-
<artifactId>httpmime</artifactId>
89-
<version>4.5.14</version>
86+
<groupId>org.apache.httpcomponents.core5</groupId>
87+
<artifactId>httpcore5</artifactId>
88+
<version>5.3.4</version>
9089
</dependency>
9190
<dependency>
9291
<groupId>no.digipost</groupId>
93-
<artifactId>http-client-builder3</artifactId>
94-
<version>3.0.1</version>
92+
<artifactId>http-client-builder5</artifactId>
93+
<version>5.0.1</version>
9594
</dependency>
9695
<dependency>
9796
<groupId>commons-io</groupId>
@@ -385,14 +384,13 @@
385384
<include>com.google.j2objc:j2objc-annotations</include>
386385
<include>com.google.errorprone:error_prone_annotations</include>
387386
<include>com.github.ben-manes.caffeine:caffeine</include>
388-
<include>no.digipost:http-client-builder3</include>
387+
<include>no.digipost:http-client-builder5</include>
389388
<include>commons-io:commons-io</include>
390389
<include>com.google.guava:failureaccess</include>
391390
<include>commons-codec:commons-codec</include>
392391
<include>org.apache.commons:commons-lang3</include>
393-
<include>org.apache.httpcomponents:httpclient</include>
394-
<include>org.apache.httpcomponents:httpcore</include>
395-
<include>org.apache.httpcomponents:httpmime</include>
392+
<include>org.apache.httpcomponents.client5:httpclient5</include>
393+
<include>org.apache.httpcomponents.core5:httpcore5</include>
396394
<include>no.digipost:digipost-data-types</include>
397395
<include>com.fasterxml.jackson.core:jackson-core</include>
398396
<include>com.fasterxml.jackson.core:jackson-databind</include>

src/main/java/no/digipost/api/client/DigipostClient.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@
5656
import no.digipost.api.client.shareddocuments.SharedDocumentsApi;
5757
import no.digipost.api.client.tag.TagApi;
5858
import no.digipost.api.client.util.JAXBContextUtils;
59-
import no.digipost.http.client3.DigipostHttpClientFactory;
60-
import org.apache.http.client.methods.CloseableHttpResponse;
61-
import org.apache.http.impl.client.HttpClientBuilder;
59+
import no.digipost.http.client.HttpClientFactory;
60+
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
61+
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
6262
import org.slf4j.Logger;
6363
import org.slf4j.LoggerFactory;
6464

@@ -98,7 +98,7 @@ public class DigipostClient {
9898

9999

100100
public DigipostClient(DigipostClientConfig config, BrokerId brokerId, Signer signer) {
101-
this(config, brokerId, signer, DigipostHttpClientFactory.createDefaultBuilder());
101+
this(config, brokerId, signer, HttpClientFactory.createDefaultBuilder());
102102
}
103103

104104
public DigipostClient(DigipostClientConfig config, BrokerId brokerId, Signer signer, HttpClientBuilder clientBuilder) {

src/main/java/no/digipost/api/client/archive/ArchiveApi.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
import no.digipost.api.client.representations.archive.ArchiveDocument;
2121
import no.digipost.api.client.representations.archive.ArchiveDocumentContent;
2222
import no.digipost.api.client.representations.archive.Archives;
23-
import org.apache.http.HttpEntity;
24-
import org.apache.http.client.methods.CloseableHttpResponse;
23+
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
24+
import org.apache.hc.core5.http.HttpEntity;
2525

2626
import java.io.ByteArrayInputStream;
2727
import java.io.InputStream;

src/main/java/no/digipost/api/client/delivery/MessageDeliveryApi.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
import no.digipost.api.client.representations.accounts.UserAccount;
2626
import no.digipost.api.client.representations.accounts.UserInformation;
2727
import no.digipost.api.client.representations.sender.SenderInformation;
28-
import org.apache.http.HttpEntity;
29-
import org.apache.http.client.methods.CloseableHttpResponse;
28+
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
29+
import org.apache.hc.core5.http.HttpEntity;
3030

3131
import java.net.URI;
3232

src/main/java/no/digipost/api/client/filters/response/LoggingClientResponseInterceptor.java

Lines changed: 0 additions & 41 deletions
This file was deleted.

src/main/java/no/digipost/api/client/internal/ApiServiceImpl.java

Lines changed: 52 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -71,23 +71,24 @@
7171
import no.digipost.api.datatypes.DataType;
7272
import no.digipost.api.datatypes.types.share.ShareDocumentsRequestSharingStopped;
7373
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;
9192
import org.slf4j.Logger;
9293
import org.slf4j.LoggerFactory;
9394

@@ -112,6 +113,7 @@
112113
import static no.digipost.api.client.internal.http.UriUtils.withQueryParams;
113114
import static no.digipost.api.client.internal.http.response.HttpResponseUtils.checkResponse;
114115
import static no.digipost.api.client.internal.http.response.HttpResponseUtils.safelyOfferEntityStreamExternally;
116+
import static no.digipost.api.client.representations.MediaTypes.DIGIPOST_MEDIA_TYPE_V8;
115117
import static no.digipost.api.client.util.JAXBContextUtils.jaxbContext;
116118
import static no.digipost.api.client.util.JAXBContextUtils.marshal;
117119
import 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
}

src/main/java/no/digipost/api/client/internal/delivery/ArchiveDeliverer.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@
2525
import no.digipost.api.client.representations.archive.ArchiveDocument;
2626
import org.apache.commons.io.IOUtils;
2727
import org.apache.commons.io.output.ByteArrayOutputStream;
28-
import org.apache.http.client.methods.CloseableHttpResponse;
29-
import org.apache.http.entity.ContentType;
30-
import org.apache.http.entity.mime.FormBodyPartBuilder;
31-
import org.apache.http.entity.mime.HttpMultipartMode;
32-
import org.apache.http.entity.mime.MultipartEntityBuilder;
33-
import org.apache.http.entity.mime.content.ByteArrayBody;
28+
import org.apache.hc.client5.http.entity.mime.ByteArrayBody;
29+
import org.apache.hc.client5.http.entity.mime.FormBodyPartBuilder;
30+
import org.apache.hc.client5.http.entity.mime.HttpMultipartMode;
31+
import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder;
32+
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
33+
import org.apache.hc.core5.http.ContentType;
3434
import org.slf4j.Logger;
3535
import org.slf4j.LoggerFactory;
3636

src/main/java/no/digipost/api/client/internal/delivery/MessageDeliverer.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@
4040

4141
import org.apache.commons.io.IOUtils;
4242
import org.apache.commons.io.output.ByteArrayOutputStream;
43-
import org.apache.http.client.methods.CloseableHttpResponse;
44-
import org.apache.http.entity.ContentType;
45-
import org.apache.http.entity.mime.FormBodyPartBuilder;
46-
import org.apache.http.entity.mime.HttpMultipartMode;
47-
import org.apache.http.entity.mime.MultipartEntityBuilder;
48-
import org.apache.http.entity.mime.content.ByteArrayBody;
43+
import org.apache.hc.client5.http.entity.mime.ByteArrayBody;
44+
import org.apache.hc.client5.http.entity.mime.FormBodyPartBuilder;
45+
import org.apache.hc.client5.http.entity.mime.HttpMultipartMode;
46+
import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder;
47+
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
48+
import org.apache.hc.core5.http.ContentType;
4949
import org.slf4j.Logger;
5050
import org.slf4j.LoggerFactory;
5151

src/main/java/no/digipost/api/client/internal/http/Headers.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515
*/
1616
package no.digipost.api.client.internal.http;
1717

18-
import org.apache.http.Header;
19-
import org.apache.http.message.BasicHeader;
18+
19+
import org.apache.hc.core5.http.Header;
20+
import org.apache.hc.core5.http.message.BasicHeader;
2021

2122
import static no.digipost.api.client.representations.MediaTypes.DIGIPOST_MEDIA_TYPE_V8;
22-
import static org.apache.http.HttpHeaders.ACCEPT;
23-
import static org.apache.http.HttpHeaders.CONTENT_TYPE;
23+
import static org.apache.hc.core5.http.HttpHeaders.ACCEPT;
24+
import static org.apache.hc.core5.http.HttpHeaders.CONTENT_TYPE;
2425

2526
public final class Headers {
2627

0 commit comments

Comments
 (0)