From af8c368c6f971a2fcf8c0937ddc30d968091b0b9 Mon Sep 17 00:00:00 2001 From: Rune Flobakk Date: Tue, 14 Oct 2025 15:31:36 +0200 Subject: [PATCH 1/4] Use parse to avoid choking on charset The ContentType.create constructors are used for supplying the actual constituents of a ContentType, and will fail if ContentType.create(String) is passed e.g. 'application/xml; charset=UTF8'. When reading a header value (i.e. parsing), ContentType.parse must be used. --- .../api/client/internal/http/response/HttpResponseUtils.java | 1 - .../response/interceptor/ResponseContentSHA256Interceptor.java | 2 +- .../no/digipost/api/client/representations/DocumentTest.java | 1 + 3 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/no/digipost/api/client/internal/http/response/HttpResponseUtils.java b/src/main/java/no/digipost/api/client/internal/http/response/HttpResponseUtils.java index 64fd62fc..22452a37 100644 --- a/src/main/java/no/digipost/api/client/internal/http/response/HttpResponseUtils.java +++ b/src/main/java/no/digipost/api/client/internal/http/response/HttpResponseUtils.java @@ -21,7 +21,6 @@ import no.digipost.api.client.errorhandling.ErrorCode; import no.digipost.api.client.representations.ErrorMessage; import no.digipost.api.client.representations.ErrorType; -import org.apache.commons.lang3.StringUtils; import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; import org.apache.hc.core5.http.ClassicHttpResponse; import org.apache.hc.core5.http.HttpEntity; diff --git a/src/main/java/no/digipost/api/client/internal/http/response/interceptor/ResponseContentSHA256Interceptor.java b/src/main/java/no/digipost/api/client/internal/http/response/interceptor/ResponseContentSHA256Interceptor.java index a587fb80..2b95fcc5 100644 --- a/src/main/java/no/digipost/api/client/internal/http/response/interceptor/ResponseContentSHA256Interceptor.java +++ b/src/main/java/no/digipost/api/client/internal/http/response/interceptor/ResponseContentSHA256Interceptor.java @@ -52,7 +52,7 @@ public void process(HttpResponse response, EntityDetails entityDetails, HttpCont X_Content_SHA256, response.getCode()))); byte[] entityBytes = EntityUtils.toByteArray(entity); validerBytesMotHashHeader(hashHeaderValue, entityBytes); - classicHttpResponse.setEntity(new ByteArrayEntity(entityBytes, ContentType.create(entityDetails.getContentType()))); + classicHttpResponse.setEntity(new ByteArrayEntity(entityBytes, ContentType.parse(entityDetails.getContentType()))); } } diff --git a/src/test/java/no/digipost/api/client/representations/DocumentTest.java b/src/test/java/no/digipost/api/client/representations/DocumentTest.java index d1676ee8..141f0662 100644 --- a/src/test/java/no/digipost/api/client/representations/DocumentTest.java +++ b/src/test/java/no/digipost/api/client/representations/DocumentTest.java @@ -114,6 +114,7 @@ public void isOpenedWhenTrueIsPassed() { } @Test + @SuppressWarnings("deprecation") public void isOpenedWhenTrueBuiltWithDeprecatedConstructorsIsPassed() { Document boolConstructorTrueDoc = new Document(null, null, null, null, null, null, null, null, new Boolean(true), null, ""); assertThat(boolConstructorTrueDoc.opened, is(true)); From 12e9cd9bd5f919a932023cf0634f00aa6b64d298 Mon Sep 17 00:00:00 2001 From: Rune Flobakk Date: Tue, 14 Oct 2025 16:29:13 +0200 Subject: [PATCH 2/4] Remove unnecessary exclusion commons-logging is not longer a dependency of Apache HttpClient --- pom.xml | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/pom.xml b/pom.xml index 4d74aad2..d3337dcb 100644 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,7 @@ LOCAL-SNAPSHOT Digipost API Client Java library for interacting with the Digipost REST API - + Posten Bring AS https://www.posten.no/ @@ -29,7 +29,7 @@ - + 11 11 @@ -69,12 +69,6 @@ org.apache.httpcomponents.client5 httpclient5 5.5.1 - - - commons-logging - commons-logging - - org.apache.httpcomponents.core5 From b01a70fa366fb9f412fc0ccc847978c17686161c Mon Sep 17 00:00:00 2001 From: Rune Flobakk Date: Tue, 14 Oct 2025 16:29:28 +0200 Subject: [PATCH 3/4] Fix some compiler warns in test code --- .../api/client/internal/delivery/MessageDelivererTest.java | 4 ++-- .../api/client/internal/delivery/MockfriendlyResponse.java | 7 ++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/test/java/no/digipost/api/client/internal/delivery/MessageDelivererTest.java b/src/test/java/no/digipost/api/client/internal/delivery/MessageDelivererTest.java index 600c1185..17218bed 100644 --- a/src/test/java/no/digipost/api/client/internal/delivery/MessageDelivererTest.java +++ b/src/test/java/no/digipost/api/client/internal/delivery/MessageDelivererTest.java @@ -353,13 +353,13 @@ void setMapAndMessageToPrintClonesMessage(String description, Message message) { private static Stream provideMessages() { Document primaryDoc = new Document( - UUID.randomUUID(), RandomStringUtils.randomAlphanumeric(1, 128), FileType.PDF + UUID.randomUUID(), RandomStringUtils.insecure().nextAlphanumeric(1, 128), FileType.PDF ); PrintRecipient printRecipient = new PrintRecipient(); PrintDetails printDetails = new PrintDetails(printRecipient, printRecipient); MessageRecipient directPrintRecipient = new MessageRecipient(printDetails); MessageRecipient fallbackPrintRecipient = new MessageRecipient( - new PersonalIdentificationNumber(RandomStringUtils.randomNumeric(11)), printDetails + new PersonalIdentificationNumber(RandomStringUtils.insecure().nextNumeric(11)), printDetails ); return Stream.of( Arguments.of("direct print message", Message.newMessage(UUID.randomUUID(), primaryDoc) diff --git a/src/test/java/no/digipost/api/client/internal/delivery/MockfriendlyResponse.java b/src/test/java/no/digipost/api/client/internal/delivery/MockfriendlyResponse.java index b1f338a8..3fc55592 100644 --- a/src/test/java/no/digipost/api/client/internal/delivery/MockfriendlyResponse.java +++ b/src/test/java/no/digipost/api/client/internal/delivery/MockfriendlyResponse.java @@ -189,7 +189,7 @@ public void setHeader(String name, Object value) { } @Override - public void setHeaders(Header[] headers) { + public void setHeaders(Header ... headers) { throw new UnsupportedOperationException("This is a mock"); } @@ -245,6 +245,11 @@ public HttpEntity getEntity() { return entity; } + @Override + public int getCode() { + return status; + } + @Override public void close() throws IOException { } From 0ea61449da94936d60e5a946f6509685cc3f6b54 Mon Sep 17 00:00:00 2001 From: Rune Flobakk Date: Tue, 14 Oct 2025 16:34:28 +0200 Subject: [PATCH 4/4] Replace deprecated ObjectUtils.defaultIfNull --- .../digipost/api/client/representations/Message.java | 10 +++++----- .../api/client/representations/archive/Archive.java | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/main/java/no/digipost/api/client/representations/Message.java b/src/main/java/no/digipost/api/client/representations/Message.java index a78be320..67e97a3a 100644 --- a/src/main/java/no/digipost/api/client/representations/Message.java +++ b/src/main/java/no/digipost/api/client/representations/Message.java @@ -42,7 +42,7 @@ import static no.digipost.api.client.representations.Channel.PRINT; import static org.apache.commons.lang3.ArrayUtils.INDEX_NOT_FOUND; import static org.apache.commons.lang3.ArrayUtils.indexOf; -import static org.apache.commons.lang3.ObjectUtils.defaultIfNull; +import static org.apache.commons.lang3.ObjectUtils.getIfNull; import static org.apache.commons.lang3.StringUtils.join; @XmlAccessorType(XmlAccessType.FIELD) @@ -55,7 +55,7 @@ "invoiceReference", "primaryDocument", "attachments", - "printIfUnread", + "printIfUnread", "requestForRegistration", "batch"}) @XmlRootElement(name = "message") @@ -195,7 +195,7 @@ public MessageBuilder attachments(Document ... attachments) { } public MessageBuilder attachments(Iterable attachments) { - defaultIfNull(attachments, Collections.emptyList()).forEach(this.attachments::add); + getIfNull(attachments, Collections.emptyList()).forEach(this.attachments::add); return this; } @@ -203,7 +203,7 @@ public MessageBuilder batch(UUID batchUUID) { this.batch = new Batch(batchUUID.toString()); return this; } - + public Message build() { if (recipient == null) { throw new IllegalStateException("You must specify a recipient."); @@ -227,7 +227,7 @@ private Message(String messageId, Long senderId, SenderOrganization senderOrgani this.invoiceReference = invoiceReference; this.deliveryTime = deliveryTime; this.attachments = new ArrayList<>(); - for (Document attachment : defaultIfNull(attachments, Collections.emptyList())) { + for (Document attachment : getIfNull(attachments, Collections.emptyList())) { this.attachments.add(attachment); } this.printIfUnread = printIfUnread; diff --git a/src/main/java/no/digipost/api/client/representations/archive/Archive.java b/src/main/java/no/digipost/api/client/representations/archive/Archive.java index a6cfb9b4..ebdccf4e 100644 --- a/src/main/java/no/digipost/api/client/representations/archive/Archive.java +++ b/src/main/java/no/digipost/api/client/representations/archive/Archive.java @@ -41,7 +41,7 @@ import static java.util.Arrays.asList; import static no.digipost.api.client.representations.Relation.NEXT_DOCUMENTS; -import static org.apache.commons.lang3.ObjectUtils.defaultIfNull; +import static org.apache.commons.lang3.ObjectUtils.getIfNull; @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "archive", propOrder = { @@ -134,7 +134,7 @@ public Optional getNextDocumentsWithAttributes(Map attribut public Optional getNextDocumentsWithAttributesByDate(Map attributes, OffsetDateTime from, OffsetDateTime to) { final String attributesCommaSeparated = attributes.entrySet().stream().flatMap(en -> Stream.of(en.getKey(), en.getValue())).collect(Collectors.joining(",")); - + return Optional.ofNullable(getLinkByRelationName(NEXT_DOCUMENTS)).map(Link::getUri) .map(uri -> { try { @@ -150,7 +150,7 @@ public Optional getNextDocumentsWithAttributesByDate(Map at } public Optional getNextDocumentsByDate(OffsetDateTime from, OffsetDateTime to) { - + return Optional.ofNullable(getLinkByRelationName(NEXT_DOCUMENTS)).map(Link::getUri) .map(uri -> { try { @@ -202,7 +202,7 @@ public ArchiveBuilder documents(ArchiveDocument... documents) { } public ArchiveBuilder documents(Iterable documents) { - defaultIfNull(documents, Collections.emptyList()).forEach(this.documents::add); + getIfNull(documents, Collections.emptyList()).forEach(this.documents::add); return this; } @@ -218,5 +218,5 @@ public Archive build() { private static String base64(String param){ return Base64.getEncoder().encodeToString(param.getBytes(StandardCharsets.UTF_8)); } - + }