Skip to content

Commit

Permalink
🐛 [REST API] Fixed returnNotNullEntity for generic Objects and added …
Browse files Browse the repository at this point in the history
…dedicated WebApplicationExceptionMapper

Signed-off-by: Alberto Codutti <alberto.codutti@eurotech.com>
  • Loading branch information
Coduz committed May 29, 2024
1 parent 5deecec commit 2642077
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*******************************************************************************
* 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.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

/**
* @since 2.0.0
*/
@Provider
public class WebApplicationExceptionMapper implements ExceptionMapper<WebApplicationException> {

private static final Logger LOG = LoggerFactory.getLogger(WebApplicationExceptionMapper.class);

@Inject
public WebApplicationExceptionMapper() {
}

@Override
public Response toResponse(WebApplicationException webApplicationException) {
LOG.error(webApplicationException.getMessage(), webApplicationException);
return Response
.fromResponse(webApplicationException.getResponse())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,45 +16,44 @@
import org.eclipse.kapua.model.KapuaEntity;
import org.eclipse.kapua.model.id.KapuaId;

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

import java.net.URI;

/**
*
* @author alberto.codutti
*
* @since 1.0.0
*/
public abstract class AbstractKapuaResource {

protected static final String DEFAULT_SCOPE_ID = "_"; // KapuaApiSetting.getInstance().getString(KapuaApiSettingKeys.API_PATH_PARAM_SCOPEID_WILDCARD);

/**
* Checks if the given entity is {@code null}.
* If it is <code>null</code> a {@link WebApplicationException} is raised.
* Checks if the given object is {@code null}.
* <p>
* If it is {@code null} a {@link NotFoundException} {@link WebApplicationException} is raised.
* <p>
* For {@link KapuaEntity} {@link #returnNotNullEntity(KapuaEntity, String, KapuaId)} is recommended.
* This is meant for generic {@link Object}s.
*
* @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>.
* @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}
* @param object The {@link Object} to check.
* @return The given {@link Object} if not {@code null}
* @param <T> The type of the given {@link Object}
* @since 1.0.0
* @throws NotFoundException if the given {@link Object} is {@code null}
*/
@Deprecated
public <T> T returnNotNullEntity(T entity) {
if (entity == null) {
throw new WebApplicationException(Response.Status.NOT_FOUND);
public <T> T returnNotNullEntity(T object) {
if (object == null) {
throw new NotFoundException("Requested resource not found");
}
return entity;

return object;
}

/**
* 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()}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,20 @@
import org.junit.Test;
import org.junit.experimental.categories.Category;

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

import javax.ws.rs.NotFoundException;

@Category(JUnitTests.class)
public class AbstractKapuaResourceTest {

private class AbstractKapuaResourceImpl extends AbstractKapuaResource {

private class TestKapuaResource extends AbstractKapuaResource {
}

AbstractKapuaResource abstractKapuaResource;
Object[] objects;

@Before
public void initialize() {
abstractKapuaResource = new AbstractKapuaResourceImpl();
abstractKapuaResource = new TestKapuaResource();
objects = new Object[]{new Object(), "", "string", 10, 'c', KapuaId.ONE, new Throwable(), new ScopeId("111")};
}

Expand All @@ -47,14 +44,9 @@ public void returnNotNullEntityTest() {
}
}

@Test
@Test(expected = NotFoundException.class)
public void returnNotNullEntityNullTest() {
try {
abstractKapuaResource.returnNotNullEntity(null);
Assert.fail("WebApplicationException expected.");
} catch (Exception e) {
Assert.assertEquals("WebApplicationException expected.", new WebApplicationException(Response.Status.NOT_FOUND).toString(), e.toString());
}
abstractKapuaResource.returnNotNullEntity(null);
}

@Test
Expand Down

0 comments on commit 2642077

Please sign in to comment.