CXF Session Management

CXF session Management explains about Enabling Session Handling In Apache CXF

I am showing here, How to do session management in CXF framework, this is applicable for both SOAP & RESTFul services

Message message = PhaseInterceptorChain.getCurrentMessage();
HttpServletRequest request = (HttpServletRequest)message.get(AbstractHTTPDestination.HTTP_REQUEST);
HttpSession  session = request.getSession(true);

On this code, we are getting HttpServletRequest and from that we are getting HttpSession Using CXF

You can see the below CXF Session example, on which we are storing a value to session and retrieving the same, using CXF Framework

Note

This tutorial is also applicable for JAX-WS Web services because PhaseInterceptorChain.getCurrentMessage() applicable for both JAX-WS and JAX-RS based services

You can change CXF Web Service Tutorial according to the below tutorial, if you are using JAX-WS based service

Session Management Using CXF

I am going to re-use CXF Restful Tutorial

We are making changes ChangeStudentDetailsImpl class, just adding

PhaseInterceptorChain.getCurrentMessage() and getting HttpServletRequest, so that we can access HttpSession object in CXF Framework

package com.student;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import org.apache.cxf.message.Message;
import org.apache.cxf.phase.PhaseInterceptorChain;
import org.apache.cxf.transport.http.AbstractHTTPDestination;

@Consumes("application/json")
@Produces("application/json")
public class ChangeStudentDetailsImpl implements ChangeStudentDetails {
 
 
@POST
  @Path
("/changeName")
 
public Student changeName(Student student) {
   
// Here We are getting session from Message
   
Message message = PhaseInterceptorChain.getCurrentMessage();
    HttpServletRequest request =
(HttpServletRequest)message.get(AbstractHTTPDestination.HTTP_REQUEST);
    HttpSession  session = request.getSession
(true);
    session.setAttribute
("name","HELLO "+student.getName());
    student.setName
(session.getAttribute("name").toString());
   
return student;
 
}

 
@GET
  @Path
("/getName")
 
public Student getName() {
   
Student student = new Student();
    student.setName
("Rockey");
   
return student;
 
}
}

1 2 Next





2 Responses to "CXF Session Management"
  1. sort 2012-09-10 08:05:01.0
  1. admin 2012-09-11 08:05:01.0

Your email address will not be published. Required fields are marked *