import java.io.IOException; import net.mycom.myapp.misc.myappProperties; import org.apache.log4j.Logger; import org.apache.log4j.MDC; import org.apache.struts2.dispatcher.ng.filter.*; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class PreStrutsFilter implememycom Filter { private static Logger logger = Logger.getLogger(PreStrutsFilter.class); Filter strutsFilter = new StrutsPrepareAndExecuteFilter(); private String bypassStrutsExp; private String bypassStrutsExpMobile; private String bypassVaadin; private ServletContext ctx; @Override public void destroy() { strutsFilter.destroy(); } @Override public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { if(req instanceof HttpServletRequest){ HttpServletRequest httpReq = (HttpServletRequest)req; String uri = httpReq.getRequestURI(); String ctxRoot = this.ctx.getContextPath(); uri = uri.replace(ctxRoot, ""); if(!uri.matches(bypassStrutsExp) && !uri.matches(bypassStrutsExpMobile) && !uri.matches(bypassVaadin) ){ // Browser cache images int dot = uri.lastIndexOf("."); if ((dot > 0) && (uri.substring(dot).equalsIgnoreCase(".jpg") || uri.substring(dot).equalsIgnoreCase(".jpeg") || uri.substring(dot).equalsIgnoreCase(".gif") || uri.substring(dot).equalsIgnoreCase(".png"))) { try { ((HttpServletResponse)res).setHeader("Cache-Control", "max-age=" + myappProperties.getCacheControlMaxAge()); } catch (Exception e) { ((HttpServletResponse)res).setHeader("Cache-Control", "max-age=120"); logger.error("Error retrieving cacheControlMaxAge", e); } } putMDC(httpReq); // Log user strutsFilter.doFilter(req, res, chain); MDC.clear(); // Remove from Mapped Diagnostic Context } else{ chain.doFilter(req, res); } } else{ chain.doFilter(req, res); } } @Override public void init(FilterConfig cfg) throws ServletException { strutsFilter.init(cfg); this.bypassStrutsExp = cfg.getInitParameter("struts_bypass_expression"); this.bypassStrutsExpMobile = cfg.getInitParameter("struts_bypass_expression_mobile"); this.bypassVaadin = cfg.getInitParameter("struts_bypass_vaadin"); this.ctx = cfg.getServletContext(); } private void putMDC(HttpServletRequest request) { Long accountId = (Long)request.getSession().getAttribute("accountId"); String sessionId = request.getSession().getId(); // If parameter exist put into Mapped Diagnostic Context if (accountId != null) { MDC.put("accountId", accountId); } if (sessionId != null) { MDC.put("sessionId", sessionId); } } }
Monday, January 19, 2015
Prestruts filter
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment