cxf-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Sergey Beryozkin <sbery...@progress.com>
Subject Re: How to have a super class of the resource class use CXF annotations?
Date Thu, 14 Jan 2010 17:39:07 GMT
> 
> Actually, I've just reread your email...
> 
> Injecting HttpHeaders into your abstract class is what I'd still
> recommend, but just pulling up a variable annotated with
> @HeaderParam should still work though I may need to add a test to
> verify it works with the abstract classes.

I'm pretty sure it didn't work.  I had the field in the base class, and
it didn't get set.  I just moved it down into the concrete class and it
started getting set.

> S.B, sure, I will retest...

> The problem is that an injection into fileds annotated with
> @HeaderParam is not thread-safe for singleton resources (as opposed to
> say @Context HttpHeaders headers;). If you do prefer to inject an
> individual @HeaderParam value as opposed to HttpHeaders then the
> the safest option here is to use a constructor injection of a given
> header parameter and make sure the resource has the prototype
> scope...

I had thought about the scope issue before, but I had forgotten about
it.

I guess I'll have to think about the tradeoffs here.  I'm going to have
a bunch of resource methods in the controller class, all of which will
need to get certain header parameters, but which will vary on some other
parameters.  If the controller is singleton by default, then I can't
have instance variables set from headers.  So, I either have a bunch of
parameters that are the same on each method, or I lose a little bit of
performance.

> S.B I see... You can get to individual header values from "@Context HttpHeaders headers",
ex,

@Path("/")
public AbsractController {

private @Context HttpHeaders headers;

@GET
public Bar method1(QueryParam("h") headerHame) {

    // assuming a header with a given name has a single value only
    String hValue = headers.getRequestHeaders(headerName).get(0);

}

}


cheers, Sergey

> > Hi
> >
> > this should work, there's a couple of options, assuming you have an
> abstract class which all other resource classes extend
> >
> > 1. just have a private field :
> >
> > @Context
> > private HttpHeaders headers;
> >
> > 2. Alternatively, have a setter :
> >
> > // note, no @Context
> > private HttpHeaders headers;
> >
> > public void setHttpHeaders(HttpHeaders headers) {this.headers =
> headers;}
> >
> > this 2nd option may work better if you have Spring proxifying your
> resource classes; for ex, you could have a dedicated interface
> > like Injectable (the name of the interface is up to a user to
choose)
> which this abstract class will implement and thus will help
> > Spring to inject if Spring proxies are pure JDK proxies as opposed
to
> CGLIB ones...
> >
> > cheers, Sergey
> >
> > ----- Original Message -----
> > From: "KARR, DAVID (ATTCINW)" <dk068x@att.com>
> > To: <users@cxf.apache.org>
> > Sent: Monday, January 11, 2010 4:41 PM
> > Subject: How to have a super class of the resource class use CXF
> annotations?
> >
> >
> > That subject isn't worded quite right, but hopefully I can clear
that
> > up.
> >
> > I started out with a small handful of resource classes, each of
which
> > points to a separate portion of my domain classes.  I realized that
> > there are some HTTP headers that I'd like to get automatically,
> without
> > specifying them explicitly in each method.  I realized I could just
> > define an instance variable in the resource class and then put the
> > "@HeaderParam annotation on the field.  That works fine.  However,
> what
> > I'd really like to do is have a base class that all the resource
> classes
> > extend, which has the annotated instance variable.  All I did was
> "pull
> > up" the instance variable and the getter/setter to a base class and
> > retested that, and I found that the instance variable was not set.
> >
> > Is there something I have to do to get CXF to look at the base class
> for
> > CXF annotations, or is this a scenario that isn't possible yet?


Mime
View raw message