KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > RedirectFilter


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

15 package org.apache.tapestry;
16
17 import java.io.IOException JavaDoc;
18
19 import javax.servlet.Filter JavaDoc;
20 import javax.servlet.FilterChain JavaDoc;
21 import javax.servlet.FilterConfig JavaDoc;
22 import javax.servlet.ServletException JavaDoc;
23 import javax.servlet.ServletRequest JavaDoc;
24 import javax.servlet.ServletResponse JavaDoc;
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import javax.servlet.http.HttpServletResponse JavaDoc;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.hivemind.HiveMind;
31
32 /**
33  * Filter used to redirect a root context URL (i.e., "/context" or "/context/" to the Tapestry
34  * application servlet (typically, "/context/app"). This servlet is mapped to "/" and must have a
35  * &lt;init-parameter&;gt; <code>redirect-path</code> that is the application servlet's path
36  * (i.e., "/app"). If no value is specified, then "/app" is used. The path is always relative to the
37  * servlet context, and should always begin with a leading slash.
38  * <p>
39  * Filters are only available in Servlet API 2.3 and above.
40  * <p>
41  * Servlet API 2.4 is expected to allow a servlets in the welcome list (equivalent to index.html or
42  * index.jsp), at which point this filter should no longer be necessary.
43  *
44  * @author Howard Lewis Ship
45  * @since 3.0
46  */

47
48 public class RedirectFilter implements Filter JavaDoc
49 {
50     private static final Log LOG = LogFactory.getLog(RedirectFilter.class);
51
52     public static final String JavaDoc REDIRECT_PATH_PARAM = "redirect-path";
53
54     private String JavaDoc _redirectPath;
55
56     public void init(FilterConfig JavaDoc config) throws ServletException JavaDoc
57     {
58         _redirectPath = config.getInitParameter(REDIRECT_PATH_PARAM);
59
60         if (HiveMind.isBlank(_redirectPath))
61             _redirectPath = "/app";
62
63         if (LOG.isDebugEnabled())
64             LOG.debug(Tapestry.format("RedirectServlet.redirect-path", _redirectPath));
65     }
66
67     public void destroy()
68     {
69
70     }
71
72     /**
73      * This filter intercepts the so-called "default" servlet, whose job is to provide access to
74      * standard resources packaged within the web application context. This code is interested in
75      * only the very root, redirecting to the appropriate Tapestry application servlet. Other values
76      * are passed through unchanged.
77      */

78     public void doFilter(ServletRequest JavaDoc request, ServletResponse JavaDoc response, FilterChain JavaDoc chain)
79             throws IOException JavaDoc, ServletException JavaDoc
80     {
81         HttpServletRequest JavaDoc hrequest = (HttpServletRequest JavaDoc) request;
82         HttpServletResponse JavaDoc hresponse = (HttpServletResponse JavaDoc) response;
83
84         String JavaDoc servletPath = hrequest.getServletPath();
85         String JavaDoc pathInfo = hrequest.getPathInfo();
86
87         // Been experimenting with different servlet containers. In Jetty 4.2.8 and Tomcat 4.1,
88
// resources have a non-null servletPath. If JBossWeb 3.0.6, the servletPath is
89
// null and the pathInfo indicates the relative location of the resource.
90

91         if ((HiveMind.isBlank(servletPath) || servletPath.equals("/"))
92                 && (HiveMind.isBlank(pathInfo) || pathInfo.equals("/")))
93         {
94             String JavaDoc path = hrequest.getContextPath() + _redirectPath;
95
96             if (LOG.isDebugEnabled())
97                 LOG.debug(Tapestry.format("RedirectServlet.redirecting", path));
98
99             hresponse.sendRedirect(path);
100             return;
101         }
102
103         chain.doFilter(request, response);
104     }
105
106 }
Popular Tags