KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > webapp > balancer > BalancerFilter


1 /*
2  * Copyright 2000,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.webapp.balancer;
17
18 import java.io.IOException JavaDoc;
19 import java.io.InputStream JavaDoc;
20
21 import java.net.URL JavaDoc;
22
23 import javax.servlet.Filter JavaDoc;
24 import javax.servlet.FilterChain JavaDoc;
25 import javax.servlet.FilterConfig JavaDoc;
26 import javax.servlet.ServletContext JavaDoc;
27 import javax.servlet.ServletException JavaDoc;
28 import javax.servlet.ServletRequest JavaDoc;
29 import javax.servlet.ServletResponse JavaDoc;
30 import javax.servlet.http.HttpServletRequest JavaDoc;
31 import javax.servlet.http.HttpServletResponse JavaDoc;
32
33
34 /**
35  * The balancer filter redirects incoming requests
36  * based on what rules they match. The rules
37  * are configurable via an XML document whose URL
38  * is specified as an init-param to this filter.
39  *
40  * @author Yoav Shapira
41  */

42 public class BalancerFilter implements Filter JavaDoc {
43     /**
44      * The rules this filter consults.
45      */

46     private RuleChain ruleChain;
47
48     /**
49      * The servlet context.
50      */

51     private ServletContext JavaDoc context;
52
53     /**
54      * Returns the rule chain.
55      *
56      * @return The rule chain
57      */

58     protected RuleChain getRuleChain() {
59         return ruleChain;
60     }
61
62     /**
63      * Initialize this filter.
64      *
65      * @param filterConfig The filter config
66      * @throws ServletException If an error occurs
67      */

68     public void init(FilterConfig JavaDoc filterConfig) throws ServletException JavaDoc {
69         context = filterConfig.getServletContext();
70
71         String JavaDoc configUrlParam = filterConfig.getInitParameter("configUrl");
72
73         if (configUrlParam == null) {
74             throw new ServletException JavaDoc("configUrl is required.");
75         }
76
77         try {
78             InputStream JavaDoc input = context.getResourceAsStream(configUrlParam);
79             RulesParser parser = new RulesParser(input);
80             ruleChain = parser.getResult();
81             context.log(
82                 getClass().getName() + ": init(): ruleChain: " + ruleChain);
83         } catch (Exception JavaDoc e) {
84             throw new ServletException JavaDoc(e);
85         }
86     }
87
88     /**
89      * Filter the incoming request.
90      * Consults the rule chain to see if
91      * any rules match this request, and if
92      * so redirects. Otherwise simply
93      * let request through.
94      *
95      * @param request The request
96      * @param response The response
97      * @param chain The filter chain
98      * @throws IOException If an error occurs
99      * @throws ServletException If an error occurs
100      */

101     public void doFilter(
102         ServletRequest JavaDoc request, ServletResponse JavaDoc response, FilterChain JavaDoc chain)
103         throws IOException JavaDoc, ServletException JavaDoc {
104         if (response.isCommitted()) {
105             context.log(
106                 getClass().getName()
107                 + ": doFilter(): not inspecting committed response.");
108             chain.doFilter(request, response);
109         } else if (!(request instanceof HttpServletRequest JavaDoc)) {
110             context.log(
111                 getClass().getName()
112                 + ": doFilter(): not inspecting non-Http request.");
113             chain.doFilter(request, response);
114         } else {
115             HttpServletRequest JavaDoc hreq = (HttpServletRequest JavaDoc) request;
116             HttpServletResponse JavaDoc hres = (HttpServletResponse JavaDoc) response;
117
118             URL JavaDoc redirectUrl = getRuleChain().evaluate(hreq);
119
120             if (redirectUrl != null) {
121                 String JavaDoc encoded =
122                     hres.encodeRedirectURL(redirectUrl.toString());
123
124                 context.log(
125                     getClass().getName()
126                     + ": doFilter(): redirecting request for "
127                     + hreq.getRequestURL().toString() + " to " + encoded);
128
129                 hres.sendRedirect(encoded);
130             } else {
131                 chain.doFilter(request, response);
132             }
133         }
134     }
135
136     /**
137      * Destroy this filter.
138      */

139     public void destroy() {
140         context = null;
141         ruleChain = null;
142     }
143 }
144
145
146 // End of file: BalanceFilter.java
147
Popular Tags