KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > dispatcher > ServletRedirectResult


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.webwork.dispatcher;
6
7 import com.opensymphony.webwork.ServletActionContext;
8 import com.opensymphony.webwork.dispatcher.mapper.ActionMapperFactory;
9 import com.opensymphony.xwork.ActionInvocation;
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12
13 import javax.servlet.http.HttpServletRequest JavaDoc;
14 import javax.servlet.http.HttpServletResponse JavaDoc;
15
16
17 /**
18  * Calls the {@link HttpServletResponse#sendRedirect(String) sendRedirect} method to the location specified. <p>
19  * <p/>
20  * This result follows the same rules from {@link WebWorkResultSupport}.
21  *
22  * @author Patrick Lightbody
23  */

24 public class ServletRedirectResult extends WebWorkResultSupport {
25     //~ Static fields/initializers /////////////////////////////////////////////
26

27     private static final Log log = LogFactory.getLog(ServletRedirectResult.class);
28
29     //~ Instance fields ////////////////////////////////////////////////////////
30

31     protected boolean prependServletContext = true;
32
33     //~ Methods ////////////////////////////////////////////////////////////////
34

35     /**
36      * Sets whether or not to prepend the servlet context path to the redirected URL.
37      *
38      * @param prependServletContext <tt>true</tt> to prepend the location with the servlet context path,
39      * <tt>false</tt> otherwise.
40      */

41     public void setPrependServletContext(boolean prependServletContext) {
42         this.prependServletContext = prependServletContext;
43     }
44
45     /**
46      * Redirects to the location specified by calling {@link HttpServletResponse#sendRedirect(String)}.
47      *
48      * @param finalLocation the location to redirect to.
49      * @param invocation an encapsulation of the action execution state.
50      * @throws Exception if an error occurs when redirecting.
51      */

52     protected void doExecute(String JavaDoc finalLocation, ActionInvocation invocation) throws Exception JavaDoc {
53         HttpServletRequest JavaDoc request = ServletActionContext.getRequest();
54         HttpServletResponse JavaDoc response = ServletActionContext.getResponse();
55
56         if (isPathUrl(finalLocation)) {
57             if (!finalLocation.startsWith("/")) {
58                 String JavaDoc namespace = ActionMapperFactory.getMapper().getMapping(request).getNamespace();
59
60                 if ((namespace != null) && (namespace.length() > 0)) {
61                     finalLocation = namespace + "/" + finalLocation;
62                 } else {
63                     finalLocation = "/" + finalLocation;
64                 }
65             }
66
67             // if the URL's are relative to the servlet context, append the servlet context path
68
if (prependServletContext && (request.getContextPath() != null) && (request.getContextPath().length() > 0)) {
69                 finalLocation = request.getContextPath() + finalLocation;
70             }
71
72             finalLocation = response.encodeRedirectURL(finalLocation);
73         }
74
75         if (log.isDebugEnabled()) {
76             log.debug("Redirecting to finalLocation " + finalLocation);
77         }
78
79         response.sendRedirect(finalLocation);
80     }
81
82     private static boolean isPathUrl(String JavaDoc url) {
83         // filter out "http:", "https:", "mailto:", "file:", "ftp:"
84
// since the only valid places for : in URL's is before the path specification
85
// either before the port, or after the protocol
86
return (url.indexOf(':') == -1);
87     }
88 }
89
Popular Tags