Coverage details for org.chwf.servlet.engine.RedirectFilter

LineHitsSource
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  */
5512public 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  
757    chain.doFilter(request, response);
766    InvocationContext context = InvokerFilter.getContext(request);
776    if (context.hasError()) {
782      handleError(context, request);
79     } else {
804      String url = context.getRedirectURL();
814      redirect(request, response, url);
82     }
836  }
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     
995    if ((url.charAt(0) == '/') && URIMapper.PREPEND_CONTEXT) {
1004      url = request.getContextPath() + url;
101     }
1025    response.sendRedirect(response.encodeRedirectURL(url.toString()));
1035  }
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  
1182    String errorPage = context.getErrorPage();
1192    Throwable error = context.getError();
1202    Logger.getInstance().log(CommandFilter.class, error.getMessage(), error);
1212    if (errorPage.equals(Controller.REFERER_PAGE)) {
1221      String referer = request.getHeader(REFERER_HEADER);
1231      ServletData.redirectError(referer, error);
1241      ServletUtils.cacheError(request, error);
125     } else {
1261      ServletData.forwardError(errorPage, error);
127     }
1282  }
129 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.