Code development platform for open source projects from the European Union institutions

Skip to content
Snippets Groups Projects
Commit a865fcd2 authored by Joze RIHTARSIC's avatar Joze RIHTARSIC
Browse files

Synchronize response error on empty payload

parent 22c55902
No related branches found
No related tags found
No related merge requests found
Pipeline #112844 passed with warnings
......@@ -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();
......
......@@ -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;
......
......@@ -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) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment