jersey
  1. jersey
  2. JERSEY-1694

Not only globally bound interceptor in the ContainerResponse chain is invoked when exception is thrown

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Blocker Blocker
    • Resolution: Fixed
    • Affects Version/s: 2.0-m12
    • Fix Version/s: 2.0-rc1, 2.0
    • Component/s: core
    • Labels:
      None

      Description

      Spec. Section 6.7, says:

      1) If a web resource had been matched before the exception was thrown, then all the filters in the ContainerResponse
      chain for that resource MUST be invoked;
      2) Otherwise, only globally bound filters in the ContainerResponse chain MUST be invoked.

      Assuming this holds for both filters and interceptors,
      throwing exception in @PreMathing filter, catching in ExceptionMapper, all interceptors are invoked:

      @Provider
      @PreMatching
      public class PreMatchingThrowingFilter implements ContainerRequestFilter {
      	public static final String EXCEPTION_FIRING_HEADER = "PreMatchingFilterThrowsException";
      
      	@Override
      	public void filter(ContainerRequestContext requestContext)
      			throws IOException {
      		String header = requestContext.getHeaderString(EXCEPTION_FIRING_HEADER);
      		if (header != null)
      			throw new RuntimeException(header); //throws
      	}
      
      }
      
      @Provider
      public class RuntimeExceptionMapper implements ExceptionMapper<RuntimeException> {
      	@Override
      	public Response toResponse(RuntimeException exception) {
      		String message = exception.getMessage();
      		return Response.ok(message).build(); //Catch and return response
      	}
      }
      
      @Provider
      @Logged
      public class AddInterceptor implements
      		WriterInterceptor {
      	private int amount;
      	public AbstractAddInterceptor() {
      		this.amount = 1;
      	}
      
      	@Override
      	public void aroundWriteTo(WriterInterceptorContext context)
      			throws IOException, WebApplicationException {
      		String entity = (String) context.getEntity();
      		Integer i = Integer.parseInt(entity);
      		entity = String.valueOf(i + amount);
      		context.setEntity(entity);
      		context.proceed(); //Add one
      	}
      }
      

      With request:

      >> POST http://localhost:6080/echo // not important
      >> Accept:/, PreMatchingFilterThrowsException:100, Content-Type:/, // headers are important
      >> 0 // entity not important

      The response is 101, the name bound interceptor is called.

        Activity

        There are no comments yet on this issue.

          People

          • Assignee:
            Miroslav Fuksa
            Reporter:
            jan.supol
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Time Tracking

              Estimated:
              Original Estimate - 3 hours Original Estimate - 3 hours
              3h
              Remaining:
              Remaining Estimate - 0 minutes
              0m
              Logged:
              Time Spent - 16 hours
              16h