RE: How use javax.ws.rs.ext.ExceptionMapper in osgi module

thumbnail
Joaquin Cabal, modified 6 Years ago. Regular Member Posts: 106 Join Date: 9/7/09 Recent Posts
Hi! Im trying to use an exception mapper for a osgi module, but is not working.

The problem I think is tha the providers exception mapper are not registered as the jax-rs context. But I dont know how to config it ok.

The code is something like this:
@Provider
public class UnauthorizedUserExceptionMapper implements ExceptionMapper<unauthorizeduserexception> {

&nbsp;&nbsp;&nbsp; @Override
&nbsp;&nbsp;&nbsp; public Response toResponse(UnauthorizedUserException e) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return Response
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .status(Response.Status.UNAUTHORIZED)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .type(MediaType.APPLICATION_JSON)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .entity(e.getMessage())
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .build();
&nbsp;&nbsp;&nbsp; }
}

</unauthorizeduserexception>


The rest services are working ok. I tryed to use the Exceptoin mapper as osgi @Component and also add the object to the Rest application singletons.

But is not working yet.
Any helo on this, how to cacth the exception inside this mapper?

Thanks in advance!
thumbnail
Koen Olaerts, modified 2 Years ago. New Member Posts: 19 Join Date: 12/21/10 Recent Posts

Hello Joaquin,

I came across your question today when investigating the same requirement. Sadly there has been no response on your question yet. Perhaps you have solved it already given it has been 4 years?

After some research I found an answer to our problem. Using the example in this pull request from last year: https://github.com/liferay-appsec/liferay-portal/pull/546/files#diff-4ab4c23659f671466b78053e7413e5d2a1e53a069a3976a77e7ff9022060713d

Configure the @Component annotation as below and your custom ExceptionMapper is used for the specific exception. You also don't need to provide the @Provider annotation.

@Component(
		property = {
				"osgi.jaxrs.application.select=(!(liferay.access.control.disable=true))",
				"osgi.jaxrs.extension=true"
		},
		service = ExceptionMapper.class
)

 

 Next step in my research: is it possible to combine multiple ExceptionMappers in one global class/configuration (iso having separate components per exceptionmapper/exceptiontype). Sort of like how in Spring Boot the @ControllerAdvice works with multiple @ExceptionHandler methods.
Maybe this will bring me to a solution: https://github.com/eugenp/tutorials/blob/master/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/ExceptionHandlingConfig.java. But I still have to investigate, will come back to you on this.
Or maybe you already have some kind of solution for this?