users@jersey.java.net

Re: [Jersey] Guice / Reference to WebApplication

From: Christopher Piggott <cpiggott_at_gmail.com>
Date: Wed, 2 Jun 2010 14:59:43 -0400

I'm not so sure about the second part, but I'm using Guice and what
you are asking about worked for me:

public class RootResource {
    @Context private UriInfo uriInfo; // injected by jersey/servlet/etc
    @Inject @Named("DataDir") private File dataDir; // Injected by Guice
    ...
}

and that works fine for me.

When I created my injector, I gave it bindings for this class:

public class MyServletModule extends ServletModule {
   ...
   @Override
   protected void configureServlets() {
      ...
      bind(RootResource.class);
   }
}

but I didn't do anything else.



On Wed, Jun 2, 2010 at 2:07 PM, Johannes Schneider
<mailings_at_cedarsoft.com> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> I used a profiler to find all references to WebApplication. Just found
> one... And with some dirty hacks it resulted in that module configuration:
>
>
>    @Singleton
>    @Provides
>    public WebApplication webApp( @NotNull GuiceContainer guiceContainer ) {
>      WebComponent component = Reflection.field( "webComponent"
> ).ofType( WebComponent.class ).in( guiceContainer ).get();
>      WebApplication webApplication = Reflection.field( "application"
> ).ofType( WebApplication.class ).in( component ).get();
>      return webApplication;
>    }
>
>    @RequestScoped
>    @Provides
>    public HttpContext httpContext( @NotNull WebApplication
> webApplication ) {
>      return webApplication.getThreadLocalHttpContext();
>    }
>
>    @RequestScoped
>    @Provides
>    public UriInfo uriInfo( @NotNull HttpContext httpContext ) {
>      return httpContext.getUriInfo();
>    }
>    @RequestScoped
>    @Provides
>    public HttpRequestContext requestContext( HttpContext httpContext ) {
>      return httpContext.getRequest();
>    }
>    @RequestScoped
>    @Provides
>    public HttpHeaders httpHeaders( HttpContext httpContext ) {
>      return httpContext.getRequest();
>    }
>    @RequestScoped
>    @Provides
>    public Request request( HttpContext httpContext ) {
>      return httpContext.getRequest();
>    }
>    @RequestScoped
>    @Provides
>    public SecurityContext securityContext( HttpContext httpContext ) {
>      return httpContext.getRequest();
>    }
>
>
> On 06/02/2010 07:39 PM, Johannes Schneider wrote:
>> At the moment I am struggling with the Guice integration.
>>
>> It is working based on that blog entry:
>> http://zcox.wordpress.com/2009/09/22/jersey-guice-scala/
>>
>>
>> When I mark an constructor of a resources with @com.google.inject.Inject
>> it is injected using Guice.
>> But I also want to add some context stuff (UriInfo) etc.
>>
>> Therefore I tried to add some bindings to Guice. But I don't know how I
>> can get the current UriInfo. I have found that code:
>> http://pastie.org/521175
>> But I think that is wrong, since there an additional instance of
>> WebApplicationImpl is created (or am I missing something?).
>>
>> So is there any possibility to get the reference to the WebApplication?
>> Any magic/static method?
>>
>>
>>
>> I could extend com.sun.jersey.guice.spi.container.servlet.GuiceContainer
>> and use the instance passed to #initiate. But that looks a little bit
>> dirty because it is too late. Guice has still been created (see the
>> injected Injector to the constructor).
>>
>> Any hints?
>>
>>
>>
>> Thanks,
>>
>> Johannes
>
> - ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>
>
> - --
> Johannes Schneider - blog.cedarsoft.com
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.10 (GNU/Linux)
>
> iQEcBAEBAgAGBQJMBp3IAAoJEAytD9R7Qv6dcFIH/30TTyH3EZFRkaV/FIbDkoom
> PLtT24BLSK3Qvy0E0oApRh/1tOWZ5UVNVLH8wQ7fOXVUNqfOj8HUao8CPt9ERuzu
> EVsTNuRT0i+u72Q3qCDItXsmBkMXyJ7YRT0RHP8y6Ddr0fQFbodXTHYDSf+mfGbd
> uXPpjhV/zjWpVSu5G7jCpqMRhiSiDLJ58upJBwWbcRmx1HkEV5P735IwpPUxQbD5
> ZTCY8qVEfCvLLm7wwSiD7kmOWogyoL2UzI8C5sMKFIpgI4MIr8rd5WLTKCWeu77b
> YqaCrr5SP5vw1CeqGe/2zzak4shyWR7xyMYpabDzAKrNqUbo9uK+kh3MudkuUuM=
> =Q+FF
> -----END PGP SIGNATURE-----
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>
>