Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
RedirectFilter |
|
| 2.6666666666666665;2.667 |
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; |
|
18 | ||
19 | import javax.servlet.Filter; |
|
20 | import javax.servlet.FilterChain; |
|
21 | import javax.servlet.FilterConfig; |
|
22 | import javax.servlet.ServletException; |
|
23 | import javax.servlet.ServletRequest; |
|
24 | import javax.servlet.ServletResponse; |
|
25 | import javax.servlet.http.HttpServletRequest; |
|
26 | import javax.servlet.http.HttpServletResponse; |
|
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 | * <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 | 0 | public class RedirectFilter implements Filter |
49 | { |
|
50 | public static final String REDIRECT_PATH_PARAM = "redirect-path"; |
|
51 | ||
52 | 0 | private static final Log LOG = LogFactory.getLog(RedirectFilter.class); |
53 | ||
54 | private String _redirectPath; |
|
55 | ||
56 | public void init(FilterConfig config) throws ServletException |
|
57 | { |
|
58 | 0 | _redirectPath = config.getInitParameter(REDIRECT_PATH_PARAM); |
59 | ||
60 | 0 | if (HiveMind.isBlank(_redirectPath)) |
61 | 0 | _redirectPath = "/app"; |
62 | ||
63 | 0 | if (LOG.isDebugEnabled()) |
64 | 0 | LOG.debug(Tapestry.format("RedirectServlet.redirect-path", _redirectPath)); |
65 | 0 | } |
66 | ||
67 | public void destroy() |
|
68 | { |
|
69 | ||
70 | 0 | } |
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 request, ServletResponse response, FilterChain chain) |
|
79 | throws IOException, ServletException |
|
80 | { |
|
81 | 0 | HttpServletRequest hrequest = (HttpServletRequest) request; |
82 | 0 | HttpServletResponse hresponse = (HttpServletResponse) response; |
83 | ||
84 | 0 | String servletPath = hrequest.getServletPath(); |
85 | 0 | String 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 | 0 | if ((HiveMind.isBlank(servletPath) || servletPath.equals("/")) |
92 | && (HiveMind.isBlank(pathInfo) || pathInfo.equals("/"))) |
|
93 | { |
|
94 | 0 | String path = hrequest.getContextPath() + _redirectPath; |
95 | ||
96 | 0 | if (LOG.isDebugEnabled()) |
97 | 0 | LOG.debug(Tapestry.format("RedirectServlet.redirecting", path)); |
98 | ||
99 | 0 | hresponse.sendRedirect(path); |
100 | 0 | return; |
101 | } |
|
102 | ||
103 | 0 | chain.doFilter(request, response); |
104 | 0 | } |
105 | ||
106 | } |