Skip to content

Commit cb0339b

Browse files
committed
✨ [REST API] Implemented StorableNotFoundExceptionMapper and releated resources
Signed-off-by: Alberto Codutti <[email protected]>
1 parent 3d3277c commit cb0339b

File tree

11 files changed

+167
-6
lines changed

11 files changed

+167
-6
lines changed

commons-rest/errors/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
<groupId>org.eclipse.kapua</groupId>
3232
<artifactId>kapua-endpoint-api</artifactId>
3333
</dependency>
34+
<dependency>
35+
<groupId>org.eclipse.kapua</groupId>
36+
<artifactId>kapua-service-storable-api</artifactId>
37+
</dependency>
3438

3539
<!-- re-declare as provided as our web container will provide this -->
3640
<dependency>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2017, 2022 Eurotech and/or its affiliates and others
3+
*
4+
* This program and the accompanying materials are made
5+
* available under the terms of the Eclipse Public License 2.0
6+
* which is available at https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*
10+
* Contributors:
11+
* Eurotech - initial API and implementation
12+
*******************************************************************************/
13+
package org.eclipse.kapua.commons.rest.errors;
14+
15+
import org.eclipse.kapua.commons.rest.model.errors.StorableNotFoundExceptionInfo;
16+
import org.eclipse.kapua.service.storable.exception.StorableNotFoundException;
17+
import org.slf4j.Logger;
18+
import org.slf4j.LoggerFactory;
19+
20+
import javax.inject.Inject;
21+
import javax.ws.rs.core.Response;
22+
import javax.ws.rs.core.Response.Status;
23+
import javax.ws.rs.ext.ExceptionMapper;
24+
import javax.ws.rs.ext.Provider;
25+
26+
@Provider
27+
public class StorableNotFoundExceptionMapper implements ExceptionMapper<StorableNotFoundException> {
28+
29+
private static final Logger LOG = LoggerFactory.getLogger(StorableNotFoundExceptionMapper.class);
30+
31+
private final boolean showStackTrace;
32+
33+
@Inject
34+
public StorableNotFoundExceptionMapper(ExceptionConfigurationProvider exceptionConfigurationProvider) {
35+
this.showStackTrace = exceptionConfigurationProvider.showStackTrace();
36+
}
37+
38+
@Override
39+
public Response toResponse(StorableNotFoundException kapuaEntityNotFoundException) {
40+
LOG.error(kapuaEntityNotFoundException.getMessage(), kapuaEntityNotFoundException);
41+
return Response
42+
.status(Status.NOT_FOUND)
43+
.entity(new StorableNotFoundExceptionInfo(Status.NOT_FOUND.getStatusCode(), kapuaEntityNotFoundException, showStackTrace))
44+
.build();
45+
}
46+
}

commons-rest/model/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@
3838
<groupId>org.eclipse.kapua</groupId>
3939
<artifactId>kapua-job-engine-api</artifactId>
4040
</dependency>
41+
<dependency>
42+
<groupId>org.eclipse.kapua</groupId>
43+
<artifactId>kapua-service-storable-api</artifactId>
44+
</dependency>
4145

4246
<!-- Testing dependencies -->
4347
<dependency>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2024, 2022 Eurotech and/or its affiliates and others
3+
*
4+
* This program and the accompanying materials are made
5+
* available under the terms of the Eclipse Public License 2.0
6+
* which is available at https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*
10+
* Contributors:
11+
* Eurotech - initial API and implementation
12+
*******************************************************************************/
13+
package org.eclipse.kapua.commons.rest.model.errors;
14+
15+
import org.eclipse.kapua.model.id.KapuaIdAdapter;
16+
import org.eclipse.kapua.service.storable.exception.StorableNotFoundException;
17+
import org.eclipse.kapua.service.storable.model.id.StorableId;
18+
19+
import javax.xml.bind.annotation.XmlAccessType;
20+
import javax.xml.bind.annotation.XmlAccessorType;
21+
import javax.xml.bind.annotation.XmlElement;
22+
import javax.xml.bind.annotation.XmlRootElement;
23+
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
24+
25+
@XmlRootElement(name = "storableNotFoundExceptionInfo")
26+
@XmlAccessorType(XmlAccessType.FIELD)
27+
public class StorableNotFoundExceptionInfo extends ExceptionInfo {
28+
29+
@XmlElement(name = "storableType")
30+
private String storableType;
31+
32+
@XmlElement(name = "storableId")
33+
@XmlJavaTypeAdapter(KapuaIdAdapter.class)
34+
private StorableId storableId;
35+
36+
/**
37+
* Constructor.
38+
*
39+
* @since 2.0.0
40+
*/
41+
protected StorableNotFoundExceptionInfo() {
42+
super();
43+
}
44+
45+
/**
46+
* Constructor.
47+
*
48+
* @param httpStatusCode The http status code of the response containing this info
49+
* @param storableNotFoundException The {@link StorableNotFoundException}.
50+
* @since 2.0.0
51+
*/
52+
public StorableNotFoundExceptionInfo(int httpStatusCode, StorableNotFoundException storableNotFoundException, boolean showStackTrace) {
53+
super(httpStatusCode, storableNotFoundException, showStackTrace);
54+
55+
this.storableType = storableNotFoundException.getStorableType();
56+
this.storableId = storableNotFoundException.getStorableId();
57+
}
58+
59+
/**
60+
* Gets the {@link StorableNotFoundException#getStorableType()}
61+
*
62+
* @return The {@link StorableNotFoundException#getStorableType()}.
63+
* @since 2.0.0
64+
*/
65+
public String getEntityType() {
66+
return storableType;
67+
}
68+
69+
/**
70+
* Gets the {@link StorableNotFoundException#getStorableId()}.
71+
*
72+
* @return The {@link StorableNotFoundException#getStorableId()}.
73+
* @since 2.0.0
74+
*/
75+
public StorableId getStorableId() {
76+
return storableId;
77+
}
78+
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ public JsonDatastoreMessage(DatastoreMessage datastoreMessage) {
5656
setPayload(datastoreMessage.getPayload());
5757
}
5858

59+
@Override
60+
public String getType() {
61+
return DatastoreMessage.TYPE;
62+
}
63+
5964
@XmlElement(name = "datastoreId")
6065
@XmlJavaTypeAdapter(StorableIdXmlAdapter.class)
6166
public StorableId getDatastoreId() {

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
import org.eclipse.kapua.KapuaEntityNotFoundException;
1616
import org.eclipse.kapua.model.KapuaEntity;
1717
import org.eclipse.kapua.model.id.KapuaId;
18+
import org.eclipse.kapua.service.storable.exception.StorableNotFoundException;
19+
import org.eclipse.kapua.service.storable.model.Storable;
20+
import org.eclipse.kapua.service.storable.model.id.StorableId;
1821

1922
import javax.ws.rs.NotFoundException;
2023
import javax.ws.rs.WebApplicationException;
@@ -68,6 +71,27 @@ public <T extends KapuaEntity> T returnNotNullEntity(T entity, String entityType
6871
return entity;
6972
}
7073

74+
/**
75+
* Checks id the given {@link Storable} is {@code null}.
76+
* <p>
77+
* Similar to {@link #returnNotNullEntity(KapuaEntity, String, KapuaId)} but for {@link Storable}s.
78+
*
79+
* @param storable The {@link Storable} to check.
80+
* @param storableType The {@link Storable#getType()}
81+
* @param storableId The {@link StorableId}
82+
* @return The given {@link Storable} if not {@code null}
83+
* @param <T> The type of the {@link Storable}.
84+
* @throws StorableNotFoundException if given {@link Storable} is {@code null}.
85+
* @since 2.0.0
86+
*/
87+
public <T extends Storable> T returnNotNullStorable(T storable, String storableType, StorableId storableId) throws StorableNotFoundException {
88+
if (storable == null) {
89+
throw new StorableNotFoundException(storableType, storableId);
90+
}
91+
92+
return storable;
93+
}
94+
7195
/**
7296
* Builds a 200 HTTP Response.
7397
*

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,6 @@ public ChannelInfo find(@PathParam("scopeId") ScopeId scopeId,
160160
throws KapuaException {
161161
ChannelInfo channelInfo = channelInfoRegistryService.find(scopeId, channelInfoId);
162162

163-
return returnNotNullEntity(channelInfo);
163+
return returnNotNullStorable(channelInfo, ChannelInfo.TYPE, channelInfoId);
164164
}
165165
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,6 @@ public ClientInfo find(@PathParam("scopeId") ScopeId scopeId,
142142
throws KapuaException {
143143
ClientInfo clientInfo = clientInfoRegistryService.find(scopeId, clientInfoId);
144144

145-
return returnNotNullEntity(clientInfo);
145+
return returnNotNullStorable(clientInfo, ClientInfo.TYPE, clientInfoId);
146146
}
147147
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public DatastoreMessage find(@PathParam("scopeId") ScopeId scopeId,
184184
throws KapuaException {
185185
DatastoreMessage datastoreMessage = messageStoreService.find(scopeId, datastoreMessageId, StorableFetchStyle.SOURCE_FULL);
186186

187-
return returnNotNullEntity(datastoreMessage);
187+
return returnNotNullStorable(datastoreMessage, DatastoreMessage.TYPE, datastoreMessageId);
188188
}
189189

190190

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,11 +205,11 @@ public JsonMessageListResult queryJson(@PathParam("scopeId") ScopeId scopeId,
205205
public JsonDatastoreMessage findJson(@PathParam("scopeId") ScopeId scopeId,
206206
@PathParam("datastoreMessageId") StorableEntityId datastoreMessageId)
207207
throws KapuaException {
208-
DatastoreMessage datastoreMessage = returnNotNullEntity(messageStoreService.find(scopeId, datastoreMessageId, StorableFetchStyle.SOURCE_FULL));
208+
DatastoreMessage datastoreMessage = returnNotNullStorable(messageStoreService.find(scopeId, datastoreMessageId, StorableFetchStyle.SOURCE_FULL), DatastoreMessage.TYPE, datastoreMessageId);
209209

210210
JsonDatastoreMessage jsonDatastoreMessage = new JsonDatastoreMessage(datastoreMessage);
211211

212-
return returnNotNullEntity(jsonDatastoreMessage);
212+
return returnNotNullStorable(jsonDatastoreMessage, DatastoreMessage.TYPE, datastoreMessageId);
213213
}
214214

215215
private MessageQuery convertQuery(JsonMessageQuery query) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,6 @@ public MetricInfo find(@PathParam("scopeId") ScopeId scopeId,
150150
throws KapuaException {
151151
MetricInfo metricInfo = metricInfoRegistryService.find(scopeId, metricInfoId);
152152

153-
return returnNotNullEntity(metricInfo);
153+
return returnNotNullStorable(metricInfo, MetricInfo.TYPE, metricInfoId);
154154
}
155155
}

0 commit comments

Comments
 (0)