Martynas,
Side Note: Injection of ServletContext is enforced by JAX-RS 1.1 only in case you are running in a servlet based implementation. This happily is the case here. But not that other JAX-RS implementations might not.
Regards
Markus
-----Original Message-----
From: Martin Matula [mailto:martin.matula_at_oracle.com]
Sent: Mittwoch, 9. März 2011 12:36
To: users_at_jersey.java.net
Subject: [Jersey] Re: UriInfo injection at resource initialization time
I see. OK, it is a little more complicated. You can inject
ServletContext (which is app-scoped, so injection on singletons should
work) and get the context path info from there.
Martin
On 9.3.2011 12:10, Martynas Jusevicius wrote:
> You're right, I haven't noticed UriInfo.fromResource() - thanks for the tip.
>
> However it returns relative Resource URI - how do I make it absolute?
> RDF works on absolute URIs.
>
> On Wed, Mar 9, 2011 at 11:24 AM, Markus Karg<karg_at_quipsy.de> wrote:
>> You're right, I missed the word 'static' in his request.
>>
>> So I wonder what his problem actually is, as he could just paste your below code line into his constructor body without any need to pass anything at runtime. Maybe he just missed the existence of the fromResource method.
>>
>> Regards
>> Markus
>>
>> -----Original Message-----
>> From: Martin Matula [mailto:martin.matula_at_oracle.com]
>> Sent: Mittwoch, 9. März 2011 11:18
>> To: users_at_jersey.java.net
>> Subject: [Jersey] Re: UriInfo injection at resource initialization time
>>
>> On 9.3.2011 11:10, Markus Karg wrote:
>>> Martin,
>>>
>>> but that will give the static uri. Doesn't Martynas expect to get the actual uri (including any trailing IDs)?
>> I would be surprised, given he wants to do it in the singleton
>> constructor and he says: "I see no reason why root Resources with static
>> @Path could not have access to their UriInfo at any time - their URI is
>> known right from the start?"
>> Regards,
>> Martin
>>> Regards
>>> Markus
>>>
>>> -----Original Message-----
>>> From: Martin Matula [mailto:martin.matula_at_oracle.com]
>>> Sent: Mittwoch, 9. März 2011 10:58
>>> To: users_at_jersey.java.net
>>> Subject: [Jersey] Re: UriInfo injection at resource initialization time
>>>
>>> Hi Martynas,
>>> UriInfo is specific to the current request (has some properties that
>>> provide info for the specific request). You can't inject such thing to
>>> an instance variable of a singleton. Does not make sense.
>>> I believe UriBuilder.fromResource(TestResource.class).build() is what
>>> you are looking for.
>>> Martin
>>>
>>> On 9.3.2011 2:39, Martynas Jusevicius wrote:
>>>> Hey list,
>>>>
>>>> I'm new here and would like to start with a question :)
>>>>
>>>> I have Resource classes implementing JAX-RS, however I also want to
>>>> combine it with RDF using Jena API.
>>>> Each Resource should have a reference to Individual (RDF resource) -
>>>> and they share the same absolute URI.
>>>> This is a root Resource class:
>>>>
>>>> @Path("")
>>>> @Singleton
>>>> public class TestResource
>>>> {
>>>> private Individual individual = null;
>>>>
>>>> ...
>>>> }
>>>>
>>>> This means Individual has to be initiated at some point, and at that
>>>> point the Resource needs to know its URI to be able to look up
>>>> Individual in the RDF model.
>>>>
>>>> What I've tried (the code goes inside the TestResource defined above):
>>>>
>>>> 1. Injected class field
>>>>
>>>> @Context UriInfo uriInfo;
>>>>
>>>> public TestResource()
>>>> {
>>>> System.out.println("TestResource.uriInfo: " + uriInfo);
>>>> }
>>>>
>>>> Doesn't work - returns null
>>>>
>>>> 2. Injected constructor parameter
>>>>
>>>> public TestResource(@Context UriInfo uriInfo)
>>>> {
>>>> System.out.println("TestResource(uriInfo): " + uriInfo);
>>>> }
>>>>
>>>> Doesn't work - I get
>>>> java.lang.IllegalStateException
>>>> com.sun.jersey.server.impl.ThreadLocalHttpContext.getUriInfo
>>>> as described in
>>>> http://jersey.576304.n2.nabble.com/injecting-UriInfo-in-constructor-td4510740.html
>>>> Same message suggests "access the reference when a request is in
>>>> scope" -- but this is not good enough.
>>>>
>>>> However constructor param is used in JAX-RS tutorial:
>>>> http://wikis.sun.com/display/Jersey/Overview+of+JAX-RS+1.0+Features#OverviewofJAX-RS1.0Features-ConditionalGETsandReturning304%28NotModified%29Responses
>>>>
>>>> 3. @PostConstruct method
>>>>
>>>> @PostConstruct
>>>> public void init()
>>>> {
>>>> System.out.println("@PostConstruct UriInfo: " + uriInfo);
>>>> }
>>>>
>>>> Doesn't work - never gets executed
>>>>
>>>> Am I missing something? It seems as such a common use case - why isn't
>>>> there a simple solution?
>>>> I see no reason why root Resources with static @Path could not have
>>>> access to their UriInfo at any time - their URI is known right from
>>>> the start?
>>>> And why does @PostConstruct not work? I'm running Tomcat 6.0.26.
>>>>
>>>> Thanks,
>>>>
>>>> Martynas
>>>> semantic-web.dk