Hi,
I'm trying to get session in the web service implementation class. But I'm
webservicecontext is coming as null.. The class and the configuration is as
below. How can i get the session or session cookie? It will be great if
anyone can provide some help or some example.
*******class*********
@Path("/userservice/")
@ProduceMime("application/xml")
public class CartServiceImpl
implements CartService
{
@Context
protected WebServiceContext wsContext;
@Resource
private HttpServletRequest request;
@Resource
public void setContext(WebServiceContext context)
{
System.out.println("Request in setContext-->"+ request );
System.out.println("Setting the context..." + context );
this.request
=(HttpServletRequest)context.getMessageContext().get(MessageContext.SERVLET_REQUEST);
this.wsContext = context;
}
@POST
@Path("/addtocart")
public Response addToCart ( Product product )
{
... ....
}
******** web.xml ******
<web-app>
<servlet>
<display-name>CXF Servlet</display-name>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
...<<Spring configuration>>
</web-app>
****** spring config file ********
<beans>
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml"
/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<jaxrs:server id="cartService" address="/">
<jaxrs:serviceBeans>
<ref bean="cartServiceBean" />
</jaxrs:serviceBeans>
</jaxrs:server>
<bean id="cartServiceBean" class="com.poc.web.serviceimpl.CartServiceImpl"
autowire="autodetect"/>
</beans>
}
Sergey Beryozkin wrote:
>
> 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
>
>
--
View this message in context:
http://www.nabble.com/getting-hold-of-the-session-in-a-jaxrs-service-tp17878568p22707456.html
Sent from the cxf-user mailing list archive at Nabble.com.