Debug/Log Portlet Filter

One of the nicest way to learn about how the portlet containers work is by looking at what type type of data is stored in PortletRequest and underlying HttpServletRequest, including data stored in PortletSession, HttpSession and PortletPreferences. So i built this DebugPortletFilter that you can copy in your project and apply to your portlet.

This Filter will be helpful even if your working with say a MVC framework like Spring Portlet MVC framework and your not sure about what request parameters or attributes are getting stored


package com.webspherenotes.portlet.filter;

import java.io.IOException;
import java.util.Enumeration;
import javax.portlet.PortletException;
import javax.portlet.PortletPreferences;
import javax.portlet.PortletRequest;
import javax.portlet.PortletSession;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.filter.FilterChain;
import javax.portlet.filter.FilterConfig;
import javax.portlet.filter.RenderFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DebugPortletFilter implements RenderFilter,ActionFilter,ResourceFilter{
private static final Logger logger = LoggerFactory.getLogger(DebugPortletFilter.class);
public void doFilter(RenderRequest request, RenderResponse response,
FilterChain filterChain) throws IOException, PortletException {
logger.debug("****************** Entering render phase *********************");
printPortletRelatedInfo(request);
printHttpServletRequestRelatedInfo(request);
filterChain.doFilter(request, response);
logger.debug("****************** Exiting render phase *********************");
}
public void doFilter(ResourceRequest request, ResourceResponse response,
FilterChain filterChain) throws IOException, PortletException {
logger.debug("****************** Entering resource phase *********************");
printPortletRelatedInfo(request);
printHttpServletRequestRelatedInfo(request);
filterChain.doFilter(request, response);
logger.debug("****************** Exiting resource phase *********************");
}
public void doFilter(ActionRequest request, ActionResponse response
FilterChain filterChain) throws IOException, PortletException {
logger.debug("****************** Entering action phase *********************");
printPortletRelatedInfo(request);
printHttpServletRequestRelatedInfo(request);
filterChain.doFilter(request, response);
logger.debug("****************** Exiting action phase *********************");
}

public void destroy() {
}

public void init(FilterConfig arg0) throws PortletException {
logger.debug("Inside DebugPortletFilter.init()");
}

private void printHttpServletRequestRelatedInfo(PortletRequest portletRequest){
logger.debug(" Information stored at HttpServlet level ");
HttpServletRequest httpServletRequest = getHttpServletRequest(portletRequest);
printHttpServletRequestParameter(httpServletRequest);
printHttpServletRequestAttribute(httpServletRequest);
printHttpSessionAttribute(httpServletRequest);
}
private void printPortletRelatedInfo(PortletRequest portletRequest){
logger.debug(" Information stored at portlet level ");
logger.debug( "Portlet Mode " + portletRequest.getPortletMode());
logger.debug( "Window State " + portletRequest.getWindowState());
logger.debug( "Window ID " + portletRequest.getWindowID());
printPortletRequestParameter(portletRequest);
printPortletRequestAttribute(portletRequest);
printPortletPreferences(portletRequest);
printPortletSessionInfo(portletRequest);
}
private void printPortletRequestParameter(PortletRequest portletRequest){
logger.debug(" Printing Portlet Parameters ");
Enumeration paramEnum = portletRequest.getParameterNames();
while(paramEnum.hasMoreElements()){
String paramName = paramEnum.nextElement();
String paramValue = portletRequest.getParameter(paramName);
logger.debug(paramName + " -> " + paramValue);
}
}
private void printPortletRequestAttribute(PortletRequest portletRequest){
logger.debug(" Printing Portlet Attributes ");
Enumeration attributeEnum = portletRequest.getAttributeNames();
while(attributeEnum.hasMoreElements()){
String attrName = attributeEnum.nextElement();
Object attrValue = portletRequest.getAttribute(attrName);
logger.debug(attrName + " -> " + attrValue );
}
}
private void printPortletPreferences(PortletRequest portletRequest){
logger.debug(" Printing PortletPreferences ");
PortletPreferences pref = portletRequest.getPreferences();
Enumeration prefEnum = pref.getNames();
while(prefEnum.hasMoreElements()){
String prefName = prefEnum.nextElement();
String prefValue = pref.getValue(prefName, "");
logger.debug(prefName +" -> " + prefValue);
}
}
private void printPortletSessionInfo(PortletRequest portletRequest){
logger.debug(" Printing PortletSession attributes ");
PortletSession session = portletRequest.getPortletSession();
Enumeration attribEnum = session.getAttributeNames();
while(attribEnum.hasMoreElements()){
String attrName = attribEnum.nextElement();
Object attrValue = session.getAttribute(attrName);
}
}
private HttpServletRequest getHttpServletRequest(PortletRequest request){
HttpServletRequest httpServletRequest = (HttpServletRequest)request;
while(httpServletRequest instanceof HttpServletRequestWrapper){
HttpServletRequestWrapper httpServletRequestWrapper =
(HttpServletRequestWrapper)httpServletRequest;
httpServletRequest = (HttpServletRequest)httpServletRequestWrapper.getRequest();
}
return httpServletRequest;
}
private void printHttpServletRequestParameter(HttpServletRequest httpServletRequest){
logger.debug(" Printing HttpServletRequest parameter ");
Enumeration paramEnum = httpServletRequest.getParameterNames();
while(paramEnum.hasMoreElements()){
String paramName = paramEnum.nextElement();
String paramValue = httpServletRequest.getParameter(paramName);
logger.debug(paramName +" -> " + paramValue);
}
}
private void printHttpServletRequestAttribute(HttpServletRequest httpServletRequest){
logger.debug(" Printing HttpServletRequest attributes ");
Enumeration attrEnum = httpServletRequest.getAttributeNames();
while(attrEnum.hasMoreElements()){
String attrName = attrEnum.nextElement();
Object attrValue = httpServletRequest.getAttribute(attrName);
logger.debug(attrName + " -> " + attrValue);
}
}
private void printHttpSessionAttribute(HttpServletRequest httpServletRequest){
logger.debug(" Printing HttpSession attributes ");
HttpSession httpSession = httpServletRequest.getSession();
Enumeration attrEnum = httpSession.getAttributeNames();
while(attrEnum.hasMoreElements()){
String attrName = attrEnum.nextElement();
Object attrValue = httpSession.getAttribute(attrName);
logger.debug(attrName + " -> " + attrValue);
}
}
}


The DebugPortletFilter is implementing only RenderFilter, ActionFilter and ResourceFilter in all three methods it first prints all the portlet related information first and then it finds out the underlying HttpServletRequest object and prints the information related to it

You can apply the filter to your portlet by adding a filter declaration and mapping in your portlet.xml like this

<filter>
<filter-name>DebugPortletFilter</filter-name>
<filter-class>com.webspherenotes.portlet.filter.DebugPortletFilter</filter-class>
<lifecycle>RENDER_PHASE</lifecycle>
</filter>
<filter-mapping>
<filter-name>DebugPortletFilter</filter-name>
<portlet-name>DemoPortlet</portlet-name>
</filter-mapping>


Important Note Please be aware that the DebugPortletFilter generates huge amount of log information so use it only during development

1 comment: