1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
43 /**
44 * Command factory instance.
45 */
46 private CommandFactory commandFactory;
47
48 /**
49 * Command helper instance.
50 */
51 private CommandHelper commandHelper;
52
53
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
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 }