Return-Path: Delivered-To: apmail-jakarta-struts-dev-archive@apache.org Received: (qmail 26193 invoked from network); 2 Jun 2003 13:39:36 -0000 Received: from exchange.sun.com (192.18.33.10) by daedalus.apache.org with SMTP; 2 Jun 2003 13:39:36 -0000 Received: (qmail 19704 invoked by uid 97); 2 Jun 2003 13:41:49 -0000 Delivered-To: qmlist-jakarta-archive-struts-dev@nagoya.betaversion.org Received: (qmail 19697 invoked from network); 2 Jun 2003 13:41:49 -0000 Received: from daedalus.apache.org (HELO apache.org) (208.185.179.12) by nagoya.betaversion.org with SMTP; 2 Jun 2003 13:41:49 -0000 Received: (qmail 22687 invoked by uid 500); 2 Jun 2003 13:38:43 -0000 Mailing-List: contact struts-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Struts Developers List" Reply-To: "Struts Developers List" Delivered-To: mailing list struts-dev@jakarta.apache.org Received: (qmail 22638 invoked from network); 2 Jun 2003 13:38:42 -0000 Received: from unknown (HELO interscan.magnetbanking.com) (12.108.246.5) by daedalus.apache.org with SMTP; 2 Jun 2003 13:38:42 -0000 Received: by mag_exchange.magnetbanking.com with Internet Mail Service (5.5.2653.19) id ; Mon, 2 Jun 2003 09:39:39 -0400 Message-ID: <9DBAE48ABABAD611966500508BCDF3713D0486@MAGEXCH01> From: Jeff Robertson To: 'Struts Developers List' Subject: RE: composable RequestProcessor Date: Mon, 2 Jun 2003 09:41:19 -0400 MIME-Version: 1.0 X-Mailer: Internet Mail Service (5.5.2653.19) Content-Type: text/plain; charset="iso-8859-1" X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N Ok, here is exactly what we use here at Magnet. Please ignore the 1.0-ness. Also excuse the scarcity of Javadocs for the individual methods.. hopefully the stuff at the head of the class explains it. package com.magnetbanking.util.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.magnetbanking.util.logging.Loggers; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import java.util.*; /** * *

This servlet allows custom code to be inserted into various places in * the Struts request processing cycle. The custom classes are specified * by the init-parameters in web.xml and must implement the interfaces * described in the following table.

* *

All custom functionality that was once directly embedded in this * class has been factored out into processing classed that can be * plugged in here.

* * * * * * * * * * * * * * * * * * * * * * * * * * * *
init-paraminterfacedescription
preProcessorcom.magnetbanking.util.servlet.PreProcessorThis will be called within the processPreProcess method. * The process method's return code has the same semantics as * the processPreProcess mehtod itself - if false is returned, * no further process will take place. *
mappingProcessorcom.magnetbanking.util.servlet.MappingProcessorThis will be called as soon as the request path has been resolved * into an ActionMapping. If the process method returns * true, everything will proceed normally. If it returns false, the * servlet will behave as if no action mapping could be found for the * path specified. This is ideal for security checking, since it is done * before any Action or ActionForm instances are created, so if anything * strange is detected, the request can be stopped as soon as possible. * The disadvantage is that we don't have access to the ActionForm * or to the response object. *
actionPreProcessorcom.magnetbanking.util.servlet.ActionProcessorThe process method will be called immediately before * the Action instance's perform method. If the process * method returns false, the action will not be performed. *
actionPostProcessorcom.magnetbanking.util.servlet.ActionProcessorThe process method will be called immediately after * the Action instance's perform method. *
*

* If multiple classnames (separated by whitespace) are provided for any of the * above parameters, the classes will be called in the order in which they are * listed. There is a "logical AND" short-circuit behavior: if any processor * returns false from its process method, the following ones will not be called. * * @author Jeff Robertson * @author Scott Alexander Long * @version $Revision: 1.68.2.4 $ $Date: 2001/10/07 04:49:15 $ */ public class FilteringActionServlet extends org.apache.struts.action.ActionServlet { protected Collection preProcs = new ArrayList(1); protected Collection mappingProcs = new ArrayList(1); protected Collection actionPreProcs = new ArrayList(1); protected Collection actionPostProcs = new ArrayList(1); protected void initMapping() throws IOException, ServletException { super.initMapping(); initProcessors(); } protected void initProcessors() throws IOException, ServletException { initProcessors(this.preProcs, getServletConfig().getInitParameter("preProcessor")); initProcessors(this.mappingProcs, getServletConfig().getInitParameter("mappingProcessor")); initProcessors(this.actionPreProcs, getServletConfig().getInitParameter("actionPreProcessor")); initProcessors(this.actionPostProcs, getServletConfig().getInitParameter("actionPostProcessor")); } private void initProcessors(Collection procs, String s) { if( s == null || s.trim().length() == 0 ) { return; } for( StringTokenizer st = new StringTokenizer(s); st.hasMoreTokens(); ) { String procClass = st.nextToken().trim(); try { ProcessorConfig current = (ProcessorConfig) Class.forName(procClass).newInstance(); current.init(this); procs.add( current ); } catch (Exception e) { Loggers.util.servlet.error("Unable to instantiate processor " + procClass, e); } } } protected ActionMapping processMapping(String path, HttpServletRequest request) { ActionMapping mapping = super.processMapping(path, request); for( Iterator i = this.mappingProcs.iterator(); i.hasNext();) { MappingProcessor mp = (MappingProcessor) i.next(); if( ! mp.processMapping(mapping, request)) { return null; } } return mapping; } protected boolean processPreprocess(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { for( Iterator i = this.preProcs.iterator(); i.hasNext();) { PreProcessor p = (PreProcessor) i.next(); if( ! p.process(request,response)) { return false; } } return true; } protected ActionForward processActionPerform(Action action, ActionMapping mapping, ActionForm formInstance, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { for( Iterator i = this.actionPreProcs.iterator(); i.hasNext();) { ActionProcessor ap = (ActionProcessor) i.next(); if( ! ap.process(action,mapping, formInstance, request, response)) { return null; } } ActionForward forward = super.processActionPerform(action, mapping, formInstance, request, response); for( Iterator i = this.actionPreProcs.iterator(); i.hasNext();) { ActionProcessor ap = (ActionProcessor) i.next(); if( ! ap.process(action,mapping, formInstance, request, response)) { return forward; } } return (forward); } } --------------------------------------------------------------------- To unsubscribe, e-mail: struts-dev-unsubscribe@jakarta.apache.org For additional commands, e-mail: struts-dev-help@jakarta.apache.org