From a865fcd2aa81d12f5d4d248f94c21ee12018d4a3 Mon Sep 17 00:00:00 2001 From: RIHTARSIC Joze <joze.rihtarsic@ext.ec.europa.eu> Date: Thu, 16 Nov 2023 08:51:48 +0100 Subject: [PATCH] Synchronize response error on empty payload --- .../europa/ec/edelivery/smp/data/dao/DocumentDao.java | 7 +++++-- .../europa/ec/edelivery/smp/exceptions/ErrorCode.java | 10 +++------- .../smp/services/resource/AbstractResourceHandler.java | 9 +++++++-- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/data/dao/DocumentDao.java b/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/data/dao/DocumentDao.java index 40b143076..5080a113f 100644 --- a/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/data/dao/DocumentDao.java +++ b/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/data/dao/DocumentDao.java @@ -30,7 +30,7 @@ public class DocumentDao extends BaseDao<DBDocument> { * Method returns the document for the resource * * @param dbResource resource - * @return + * @return document for the resource or empty if not found */ public Optional<DBDocument> getDocumentForResource(DBResource dbResource) { try { @@ -67,7 +67,10 @@ public class DocumentDao extends BaseDao<DBDocument> { query.setParameter(PARAM_SUBRESOURCE_ID, subresource.getId()); return Optional.of(query.getSingleResult()); } catch (NonUniqueResultException e) { - throw new SMPRuntimeException(ErrorCode.RESOURCE_DOCUMENT_ERROR, subresource.getIdentifierValue(), subresource.getIdentifierScheme(), + DBResource resource = subresource.getResource(); + throw new SMPRuntimeException(ErrorCode.SUBRESOURCE_DOCUMENT_ERROR, + subresource.getIdentifierValue(), subresource.getIdentifierScheme(), + resource.getIdentifierValue(), resource.getIdentifierScheme(), "Multiple documents for subresource"); } catch (NoResultException e) { return Optional.empty(); diff --git a/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/exceptions/ErrorCode.java b/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/exceptions/ErrorCode.java index 6ea193026..4770be0ba 100644 --- a/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/exceptions/ErrorCode.java +++ b/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/exceptions/ErrorCode.java @@ -70,13 +70,9 @@ public enum ErrorCode { MAIL_SUBMISSION_ERROR (500,"SMP:550",ErrorBusinessCode.TECHNICAL, "Mail submission error: %s!"), RESOURCE_DOCUMENT_MISSING(500,"SMP:180",ErrorBusinessCode.TECHNICAL, "Empty document for the resource: [id: '%s', sch.: '%s']!"), - RESOURCE_DOCUMENT_ERROR(500,"SMP:180",ErrorBusinessCode.TECHNICAL, "Error occurred while reading the resource document: [id: '%s', sch.: '%s']! Error [%s]"), - - - - - - // + RESOURCE_DOCUMENT_ERROR(500,"SMP:181",ErrorBusinessCode.TECHNICAL, "Error occurred while reading the resource document: [id: '%s', sch.: '%s']! Error [%s]"), + SUBRESOURCE_DOCUMENT_MISSING(500,"SMP:182",ErrorBusinessCode.TECHNICAL, "Empty document for the subresource: [docId: '%s', docSch.: '%s'] of the resource [id: '%s', sch.: '%s']"), + SUBRESOURCE_DOCUMENT_ERROR(500,"SMP:183",ErrorBusinessCode.TECHNICAL, "Error occurred while reading the subresource document: : [docId: '%s', docSch.: '%s'] of the resource[id: '%s', sch: '%s']! Error [%s]"), ; private final int httpCode; diff --git a/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/services/resource/AbstractResourceHandler.java b/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/services/resource/AbstractResourceHandler.java index e6710ab21..28595b752 100644 --- a/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/services/resource/AbstractResourceHandler.java +++ b/smp-server-library/src/main/java/eu/europa/ec/edelivery/smp/services/resource/AbstractResourceHandler.java @@ -105,10 +105,15 @@ public class AbstractResourceHandler { public RequestData buildRequestDataForSubResource(DBDomain domain, DBResource resource, DBSubresource subresource) { byte[] content = resourceStorage.getDocumentContentForSubresource(subresource); + if (content==null || content.length == 0) { + throw new SMPRuntimeException(ErrorCode.SUBRESOURCE_DOCUMENT_MISSING, + subresource.getIdentifierValue(), subresource.getIdentifierScheme(), + resource.getIdentifierValue(), resource.getIdentifierScheme()); + } return new SpiRequestData(domain.getDomainCode(), SPIUtils.toUrlIdentifier(resource), SPIUtils.toUrlIdentifier(subresource), - new ByteArrayInputStream(content == null?new byte[]{}:content)); + new ByteArrayInputStream(content)); } public RequestData buildRequestDataForSubResource(DBDomain domain, DBResource resource, DBSubresource subresource, InputStream inputStream) { @@ -124,7 +129,7 @@ public class AbstractResourceHandler { if (StringUtils.isNotBlank(responseData.getContentType())) { resourceResponse.setContentType(responseData.getContentType()); } - responseData.getHttpHeaders().entrySet().stream() + responseData.getHttpHeaders().entrySet() .forEach(entry -> resourceResponse.setHttpHeader(entry.getKey(), entry.getValue())); } catch (ResourceException e) { -- GitLab