Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ [Core] Implemented StorableNotFoundException #4042

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions commons-rest/errors/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
<groupId>org.eclipse.kapua</groupId>
<artifactId>kapua-endpoint-api</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.kapua</groupId>
<artifactId>kapua-service-storable-api</artifactId>
</dependency>

<!-- re-declare as provided as our web container will provide this -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -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<StorableNotFoundException> {

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();
}
}
4 changes: 4 additions & 0 deletions commons-rest/model/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@
<groupId>org.eclipse.kapua</groupId>
<artifactId>kapua-scheduler-api</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.kapua</groupId>
<artifactId>kapua-service-storable-api</artifactId>
</dependency>

<!-- Testing dependencies -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*******************************************************************************
* Copyright (c) 2024, 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 2.0.0
*/
protected StorableNotFoundExceptionInfo() {
super();
}

/**
* Constructor.
*
* @param httpStatusCode The http status code of the response containing this info
* @param storableNotFoundException The {@link StorableNotFoundException}.
* @since 2.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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -68,6 +71,27 @@ public <T extends KapuaEntity> T returnNotNullEntity(T entity, String entityType
return entity;
}

/**
* Checks id the given {@link Storable} is {@code null}.
* <p>
* 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 <T> The type of the {@link Storable}.
* @throws StorableNotFoundException if given {@link Storable} is {@code null}.
* @since 2.0.0
*/
public <T extends Storable> 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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,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);
}

//TODO: move this logic within the service, or at least in a collaborator shared with DataMessagesJson
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import org.eclipse.kapua.commons.rest.model.errors.SelfManagedOnlyExceptionInfo;
import org.eclipse.kapua.commons.rest.model.errors.ServiceConfigurationLimitExceededExceptionInfo;
import org.eclipse.kapua.commons.rest.model.errors.ServiceConfigurationParentLimitExceededExceptionInfo;
import org.eclipse.kapua.commons.rest.model.errors.StorableNotFoundExceptionInfo;
import org.eclipse.kapua.commons.rest.model.errors.SubjectUnauthorizedExceptionInfo;
import org.eclipse.kapua.commons.rest.model.errors.ThrowableInfo;
import org.eclipse.kapua.commons.service.event.store.api.EventStoreRecordCreator;
Expand Down Expand Up @@ -366,6 +367,7 @@ public JAXBContext getJAXBContext() throws KapuaException {
IllegalArgumentExceptionInfo.class,
IllegalNullArgumentExceptionInfo.class,
MfaRequiredExceptionInfo.class,
StorableNotFoundExceptionInfo.class,

// Jobs Exception Info
CleanJobDataExceptionInfo.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,12 @@ public enum StorableErrorCodes implements KapuaErrorCode {
*
* @since 1.3.0
*/
UNSUPPORTED_TYPE
UNSUPPORTED_TYPE,

/**
* See {@link StorableNotFoundException}
*
* @since 2.0.0
*/
STORABLE_NOT_FOUND
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*******************************************************************************
* Copyright (c) 2024, 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.service.storable.exception;

import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.service.storable.model.Storable;
import org.eclipse.kapua.service.storable.model.id.StorableId;

/**
* {@link StorableNotFoundException} is thrown when an {@link Storable} could not be loaded from the database.
*
* @since 2.0.0
*/
public class StorableNotFoundException extends KapuaException {

private final String storableType;
private final StorableId storableId;

/**
* Constructor.
*
* @param storableType The {@link Storable#getType()}.
* @param storableId The {@link StorableId}.
* @since 2.0.0
*/
public StorableNotFoundException(String storableType, StorableId storableId) {
super(StorableErrorCodes.STORABLE_NOT_FOUND, storableType, storableId);

this.storableType = storableType;
this.storableId = storableId;
}

/**
* Gets the {@link Storable#getType()}.
*
* @return The {@link Storable#getType()}.
* @since 2.0.0
*/
public String getStorableType() {
return storableType;
}

/**
* Gets the {@link StorableId}.
*
* @return The {@link StorableId}.
* @since 2.0.0
*/
public StorableId getStorableId() {
return storableId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.eclipse.kapua.model.id.KapuaIdAdapter;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

Expand All @@ -30,6 +31,15 @@
@XmlType(propOrder = {"scopeId"})
public interface Storable extends KapuaSerializable {

/**
* Gets the type.
*
* @return The type.
* @since 2.0.0
*/
@XmlTransient
String getType();

/**
* Gets the scope {@link KapuaId}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@
###############################################################################
UNSUPPORTED_TYPE=The conversion of mapping {0} of value {1} of type {2} with is not supported!
INVALID_VALUE=The value of mapping {0} of value {1} is not compatible with type {2}.
STORABLE_NOT_FOUND=The storable of type {0} with id {1} was not found.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@
})
public interface ChannelInfo extends Storable {

String TYPE = "ChannelInfo";

@Override
default String getType() {
return TYPE;
}

/**
* Gets the {@link StorableId}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@
})
public interface ClientInfo extends Storable {

String TYPE = "ClientInfo";

@Override
default String getType() {
return TYPE;
}

/**
* Gets the {@link StorableId}
*
Expand Down
Loading