ParameterNamesProcessor for mapping parameter names

In the Logging custom business event using Site Analysis, i blogged about how you can log custom event from the render() method and when you do, the portal adds the render parameter in the log.

This is sample log generated by render() method, when it had com.webspherenotes.test render parameter

localhost.localdomain - wasadmin [09/Sep/2010:06:56:41 -0700] "GET /Portlet/Z5_OGFLMKG108IGF0IANFDTU13085/Site_Analytics_portlet/SampleRenderEvent?PortletPID=Z5_OGFLMKG108IGF0IANFDTU13085&PortletMode=view&PortletState=normal&RequestType=render&com.webspherenotes.test=testValue HTTP/1.1" 200 -1 "http://localhost/Page/Z6_OGFLMKG108IGF0IANFDTU130G6/Site_Analytics" "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.9) Gecko/20100824 Firefox/3.6.9" "JSESSIONID=0000iLVC75NHzLRaJr-0mNVA6T5:-1"


Did you notice that it dumps the render parameter name and value in the log. This might be ok in most of the cases but what if you want to say replace this parameter name. If thats the case then you should create a class implementing com.ibm.portal.portlet.service.siteanalyzer.ParameterNamesProcessor interface like this


package com.webspherenotes.wps70.sa;

import java.util.Hashtable;

import com.ibm.portal.portlet.service.siteanalyzer.ParameterNamesProcessor;

public class WPNotesNamesProcessor implements ParameterNamesProcessor{

private static final Hashtable PARAM_NAMES_MAP;

static
{
PARAM_NAMES_MAP = new Hashtable();
PARAM_NAMES_MAP.put("com.webspherenotes.test", "test");
}

public String processParameterName(String paramName)
{
// get a replacement for the given parameter name or
// return the original key if not replacement exists
if (PARAM_NAMES_MAP.containsKey(paramName))
{
return PARAM_NAMES_MAP.get(paramName);
}
else
{
return paramName;
}
}



}


Here i am saying that if SiteAnalysis Service wants to log com.webspherenotes.test parameter it should use test as parameter name instead of the full name.

Then inside the portlet code whenever your getting instance of PortletSiteAnalyzerLogger you should pass it instance of WPNotesNamesProcessor, this is the modified version of the SiteAnalyticsDebugPortlet that i used in the last entry

package com.webspherenotes.wps70.sa;

import java.io.IOException;

import javax.naming.NamingException;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;

import com.ibm.portal.portlet.service.PortletServiceHome;
import com.ibm.portal.portlet.service.siteanalyzer.ParameterNamesProcessor;
import com.ibm.portal.portlet.service.siteanalyzer.PortletSiteAnalyzerLogger;
import com.ibm.portal.portlet.service.siteanalyzer.PortletSiteAnalyzerLoggingServiceHome;

public class SiteAnalyticsDebugPortlet extends GenericPortlet {

PortletSiteAnalyzerLoggingServiceHome portletSiteAnalyzerLoggingServiceHome;
ParameterNamesProcessor parameterNameProcessor;
public void init() throws PortletException {
System.out.println("Entering SiteAnalyticsDebugPortlet.init()");
com.ibm.portal.portlet.service.PortletServiceHome psh;
try {
javax.naming.Context ctx = new javax.naming.InitialContext();
psh = (PortletServiceHome) ctx
.lookup(PortletSiteAnalyzerLoggingServiceHome.JNDI_NAME);
portletSiteAnalyzerLoggingServiceHome = (PortletSiteAnalyzerLoggingServiceHome) psh
.getPortletService(PortletSiteAnalyzerLoggingServiceHome.class);
parameterNameProcessor = new WPNotesNamesProcessor();
} catch (javax.naming.NameNotFoundException e) {
e.printStackTrace(System.out);
} catch (NamingException e) {
e.printStackTrace(System.out);
}

System.out.println("Exiting SiteAnalyticsDebugPortlet.init()");
}

protected void doEdit(RenderRequest request, RenderResponse response)
throws PortletException, IOException {
response.setContentType("text/html");
getPortletContext().getRequestDispatcher("/mode.jsp").include(request,
response);
}

protected void doHelp(RenderRequest request, RenderResponse response)
throws PortletException, IOException {
response.setContentType("text/html");
getPortletContext().getRequestDispatcher("/mode.jsp").include(request,
response);
}

protected void doView(RenderRequest request, RenderResponse response)
throws PortletException, IOException {
response.setContentType("text/html");
PortletSiteAnalyzerLogger logger =
portletSiteAnalyzerLoggingServiceHome.getLogger(request, response,parameterNameProcessor);

if (logger.isLogging()) {
logger.log("SampleRenderEvent");
}

getPortletContext().getRequestDispatcher("/view.jsp").include(request,
response);
}

public void processAction(ActionRequest request, ActionResponse response)
throws PortletException, IOException {
System.out.println("Inside SiteAnalyticsDebugPortlet.processAction");
PortletSiteAnalyzerLogger logger = portletSiteAnalyzerLoggingServiceHome
.getLogger(request, response);
if (logger.isLogging()) {
logger.log("SampleActionEvent");
}
response.setRenderParameter("com.webspherenotes.test", "testValue");
}

public void serveResource(ResourceRequest request, ResourceResponse response)
throws PortletException, IOException {
response.setContentType("text/html");
response.getWriter().println(
"Hello from SiteAnalyticsDebugPortlet.serveResource()");
PortletSiteAnalyzerLogger logger = portletSiteAnalyzerLoggingServiceHome
.getLogger(request, response);
if (logger.isLogging()) {
logger.log("Sample serveResource event");
}
}
}


This is how the same log entry looks like after you start using the WPNotesNamesProcessor in the portlet

localhost.localdomain - wasadmin [09/Sep/2010:06:59:53 -0700] "GET /Portlet/Z5_OGFLMKG108IGF0IANFDTU13085/Site_Analytics_portlet/SampleRenderEvent?PortletPID=Z5_OGFLMKG108IGF0IANFDTU13085&PortletMode=view&PortletState=normal&RequestType=render&test=testValue HTTP/1.1" 200 -1 "http://localhost/Page/Z6_OGFLMKG108IGF0IANFDTU130G6/Site_Analytics" "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.9) Gecko/20100824 Firefox/3.6.9" "JSESSIONID=0000iLVC75NHzLRaJr-0mNVA6T5:-1