You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by "Ganesan, Chandru" <ch...@hp.com> on 2012/03/01 00:07:25 UTC

RE: How to set the HttpHeader in the base class of a singleton resource

Thanks Sergey, it works great. 




-----Original Message-----
From: Sergey Beryozkin [mailto:sberyozkin@gmail.com] 
Sent: Wednesday, February 29, 2012 3:37 PM
To: users@cxf.apache.org
Subject: Re: How to set the HttpHeader in the base class of a singleton resource

Hi

On 29/02/12 18:16, cganesan wrote:
> Hi
>
> I'd like the base class of a resource class to store the injected
> HttpHeaders property. The resource class is a singleton and I'm using spring
> appl context to load the beans. I'm using following sample code where the
> HttpHeaders is not getting injected
>
> Base Class:
> =============
> @Context
> private HttpHeaders httpHeaders;
>
> public abstract class BaseResource {
>
>      public String getHeaderProperty(String key) {
>         // retrieve string property value from httpHeaders;
>      }
>
>
> }
>
>
> Resource Class
> =============
> @Path("/customer/{id}")
> public TestResource extends BaseResource {
>
> @POST
> @Consumes(MediaType.APPLICATION_JSON)
> @Path("/media")
>
>    public Response postMedia() {
>
>      // fetch http header property
>       String str = getHeaderProperty("key");
>    }
> }
>
> Does the base class need to be a Root resource (have a @Path injected) for
> this to work?  Any help is appreciated.

This should work unless you have Spring Security or similar proxifying 
the root resource class...
If so then start from introducing a dedicated setter:

 > Base Class:
 > =============
 >
 > public abstract class BaseResource {
        // drop @Context here
        private HttpHeaders httpHeaders;

        @Context
        public void setHttpHeaders(HttpHeaders headers) {
            httpHeaders = headers;
        }

 >
 >
 > }

if that does not work still then introduce a dedicated interface:

public interface Contexts {
      @Context
      void setHttpHeaders(HttpHeaders headers);
}

public abstract class BaseResource implements Contexts {
        public void setHttpHeaders(HttpHeaders headers) {
            httpHeaders = headers;
        }
}

that should definitely work :-)

HTH, SErgey

>
> Thanks
> Chandru
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/How-to-set-the-HttpHeader-in-the-base-class-of-a-singleton-resource-tp5525703p5525703.html
> Sent from the cxf-user mailing list archive at Nabble.com.