WebSphere Portal Filter

News Site news RSS

Latest articles New articles RSS

Latest news from Twitter Follow Secretkeeper4 and www.svesoftware.com on Twitter

WebSphere Portal Filter

Example of Portal filter which, if user agent is recognized as a bot, fixes a bug in portal url normalization for search crawlers.

In web.xml which can be found in

<path>\wp_profile\config\cells\<cell>\applications\wps.ear\deployments\wps\wps.war\WEB-INF

 

filter mapping must be set as first in order, and filter class:
 
 
<filter>
        <filter-name>SEO Portal Filter</filter-name>
        <filter-class>hr.portal.seo.SEOPortalFilter</filter-class>
</filter>
...
<filter-mapping>
        <filter-name>SEO Portal Filter</filter-name>
        <url-pattern>/myportal/*</url-pattern>        
</filter-mapping> 
<filter-mapping>
        <filter-name>SEO Portal Filter</filter-name>
        <url-pattern>/portal/*</url-pattern>        
</filter-mapping>

 

Source code of filter:
 
package hr.portal.seo;
 
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import com.ibm.wps.engine.HttpServletResponseWrapperOnOutputStream;
 
/**
 * Servlet Filter implementation class SEOFilter
 */
public class SEOPortalFilter implements Filter {
 
   Pattern pattern_bot = Pattern.compile("bot|ahoo");
   Pattern pattern_url = Pattern.compile("/ren/");
   private static String missing = "/pw/";
 
   private Logger log = Logger.getLogger(SEOPortalFilter.class.getName());
 
   private static final String USER_AGENT = "User-Agent";
   private static final String DEFAULT_CHARSET = "UTF-8";
 
   /**
    * Default constructor.
    */
   public SEOPortalFilter() {
      log.fine("Constructing: " + SEOPortalFilter.class.getName());
   }
 
   /**
    * @see Filter#destroy()
    */
   public void destroy() {
      log.finest("Destroying: " + SEOPortalFilter.class.getName());
   }
 
   /**
    * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
    */
   public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
      if (log.isLoggable(Level.FINEST)) {
         log.finest("Entering: doFilter");
      }
 
      String userAgent = ((HttpServletRequest) request).getHeader(USER_AGENT);
      Matcher matcher = pattern_bot.matcher(userAgent);
      Boolean bot = false;
      while (matcher.find()) {
         bot = true;
      }
      if (!response.isCommitted()) {
         if (log.isLoggable(Level.FINEST)) {
            log.finest("Response was not commited.");
         }
         if (chain != null) {
 
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            HttpServletResponseWrapperOnOutputStream hsrwoos = new HttpServletResponseWrapperOnOutputStream(bos, (HttpServletResponse) response);
            chain.doFilter(request, hsrwoos);
 
            bos.flush();
 
            StringBuffer fixed = null;
            if (log.isLoggable(Level.FINEST)) {
               log.finest("Character encoding: " + hsrwoos.getCharacterEncoding());
            }
            try {
               fixed = new StringBuffer(bos.toString(hsrwoos.getCharacterEncoding()));
            } catch (UnsupportedEncodingException e) {
               log.fine(e.getMessage());
               fixed = new StringBuffer(bos.toString(DEFAULT_CHARSET));
            }
 
            if (bot) {
               if (log.isLoggable(Level.FINEST)) {
                  log.finest("BOT has been detected, fixing normalized urls.");
               }
               List<Integer> places = new ArrayList<Integer>();
               Matcher matcher_bos = pattern_url.matcher(fixed);
               // Check all occurance
               while (matcher_bos.find()) {
                  places.add(matcher_bos.start() + 1 - 30);
               }
               int j = 0;
               if (log.isLoggable(Level.FINEST)) {
                  log.finest("Found places: " + places.size());
               }
               for (Integer i : places) {
                  fixed.insert(i + (j * missing.length()), missing);
                  j = j + 1;
               }
            }
 
            if (log.isLoggable(Level.FINEST)) {
               log.finest("Returning fixed: " + fixed.length());
            }
            response.getWriter().print(fixed.toString());
         }
 
      } else {
         if (log.isLoggable(Level.FINEST)) {
            log.finest("Response was comitted.");
         }
         if (chain != null) {
            chain.doFilter(request, response);
         }
      }
 
   }
 
   /**
    * @see Filter#init(FilterConfig)
    */
   public void init(FilterConfig fConfig) throws ServletException {
      log.fine("Filter has been initialized.");
   }
 
}
 
 
Jar file must be placed in PortalServer/shared/app folder, where portlet filters are also placed, for instance.