Line | Hits | Source |
---|---|---|
1 | /* | |
2 | Chrysalis Web Framework [http://chrysalis.sourceforge.net] | |
3 | Copyright (c) 2002, 2003, 2004, Paul Strack | |
4 | ||
5 | All rights reserved. | |
6 | ||
7 | Redistribution and use in source and binary forms, with or without | |
8 | modification, are permitted provided that the following conditions are met: | |
9 | ||
10 | 1. Redistributions of source code must retain the above copyright notice, this | |
11 | list of conditions and the following disclaimer. | |
12 | ||
13 | 2. Redistributions in binary form must reproduce the above copyright notice, | |
14 | this list of conditions and the following disclaimer in the documentation | |
15 | and/or other materials provided with the distribution. | |
16 | ||
17 | 3. Neither the name of the copyright holder nor the names of its contributors | |
18 | may be used to endorse or promote products derived from this software without | |
19 | specific prior written permission. | |
20 | ||
21 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | |
22 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
23 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
24 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR | |
25 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
26 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
27 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | |
28 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
29 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
30 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
31 | */ | |
32 | ||
33 | package org.chwf.servlet.engine; | |
34 | ||
35 | import java.io.IOException; | |
36 | ||
37 | import javax.servlet.FilterChain; | |
38 | import javax.servlet.ServletException; | |
39 | import javax.servlet.http.HttpServletRequest; | |
40 | import javax.servlet.http.HttpServletResponse; | |
41 | ||
42 | import org.chwf.plugin.Logger; | |
43 | import org.chwf.servlet.Controller; | |
44 | import org.chwf.servlet.InvocationContext; | |
45 | import org.chwf.servlet.ServletData; | |
46 | import org.chwf.servlet.ServletUtils; | |
47 | import org.chwf.servlet.filter.FilterSupport; | |
48 | ||
49 | /** | |
50 | * A filter that performs redirect operations to the view or error page, | |
51 | * based on the information in the InvocationContext. | |
52 | * | |
53 | * @author <a href="mailto:pfstrack@users.sourceforge.net">Paul Strack</a> | |
54 | */ | |
55 | 12 | public class RedirectFilter extends FilterSupport { |
56 | ||
57 | /** HTTP header for referer page. */ | |
58 | private static final String REFERER_HEADER = "referer"; | |
59 | ||
60 | /** | |
61 | * Process filter. | |
62 | * | |
63 | * @param request The request. | |
64 | * @param response The response. | |
65 | * @param chain The filter chain. | |
66 | * @throws ServletException For servlet exceptions. | |
67 | * @throws IOException For IO exceptions. | |
68 | */ | |
69 | public void doHttpFilter( | |
70 | HttpServletRequest request, | |
71 | HttpServletResponse response, | |
72 | FilterChain chain) | |
73 | throws IOException, ServletException { | |
74 | ||
75 | 7 | chain.doFilter(request, response); |
76 | 6 | InvocationContext context = InvokerFilter.getContext(request); |
77 | 6 | if (context.hasError()) { |
78 | 2 | handleError(context, request); |
79 | } else { | |
80 | 4 | String url = context.getRedirectURL(); |
81 | 4 | redirect(request, response, url); |
82 | } | |
83 | 6 | } |
84 | ||
85 | /** | |
86 | * Redirect to given URL. | |
87 | * | |
88 | * @param request The request. | |
89 | * @param response The response. | |
90 | * @param url URL | |
91 | * @throws IOException For I/O errors. | |
92 | */ | |
93 | public static void redirect( | |
94 | HttpServletRequest request, | |
95 | HttpServletResponse response, | |
96 | String url) | |
97 | throws IOException { | |
98 | ||
99 | 5 | if ((url.charAt(0) == '/') && URIMapper.PREPEND_CONTEXT) { |
100 | 4 | url = request.getContextPath() + url; |
101 | } | |
102 | 5 | response.sendRedirect(response.encodeRedirectURL(url.toString())); |
103 | 5 | } |
104 | ||
105 | /** | |
106 | * Handle error message. | |
107 | * | |
108 | * @param context The invocation context | |
109 | * @param request The request | |
110 | * @throws IOException For IO errors | |
111 | * @throws ServletException For servlet errors | |
112 | */ | |
113 | private static void handleError( | |
114 | InvocationContext context, | |
115 | HttpServletRequest request) | |
116 | throws IOException, ServletException { | |
117 | ||
118 | 2 | String errorPage = context.getErrorPage(); |
119 | 2 | Throwable error = context.getError(); |
120 | 2 | Logger.getInstance().log(CommandFilter.class, error.getMessage(), error); |
121 | 2 | if (errorPage.equals(Controller.REFERER_PAGE)) { |
122 | 1 | String referer = request.getHeader(REFERER_HEADER); |
123 | 1 | ServletData.redirectError(referer, error); |
124 | 1 | ServletUtils.cacheError(request, error); |
125 | } else { | |
126 | 1 | ServletData.forwardError(errorPage, error); |
127 | } | |
128 | 2 | } |
129 | } |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |