2012-06-11 11 views
5

È possibile utilizzare Guice AOP per intercettare un metodo annotato su una risorsa Jersey?Intercettazione del metodo in Jersey con Guice AOP

Ho un'integrazione di Guice configurata correttamente che funziona con Jersey rispetto all'iniezione di dipendenza senza problemi, tuttavia il mio Interceptor configurato non sta intercettando affatto il mio metodo annotato.

web.xml

<listener> 
    <listener-class>my.package.GuiceConfig</listener-class> 
</listener> 
<filter> 
    <filter-name>guiceFilter</filter-name> 
    <filter-class>com.google.inject.servlet.GuiceFilter</filter-class> 
</filter> 
<filter-mapping> 
    <filter-name>guiceFilter</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping> 

modulo di configurazione GuiceConfig

public class GuiceConfig extends GuiceServletContextListener { 

@Override 
protected Injector getInjector() { 
    return Guice.createInjector(new JerseyServletModule() { 

      @Override 
      protected void configureServlets() { 

       bindInterceptor(Matchers.any(), 
           Matchers.annotatedWith(RequiredAuthority.class), 
           new AuthorisationInterceptor()); 

       Map<String, String> params = new HashMap<String, String>(); 
       params.put(JSP_TEMPLATES_BASE_PATH, "/WEB-INF/jsp"); 
       params.put(FEATURE_FILTER_FORWARD_ON_404, "true"); 
       params.put(PROPERTY_PACKAGES, "my.service.package"); 

       filter("/*").through(GuiceContainer.class, params); 
      } 
     }); 
    } 
} 

RequiredAuthority annotazione

@Target(ElementType.METHOD) 
@Retention(RetentionPolicy.RUNTIME) 
public @interface RequiredAuthority { 
    String value(); 
} 

AuthorisationInterceptor aspetto

public class AuthorisationInterceptor implements MethodInterceptor { 

    public Object invoke(MethodInvocation methodInvocation) throws Throwable { 

     // Allow invocation to process or throw an appropriate exception 
    } 
} 

classe di risorse TempResource JAX-RS

@Path("/temp") 
public class TempResource { 

    @GET 
    @Produces(MediaType.APPLICATION_JSON) 
    @RequiredAuthority("PERMISSION") 
    public String getTemp() { 

     // Return resource normally 
    } 
} 
+1

in ritardo alla festa, ma sembra ['@ BindingAnnotation'] (http://google-guice.googlecode.com/git/javadoc/com/google/inject/BindingAnnotation. html) manca anche da 'RequiredAuthority'. –

+0

Grazie, sì anche questo ha causato un problema in quel momento. Vale la pena notare qui. – Kynth

risposta

5

Sembra configureServlets() non sta chiamando:

bind(TempResource.class); 
+0

Grazie, era il bind (TempResource.class) che mi mancava, avevo l'impressione che il parametro PROPERTY_PACKAGES eseguisse la scansione del pacchetto per le risorse. Alla fine non avevo bisogno di @Singleton. – Kynth

+0

Lo stesso problema può verificarsi con @Transactional della gestione di Google-Persist –

Problemi correlati