Skip to content

Commit

Permalink
✨ [REST API] Implemented StorableNotFoundExceptionMapper and releated…
Browse files Browse the repository at this point in the history
… resources

Signed-off-by: Alberto Codutti <alberto.codutti@eurotech.com>
  • Loading branch information
Coduz committed May 22, 2024
1 parent bfb9611 commit c4e607c
Show file tree
Hide file tree
Showing 11 changed files with 167 additions and 6 deletions.
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);

Check warning on line 29 in commons-rest/errors/src/main/java/org/eclipse/kapua/commons/rest/errors/StorableNotFoundExceptionMapper.java

View check run for this annotation

Codecov / codecov/patch

commons-rest/errors/src/main/java/org/eclipse/kapua/commons/rest/errors/StorableNotFoundExceptionMapper.java#L29

Added line #L29 was not covered by tests

private final boolean showStackTrace;

@Inject
public StorableNotFoundExceptionMapper(ExceptionConfigurationProvider exceptionConfigurationProvider) {
this.showStackTrace = exceptionConfigurationProvider.showStackTrace();
}

Check warning on line 36 in commons-rest/errors/src/main/java/org/eclipse/kapua/commons/rest/errors/StorableNotFoundExceptionMapper.java

View check run for this annotation

Codecov / codecov/patch

commons-rest/errors/src/main/java/org/eclipse/kapua/commons/rest/errors/StorableNotFoundExceptionMapper.java#L34-L36

Added lines #L34 - L36 were not covered by tests

@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();

Check warning on line 44 in commons-rest/errors/src/main/java/org/eclipse/kapua/commons/rest/errors/StorableNotFoundExceptionMapper.java

View check run for this annotation

Codecov / codecov/patch

commons-rest/errors/src/main/java/org/eclipse/kapua/commons/rest/errors/StorableNotFoundExceptionMapper.java#L40-L44

Added lines #L40 - L44 were not covered by tests
}
}
4 changes: 4 additions & 0 deletions commons-rest/model/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
<groupId>org.eclipse.kapua</groupId>
<artifactId>kapua-job-engine-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) 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();
}

Check warning on line 43 in commons-rest/model/src/main/java/org/eclipse/kapua/commons/rest/model/errors/StorableNotFoundExceptionInfo.java

View check run for this annotation

Codecov / codecov/patch

commons-rest/model/src/main/java/org/eclipse/kapua/commons/rest/model/errors/StorableNotFoundExceptionInfo.java#L42-L43

Added lines #L42 - L43 were not covered by tests

/**
* 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);

Check warning on line 53 in commons-rest/model/src/main/java/org/eclipse/kapua/commons/rest/model/errors/StorableNotFoundExceptionInfo.java

View check run for this annotation

Codecov / codecov/patch

commons-rest/model/src/main/java/org/eclipse/kapua/commons/rest/model/errors/StorableNotFoundExceptionInfo.java#L53

Added line #L53 was not covered by tests

this.storableType = storableNotFoundException.getStorableType();
this.storableId = storableNotFoundException.getStorableId();
}

Check warning on line 57 in commons-rest/model/src/main/java/org/eclipse/kapua/commons/rest/model/errors/StorableNotFoundExceptionInfo.java

View check run for this annotation

Codecov / codecov/patch

commons-rest/model/src/main/java/org/eclipse/kapua/commons/rest/model/errors/StorableNotFoundExceptionInfo.java#L55-L57

Added lines #L55 - L57 were not covered by tests

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

Check warning on line 66 in commons-rest/model/src/main/java/org/eclipse/kapua/commons/rest/model/errors/StorableNotFoundExceptionInfo.java

View check run for this annotation

Codecov / codecov/patch

commons-rest/model/src/main/java/org/eclipse/kapua/commons/rest/model/errors/StorableNotFoundExceptionInfo.java#L66

Added line #L66 was not covered by tests
}

/**
* Gets the {@link StorableNotFoundException#getStorableId()}.
*
* @return The {@link StorableNotFoundException#getStorableId()}.
* @since 2.0.0
*/
public StorableId getStorableId() {
return storableId;

Check warning on line 76 in commons-rest/model/src/main/java/org/eclipse/kapua/commons/rest/model/errors/StorableNotFoundExceptionInfo.java

View check run for this annotation

Codecov / codecov/patch

commons-rest/model/src/main/java/org/eclipse/kapua/commons/rest/model/errors/StorableNotFoundExceptionInfo.java#L76

Added line #L76 was not covered by tests
}
}
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;

Check warning on line 61 in rest-api/core/src/main/java/org/eclipse/kapua/app/api/core/model/data/JsonDatastoreMessage.java

View check run for this annotation

Codecov / codecov/patch

rest-api/core/src/main/java/org/eclipse/kapua/app/api/core/model/data/JsonDatastoreMessage.java#L61

Added line #L61 was not covered by tests
}

@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);

Check warning on line 89 in rest-api/core/src/main/java/org/eclipse/kapua/app/api/core/resources/AbstractKapuaResource.java

View check run for this annotation

Codecov / codecov/patch

rest-api/core/src/main/java/org/eclipse/kapua/app/api/core/resources/AbstractKapuaResource.java#L89

Added line #L89 was not covered by tests
}

return storable;

Check warning on line 92 in rest-api/core/src/main/java/org/eclipse/kapua/app/api/core/resources/AbstractKapuaResource.java

View check run for this annotation

Codecov / codecov/patch

rest-api/core/src/main/java/org/eclipse/kapua/app/api/core/resources/AbstractKapuaResource.java#L92

Added line #L92 was not covered by tests
}

/**
* Builds a 200 HTTP Response.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Check warning on line 163 in rest-api/resources/src/main/java/org/eclipse/kapua/app/api/resources/v1/resources/DataChannels.java

View check run for this annotation

Codecov / codecov/patch

rest-api/resources/src/main/java/org/eclipse/kapua/app/api/resources/v1/resources/DataChannels.java#L163

Added line #L163 was not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Check warning on line 145 in rest-api/resources/src/main/java/org/eclipse/kapua/app/api/resources/v1/resources/DataClients.java

View check run for this annotation

Codecov / codecov/patch

rest-api/resources/src/main/java/org/eclipse/kapua/app/api/resources/v1/resources/DataClients.java#L145

Added line #L145 was not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Check warning on line 187 in rest-api/resources/src/main/java/org/eclipse/kapua/app/api/resources/v1/resources/DataMessages.java

View check run for this annotation

Codecov / codecov/patch

rest-api/resources/src/main/java/org/eclipse/kapua/app/api/resources/v1/resources/DataMessages.java#L187

Added line #L187 was not covered by tests
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Check warning on line 208 in rest-api/resources/src/main/java/org/eclipse/kapua/app/api/resources/v1/resources/DataMessagesJson.java

View check run for this annotation

Codecov / codecov/patch

rest-api/resources/src/main/java/org/eclipse/kapua/app/api/resources/v1/resources/DataMessagesJson.java#L208

Added line #L208 was not covered by tests

JsonDatastoreMessage jsonDatastoreMessage = new JsonDatastoreMessage(datastoreMessage);

return returnNotNullEntity(jsonDatastoreMessage);
return returnNotNullStorable(jsonDatastoreMessage, DatastoreMessage.TYPE, datastoreMessageId);

Check warning on line 212 in rest-api/resources/src/main/java/org/eclipse/kapua/app/api/resources/v1/resources/DataMessagesJson.java

View check run for this annotation

Codecov / codecov/patch

rest-api/resources/src/main/java/org/eclipse/kapua/app/api/resources/v1/resources/DataMessagesJson.java#L212

Added line #L212 was not covered by tests
}

private MessageQuery convertQuery(JsonMessageQuery query) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Check warning on line 153 in rest-api/resources/src/main/java/org/eclipse/kapua/app/api/resources/v1/resources/DataMetrics.java

View check run for this annotation

Codecov / codecov/patch

rest-api/resources/src/main/java/org/eclipse/kapua/app/api/resources/v1/resources/DataMetrics.java#L153

Added line #L153 was not covered by tests
}
}

0 comments on commit c4e607c

Please sign in to comment.