diff --git a/commons-rest/errors/pom.xml b/commons-rest/errors/pom.xml index 910d050272e..508547af40f 100644 --- a/commons-rest/errors/pom.xml +++ b/commons-rest/errors/pom.xml @@ -31,6 +31,10 @@ org.eclipse.kapua kapua-endpoint-api + + org.eclipse.kapua + kapua-service-storable-api + diff --git a/commons-rest/errors/src/main/java/org/eclipse/kapua/commons/rest/errors/StorableNotFoundExceptionMapper.java b/commons-rest/errors/src/main/java/org/eclipse/kapua/commons/rest/errors/StorableNotFoundExceptionMapper.java new file mode 100644 index 00000000000..febe55d65d4 --- /dev/null +++ b/commons-rest/errors/src/main/java/org/eclipse/kapua/commons/rest/errors/StorableNotFoundExceptionMapper.java @@ -0,0 +1,46 @@ +/******************************************************************************* + * Copyright (c) 2017, 2022 Eurotech and/or its affiliates and others + * + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Eurotech - initial API and implementation + *******************************************************************************/ +package org.eclipse.kapua.commons.rest.errors; + +import org.eclipse.kapua.commons.rest.model.errors.StorableNotFoundExceptionInfo; +import org.eclipse.kapua.service.storable.exception.StorableNotFoundException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.inject.Inject; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.Status; +import javax.ws.rs.ext.ExceptionMapper; +import javax.ws.rs.ext.Provider; + +@Provider +public class StorableNotFoundExceptionMapper implements ExceptionMapper { + + private static final Logger LOG = LoggerFactory.getLogger(StorableNotFoundExceptionMapper.class); + + private final boolean showStackTrace; + + @Inject + public StorableNotFoundExceptionMapper(ExceptionConfigurationProvider exceptionConfigurationProvider) { + this.showStackTrace = exceptionConfigurationProvider.showStackTrace(); + } + + @Override + public Response toResponse(StorableNotFoundException kapuaEntityNotFoundException) { + LOG.error(kapuaEntityNotFoundException.getMessage(), kapuaEntityNotFoundException); + return Response + .status(Status.NOT_FOUND) + .entity(new StorableNotFoundExceptionInfo(Status.NOT_FOUND.getStatusCode(), kapuaEntityNotFoundException, showStackTrace)) + .build(); + } +} diff --git a/commons-rest/model/pom.xml b/commons-rest/model/pom.xml index 6ecfed3de79..8ff78a700e5 100644 --- a/commons-rest/model/pom.xml +++ b/commons-rest/model/pom.xml @@ -38,6 +38,10 @@ org.eclipse.kapua kapua-job-engine-api + + org.eclipse.kapua + kapua-service-storable-api + diff --git a/commons-rest/model/src/main/java/org/eclipse/kapua/commons/rest/model/errors/StorableNotFoundExceptionInfo.java b/commons-rest/model/src/main/java/org/eclipse/kapua/commons/rest/model/errors/StorableNotFoundExceptionInfo.java new file mode 100644 index 00000000000..a08144ec947 --- /dev/null +++ b/commons-rest/model/src/main/java/org/eclipse/kapua/commons/rest/model/errors/StorableNotFoundExceptionInfo.java @@ -0,0 +1,78 @@ +/******************************************************************************* + * Copyright (c) 2017, 2022 Eurotech and/or its affiliates and others + * + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Eurotech - initial API and implementation + *******************************************************************************/ +package org.eclipse.kapua.commons.rest.model.errors; + +import org.eclipse.kapua.model.id.KapuaIdAdapter; +import org.eclipse.kapua.service.storable.exception.StorableNotFoundException; +import org.eclipse.kapua.service.storable.model.id.StorableId; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; + +@XmlRootElement(name = "storableNotFoundExceptionInfo") +@XmlAccessorType(XmlAccessType.FIELD) +public class StorableNotFoundExceptionInfo extends ExceptionInfo { + + @XmlElement(name = "storableType") + private String storableType; + + @XmlElement(name = "storableId") + @XmlJavaTypeAdapter(KapuaIdAdapter.class) + private StorableId storableId; + + /** + * Constructor. + * + * @since 1.0.0 + */ + protected StorableNotFoundExceptionInfo() { + super(); + } + + /** + * Constructor. + * + * @param httpStatusCode The http status code of the response containing this info + * @param storableNotFoundException The {@link StorableNotFoundException}. + * @since 1.0.0 + */ + public StorableNotFoundExceptionInfo(int httpStatusCode, StorableNotFoundException storableNotFoundException, boolean showStackTrace) { + super(httpStatusCode, storableNotFoundException, showStackTrace); + + this.storableType = storableNotFoundException.getStorableType(); + this.storableId = storableNotFoundException.getStorableId(); + } + + /** + * Gets the {@link StorableNotFoundException#getStorableType()} + * + * @return The {@link StorableNotFoundException#getStorableType()}. + * @since 2.0.0 + */ + public String getEntityType() { + return storableType; + } + + /** + * Gets the {@link StorableNotFoundException#getStorableId()}. + * + * @return The {@link StorableNotFoundException#getStorableId()}. + * @since 2.0.0 + */ + public StorableId getStorableId() { + return storableId; + } +} diff --git a/rest-api/core/src/main/java/org/eclipse/kapua/app/api/core/model/data/JsonDatastoreMessage.java b/rest-api/core/src/main/java/org/eclipse/kapua/app/api/core/model/data/JsonDatastoreMessage.java index 3cac692563a..fb95b818f38 100644 --- a/rest-api/core/src/main/java/org/eclipse/kapua/app/api/core/model/data/JsonDatastoreMessage.java +++ b/rest-api/core/src/main/java/org/eclipse/kapua/app/api/core/model/data/JsonDatastoreMessage.java @@ -56,6 +56,11 @@ public JsonDatastoreMessage(DatastoreMessage datastoreMessage) { setPayload(datastoreMessage.getPayload()); } + @Override + public String getType() { + return DatastoreMessage.TYPE; + } + @XmlElement(name = "datastoreId") @XmlJavaTypeAdapter(StorableIdXmlAdapter.class) public StorableId getDatastoreId() { diff --git a/rest-api/core/src/main/java/org/eclipse/kapua/app/api/core/resources/AbstractKapuaResource.java b/rest-api/core/src/main/java/org/eclipse/kapua/app/api/core/resources/AbstractKapuaResource.java index 67a852ce6d3..fbe6dc46aed 100644 --- a/rest-api/core/src/main/java/org/eclipse/kapua/app/api/core/resources/AbstractKapuaResource.java +++ b/rest-api/core/src/main/java/org/eclipse/kapua/app/api/core/resources/AbstractKapuaResource.java @@ -15,6 +15,9 @@ import org.eclipse.kapua.KapuaEntityNotFoundException; import org.eclipse.kapua.model.KapuaEntity; import org.eclipse.kapua.model.id.KapuaId; +import org.eclipse.kapua.service.storable.exception.StorableNotFoundException; +import org.eclipse.kapua.service.storable.model.Storable; +import org.eclipse.kapua.service.storable.model.id.StorableId; import javax.ws.rs.NotFoundException; import javax.ws.rs.WebApplicationException; @@ -68,6 +71,27 @@ public T returnNotNullEntity(T entity, String entityType return entity; } + /** + * Checks id the given {@link Storable} is {@code null}. + *

+ * Similar to {@link #returnNotNullEntity(KapuaEntity, String, KapuaId)} but for {@link Storable}s. + * + * @param storable The {@link Storable} to check. + * @param storableType The {@link Storable#getType()} + * @param storableId The {@link StorableId} + * @return The given {@link Storable} if not {@code null} + * @param The type of the {@link Storable}. + * @throws StorableNotFoundException if given {@link Storable} is {@code null}. + * @since 2.0.0 + */ + public T returnNotNullStorable(T storable, String storableType, StorableId storableId) throws StorableNotFoundException { + if (storable == null) { + throw new StorableNotFoundException(storableType, storableId); + } + + return storable; + } + /** * Builds a 200 HTTP Response. * diff --git a/rest-api/resources/src/main/java/org/eclipse/kapua/app/api/resources/v1/resources/DataChannels.java b/rest-api/resources/src/main/java/org/eclipse/kapua/app/api/resources/v1/resources/DataChannels.java index e5eed7e109d..10ae28ef3d4 100644 --- a/rest-api/resources/src/main/java/org/eclipse/kapua/app/api/resources/v1/resources/DataChannels.java +++ b/rest-api/resources/src/main/java/org/eclipse/kapua/app/api/resources/v1/resources/DataChannels.java @@ -160,6 +160,6 @@ public ChannelInfo find(@PathParam("scopeId") ScopeId scopeId, throws KapuaException { ChannelInfo channelInfo = channelInfoRegistryService.find(scopeId, channelInfoId); - return returnNotNullEntity(channelInfo); + return returnNotNullStorable(channelInfo, ChannelInfo.TYPE, channelInfoId); } } diff --git a/rest-api/resources/src/main/java/org/eclipse/kapua/app/api/resources/v1/resources/DataClients.java b/rest-api/resources/src/main/java/org/eclipse/kapua/app/api/resources/v1/resources/DataClients.java index 3583c44c076..2e1329cac32 100644 --- a/rest-api/resources/src/main/java/org/eclipse/kapua/app/api/resources/v1/resources/DataClients.java +++ b/rest-api/resources/src/main/java/org/eclipse/kapua/app/api/resources/v1/resources/DataClients.java @@ -142,6 +142,6 @@ public ClientInfo find(@PathParam("scopeId") ScopeId scopeId, throws KapuaException { ClientInfo clientInfo = clientInfoRegistryService.find(scopeId, clientInfoId); - return returnNotNullEntity(clientInfo); + return returnNotNullStorable(clientInfo, ClientInfo.TYPE, clientInfoId); } } diff --git a/rest-api/resources/src/main/java/org/eclipse/kapua/app/api/resources/v1/resources/DataMessages.java b/rest-api/resources/src/main/java/org/eclipse/kapua/app/api/resources/v1/resources/DataMessages.java index 85e66421067..59a8da169de 100644 --- a/rest-api/resources/src/main/java/org/eclipse/kapua/app/api/resources/v1/resources/DataMessages.java +++ b/rest-api/resources/src/main/java/org/eclipse/kapua/app/api/resources/v1/resources/DataMessages.java @@ -184,7 +184,7 @@ public DatastoreMessage find(@PathParam("scopeId") ScopeId scopeId, throws KapuaException { DatastoreMessage datastoreMessage = messageStoreService.find(scopeId, datastoreMessageId, StorableFetchStyle.SOURCE_FULL); - return returnNotNullEntity(datastoreMessage); + return returnNotNullStorable(datastoreMessage, DatastoreMessage.TYPE, datastoreMessageId); } diff --git a/rest-api/resources/src/main/java/org/eclipse/kapua/app/api/resources/v1/resources/DataMessagesJson.java b/rest-api/resources/src/main/java/org/eclipse/kapua/app/api/resources/v1/resources/DataMessagesJson.java index afa058ddafb..cd756acf5e3 100644 --- a/rest-api/resources/src/main/java/org/eclipse/kapua/app/api/resources/v1/resources/DataMessagesJson.java +++ b/rest-api/resources/src/main/java/org/eclipse/kapua/app/api/resources/v1/resources/DataMessagesJson.java @@ -205,11 +205,11 @@ public JsonMessageListResult queryJson(@PathParam("scopeId") ScopeId scopeId, public JsonDatastoreMessage findJson(@PathParam("scopeId") ScopeId scopeId, @PathParam("datastoreMessageId") StorableEntityId datastoreMessageId) throws KapuaException { - DatastoreMessage datastoreMessage = returnNotNullEntity(messageStoreService.find(scopeId, datastoreMessageId, StorableFetchStyle.SOURCE_FULL)); + DatastoreMessage datastoreMessage = returnNotNullStorable(messageStoreService.find(scopeId, datastoreMessageId, StorableFetchStyle.SOURCE_FULL), DatastoreMessage.TYPE, datastoreMessageId); JsonDatastoreMessage jsonDatastoreMessage = new JsonDatastoreMessage(datastoreMessage); - return returnNotNullEntity(jsonDatastoreMessage); + return returnNotNullStorable(jsonDatastoreMessage, DatastoreMessage.TYPE, datastoreMessageId); } private MessageQuery convertQuery(JsonMessageQuery query) { diff --git a/rest-api/resources/src/main/java/org/eclipse/kapua/app/api/resources/v1/resources/DataMetrics.java b/rest-api/resources/src/main/java/org/eclipse/kapua/app/api/resources/v1/resources/DataMetrics.java index 93f12a6a485..dbab38cc7e0 100644 --- a/rest-api/resources/src/main/java/org/eclipse/kapua/app/api/resources/v1/resources/DataMetrics.java +++ b/rest-api/resources/src/main/java/org/eclipse/kapua/app/api/resources/v1/resources/DataMetrics.java @@ -150,6 +150,6 @@ public MetricInfo find(@PathParam("scopeId") ScopeId scopeId, throws KapuaException { MetricInfo metricInfo = metricInfoRegistryService.find(scopeId, metricInfoId); - return returnNotNullEntity(metricInfo); + return returnNotNullStorable(metricInfo, MetricInfo.TYPE, metricInfoId); } }