jersey
  1. jersey
  2. JERSEY-801

Issue throwing WebApplicationException derived exception which include an entity value

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.9.1
    • Fix Version/s: 1.18
    • Component/s: None
    • Labels:
      None
    • Environment:

      Windows 7 Ultimate.

      Description

      The exception through is a pretty straight forward one. The message of the exception goes to the response entity member which is the common practice in the Jersey community. The code throwing my custom exception is:
      if (authentication == null) {
      throw new MappableContainerException(new AuthenticationException("Authentication credentials are required\r\n", "MyRealm"));

      I get the following stack exception:
      Nov 2, 2011 1:14:20 PM com.sun.jersey.server.impl.application.WebApplicationImpl _handleRequest
      SEVERE: The RuntimeException could not be mapped to a response, re-throwing to the HTTP container
      java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
      at java.util.LinkedList.entry(LinkedList.java:365)
      at java.util.LinkedList.get(LinkedList.java:315)
      at com.sun.jersey.server.linking.impl.LinkProcessor.getLinkHeaderValues(LinkProcessor.java:80)
      at com.sun.jersey.server.linking.impl.LinkProcessor.processLinkHeaders(LinkProcessor.java:73)
      at com.sun.jersey.server.linking.LinkFilter.filter(LinkFilter.java:95)
      at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1421)
      at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)
      at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)
      at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
      at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
      at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:708)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
      at com.sun.grizzly.http.servlet.FilterChainImpl.doFilter(FilterChainImpl.java:195)
      at com.sun.grizzly.http.servlet.FilterChainImpl.invokeFilterChain(FilterChainImpl.java:139)
      at com.sun.grizzly.http.servlet.ServletAdapter.doService(ServletAdapter.java:376)
      at com.sun.grizzly.http.servlet.ServletAdapter.service(ServletAdapter.java:324)
      at com.sun.grizzly.tcp.http11.GrizzlyAdapter.service(GrizzlyAdapter.java:166)
      at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)
      at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693)
      at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)
      at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)
      at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135)
      at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)
      at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)
      at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
      at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)
      at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
      at com.sun.grizzly.ContextTask.run(ContextTask.java:69)
      at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
      at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
      at java.lang.Thread.run(Thread.java:662)

      It would seem that on getLinkHeaderValues we should check if we even have a valid resource. Instead:
      Object resource = uriInfo.getMatchedResources().get(0);
      Use:
      if (uriInfo.getMatchedResources().isEmpty())
      {
      return new ArrayList<String>();
      }

      I'm not sure how to contribute the fix to the SVN. I've changed it locally.

        Activity

        Hide
        gabrielkohen added a comment -

        Sorry. The actual fix should be in the LinkFilter class on the filter method. We should probably check if there is a valid resource to work with:

         
            public ContainerResponse filter(ContainerRequest request, ContainerResponse response) {
                Object entity  = response.getEntity();
         
                if (entity != null&&!uriInfo.getMatchedResources().isEmpty()) {
                    Class<?> entityClass = entity.getClass();
                    LinkProcessor lhp = new LinkProcessor(entityClass);
                    lhp.processLinkHeaders(entity, uriInfo, response.getHttpHeaders());
                    RefProcessor lp = new RefProcessor(entityClass);
                    lp.processLinks(entity, uriInfo);
                }
                return response;
            }
        
        Show
        gabrielkohen added a comment - Sorry. The actual fix should be in the LinkFilter class on the filter method. We should probably check if there is a valid resource to work with:   public ContainerResponse filter(ContainerRequest request, ContainerResponse response) { Object entity = response.getEntity(); if (entity != null &&!uriInfo.getMatchedResources().isEmpty()) { Class <?> entityClass = entity.getClass(); LinkProcessor lhp = new LinkProcessor(entityClass); lhp.processLinkHeaders(entity, uriInfo, response.getHttpHeaders()); RefProcessor lp = new RefProcessor(entityClass); lp.processLinks(entity, uriInfo); } return response; }
        Hide
        Andreas Klein added a comment - - edited

        Having the same issue in 1.12.

        I wish to use com.sun.jersey.server.linking.LinkFilter by including it in my ContainerResponseFilters chain.

        At the same time I use a custom ContainerRequestFilter to handle authentication and authorization.

        Now if I try to throw Exceptions inside that request filter and have them handled by a registered ExceptionMapper, which shall include the exception's message String as entity (i.e. response body), then LinkFilter, or rather LinkProcessor's getLinkHeaderValues() method, throws the mentioned IndexOutOfBoundsException.

        Handling exceptions thrown from inside resource methods works fine, the problem only comes up when an exception is to be processed earlier on, in the request filter chain. I guess gabrielkohen's proposed fix should do the trick.

        Show
        Andreas Klein added a comment - - edited Having the same issue in 1.12. I wish to use com.sun.jersey.server.linking.LinkFilter by including it in my ContainerResponseFilters chain. At the same time I use a custom ContainerRequestFilter to handle authentication and authorization. Now if I try to throw Exceptions inside that request filter and have them handled by a registered ExceptionMapper, which shall include the exception's message String as entity (i.e. response body), then LinkFilter, or rather LinkProcessor's getLinkHeaderValues() method, throws the mentioned IndexOutOfBoundsException. Handling exceptions thrown from inside resource methods works fine, the problem only comes up when an exception is to be processed earlier on, in the request filter chain. I guess gabrielkohen's proposed fix should do the trick.

          People

          • Assignee:
            Michal Gajdos
            Reporter:
            gabrielkohen
          • Votes:
            6 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Time Tracking

              Estimated:
              Original Estimate - 6 hours
              6h
              Remaining:
              Remaining Estimate - 0 minutes
              0m
              Logged:
              Time Spent - 1 hour, 30 minutes Time Not Required
              1h 30m