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 java.io.IOException;
26
27 import javax.servlet.Filter;
28 import javax.servlet.FilterChain;
29 import javax.servlet.FilterConfig;
30 import javax.servlet.ServletException;
31 import javax.servlet.ServletRequest;
32 import javax.servlet.ServletResponse;
33 import javax.servlet.UnavailableException;
34
35 import org.talika.tarsis.Globals;
36 import org.talika.tarsis.context.Context;
37 import org.talika.tarsis.log.Logger;
38
39 /**
40 * Base implementation of <code>Filter</code> interface for all web application
41 * filters of Tarsis MV Framework architecture.
42 *
43 * @author Jose M. Palomar
44 * @version $Revision: 269 $
45 */
46 public abstract class AbstractFilter implements Filter {
47
48
49 /**
50 * Filter configuration.
51 */
52 private FilterConfig filterConfig;
53
54 /**
55 * Tarsis context.
56 */
57 private Context context;
58
59 /**
60 * Tarsis logger.
61 */
62 private Logger logger;
63
64
65 /**
66 * Called by the web container to indicate to a filter that it is being placed
67 * into service.<br>
68 * <br>
69 * Base initialization consits in obtaining Tarsis context from servlet context,
70 * and saving filter configuration, Tarsis context and Tarsis logger for further
71 * use.
72 *
73 * @param filterConfig FilterConfig filter configutarion.
74 * @throws ServletException if an exception has occurred that interferes with the
75 * filter's normal operation
76 * @see javax.servlet.Filter#init(FilterConfig)
77 */
78 public void init(FilterConfig filterConfig) throws ServletException {
79 this.filterConfig = filterConfig;
80
81
82 this.context = (Context) filterConfig.getServletContext().getAttribute(Globals.CONTEXT_ATTR);
83 if (this.context == null) {
84 throw new UnavailableException("Null context!");
85 }
86 this.logger = this.context.getLogger();
87
88 if (this.logger.isInfoEnabled()) {
89 this.logger.logInfo(filterConfig.getFilterName() + ": init");
90 }
91
92 }
93
94 /**
95 * Called by the web container to indicate to a filter that it is being taken out
96 * of service.
97 *
98 * @see javax.servlet.Filter#destroy()
99 */
100 public void destroy() {
101
102 if (this.logger.isInfoEnabled()) {
103 this.logger.logInfo(filterConfig.getFilterName() + ": destroy");
104 }
105
106 }
107
108 /**
109 * The <code>doFilter</code> method of the Filter is called by the container each
110 * time a request/response pair is passed through the chain due to a client
111 * request for a resource at the end of the chain.
112 *
113 * @param servletRequest ServletRequest the <code>ServletRequest</code> object
114 * that contains the client's request.
115 * @param servletResponse ServletResponse the <code>ServletResponse</code> object
116 * that contains the servlet's response.
117 * @param filterChain FilterChain invocation chain of filtered request.
118 * @throws IOException if an input or output exception occurs.
119 * @throws ServletException if an exception has occurred that interferes with the
120 * filter's normal operation
121 * @see javax.servlet.Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
122 */
123 public abstract void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
124 FilterChain filterChain) throws IOException, ServletException;
125
126 /**
127 * Returns filter configuration.
128 *
129 * @return FilterConfig filter configuration.
130 */
131 protected final FilterConfig getFilterConfig() {
132 return filterConfig;
133 }
134
135 /**
136 * Returns the Tarsis context.
137 *
138 * @return Context the Tarsis context.
139 */
140 protected final Context getContext() {
141 return context;
142 }
143
144 /**
145 * Returns the Tarsis logger.
146 *
147 * @return Logger the Tarsis logger.
148 */
149 protected final Logger getLogger() {
150 return logger;
151 }
152
153 }