Query String Filter

Before couple of days i built a Getting Query String of portal URL from inside the portlet to demonstrate how you can read query string appended to the portal URL inside your portlet. That solution works by getting access to underlying HttpServletRequest object and reading query string from it. The QueryStringPortlet portlet developed in that blog entry works when accessed locally on the same portal server but it does not work if you consume that portlet on some other server using WSRP.

I built QueryStringFilter that will read the query parameters added to the portal URl and set them as normal render parameter, then i applied to the WSRP Proxy portlet and it works


import javax.servlet.http.HttpServletRequestWrapper;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class QueryStringFilter implements RenderFilter{
private static final Logger logger = LoggerFactory.getLogger(PageNameFilter.class);
public void doFilter(RenderRequest request, RenderResponse response,
FilterChain filterChain) throws IOException, PortletException {
logger.debug("Entering QueryStringFilter.doFilter()");
HttpServletRequest httpServletRequest = getHttpServletRequest(request);
System.out.println("Request parameter map " + httpServletRequest.getParameterMap());
QueryStringRenderRequestWrapper queryStringRequestWrapper = new QueryStringRenderRequestWrapper(request,httpServletRequest.getParameterMap());
filterChain.doFilter(queryStringRequestWrapper, response);
logger.debug("Exiting QueryStringFilter.doFilter()");
}
public void destroy() {
}
public void init(FilterConfig filterChain) throws PortletException {
}
private HttpServletRequest getHttpServletRequest(PortletRequest request){
HttpServletRequest httpServletRequest = (HttpServletRequest)request;
while(httpServletRequest instanceof HttpServletRequestWrapper){
HttpServletRequestWrapper httpServletRequestWrapper =
(HttpServletRequestWrapper)httpServletRequest;
httpServletRequest = (HttpServletRequest)httpServletRequestWrapper.getRequest();
}
return httpServletRequest;
}
}


The QueryStringFilter implements RequestFilter and in the doFilter() method i am reading underlying HttpServletRequest object and reading its parameters.

I also built this QueryStringRenderRequestWrapper that takes the query parameter from underlying HttpServletRequest object and forwards them as normal render parameter to the WSRP Proxy portlet


package com.webspherenotes.portlet.filter;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;
import javax.portlet.RenderRequest;
import javax.portlet.filter.RenderRequestWrapper;

public class QueryStringRenderRequestWrapper extends RenderRequestWrapper{
Map queryParameterMap ;
public QueryStringRenderRequestWrapper(RenderRequest request, Map queryParameterMap) {
super(request);
this.queryParameterMap = queryParameterMap;
}
public String getParameter(String name) {
System.out.println("Getparameter " + name);
String[] value = queryParameterMap.get(name);
System.out.println("Getparameter value " + value);
if(value != null)
return value[0];
return super.getParameter(name);
}
public Map getParameterMap() {
System.out.println();
Map oldParamMap = super.getParameterMap();
Map newParamMap = new HashMap();
Iterator oldParamIt = oldParamMap.keySet().iterator();
while(oldParamIt.hasNext()){
String paramName = oldParamIt.next();
String[] paramValue = super.getParameterValues(paramName);
newParamMap.put(paramName, paramValue);
}
Iterator queryParamIt = queryParameterMap.keySet().iterator();
while(queryParamIt.hasNext()){
String paramName = queryParamIt.next();
String[] paramValue = queryParameterMap.get(paramName);
newParamMap.put(paramName, paramValue);
}
return newParamMap;
}
public Enumeration getParameterNames() {
Enumeration oldParameters = super.getParameterNames();
Vector newParameter = new Vector();
while(oldParameters.hasMoreElements()){
String paramName = oldParameters.nextElement();
newParameter.add(paramName);
}
Iterator queryParamIt = queryParameterMap.keySet().iterator();
while(queryParamIt.hasNext()){
newParameter.add(queryParamIt.next());
}
System.out.println("Get parameterNames " + newParameter);
return newParameter.elements();
}
public String[] getParameterValues(String name) {
System.out.println("getParameterValues " + name);
String[] value = queryParameterMap.get(name);
if(value != null)
return value;
return super.getParameterValues(name);
}
}


The QueryStringRenderRequestWrapper class overrides RenderRequestWrapper and overrides its parameter related methods to inject some additional parameters that it got from the underlying HttpServletRequest

3 comments:

Indrasena Gangasani said...

This is npt working for me

Indrasena Gangasani said...

Hi sunil,

The same I applied,but it is NOT working for me

Abhi said...

Thanks for info....
SEO Company in Bangalore