Making exception mapper working
This commit is contained in:
parent
3addec0a27
commit
f71b7d1620
|
@ -1,57 +0,0 @@
|
|||
package org.gcube.service.helloworld.mappers;
|
||||
|
||||
|
||||
import org.gcube.service.helloworld.beans.ResponseBean;
|
||||
|
||||
import jakarta.ws.rs.ForbiddenException;
|
||||
import jakarta.ws.rs.WebApplicationException;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import jakarta.ws.rs.core.Response.Status;
|
||||
import jakarta.ws.rs.ext.ExceptionMapper;
|
||||
import jakarta.ws.rs.ext.Provider;
|
||||
|
||||
/**
|
||||
* @author Alfredo Oliviero (ISTI - CNR)
|
||||
*/
|
||||
@Provider
|
||||
public class ForbiddenExceptionMapper implements ExceptionMapper<ForbiddenException> {
|
||||
|
||||
@Override
|
||||
public Response toResponse(ForbiddenException exception) {
|
||||
|
||||
Status status = Status.INTERNAL_SERVER_ERROR;
|
||||
String exceptionMessage = exception.getMessage();
|
||||
|
||||
ResponseBean responseBean = null;
|
||||
|
||||
try {
|
||||
if (exception.getCause() != null) {
|
||||
exceptionMessage = exception.getCause().getMessage();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
exceptionMessage = exception.getMessage();
|
||||
}
|
||||
|
||||
MediaType mediaType = MediaType.TEXT_PLAIN_TYPE;
|
||||
|
||||
if (WebApplicationException.class.isAssignableFrom(exception.getClass())) {
|
||||
Response gotResponse = ((WebApplicationException) exception).getResponse();
|
||||
Object entity = gotResponse.getEntity();
|
||||
if (entity != null && ResponseBean.class.isAssignableFrom(entity.getClass())) {
|
||||
responseBean = (ResponseBean) entity;
|
||||
}
|
||||
status = Status.fromStatusCode(gotResponse.getStatusInfo().getStatusCode());
|
||||
}
|
||||
|
||||
if (responseBean == null) {
|
||||
responseBean = new ResponseBean();
|
||||
}
|
||||
responseBean.setSuccess(false);
|
||||
responseBean.setMessage(exceptionMessage);
|
||||
// responseBean.set
|
||||
|
||||
return Response.status(status).entity(responseBean).type(mediaType).build();
|
||||
}
|
||||
|
||||
}
|
|
@ -1,7 +1,9 @@
|
|||
package org.gcube.service.helloworld.rest;
|
||||
|
||||
import jakarta.ws.rs.GET;
|
||||
import jakarta.ws.rs.NotFoundException;
|
||||
import jakarta.ws.rs.Path;
|
||||
import jakarta.ws.rs.WebApplicationException;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
@ -28,4 +30,10 @@ public class ExcludeAuthorizationService {
|
|||
logger.info("executed whithout any authorization");
|
||||
return "executed whithout any authorization";
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("exception")
|
||||
public String exc() throws WebApplicationException {
|
||||
throw new NotFoundException();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue