View Javadoc

1   /*
2    * $Id: CommandFilter.java 133 2004-11-28 13:37:27Z josem $
3    *
4    * Tarsis
5    * Copyright (C) 2002 Talika Open Source Group
6    *
7    * This program is free software; you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation; either version 2 of the License, or
10   * (at your option) any later version.
11   *
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program; if not, write to the Free Software
19   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20   *
21   */
22  
23  package org.talika.tarsis.filters;
24  
25  import javax.servlet.FilterConfig;
26  import javax.servlet.ServletException;
27  import javax.servlet.ServletRequest;
28  import javax.servlet.http.HttpServletRequest;
29  
30  import org.talika.tarsis.command.Command;
31  import org.talika.tarsis.command.factory.CommandFactory;
32  import org.talika.tarsis.util.CommandHelper;
33  
34  /**
35   * Base <code>Filter</code> implementation for all command aware filters.
36   *
37   * @author  Jose M. Palomar
38   * @version $Revision: 133 $
39   */
40  public abstract class CommandFilter extends AbstractFilter {
41  
42      // Fields
43      /**
44       * Command factory instance.
45       */
46      private CommandFactory commandFactory;
47  
48      /**
49       * Command helper instance.
50       */
51      private CommandHelper commandHelper;
52  
53      // Methods
54      /**
55       * Called by the web container to indicate to a filter that it is being placed
56       * into service.<br>
57       * <br>
58       * Initialization consits in calling super <code>init</code> method and storing
59       * in a local variable command factory instance for further use.
60       *
61       * @param filterConfig FilterConfig filter configutarion.
62       * @throws ServletException if an exception has occurred that interferes with the
63       * filter's normal operation
64       * @see javax.servlet.Filter#init(FilterConfig)
65       */
66      public void init(FilterConfig filterConfig) throws ServletException {
67  
68          super.init(filterConfig);
69  
70          this.commandFactory = getContext().getCommandFactory();
71          this.commandHelper = CommandHelper.getInstance();
72  
73      }
74  
75      /**
76       * Called by the web container to indicate to a filter that it is being taken out
77       * of service.
78       *
79       * @see javax.servlet.Filter#destroy()
80       */
81      public void destroy() {
82  
83          this.commandFactory = null;
84          this.commandHelper = null;
85  
86          super.destroy();
87  
88      }
89  
90     /**
91      * Returns <code>Command</code> instance for current request.
92      *
93      * @param request ServletRequest the <code>ServletResponse</code> object
94      * that contains the servlet's response.
95      * @return Command <code>Command</code> instance for current request or
96      * <code>null</code> if no command is found.
97      */
98     protected final Command findCommand(ServletRequest request) {
99  
100         // Find command
101         String commandName = commandHelper.getCommand((HttpServletRequest) request);
102         Command command = commandFactory.findCommand(commandName);
103 
104         return command;
105 
106    }
107 
108    /**
109     * Returns command factory instance.
110     *
111     * @return CommandFactory command factory instance.
112     */
113    protected final CommandFactory getCommandFactory() {
114        return commandFactory;
115    }
116 
117 }