You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Simon Chen <si...@gmail.com> on 2011/04/11 22:27:35 UTC

object self-report uri path

Hi all,

I'm wondering if there is a way for an object in the web service to
somehow self-report where it is, in term of uri path?

For example, we have:

@path("/webstore/")
class WebStore {
  @path("/customers/")
  List<Customer> customers;
}

Given a link "cl" to a Customer object, can we somehow figure out that
it is under "/webstore/customers"? I understand that we can probably
easily hard-code this, but I want to do it more automatically...

Thanks.
-SImon

Re: object self-report uri path

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi Simon

thanks for the confirmation. I did have some exposure to EMF awhile back, it
took me awhile to understand how advanced and sophisticated that technogy
was, but one thing I did remember was that it was using proxies a lot :-)
Exposing EMF models over plain HTTP is an interesting and promising idea

Cheers, Sergey

On Wed, Apr 13, 2011 at 5:15 PM, Simon Chen <si...@gmail.com> wrote:

> It's finally working, all thanks to Sergey :-)
>
> Basically, the problem comes from the proxification of EMF, which
> prevents CXF from finding the fields to play with. Sergey's solution,
> which works nicely on my project, is as follows:
> 1) define an interface:
> public interface IContext {
>    @Context
>    public void setUriInfo(UriInfo ui);
> }
>
> 2) let the base resource class implement this interface, for me I have:
>
> class WebStore implements IContext {
>  protected UriInfo uriInfo;
>  public void setUriInfo(UriInfo ui) {
>    this.uriInfo = ui;
>  }
> }
>
> 3) Enjoy!
>
> Thanks.
> -Simon
>
>
> On Tue, Apr 12, 2011 at 4:58 PM, Sergey Beryozkin <sb...@gmail.com>
> wrote:
> > Hi Simon
> >
> > On Tue, Apr 12, 2011 at 9:01 PM, Simon Chen <si...@gmail.com>
> wrote:
> >>
> >> Hi Sergey,
> >>
> >> I tried adding "@context UriInfo uriInfo;" into my class definition,
> >> however, when I try to get uri information from the uriInfo object, I
> >> always get a NullPointerException. Do I need to initialize the uriInfo
> >> in any way?
> >>
> >
> > Is it the root resource that you add the Context fields to ? Do you mean
> the
> > context field is simply not initialized and this causes NPE ? How do you
> > register this resource. do you have Spring AOP involved ?
> >
> > Cheers. Sergey
> >
> >>
> >> I don't think this example here did anything special:
> >>
> http://download.oracle.com/docs/cd/E19776-01/820-4867/6nga7f5og/index.html
> >>
> >> Just to give some context, I am trying to embed some URI information
> >> into the XML serialization. I am doing @XmlIDREF and @XmlElement
> >> markup for any non-containment references. I thought that in addition
> >> to print out a name (marked by @XmlID), it might be useful to give the
> >> associated URI as well.
> >>
> >> Thanks.
> >> -Simon
> >>
> >> On Mon, Apr 11, 2011 at 5:08 PM, Sergey Beryozkin <sberyozkin@gmail.com
> >
> >> wrote:
> >> > Hi
> >> >
> >> > On Mon, Apr 11, 2011 at 9:27 PM, Simon Chen <si...@gmail.com>
> >> > wrote:
> >> >>
> >> >> Hi all,
> >> >>
> >> >> I'm wondering if there is a way for an object in the web service to
> >> >> somehow self-report where it is, in term of uri path?
> >> >>
> >> >> For example, we have:
> >> >>
> >> >> @path("/webstore/")
> >> >> class WebStore {
> >> >>  @path("/customers/")
> >> >>  List<Customer> customers;
> >> >> }
> >> >>
> >> >> Given a link "cl" to a Customer object, can we somehow figure out
> that
> >> >> it is under "/webstore/customers"? I understand that we can probably
> >> >> easily hard-code this, but I want to do it more automatically...
> >> >>
> >> > Have JAX-RS UriInfo injected:
> >> >
> >> > @path("/webstore/")
> >> > class WebStore {
> >> >   @Context
> >> >   private UriInfo uriInfo;
> >> > }
> >> >
> >> > then you can do:
> >> >
> >> > uriInfo.getBaseUri()
> >> >
> >> > or
> >> >
> >> > UriBuilder builder = uriInfo.getBaseUriBuilder();
> >> >
> >> > Cheers, Sergey
> >> >
> >> >> Thanks.
> >> >> -SImon
> >> >
> >> >
> >> >
> >> > --
> >> > Sergey Beryozkin
> >> >
> >> > Application Integration Division of Talend
> >> > http://sberyozkin.blogspot.com
> >> >
> >
> >
> >
> > --
> > Sergey Beryozkin
> >
> > Application Integration Division of Talend
> > http://sberyozkin.blogspot.com
> >
>



-- 
Sergey Beryozkin

Application Integration Division of Talend <http://www.talend.com/>
http://sberyozkin.blogspot.com

Re: object self-report uri path

Posted by Simon Chen <si...@gmail.com>.
It's finally working, all thanks to Sergey :-)

Basically, the problem comes from the proxification of EMF, which
prevents CXF from finding the fields to play with. Sergey's solution,
which works nicely on my project, is as follows:
1) define an interface:
public interface IContext {
    @Context
    public void setUriInfo(UriInfo ui);
}

2) let the base resource class implement this interface, for me I have:

class WebStore implements IContext {
  protected UriInfo uriInfo;
  public void setUriInfo(UriInfo ui) {
    this.uriInfo = ui;
  }
}

3) Enjoy!

Thanks.
-Simon


On Tue, Apr 12, 2011 at 4:58 PM, Sergey Beryozkin <sb...@gmail.com> wrote:
> Hi Simon
>
> On Tue, Apr 12, 2011 at 9:01 PM, Simon Chen <si...@gmail.com> wrote:
>>
>> Hi Sergey,
>>
>> I tried adding "@context UriInfo uriInfo;" into my class definition,
>> however, when I try to get uri information from the uriInfo object, I
>> always get a NullPointerException. Do I need to initialize the uriInfo
>> in any way?
>>
>
> Is it the root resource that you add the Context fields to ? Do you mean the
> context field is simply not initialized and this causes NPE ? How do you
> register this resource. do you have Spring AOP involved ?
>
> Cheers. Sergey
>
>>
>> I don't think this example here did anything special:
>> http://download.oracle.com/docs/cd/E19776-01/820-4867/6nga7f5og/index.html
>>
>> Just to give some context, I am trying to embed some URI information
>> into the XML serialization. I am doing @XmlIDREF and @XmlElement
>> markup for any non-containment references. I thought that in addition
>> to print out a name (marked by @XmlID), it might be useful to give the
>> associated URI as well.
>>
>> Thanks.
>> -Simon
>>
>> On Mon, Apr 11, 2011 at 5:08 PM, Sergey Beryozkin <sb...@gmail.com>
>> wrote:
>> > Hi
>> >
>> > On Mon, Apr 11, 2011 at 9:27 PM, Simon Chen <si...@gmail.com>
>> > wrote:
>> >>
>> >> Hi all,
>> >>
>> >> I'm wondering if there is a way for an object in the web service to
>> >> somehow self-report where it is, in term of uri path?
>> >>
>> >> For example, we have:
>> >>
>> >> @path("/webstore/")
>> >> class WebStore {
>> >>  @path("/customers/")
>> >>  List<Customer> customers;
>> >> }
>> >>
>> >> Given a link "cl" to a Customer object, can we somehow figure out that
>> >> it is under "/webstore/customers"? I understand that we can probably
>> >> easily hard-code this, but I want to do it more automatically...
>> >>
>> > Have JAX-RS UriInfo injected:
>> >
>> > @path("/webstore/")
>> > class WebStore {
>> >   @Context
>> >   private UriInfo uriInfo;
>> > }
>> >
>> > then you can do:
>> >
>> > uriInfo.getBaseUri()
>> >
>> > or
>> >
>> > UriBuilder builder = uriInfo.getBaseUriBuilder();
>> >
>> > Cheers, Sergey
>> >
>> >> Thanks.
>> >> -SImon
>> >
>> >
>> >
>> > --
>> > Sergey Beryozkin
>> >
>> > Application Integration Division of Talend
>> > http://sberyozkin.blogspot.com
>> >
>
>
>
> --
> Sergey Beryozkin
>
> Application Integration Division of Talend
> http://sberyozkin.blogspot.com
>

Re: object self-report uri path

Posted by Simon Chen <si...@gmail.com>.
I know nothing about Spring AOP :-(

The "@context UriInfo uriInfo" is not initialized, probably because I
didn't do anything except mounting the container object into a
JAXRSServerFactoryBean object running on localhost. Can you send me a
pointer how to "register" the resource?

Thanks.
-Simon

On Tue, Apr 12, 2011 at 4:58 PM, Sergey Beryozkin <sb...@gmail.com> wrote:
> Hi Simon
>
> On Tue, Apr 12, 2011 at 9:01 PM, Simon Chen <si...@gmail.com> wrote:
>>
>> Hi Sergey,
>>
>> I tried adding "@context UriInfo uriInfo;" into my class definition,
>> however, when I try to get uri information from the uriInfo object, I
>> always get a NullPointerException. Do I need to initialize the uriInfo
>> in any way?
>>
>
> Is it the root resource that you add the Context fields to ? Do you mean the
> context field is simply not initialized and this causes NPE ? How do you
> register this resource. do you have Spring AOP involved ?
>
> Cheers. Sergey
>
>>
>> I don't think this example here did anything special:
>> http://download.oracle.com/docs/cd/E19776-01/820-4867/6nga7f5og/index.html
>>
>> Just to give some context, I am trying to embed some URI information
>> into the XML serialization. I am doing @XmlIDREF and @XmlElement
>> markup for any non-containment references. I thought that in addition
>> to print out a name (marked by @XmlID), it might be useful to give the
>> associated URI as well.
>>
>> Thanks.
>> -Simon
>>
>> On Mon, Apr 11, 2011 at 5:08 PM, Sergey Beryozkin <sb...@gmail.com>
>> wrote:
>> > Hi
>> >
>> > On Mon, Apr 11, 2011 at 9:27 PM, Simon Chen <si...@gmail.com>
>> > wrote:
>> >>
>> >> Hi all,
>> >>
>> >> I'm wondering if there is a way for an object in the web service to
>> >> somehow self-report where it is, in term of uri path?
>> >>
>> >> For example, we have:
>> >>
>> >> @path("/webstore/")
>> >> class WebStore {
>> >>  @path("/customers/")
>> >>  List<Customer> customers;
>> >> }
>> >>
>> >> Given a link "cl" to a Customer object, can we somehow figure out that
>> >> it is under "/webstore/customers"? I understand that we can probably
>> >> easily hard-code this, but I want to do it more automatically...
>> >>
>> > Have JAX-RS UriInfo injected:
>> >
>> > @path("/webstore/")
>> > class WebStore {
>> >   @Context
>> >   private UriInfo uriInfo;
>> > }
>> >
>> > then you can do:
>> >
>> > uriInfo.getBaseUri()
>> >
>> > or
>> >
>> > UriBuilder builder = uriInfo.getBaseUriBuilder();
>> >
>> > Cheers, Sergey
>> >
>> >> Thanks.
>> >> -SImon
>> >
>> >
>> >
>> > --
>> > Sergey Beryozkin
>> >
>> > Application Integration Division of Talend
>> > http://sberyozkin.blogspot.com
>> >
>
>
>
> --
> Sergey Beryozkin
>
> Application Integration Division of Talend
> http://sberyozkin.blogspot.com
>

Re: object self-report uri path

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi Simon

On Tue, Apr 12, 2011 at 9:01 PM, Simon Chen <si...@gmail.com> wrote:

> Hi Sergey,
>
> I tried adding "@context UriInfo uriInfo;" into my class definition,
> however, when I try to get uri information from the uriInfo object, I
> always get a NullPointerException. Do I need to initialize the uriInfo
> in any way?
>
>
Is it the root resource that you add the Context fields to ? Do you mean the
context field is simply not initialized and this causes NPE ? How do you
register this resource. do you have Spring AOP involved ?

Cheers. Sergey


> I don't think this example here did anything special:
> http://download.oracle.com/docs/cd/E19776-01/820-4867/6nga7f5og/index.html
>
> Just to give some context, I am trying to embed some URI information
> into the XML serialization. I am doing @XmlIDREF and @XmlElement
> markup for any non-containment references. I thought that in addition
> to print out a name (marked by @XmlID), it might be useful to give the
> associated URI as well.
>
> Thanks.
> -Simon
>
> On Mon, Apr 11, 2011 at 5:08 PM, Sergey Beryozkin <sb...@gmail.com>
> wrote:
> > Hi
> >
> > On Mon, Apr 11, 2011 at 9:27 PM, Simon Chen <si...@gmail.com>
> wrote:
> >>
> >> Hi all,
> >>
> >> I'm wondering if there is a way for an object in the web service to
> >> somehow self-report where it is, in term of uri path?
> >>
> >> For example, we have:
> >>
> >> @path("/webstore/")
> >> class WebStore {
> >>  @path("/customers/")
> >>  List<Customer> customers;
> >> }
> >>
> >> Given a link "cl" to a Customer object, can we somehow figure out that
> >> it is under "/webstore/customers"? I understand that we can probably
> >> easily hard-code this, but I want to do it more automatically...
> >>
> > Have JAX-RS UriInfo injected:
> >
> > @path("/webstore/")
> > class WebStore {
> >   @Context
> >   private UriInfo uriInfo;
> > }
> >
> > then you can do:
> >
> > uriInfo.getBaseUri()
> >
> > or
> >
> > UriBuilder builder = uriInfo.getBaseUriBuilder();
> >
> > Cheers, Sergey
> >
> >> Thanks.
> >> -SImon
> >
> >
> >
> > --
> > Sergey Beryozkin
> >
> > Application Integration Division of Talend
> > http://sberyozkin.blogspot.com
> >
>



-- 
Sergey Beryozkin

Application Integration Division of Talend <http://www.talend.com>
http://sberyozkin.blogspot.com

Re: object self-report uri path

Posted by Simon Chen <si...@gmail.com>.
Hi Sergey,

I tried adding "@context UriInfo uriInfo;" into my class definition,
however, when I try to get uri information from the uriInfo object, I
always get a NullPointerException. Do I need to initialize the uriInfo
in any way?

I don't think this example here did anything special:
http://download.oracle.com/docs/cd/E19776-01/820-4867/6nga7f5og/index.html

Just to give some context, I am trying to embed some URI information
into the XML serialization. I am doing @XmlIDREF and @XmlElement
markup for any non-containment references. I thought that in addition
to print out a name (marked by @XmlID), it might be useful to give the
associated URI as well.

Thanks.
-Simon

On Mon, Apr 11, 2011 at 5:08 PM, Sergey Beryozkin <sb...@gmail.com> wrote:
> Hi
>
> On Mon, Apr 11, 2011 at 9:27 PM, Simon Chen <si...@gmail.com> wrote:
>>
>> Hi all,
>>
>> I'm wondering if there is a way for an object in the web service to
>> somehow self-report where it is, in term of uri path?
>>
>> For example, we have:
>>
>> @path("/webstore/")
>> class WebStore {
>>  @path("/customers/")
>>  List<Customer> customers;
>> }
>>
>> Given a link "cl" to a Customer object, can we somehow figure out that
>> it is under "/webstore/customers"? I understand that we can probably
>> easily hard-code this, but I want to do it more automatically...
>>
> Have JAX-RS UriInfo injected:
>
> @path("/webstore/")
> class WebStore {
>   @Context
>   private UriInfo uriInfo;
> }
>
> then you can do:
>
> uriInfo.getBaseUri()
>
> or
>
> UriBuilder builder = uriInfo.getBaseUriBuilder();
>
> Cheers, Sergey
>
>> Thanks.
>> -SImon
>
>
>
> --
> Sergey Beryozkin
>
> Application Integration Division of Talend
> http://sberyozkin.blogspot.com
>

Re: object self-report uri path

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi

On Mon, Apr 11, 2011 at 9:27 PM, Simon Chen <si...@gmail.com> wrote:

> Hi all,
>
> I'm wondering if there is a way for an object in the web service to
> somehow self-report where it is, in term of uri path?
>
> For example, we have:
>
> @path("/webstore/")
> class WebStore {
>  @path("/customers/")
>  List<Customer> customers;
> }
>
> Given a link "cl" to a Customer object, can we somehow figure out that
> it is under "/webstore/customers"? I understand that we can probably
> easily hard-code this, but I want to do it more automatically...
>
> Have JAX-RS UriInfo injected:

@path("/webstore/")
class WebStore {
  @Context
  private UriInfo uriInfo;
}

then you can do:

uriInfo.getBaseUri()

or

UriBuilder builder = uriInfo.getBaseUriBuilder();

Cheers, Sergey

Thanks.
> -SImon
>



-- 
Sergey Beryozkin

Application Integration Division of Talend <http://www.talend.com>
http://sberyozkin.blogspot.com