Skip to content

Commit

Permalink
🐛 [REST API] Fixed returnNotNullEntity correctly handle null KapuaEnt…
Browse files Browse the repository at this point in the history
…ities and throw KapuaEntityNotFoundException

Signed-off-by: Alberto Codutti <alberto.codutti@eurotech.com>
  • Loading branch information
Coduz committed May 27, 2024
1 parent 44fb3cd commit 611fece
Showing 1 changed file with 29 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
*******************************************************************************/
package org.eclipse.kapua.app.api.core.resources;

import org.eclipse.kapua.KapuaEntityNotFoundException;
import org.eclipse.kapua.model.KapuaEntity;
import org.eclipse.kapua.model.id.KapuaId;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import org.eclipse.kapua.model.KapuaEntity;

import java.net.URI;

/**
Expand All @@ -36,17 +38,40 @@ public abstract class AbstractKapuaResource {
* @param entity
* The {@link KapuaEntity} to check.
* @return The entity given if not <code>null</code>.
* @throws WebApplicationException
* with {@link Status#NOT_FOUND} if the entity is <code>null</code>.
* @throws WebApplicationException with {@link Status#NOT_FOUND} if the entity is <code>null</code>.
* @deprecated Since 2.0.0. To leverage {@link ExceptionMapper}s we need to throw {@link org.eclipse.kapua.KapuaEntityNotFoundException}. Please make use of {@link #returnNotNullEntity(Object, String, KapuaId)}
* @throws WebApplicationException if given entity is {@code null}
* @since 1.0.0
*/
@Deprecated
public <T> T returnNotNullEntity(T entity) {
if (entity == null) {
throw new WebApplicationException(Response.Status.NOT_FOUND);
}
return entity;
}

/**
* Checks id the given {@link KapuaEntity} is {@code null}.
* <p>
* It replaces {@link #returnNotNullEntity(Object)} which was throwing {@link WebApplicationException} instead of {@link KapuaEntityNotFoundException} which is handled by {@link ExceptionMapper}s.
*
* @param entity The {@link KapuaEntity} to check.
* @param entityType The {@link KapuaEntity#getType()}
* @param entityId The {@link KapuaEntity#getId()}
* @return The given entity if not {@code null}
* @param <T> The {@link KapuaEntity} type.
* @throws KapuaEntityNotFoundException if given {@link KapuaEntity} is {@code null}.
* @since 2.0.0
*/
public <T extends KapuaEntity> T returnNotNullEntity(T entity, String entityType, KapuaId entityId) throws KapuaEntityNotFoundException {
if (entity == null) {
throw new KapuaEntityNotFoundException(entityType, entityId);
}

return entity;
}

/**
* Builds a 200 HTTP Response.
*
Expand Down

0 comments on commit 611fece

Please sign in to comment.