Introduction
We all work with cookies and it is one of the important aspect when it comes to session management.
There will many situations where we would need to read the cookies at server side. Usualy we follow the responseWrapper approach to read the resposne cookies which is defined in the link below
ResponseWrapper approach to read cookies
There are times when the above approach doesn't work but we would still need to read them. Some changes to commerce OOB code will get us there.
1. Extend CommerceTokenRequestHandler.java and overwrite handleRequest method. Add the below code
{
HttpServletResponseWrapper resp = (HttpServletResponseWrapper) messageContext.getAttribute(HttpServletResponse.class);
SRTServletResponse srtResponse = getSRTResponseFromResponseWrapper(resp);
responseCookies = srtResponse.getCookies();
}
private SRTServletResponse getSRTResponseFromResponseWrapper(ServletResponseWrapper respWrapper) {
ServletResponse response = respWrapper.getResponse();
if (response == null) {
return null;
} else if (response instanceof SRTServletResponse) {
return (SRTServletResponse) response;
} else if (response instanceof ServletResponseWrapper) {
return getSRTResponseFromResponseWrapper((ServletResponseWrapper) response);
} else {
return null;
}
}
2. Extend CommerceDeploymentConfiguration.java and overwrite initRequestUserHandlers() method
final List<RequestHandler> handlerList = super.initRequestUserHandlers();
if (handlerList != null) {
for (int i = 0; i < handlerList.size(); i++) {
final RequestHandler handler = handlerList.get(i);
if (handler instanceof CommerceTokenRequestHandler) {
handlerList.set(i, new ExtendedCommerceTokenRequestHandler());
}
}
}
The above customisations would suffice to read the response cookies for rest calls
We all work with cookies and it is one of the important aspect when it comes to session management.
There will many situations where we would need to read the cookies at server side. Usualy we follow the responseWrapper approach to read the resposne cookies which is defined in the link below
ResponseWrapper approach to read cookies
There are times when the above approach doesn't work but we would still need to read them. Some changes to commerce OOB code will get us there.
1. Extend CommerceTokenRequestHandler.java and overwrite handleRequest method. Add the below code
{
HttpServletResponseWrapper resp = (HttpServletResponseWrapper) messageContext.getAttribute(HttpServletResponse.class);
SRTServletResponse srtResponse = getSRTResponseFromResponseWrapper(resp);
responseCookies = srtResponse.getCookies();
}
private SRTServletResponse getSRTResponseFromResponseWrapper(ServletResponseWrapper respWrapper) {
ServletResponse response = respWrapper.getResponse();
if (response == null) {
return null;
} else if (response instanceof SRTServletResponse) {
return (SRTServletResponse) response;
} else if (response instanceof ServletResponseWrapper) {
return getSRTResponseFromResponseWrapper((ServletResponseWrapper) response);
} else {
return null;
}
}
2. Extend CommerceDeploymentConfiguration.java and overwrite initRequestUserHandlers() method
final List<RequestHandler> handlerList = super.initRequestUserHandlers();
if (handlerList != null) {
for (int i = 0; i < handlerList.size(); i++) {
final RequestHandler handler = handlerList.get(i);
if (handler instanceof CommerceTokenRequestHandler) {
handlerList.set(i, new ExtendedCommerceTokenRequestHandler());
}
}
}
The above customisations would suffice to read the response cookies for rest calls
No comments:
Post a Comment