Hi
HttpServletRequest instance can be injected in CXF JAX-RS into a @Resource
annotated field
So perhaps you can refactor your code like this, assuming you're only
interested in HttpServletRequest :
@Resource
private HttpServletRequest httpRequest;
@Resource
public void setContext(WebServiceContext context) {
this.httpRequest =
(HttpServletRequest)context.getMessageContext().get(MessageContext.SERVLET_REQUEST);
}
This way JAX-RS impl will initialize an httpRequest directly, while JAX-WS
runtime will do the same through the setContext
method. The only catch is that JAX-RS impl does not support thread local
proxies for fields (it will be...) so the above code won't be thread-safe...
In JAX-RS you can also access the cookies or various bits of a request
URI...Let me know please if you need more info about it....
Cheers, Sergey
> Hi all,
>
> As far as I've been able to trace, it looks like the
> WebServiceContextImpl threadlocal is only set for regular web
> services, not for services implemented using jaxrs. Is that on purpose
> or might it be added in a future version?
>
> This is what I'm trying to do. I have a service:
>
> @Path("/surveyservice/1.0")
> @ProduceMime("application/xml")
> public class SurveyWebServiceImpl ...
> ...
> private WebServiceContext context;
>
> @Resource
> public void setContext(WebServiceContext context) {
> this.context = context;
> }
>
> and then later in a service method I'm trying to get the servlet request:
>
> HttpServletRequest request =
> (HttpServletRequest)context.getMessageContext().get(MessageContext.SERVLET_REQUEST);
>
> The service is configured using Spring:
>
> <import resource="classpath:META-INF/cxf/cxf.xml" />
> <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
> <import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml" />
> <import resource="classpath:META-INF/cxf/cxf-extension-jaxws.xml" />
> <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
>
> <bean id="surveyWebService"
> class="ts4.component.survey.ws.v1_0.SurveyWebServiceImpl"
> autowire="autodetect"></bean>
>
> <jaxrs:server id="surveyWebServiceRs" address="/">
> <jaxrs:serviceBeans>
> <ref bean="surveyWebService" />
> </jaxrs:serviceBeans>
> </jaxrs:server>
>
> And I have the CXFServlet configured etc.
>
> The injection of the context works fine, but as the thread local in
> the WebServiceContextImpl (the only implementation of the
> WebServiceContext interface I could find) is never set for services
> over REST, the context object is quite useless :-)
> Is there another way to get the HttpServletRequest (or HttpSession, as
> that's what I'm really after)?
>
> Also, I thought the @HttpSessionScope and @Stateful annotations as
> described here:
> http://weblogs.java.net/blog/ramapulavarthi/archive/2007/02/useful_goodies.html
> are an elegant solution for what I'm trying to do (implementing
> conversational web services). Does anyone here know what the status is
> on that, particularly in CXF?
>
> Cheers,
>
> Eelco
----------------------------
IONA Technologies PLC (registered in Ireland)
Registered Number: 171387
Registered Address: The IONA Building, Shelbourne Road, Dublin 4, Ireland