craigmcc 2003/10/04 20:02:34 Modified: chain/src/java/org/apache/commons/chain/web ChainServlet.java Added: chain/src/java/org/apache/commons/chain/web/servlet PathInfoMapper.java RequestParameterMapper.java ServletPathMapper.java Log: Add a trio of useful Command implementations suitable for use as the "default" command to be executed by CommandProcessor. They support mapping of an incoming request to a particular command based on the extra path information (PathInfoMapper), a specified request parameter value (RequsetParameterMapper), or the servlet path information (ServletPathMapper) from the incoming request. Revision Changes Path 1.4 +10 -11 jakarta-commons-sandbox/chain/src/java/org/apache/commons/chain/web/ChainServlet.java Index: ChainServlet.java =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/chain/src/java/org/apache/commons/chain/web/ChainServlet.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- ChainServlet.java 4 Oct 2003 22:54:10 -0000 1.3 +++ ChainServlet.java 5 Oct 2003 03:02:34 -0000 1.4 @@ -65,12 +65,12 @@ import java.io.InputStream; import java.io.IOException; import java.net.URL; -import javax.servlet.GenericServlet; import javax.servlet.ServletConfig; import javax.servlet.ServletContext; import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import org.apache.commons.chain.Catalog; import org.apache.commons.chain.config.ConfigParser; import org.apache.commons.chain.impl.CatalogBase; @@ -130,7 +130,7 @@ * @author Ted Husted */ -public class ChainServlet extends GenericServlet { +public class ChainServlet extends HttpServlet { // ------------------------------------------------------ Manifest Constants @@ -148,7 +148,7 @@ /** * <p>The default servlet context attribute key.</p> */ - private static final String CONFIG_ATTR_DEFAULT = "catalog"; + public static final String CONFIG_ATTR_DEFAULT = "catalog"; /** @@ -253,14 +253,13 @@ * and store it in the servlet context.</p> * * @param request the request issued by the client - * * @param response the response to be returned to the cliengt * * @throws javax.servlet.ServletException (this exception is never thrown) - * * @throws java.io.IOException (this exception is never thrown) */ - public void service(ServletRequest request, ServletResponse response) + public void service(HttpServletRequest request, + HttpServletResponse response) throws ServletException, IOException { ; // do nothing 1.1 jakarta-commons-sandbox/chain/src/java/org/apache/commons/chain/web/servlet/PathInfoMapper.java Index: PathInfoMapper.java =================================================================== /* * $Header: /home/cvs/jakarta-commons-sandbox/chain/src/java/org/apache/commons/chain/web/servlet/PathInfoMapper.java,v 1.1 2003/10/05 03:02:34 craigmcc Exp $ * $Revision: 1.1 $ * $Date: 2003/10/05 03:02:34 $ * * ==================================================================== * * The Apache Software License, Version 1.1 * * Copyright (c) 1999-2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, if * any, must include the following acknowlegement: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowlegement may appear in the software itself, * if and wherever such third-party acknowlegements normally appear. * * 4. The names "The Jakarta Project", "Commons", and "Apache Software * Foundation" must not be used to endorse or promote products derived * from this software without prior written permission. For written * permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache" * nor may "Apache" appear in their names without prior written * permission of the Apache Group. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * */ package org.apache.commons.chain.web.servlet; import javax.servlet.http.HttpServletRequest; import org.apache.commons.chain.Catalog; import org.apache.commons.chain.Command; import org.apache.commons.chain.Context; /** * <p>[EMAIL PROTECTED] Command} that uses the "path info" component of the request URI * to select a [EMAIL PROTECTED] Command} from the appropriate [EMAIL PROTECTED] Catalog}, and * execute it. To use this command, you would typically map an instance * of [EMAIL PROTECTED] ChainProcessor} to a wildcard pattern like "/execute/*" and * then arrange that this is the default command to be executed. In such * an environment, a request for the context-relative URI "/execute/foo" * would cause the "/foo" command to be loaded and executed.</p> * * @author Craig R. McClanahan */ public class PathInfoMapper implements Command { // ------------------------------------------------------ Instance Variables private String catalogKey = ChainProcessor.CONFIG_ATTR_DEFAULT; // -------------------------------------------------------------- Properties /** * <p>Return the context key under which our [EMAIL PROTECTED] Catalog} has been * stored.</p> */ public String getCatalogKey() { return (this.catalogKey); } /** * <p>Set the context key under which our [EMAIL PROTECTED] Catalog} has been * stored.</p> * * @param catalogKey The new catalog key */ public void setCatalogKey(String catalogKey) { this.catalogKey = catalogKey; } // --------------------------------------------------------- Command Methods /** * <p>Look up the extra path information for this request, and use it to * select an appropriate [EMAIL PROTECTED] Command} to be executed. * * @param context Context for the current request */ public boolean execute(Context context) throws Exception { // Look up the extra path information for this request ServletWebContext swcontext = (ServletWebContext) context; HttpServletRequest request = swcontext.getRequest(); String pathInfo = (String) request.getAttribute("javax.servlet.include.path_info"); if (pathInfo == null) { pathInfo = request.getPathInfo(); } // Map to the Command specified by the extra path info Catalog catalog = (Catalog) context.get(getCatalogKey()); Command command = catalog.getCommand(pathInfo); return (command.execute(context)); } } 1.1 jakarta-commons-sandbox/chain/src/java/org/apache/commons/chain/web/servlet/RequestParameterMapper.java Index: RequestParameterMapper.java =================================================================== /* * $Header: /home/cvs/jakarta-commons-sandbox/chain/src/java/org/apache/commons/chain/web/servlet/RequestParameterMapper.java,v 1.1 2003/10/05 03:02:34 craigmcc Exp $ * $Revision: 1.1 $ * $Date: 2003/10/05 03:02:34 $ * * ==================================================================== * * The Apache Software License, Version 1.1 * * Copyright (c) 1999-2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, if * any, must include the following acknowlegement: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowlegement may appear in the software itself, * if and wherever such third-party acknowlegements normally appear. * * 4. The names "The Jakarta Project", "Commons", and "Apache Software * Foundation" must not be used to endorse or promote products derived * from this software without prior written permission. For written * permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache" * nor may "Apache" appear in their names without prior written * permission of the Apache Group. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * */ package org.apache.commons.chain.web.servlet; import javax.servlet.http.HttpServletRequest; import org.apache.commons.chain.Catalog; import org.apache.commons.chain.Command; import org.apache.commons.chain.Context; /** * <p>[EMAIL PROTECTED] Command} that uses a specified request parameter * to select a [EMAIL PROTECTED] Command} from the appropriate [EMAIL PROTECTED] Catalog}, and * execute it. To use this command, you would typically map an instance * of [EMAIL PROTECTED] ChainProcessor} to a wildcard pattern like "*.execute" and * then arrange that this is the default command to be executed. In such * an environment, a request for the context-relative path * "/foo.execute?command=bar" would cause the "/bar" command to be loaded * and executed.</p> * * @author Craig R. McClanahan */ public class RequestParameterMapper implements Command { // ------------------------------------------------------ Instance Variables private String catalogKey = ChainProcessor.CONFIG_ATTR_DEFAULT; private String parameter = "command"; // -------------------------------------------------------------- Properties /** * <p>Return the context key under which our [EMAIL PROTECTED] Catalog} has been * stored.</p> */ public String getCatalogKey() { return (this.catalogKey); } /** * <p>Set the context key under which our [EMAIL PROTECTED] Catalog} has been * stored.</p> * * @param catalogKey The new catalog key */ public void setCatalogKey(String catalogKey) { this.catalogKey = catalogKey; } /** * <p>Return the name of the request parameter to use for * selecting the [EMAIL PROTECTED] Command} to be executed.</p> */ public String getParameter() { return (this.parameter); } /** * <p>Set the name of the request parameter to use for * selecting the [EMAIL PROTECTED] Command} to be executed.</p> * * @param parameter The new parameter name */ public void setParameter(String parameter) { this.parameter = parameter; } // --------------------------------------------------------- Command Methods /** * <p>Look up the specified request paramater for this request, and use it * to select an appropriate [EMAIL PROTECTED] Command} to be executed. * * @param context Context for the current request */ public boolean execute(Context context) throws Exception { // Look up the specified request parameter for this request ServletWebContext swcontext = (ServletWebContext) context; HttpServletRequest request = swcontext.getRequest(); String value = request.getParameter(getParameter()); // Map to the Command specified by the extra path info Catalog catalog = (Catalog) context.get(getCatalogKey()); Command command = catalog.getCommand(value); return (command.execute(context)); } } 1.1 jakarta-commons-sandbox/chain/src/java/org/apache/commons/chain/web/servlet/ServletPathMapper.java Index: ServletPathMapper.java =================================================================== /* * $Header: /home/cvs/jakarta-commons-sandbox/chain/src/java/org/apache/commons/chain/web/servlet/ServletPathMapper.java,v 1.1 2003/10/05 03:02:34 craigmcc Exp $ * $Revision: 1.1 $ * $Date: 2003/10/05 03:02:34 $ * * ==================================================================== * * The Apache Software License, Version 1.1 * * Copyright (c) 1999-2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, if * any, must include the following acknowlegement: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowlegement may appear in the software itself, * if and wherever such third-party acknowlegements normally appear. * * 4. The names "The Jakarta Project", "Commons", and "Apache Software * Foundation" must not be used to endorse or promote products derived * from this software without prior written permission. For written * permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache" * nor may "Apache" appear in their names without prior written * permission of the Apache Group. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * */ package org.apache.commons.chain.web.servlet; import javax.servlet.http.HttpServletRequest; import org.apache.commons.chain.Catalog; import org.apache.commons.chain.Command; import org.apache.commons.chain.Context; /** * <p>[EMAIL PROTECTED] Command} that uses the "servlet path" component of the request URI * to select a [EMAIL PROTECTED] Command} from the appropriate [EMAIL PROTECTED] Catalog}, and * execute it. To use this command, you would typically map an instance * of [EMAIL PROTECTED] ChainProcessor} to an extension pattern like "*.execute" and * then arrange that this is the default command to be executed. In such * an environment, a request for a context relative URI of "/foo.execute" * would cause the "/foo.execute" command to be loaded and executed.</p> * * @author Craig R. McClanahan */ public class ServletPathMapper implements Command { // ------------------------------------------------------ Instance Variables private String catalogKey = ChainProcessor.CONFIG_ATTR_DEFAULT; // -------------------------------------------------------------- Properties /** * <p>Return the context key under which our [EMAIL PROTECTED] Catalog} has been * stored.</p> */ public String getCatalogKey() { return (this.catalogKey); } /** * <p>Set the context key under which our [EMAIL PROTECTED] Catalog} has been * stored.</p> * * @param catalogKey The new catalog key */ public void setCatalogKey(String catalogKey) { this.catalogKey = catalogKey; } // --------------------------------------------------------- Command Methods /** * <p>Look up the servlet path information for this request, and use it to * select an appropriate [EMAIL PROTECTED] Command} to be executed. * * @param context Context for the current request */ public boolean execute(Context context) throws Exception { // Look up the servlet path for this request ServletWebContext swcontext = (ServletWebContext) context; HttpServletRequest request = swcontext.getRequest(); String servletPath = (String) request.getAttribute("javax.servlet.include.servlet_path"); if (servletPath == null) { servletPath = request.getServletPath(); } // Map to the Command specified by the extra path info Catalog catalog = (Catalog) context.get(getCatalogKey()); Command command = catalog.getCommand(servletPath); return (command.execute(context)); } }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]