[jboss-user] [JBoss Seam] - Re: Additional filter outside Seam-filter messes up encoding

Toby451 do-not-reply at jboss.com
Tue Oct 9 03:41:23 EDT 2007


Here it is (I can't see that it interfers with encoding in any way).
Application is being deployed on JBoss 4.2.1.GA.



  | import org.apache.commons.lang.StringUtils;
  | 
  | 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 java.io.IOException;
  | import java.util.ArrayList;
  | import java.util.Collections;
  | import java.util.List;
  | import java.util.Map;
  | 
  | /**
  |  * TimingFilter messures requests times and should be placed
  |  * outside (in the filter/servlet chain) the thing to be meassured.
  |  */
  | public class TimingFilter implements Filter {
  | 
  |     private FilterConfig config;
  |     private static final int MAX_ARRAYITEMS_PRINTED = 5;
  | 
  |     public void init(FilterConfig config) throws ServletException {
  |         this.config = config;
  |     }
  | 
  |     public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
  |         String name = "<non http request>";
  |         String method = "?";
  |         if (req instanceof HttpServletRequest) {
  |             HttpServletRequest servletRequest = (HttpServletRequest) req;
  |             name = servletRequest.getRequestURI();
  |             method = servletRequest.getMethod();
  |         }
  |         Map requestMap = req.getParameterMap();
  | 
  |         long t0 = System.nanoTime();
  |         chain.doFilter(req, res);
  |         long dt = System.nanoTime() - t0;
  | 
  |         config.getServletContext().log(
  |                 StringUtils.leftPad(formatTime(dt), 10) +
  |                 StringUtils.leftPad(method + " ", 6) +
  |                 StringUtils.rightPad(name, 26) +
  |                 StringUtils.rightPad(prettify(requestMap), 100)
  |         );
  |     }
  | 
  | 
  |     /**
  |      * Will format a nanosecond value as milliseconds with one decimal.
  |      */
  |     private String formatTime(long ns) {
  |         String ms = String.valueOf(ns / 100000);
  |         int pointPos = ms.length() - 1;
  |         return ms.substring(0, pointPos) + "." + ms.substring(pointPos) + " ms";
  |     }
  | 
  | 
  |     /**
  |      * Will convert the map into something readable
  |      */
  |     private String prettify(Map map) {
  |         StringBuilder sb = new StringBuilder();
  | 
  |         //noinspection unchecked
  |         List<String> keys = new ArrayList(map.keySet());
  |         Collections.sort(keys);
  | 
  |         for (String key : keys)
  |             sb.append(key).append("=").append(prettifyValue(map.get(key))).append(" ");
  | 
  |         return sb.length() > 0 ? sb.toString() : "<no parameters>";
  |     }
  | 
  | 
  |     /**
  |      * Will loop through array values and give pretty output.
  |      * (i.e. some widgets (grouped checkboxes) give multiple values)
  |      */
  |     private String prettifyValue(Object o) {
  | 
  |         // Non arrays we just convert to String.
  |         if (!(o instanceof Object[]))
  |             return o.toString();
  | 
  |         // For arrays we build a comma separated list. 
  |         StringBuilder sb = new StringBuilder("");
  |         Object[] arr = (Object[]) o;
  |         int itemsToPrint = Math.min(arr.length, MAX_ARRAYITEMS_PRINTED);
  |         for (int i = 0; i < itemsToPrint; i++)
  |             sb.append(arr).append(i < (itemsToPrint - 1) ? ", " : "");
  | 
  |         if (itemsToPrint == 1)
  |             return sb.toString();
  |         else
  |             return "[" + sb.toString() + "]";
  |     }
  | 
  |     public void destroy() {
  |         config = null;
  |     }
  | }
  | 
  | 

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4092871#4092871

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4092871



More information about the jboss-user mailing list