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

5 comments:

ఇంద్రసేనా గంగసాని said...

This is npt working for me

ఇంద్రసేనా గంగసాని said...

Hi sunil,

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

srjwebsolutions said...

We are leading responsive website designing and development company in Noida.
We are offering mobile friendly responsive website designing, website development, e-commerce website, seo service and sem services in Noida.

Responsive Website Designing Company in Noida
Website Designing Company in Noida
SEO Services in Noida
SMO Services in Noida

Vikas Chaudhary said...

Battery Mantra is Authorized exide car battery dealer in Noida and Greater Noida. We are providing our service in Indirapuram, Delhi, Ashok Nagar.

Exide Battery Dealer in Noida
Battery Dealer in Noida
Authorized Battery Dealer in Noida
Car Battery Dealer in Noida
Car Battery Dealer
Exide Battery Dealer

EG MEDI said...

Egmedi.com is online medical store pharmacy in laxmi nagar Delhi. You can Order prescription/OTC medicines online.
Cash on Delivery available. Free Home Delivery


Online Pharmacy in Delhi
Buy Online medicine in Delhi
Online Pharmacy in laxmi nagar
Buy Online medicine in laxmi nagar
Onine Medical Store in Delhi
Online Medical store in laxmi nagar
Online medicine store in delhi
online medicine store in laxmi nagar
Purchase Medicine Online
Online Pharmacy India
Online Medical Store