KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > servlet > JumpersFilter


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.servlet;
11
12 import javax.servlet.*;
13 import javax.servlet.http.*;
14 import java.net.URI JavaDoc;
15 import java.net.URISyntaxException JavaDoc;
16 import org.mmbase.module.builders.Jumpers;
17 import org.mmbase.module.core.*;
18 import org.mmbase.util.logging.*;
19
20 /**
21  * Redirects request based on information supplied by the jumpers builder.
22  *
23  * @application Tools, Jumpers
24  * @author Jaco de Groot
25  * @version $Id: JumpersFilter.java,v 1.16 2006/06/27 14:36:58 johannes Exp $
26  */

27 public class JumpersFilter implements Filter, MMBaseStarter {
28     private static final Logger log = Logging.getLoggerInstance(JumpersFilter.class);
29     private static MMBase mmbase;
30     private static Jumpers jumpers;
31     private static String JavaDoc name;
32
33     private Thread JavaDoc initThread;
34
35     /**
36      * Supported for use with older versions of the servlet api, such as used by Orion 1.5.2
37      * This method simply thows an exception when called.
38      * @deprecated will be dropped in future versions
39      */

40     public void setFilterConfig(FilterConfig fc) {
41         log.info("Setting filter-config");
42         throw new UnsupportedOperationException JavaDoc("This method is not part of the Servlet api 2.3");
43     }
44
45     /**
46      * Supported for use with older versions of the servlet api, such as used by Orion 1.5.2
47      * This method simply thows an exception when called.
48      * @deprecated will be dropped in future versions
49      */

50     public FilterConfig getFilterConfig() {
51         log.info("Getting filter-config");
52         throw new UnsupportedOperationException JavaDoc("This method is not part of the Servlet api 2.3");
53     }
54
55     public MMBase getMMBase() {
56         return mmbase;
57     }
58
59     public void setMMBase(MMBase mmb) {
60         mmbase = mmb;
61     }
62
63     public void setInitException(ServletException se) {
64         // never mind, simply, ignore
65
}
66
67     /**
68      * Initializes the filter
69      */

70     public void init(javax.servlet.FilterConfig JavaDoc filterConfig) throws ServletException {
71         log.info("Starting JumpersFilter with " + filterConfig);
72         name = filterConfig.getFilterName();
73         MMBaseContext.init(filterConfig.getServletContext());
74         MMBaseContext.initHtmlRoot();
75         // stuff that can take indefinite amount of time (database down and so on) is done in separate thread
76
initThread = new MMBaseStartThread(this);
77         initThread.start();
78     }
79
80     /**
81      * Filters the request: tries to find a jumper and redirects to this url when found, otherwise the
82      * request will be handled somewhere else in the filterchain.
83      * @param servletRequest The ServletRequest.
84      * @param servletResponse The ServletResponse.
85      * @param filterChain The FilterChain.
86      */

87     public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws java.io.IOException JavaDoc, ServletException {
88         if (mmbase == null) {
89             filterChain.doFilter(servletRequest, servletResponse);
90             return;
91         }
92         if (jumpers == null) {
93             if (mmbase != null) {
94                 jumpers = (Jumpers)mmbase.getBuilder("jumpers");
95             }
96             if (jumpers == null) {
97                 filterChain.doFilter(servletRequest, servletResponse);
98                 return; // nothing to be done
99
}
100         }
101         HttpServletRequest req = (HttpServletRequest)servletRequest;
102         HttpServletResponse res = (HttpServletResponse)servletResponse;
103         /*
104          * getContextPath()
105          * Returns the portion of the request URI that indicates the context of the request.
106          * The context path always comes first in a request URI.
107          * The path starts with a "/" character but does not end with a "/" character.
108          * For servlets in the default (root) context, this method returns "". The container does not decode this string.
109          */

110         String JavaDoc context = "";
111         context = req.getContextPath();
112         int contextPart = context.length();
113         String JavaDoc reqURI = req.getRequestURI();
114         String JavaDoc key = "";
115         if (contextPart < reqURI.length()) {
116             // also remove the leading "/", unless it's an empty string.
117
key = req.getRequestURI().substring(contextPart+1);
118         }
119
120         if (log.isDebugEnabled()) {
121             log.debug("contextpath is: " + context);
122             log.debug("key is: " + key);
123             log.debug("uri is: " + reqURI);
124         }
125         
126         //ignore keys with extensions
127
if (key.indexOf('.') == -1 ) {
128             // because Tomcat version > 5.0.5 always adds a trailing slash if
129
// there's a directory with the same name, the trailing slash must be removed
130
if (key.endsWith("/")) {
131                 key = key.substring(0,key.length()-1);
132                 if (log.isDebugEnabled()){
133                     log.debug("after removing trailing slash key becomes: " + key);
134                 }
135             }
136             //get jumper from Jumpers builder
137
String JavaDoc url = jumpers.getJump(key);
138             if (url != null) {
139                 /*
140                  * Sends a temporary redirect response to the client using the specified redirect location URL.
141                  * This method can accept relative URLs; the servlet container must convert the relative URL
142                  * to an absolute URL before sending the response to the client. If the location is relative without a leading '/' the
143                  * container interprets it as relative to the current request URI. If the location is relative with a leading '/' the container
144                  * interprets it as relative to the servlet container root.
145                  * If the response has already been committed, this method throws an IllegalStateException.
146                  * After using this method, the response should be considered to be committed and should not be written to.
147                  */

148                 // res.sendRedirect(res.encodeRedirectURL(req.getContextPath() + url));
149

150                 // there can be different types of jumper uris which all must behandled
151
// differently:
152
// - absolute with a scheme (http://www.servername.nl/context/bla/bla123), muste be redirected
153
// - absolute without a scheme (/bla/bla123), must be redirected
154
// - relative (bla/bla123), must be made absolute and then be redirected
155
URI JavaDoc redirURI = null;
156                 try {
157                     redirURI = new URI JavaDoc(url);
158                 } catch (URISyntaxException JavaDoc ex) {
159                     log.error ("jumper URI syntax is not valid");
160                 }
161                 if (redirURI != null) {
162                     if (redirURI.isAbsolute()) {
163                         res.sendRedirect(url);
164                     } else if (url.startsWith("/")) {
165                         res.sendRedirect(url);
166                     } else {
167                         res.sendRedirect(context +"/" + url);
168                     }
169                 }
170                 return;
171             }
172         }
173         
174         filterChain.doFilter(servletRequest, servletResponse);
175     }
176
177     /**
178      * destroys the filter
179      */

180     public void destroy() {
181         log.info("Filter " + name + " destroyed.");
182         initThread.interrupt();
183     }
184
185
186 }
187
Popular Tags