jersey
  1. jersey
  2. JERSEY-2097

ContainerResponseFilter and ContainerRequestFilter never get executed

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Blocker Blocker
    • Resolution: Invalid
    • Affects Version/s: 2.2
    • Fix Version/s: None
    • Component/s: None
    • Labels:
      None
    • Environment:

      Linux, stable Jersey from getting-started guide (mvn)

      Description

      Following the getting started guide on your website:

      https://jersey.java.net/documentation/latest/getting-started.html

      I executed the following build command:

      $ mvn archetype:generate -DarchetypeArtifactId=jersey-quickstart-grizzly2 \
      -DarchetypeGroupId=org.glassfish.jersey.archetypes -DinteractiveMode=false \
      -DgroupId=com.example -DartifactId=simple-service -Dpackage=com.example \
      -DarchetypeVersion=2.2

      I then followed your tutorial on

      https://jersey.java.net/documentation/latest/filters-and-interceptors.html#d0e6783

      to add a custom ContainerResponseFilter:

      @NameBinding
      @Retention(RetentionPolicy.RUNTIME)
      static @interface CORSBinding {}

      @Provider
      @Priority(Priorities.HEADER_DECORATOR)
      @CORSBinding
      static class CrossDomainFilter implements ContainerResponseFilter {
      @Override
      public void filter(ContainerRequestContext creq, ContainerResponseContext cres) {
      Logger.getLogger("com.example").log( Level.INFO, "before:

      Unknown macro: {0}

      ", cres.getHeaders());
      cres.getHeaders().add("Access-Control-Allow-Origin", "*");
      cres.getHeaders().add("Access-Control-Allow-Headers", "origin, content-type, accept, authorization");
      cres.getHeaders().add("Access-Control-Allow-Credentials", "true");
      cres.getHeaders().add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD");
      cres.getHeaders().add("Access-Control-Max-Age", "1209600");
      Logger.getLogger("com.example").log( Level.INFO, "after:

      ", cres.getHeaders());
      }
      }

      I tried to register this filter with Named Binding and with Dynamic Binding, nothing works.

      To easily reproduce, I also tried an example from your sources:

      https://github.com/jersey/jersey/tree/2.2/examples/exception-mapping

      The same problem: the custom filters do not get executed.

        Activity

        Hide
        Jakub Podlesak added a comment -

        Just tried to reproduce, following is listing of my types:

        com/example/CORSBinding.java:

        package com.example;
        
        import java.lang.annotation.Retention;
        import java.lang.annotation.RetentionPolicy;
        import javax.ws.rs.NameBinding;
        
        @NameBinding
        @Retention(RetentionPolicy.RUNTIME)
        public @interface CORSBinding {}
        

        /com/example/CrossDomainFilter.java:

        package com.example;
        
        import java.util.logging.Level;
        import java.util.logging.Logger;
        import javax.annotation.Priority;
        import javax.ws.rs.Priorities;
        import javax.ws.rs.container.ContainerRequestContext;
        import javax.ws.rs.container.ContainerResponseContext;
        import javax.ws.rs.container.ContainerResponseFilter;
        import javax.ws.rs.ext.Provider;
        
        @Provider
        @Priority(Priorities.HEADER_DECORATOR)
        @CORSBinding
        public class CrossDomainFilter implements ContainerResponseFilter {
        
            @Override
            public void filter(ContainerRequestContext creq, ContainerResponseContext cres) {
                Logger.getLogger("com.example").log(Level.INFO, "before:", cres.getHeaders());
                cres.getHeaders().add("Access-Control-Allow-Origin", "*");
                cres.getHeaders().add("Access-Control-Allow-Headers", "origin, content-type, accept, authorization");
                cres.getHeaders().add("Access-Control-Allow-Credentials", "true");
                cres.getHeaders().add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD");
                cres.getHeaders().add("Access-Control-Max-Age", "1209600");
                Logger.getLogger("com.example").log(Level.INFO, "after:", cres.getHeaders());
            }
        }
        

        com/example/MyResource.java:

        package com.example;
        
        import javax.ws.rs.GET;
        import javax.ws.rs.Path;
        import javax.ws.rs.Produces;
        import javax.ws.rs.core.MediaType;
        
        @Path("myresource")
        public class MyResource {
        
            @GET
            @Produces(MediaType.TEXT_PLAIN)
            public String getIt() {
                return "Got it!";
            }
        
            @GET
            @CORSBinding
            @Path("cors")
            public String getCors() {
                return "cors";
            }
        }
        

        Here is curl output:

        %curl -i http://localhost:8080/myapp/myresource
        HTTP/1.1 200 OK
        Content-Type: text/plain
        Date: Tue, 15 Oct 2013 14:39:17 GMT
        Content-Length: 7
        
        Got it!
        %curl -i http://localhost:8080/myapp/myresource/cors
        HTTP/1.1 200 OK
        Access-Control-Allow-Origin: *
        Access-Control-Allow-Headers: origin, content-type, accept, authorization
        Access-Control-Allow-Credentials: true
        Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS, HEAD
        Access-Control-Max-Age: 1209600
        Content-Type: text/plain
        Date: Tue, 15 Oct 2013 14:39:20 GMT
        Content-Length: 4
        
        cors
        
        Show
        Jakub Podlesak added a comment - Just tried to reproduce, following is listing of my types: com/example/CORSBinding.java: package com.example; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import javax.ws.rs.NameBinding; @NameBinding @Retention(RetentionPolicy.RUNTIME) public @ interface CORSBinding {} /com/example/CrossDomainFilter.java: package com.example; import java.util.logging.Level; import java.util.logging.Logger; import javax.annotation.Priority; import javax.ws.rs.Priorities; import javax.ws.rs.container.ContainerRequestContext; import javax.ws.rs.container.ContainerResponseContext; import javax.ws.rs.container.ContainerResponseFilter; import javax.ws.rs.ext.Provider; @Provider @Priority(Priorities.HEADER_DECORATOR) @CORSBinding public class CrossDomainFilter implements ContainerResponseFilter { @Override public void filter(ContainerRequestContext creq, ContainerResponseContext cres) { Logger.getLogger( "com.example" ).log(Level.INFO, "before:" , cres.getHeaders()); cres.getHeaders().add( "Access-Control-Allow-Origin" , "*" ); cres.getHeaders().add( "Access-Control-Allow-Headers" , "origin, content-type, accept, authorization" ); cres.getHeaders().add( "Access-Control-Allow-Credentials" , " true " ); cres.getHeaders().add( "Access-Control-Allow-Methods" , "GET, POST, PUT, DELETE, OPTIONS, HEAD" ); cres.getHeaders().add( "Access-Control-Max-Age" , "1209600" ); Logger.getLogger( "com.example" ).log(Level.INFO, "after:" , cres.getHeaders()); } } com/example/MyResource.java: package com.example; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; @Path( "myresource" ) public class MyResource { @GET @Produces(MediaType.TEXT_PLAIN) public String getIt() { return "Got it!" ; } @GET @CORSBinding @Path( "cors" ) public String getCors() { return "cors" ; } } Here is curl output: %curl -i http: //localhost:8080/myapp/myresource HTTP/1.1 200 OK Content-Type: text/plain Date: Tue, 15 Oct 2013 14:39:17 GMT Content-Length: 7 Got it! %curl -i http: //localhost:8080/myapp/myresource/cors HTTP/1.1 200 OK Access-Control-Allow-Origin: * Access-Control-Allow-Headers: origin, content-type, accept, authorization Access-Control-Allow-Credentials: true Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS, HEAD Access-Control-Max-Age: 1209600 Content-Type: text/plain Date: Tue, 15 Oct 2013 14:39:20 GMT Content-Length: 4 cors
        Hide
        Jakub Podlesak added a comment -

        I.e. please note i have made all the types you want to register as Jersey components public.

        Show
        Jakub Podlesak added a comment - I.e. please note i have made all the types you want to register as Jersey components public.

          People

          • Assignee:
            Jakub Podlesak
            Reporter:
            kithouna
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: