HttpServletRequest, HttpServletResponse, Session, Cookie fro
antoine_h Mar 16, 2007 8:54 AMHere some code to get and use the HttpServletRequest, HttpServletResponse, HttpSession, read and write Cookies from a PortletRequest (in processAction method or doView).
This is not compliant with JSR-168. And is "as is", and may not work.
import java.util.logging.Logger;
import javax.portlet.PortletRequest;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jboss.portal.portlet.aspects.portlet.ContextDispatcherInterceptor;
import org.jboss.portal.portlet.impl.spi.AbstractInvocationContext;
import org.jboss.portal.portlet.invocation.PortletInvocation;
import org.jboss.portal.portlet.spi.InvocationContext;
/**
* A class to help manage the HttpServletRequest and Session of the portal.
*
*/
public class HttpServletRequestHelper {
private static final Logger log = Logger
.getLogger(HttpServletRequestHelper.class.getName());
/***************************************************************************
* For getting and using the HttpServletRequest and HttpServletResponse.
**************************************************************************/
/**
* This method retieve the {@link HttpServletRequest} from the
* {@link PortletRequest}. It call first the
* {@link #getAbstractInvocationContext(PortletRequest, String)} to get the
* {@link AbstractInvocationContext}, then retrive the HttpServletRequest
* from it.
*
* <p>
* See the code for details of how it retrieves it.
* </p>
*
* @param request
* The PortletRequest.
* @param msgLogPrefix
* The message prefix for all log, to tell from where this
* helping method was called.
* @return
*/
public static final HttpServletRequest getHttpServletRequest(
PortletRequest request, String msgLogPrefix) {
// Try to cast the context of invocation to an AbstractInvocationContext
AbstractInvocationContext abstractInvCtxt = getAbstractInvocationContext(
request, msgLogPrefix);
HttpServletRequest httpServletRequest = null;
if (abstractInvCtxt != null) {
// The AbstractInvocationContext exists : try to retrieve the client
// httpServletRequest.
httpServletRequest = abstractInvCtxt.getClientRequest();
if (httpServletRequest == null) {
log.warning(msgLogPrefix + " The HttpServletRequest is null !");
}
} else {
log.warning(msgLogPrefix
+ " The AbstractInvocationContext is null !");
}
return httpServletRequest;
}
/**
* This method retieve the {@link HttpServletRequest} from the
* {@link PortletRequest}. It call first the
* {@link #getAbstractInvocationContext(PortletRequest, String)} to get the
* {@link AbstractInvocationContext}, then retrive the HttpServletResponse
* from it.
*
* <p>
* See the code for details of how it retrieves it.
* </p>
*
* @param request
* The PortletRequest.
* @param msgLogPrefix
* The message prefix for all log, to tell from where this
* helping method was called.
* @return
*/
public static final HttpServletResponse getHttpServletResponse(
PortletRequest request, String msgLogPrefix) {
// Try to cast the context of invocation to an AbstractInvocationContext
AbstractInvocationContext abstractInvCtxt = getAbstractInvocationContext(
request, msgLogPrefix);
HttpServletResponse httpServletResponse = null;
if (abstractInvCtxt != null) {
// The AbstractInvocationContext exists : try to retrieve the client
// httpServletRequest.
httpServletResponse = abstractInvCtxt.getClientResponse();
if (httpServletResponse == null) {
log
.warning(msgLogPrefix
+ " The HttpServletResponse is null !");
}
} else {
log.warning(msgLogPrefix
+ " The AbstractInvocationContext is null !");
}
return httpServletResponse;
}
/**
* This method retieve the {@link AbstractInvocationContext} from the
* {@link PortletRequest}. The {@link AbstractInvocationContext} is used to
* get the ClientRequest etc...
* <p>
* See the code for details of how it retrieves it.
* </p>
*
* @param request
* The PortletRequest.
* @param msgLogPrefix
* The message prefix for all log, to tell from where this
* helping method was called.
* @return
*/
public static final AbstractInvocationContext getAbstractInvocationContext(
PortletRequest request, String msgLogPrefix) {
PortletInvocation portletInvocation = (PortletInvocation) request
.getAttribute(ContextDispatcherInterceptor.REQ_ATT_COMPONENT_INVOCATION);
// return portletInvocation.getDispatchedRequest();
// Get the context of the invocation
InvocationContext invocationContext = portletInvocation.getContext();
// Try to cast the context of invocation to an AbstractInvocationContext
AbstractInvocationContext abstractInvCtxt = null;
if (invocationContext != null) {
if (invocationContext instanceof AbstractInvocationContext) {
abstractInvCtxt = (AbstractInvocationContext) invocationContext;
} else {
log
.warning(msgLogPrefix
+ " The InvocationContext is not of instance of AbstractInvocationContext !");
log.warning(msgLogPrefix + " The InvocationContext is : "
+ invocationContext + " // "
+ invocationContext.getClass().getName() + " // "
+ invocationContext.toString());
}
} else {
log.warning(msgLogPrefix + " The invocationContext is null !");
}
return abstractInvCtxt;
}
/***************************************************************************
* End of For getting and using the HttpServletRequest and
* HttpServletResponse.
**************************************************************************/
/***************************************************************************
* Set and retrieve a Cookie
**************************************************************************/
/**
* Return the cookie with name cookieName, from the
* {@link HttpServletRequest}, or null if not found. Does not check for the path
*
* @param httpRequest
* @param cookieName
* The name of the cookie to look for
* @param msgLogPrefix
* The message prefix for all log, to tell from where this
* helping method was called.
* @return The cookie, or null if not found.
*/
public static Cookie getCookie(HttpServletRequest httpRequest,
String cookieName, String cookiePath, String msgLogPrefix) {
Cookie cookieRet = null;
Cookie[] cookieTab = httpRequest.getCookies();
// log.warning(msgLogPrefix + " The list of cookies :"
// + msgCookieListAsString(cookieTab));
if (cookieTab != null) {
for (int i = 0; i < cookieTab.length; i++) {
if (cookieTab.getName().compareTo(cookieName) == 0) {
// log.warning(msgLogPrefix + " i = " + i
// + " same name cookieTab.getName()="
// + cookieTab.getName());
// log.warning(msgLogPrefix + " i = " + i
// + " PATH cookieTab.getPath()="
// + cookieTab.getPath());
cookieRet = cookieTab;
break;
}
}
// } else {
// log
// .warning(msgLogPrefix
// + " The List of cookies is null. From HttpServletRequest :
// LocalAddr="
// + httpRequest.getLocalAddr() + ", and LocalName="
// + httpRequest.getLocalName() + ", and user="
// + httpRequest.getRemoteUser());
}
return cookieRet;
}
/**
* Store a cookie.
*
* @param httpResponse
* @param cookieName
* @param cookiePath
* @param cookieValue
* @param maxAgeSeconds
* The cookie will expire after that many seconds have passed.
* See {@link Cookie#setMaxAge(int)}. One year = 60*60*24*365 =
* 31536000 sec.
* @param msgLogPrefix
*/
public static void storeCookie(HttpServletResponse httpResponse,
String cookieName, String cookiePath, String cookieValue,
int maxAgeSeconds, String msgLogPrefix) {
Cookie cookie = new Cookie(cookieName, cookieValue);
cookie.setMaxAge(maxAgeSeconds); // one year = 60*60*24*365
// log.warning(msgLogPrefix + " cookie.setMaxAge = " +
// cookie.getMaxAge()
// + ", and LocalName=" + "");
cookie.setPath(cookiePath);
httpResponse.addCookie(cookie);
}
/**
* Return the log message of a cookie : name, path, value, maxAge.
*
* @param cookie
* @return
*/
public static String msgCookieAsString(Cookie cookie) {
String msgRet = "Cookie[" + cookie.getName() + "], path=["
+ cookie.getPath() + "]" + ", value=" + cookie.getValue()
+ ", maxAge=" + cookie.getMaxAge() + ", domain="
+ cookie.getDomain() + "";
return msgRet;
}
/**
* Return the log message of the list of cookie.
*
* @param cookieTab
* @return
*/
public static String msgCookieListAsString(Cookie[] cookieTab) {
String msgRet = null;
if (cookieTab == null) {
msgRet = "Cookie list is null !";
return msgRet;
} else {
msgRet = "Cookie list size=" + cookieTab.length;
}
for (int i = 0; i < cookieTab.length; i++) {
msgRet = msgRet + "\ni=" + i + ", "
+ msgCookieAsString(cookieTab);
}
return msgRet;
}
/***************************************************************************
* End of Set and retrieve a Cookie
**************************************************************************/
}