1 Reply Latest reply on Apr 13, 2010 10:33 AM by Ananta Amalkar

    Need Help -- Exception Mapper for http exception does not work resteasy

    Ananta Amalkar Newbie

      Hi:

       

      I'm implementing exception mapper so taht my application will throw approriate http error code.

       

      here is my implementation:

       

      1. MyRestApplication.java

       

      public class MyRestApplication extends Application{

       

          private Set<Object> singletons = new HashSet<Object>();
          private Set<Class<?>> classes = new HashSet<Class<?>>();

       

         
          public MyRestApplication() {
            

       

              classes.add(MyRestEasyExceptionMapper.class);

       

              classes.add(UnauthorizedExceptionMapper.class);   

          }
         
         
          @Override
          public Set<Class<?>> getClasses() {
              return classes;
          }

       

          /**
           * Return a set of rest easy implementation classes.
           */
          @Override
          public Set<Object> getSingletons() {
              return singletons;
          }
      }

       

      2. UnauthorizedExceptionMapper.java

       

      @Provider
      public class UnauthorizedExceptionMapper implements ExceptionMapper<UnauthorizedException> {

       

          public Response toResponse(UnauthorizedException e)
             {
              return Response.status(Status.UNAUTHORIZED)
              .entity(e)
              .build();
              }

       

      }

       

      3. MyRestEasyExceptionMapper.java

       

      @Provider
      public class MyRestEasyExceptionMapper implements ExceptionMapper<RuntimeException>{

       


          private @Context Providers providers;

       


             @SuppressWarnings("unchecked")
          public Response toResponse(RuntimeException exception)
             {
                if (exception.getCause() == null)
                {
                   return Response.serverError().build();
                }
                Class cause = exception.getCause().getClass();
                ExceptionMapper mapper = null;
                try{
                 mapper = providers.getExceptionMapper(cause);   
                }catch(Exception e){
                    System.out.println(e);
                }
              
                if (mapper == null)
                {
                   return Response.serverError().build();
                }
                else
                {
                   return mapper.toResponse(exception.getCause());
                }
             }

       

      }

       

       

      I have registred the mappers using MyRestApplication. My method throws "org.jboss.resteasy.spi.UnauthorizedException", which is wrapped up by RuntimeException, like:

       

      Class A{

       

      method a() thorws RuntimeException{

      Class B b = new B();

      b.validateAccess();

       

      }

       

       

      Class B{

       

      method validateAccess() throws UnauthorizedException{

      try{

      // validate access

      }Catch(RuntimeException e){

           throw new UnauthorizedException(e.getMessage(), e);

      }

      }

      }

       

       

      It works well till it gets to find the MyRestEasyExceptionMapper. When control goes inside toResponse method of MyRestEasyExceptionMapper,

       

      I can see the Class cause = exception.getCause().getClass(); is UnauthorizedException, which is correct.

       

      So it means, the cause of the RuntimeException is unauthorisedexception, but then it can not find the mapper for this unauthorisedexception.

      mapper = providers.getExceptionMapper(cause);  this comes as null.

       

      I'm not able to figure out what is wrong here?

       

       

      For RuntimeException, it finds the correct mapper, but then from the mapper class of runtimeexception, it can not find the mapper for unauthorisedexception.

       

      Any help will be highly appreciated here.

       

       

       

      NOTE: I do see some deployment errors:

       

      23:25:44,671 ERROR [STDERR] 4094 [main] INFO org.jboss.resteasy.spi.ResteasyDeployment - Adding class @Provider common.service.ex
      ception.NotAcceptableExceptionHandler from Application javax.ws.rs.core.Application
      23:25:44,671 ERROR [STDERR] 4094 [main] INFO org.jboss.resteasy.spi.ResteasyDeployment - Adding class @Provider common.service.ex
      ception.MyRestEasyExceptionMapper from Application javax.ws.rs.core.Application

      23:25:44,687 ERROR [STDERR] 4110 [main] INFO org.jboss.resteasy.spi.ResteasyDeployment - Adding class @Provider common.service.ex
      ception.UnauthorizedExceptionMapper from Application javax.ws.rs.core.Application

       

      From web.xml

       

      <context-param>
              <param-name>javax.ws.rs.Application</param-name>
              <param-value>common.services.MyRestApplication</param-value>
          </context-param>

       

      <listener>
              <listener-class>
                  org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
              </listener-class>
          </listener>