Forkmohit
Forkmohit

Reputation: 753

How to invalidate session by CXF Interceptor?

I am new to Cxf webservice and bit overwhelmed by information provided. I have written a cxf Interceptor(cxf:outInterceptors) which extends AbstractPhaseInterceptor. I am trying to invalidate the current session after service executes. My issues are:

  1. Whether I am using correct phase in the constructor? (Phase.Send)
  2. How to get session information from message? Right now, for below method request instance returns null.

Or Is there any better approach to this?

  public ServiceSessionInvalidator() {
      super(Phase.SEND);
  }

@Override
public void handleMessage(Message message) throws Fault {

    System.out.println("Hitting Handler");

    HttpSession session;

    HttpServletRequest req = (HttpServletRequest)message.get(AbstractHTTPDestination.HTTP_REQUEST);

    session = (req != null)? req.getSession(): null;

    if(req == null ){

        System.out.println("Request is Null");

    }


}

Output:

16 Jun 2015 13:35:40 INFO   - Inbound Message
----------------------------

ID: 1

Address: http://localhost:8090/services/VariableService/variableService/

Http-Method: GET

---
---

Hitting Handler

Request is Null

16 Jun 2015 13:35:41 INFO   - Outbound Message

---------------------------

ID: 1

Response-Code: 200

Upvotes: 0

Views: 835

Answers (2)

Ahmad R. Nazemi
Ahmad R. Nazemi

Reputation: 805

@Resource
WebServiceContext wsContext;
@Context
private HttpServletRequest request;



 private HttpServletRequest invalidateSession() {
    MessageContext mc = wsContext.getMessageContext();
    HttpServletRequest req;
    if (mc != null) {
        req = (HttpServletRequest) mc.get(MessageContext.SERVLET_REQUEST);
    } else {
        req = request;
    }

    req.getSession().invalidate();
}

Upvotes: 2

f4lco
f4lco

Reputation: 3824

I'm unsure about this one, but look at the CXF phase description, like here. Send is the last phase, after processing the message on "protocol" and "byte" level. The concept of session is related to the HTTP protocol, same is true for request (see the class names).

My suggestion: try Phase.MARSHAL.

Upvotes: 1

Related Questions