From c002935966a97c556f202c0368c1959fa2f34d60 Mon Sep 17 00:00:00 2001 From: Christophe Loiseau Date: Wed, 20 Mar 2024 11:10:19 +0100 Subject: [PATCH] Workaround for an unexpected NPE (#830) * Workaround for a case where the negotiation can be null --- CHANGELOG.md | 1 + .../TransferHistoryPageApiService.java | 41 +++++++++++-------- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f31e1535..96ca45449 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ please see [changelog_updates.md](docs/dev/changelog_updates.md). #### Patch Changes +- Add a fix for a null pointer exception in the transfer history API. - Add e2e test for double encoding of query parameters ### Deployment Migration Notes diff --git a/extensions/wrapper/wrapper/src/main/java/de/sovity/edc/ext/wrapper/api/ui/pages/transferhistory/TransferHistoryPageApiService.java b/extensions/wrapper/wrapper/src/main/java/de/sovity/edc/ext/wrapper/api/ui/pages/transferhistory/TransferHistoryPageApiService.java index a7a227aaa..a72e1ddad 100644 --- a/extensions/wrapper/wrapper/src/main/java/de/sovity/edc/ext/wrapper/api/ui/pages/transferhistory/TransferHistoryPageApiService.java +++ b/extensions/wrapper/wrapper/src/main/java/de/sovity/edc/ext/wrapper/api/ui/pages/transferhistory/TransferHistoryPageApiService.java @@ -35,6 +35,7 @@ import java.util.Comparator; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.function.BinaryOperator; import java.util.function.Function; @@ -59,6 +60,7 @@ public class TransferHistoryPageApiService { public List getTransferHistoryEntries() { var negotiationsById = getAllContractNegotiations().stream() + .filter(negotiation -> negotiation != null) .filter(negotiation -> negotiation.getContractAgreement() != null) .collect(toMap( it -> it.getContractAgreement().getId(), @@ -76,26 +78,33 @@ public List getTransferHistoryEntries() { var transferProcesses = getAllTransferProcesses(); return transferProcesses.stream().map(process -> { - var agreement = agreementsById.get(process.getDataRequest().getContractId()); - var negotiation = negotiationsById.get(process.getDataRequest().getContractId()); + var agreement = Optional.ofNullable(agreementsById.get(process.getDataRequest().getContractId())); + var negotiation = Optional.ofNullable(negotiationsById.get(process.getDataRequest().getContractId())); var asset = assetLookup(assetsById, process); - var direction = ContractAgreementDirection.fromType(negotiation.getType()); + var direction = negotiation.map(ContractNegotiation::getType).map(ContractAgreementDirection::fromType); var transferHistoryEntry = new TransferHistoryEntry(); transferHistoryEntry.setAssetId(asset.getId()); - if (direction == ContractAgreementDirection.CONSUMING) { - transferHistoryEntry.setAssetName(asset.getId()); - } else { - transferHistoryEntry.setAssetName( - StringUtils.isBlank((String) asset.getProperties().get(Prop.Dcterms.TITLE)) - ? asset.getId() - : asset.getProperties().get(Prop.Dcterms.TITLE).toString() - ); + + if (direction.isPresent()) { + if (direction.get() == ContractAgreementDirection.CONSUMING) { + transferHistoryEntry.setAssetName(asset.getId()); + } else { + transferHistoryEntry.setAssetName( + StringUtils.isBlank((String) asset.getProperties().get(Prop.Dcterms.TITLE)) + ? asset.getId() + : asset.getProperties().get(Prop.Dcterms.TITLE).toString() + ); + } } - transferHistoryEntry.setContractAgreementId(agreement.getId()); - transferHistoryEntry.setCounterPartyConnectorEndpoint(negotiation.getCounterPartyAddress()); - transferHistoryEntry.setCounterPartyParticipantId(negotiation.getCounterPartyId()); - transferHistoryEntry.setCreatedDate(utcMillisToOffsetDateTime(negotiation.getCreatedAt())); - transferHistoryEntry.setDirection(direction); + + agreement.ifPresent(it -> transferHistoryEntry.setContractAgreementId(it.getId())); + negotiation.ifPresent( it -> { + transferHistoryEntry.setCounterPartyConnectorEndpoint(it.getCounterPartyAddress()); + transferHistoryEntry.setCounterPartyParticipantId(it.getCounterPartyId()); + transferHistoryEntry.setCreatedDate(utcMillisToOffsetDateTime(it.getCreatedAt())); + }); + direction.ifPresent(transferHistoryEntry::setDirection); + transferHistoryEntry.setErrorMessage(process.getErrorDetail()); transferHistoryEntry.setLastUpdatedDate(utcMillisToOffsetDateTime(process.getUpdatedAt())); transferHistoryEntry.setState(transferProcessStateService.buildTransferProcessState(process.getState()));