diff --git a/core/src/main/java/org/apache/struts2/dispatcher/ServletActionRedirectResult.java b/core/src/main/java/org/apache/struts2/dispatcher/ServletActionRedirectResult.java index e4f7f9c93..8c80ec4e3 100644 --- a/core/src/main/java/org/apache/struts2/dispatcher/ServletActionRedirectResult.java +++ b/core/src/main/java/org/apache/struts2/dispatcher/ServletActionRedirectResult.java @@ -21,51 +21,60 @@ package org.apache.struts2.dispatcher; +import java.util.Arrays; +import java.util.List; + +import org.apache.struts2.dispatcher.mapper.ActionMapper; +import org.apache.struts2.dispatcher.mapper.ActionMapping; + import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.util.logging.Logger; import com.opensymphony.xwork2.util.logging.LoggerFactory; import com.opensymphony.xwork2.util.reflection.ReflectionExceptionHandler; -import org.apache.struts2.dispatcher.mapper.ActionMapper; -import org.apache.struts2.dispatcher.mapper.ActionMapping; - -import java.util.Arrays; -import java.util.List; /** * - * - * This result uses the {@link ActionMapper} provided by the {@link ActionMapperFactory} to redirect the browser to a - * URL that invokes the specified action and (optional) namespace. This is better than the {@link ServletRedirectResult} - * because it does not require you to encode the URL patterns processed by the {@link ActionMapper} in to your struts.xml - * configuration files. This means you can change your URL patterns at any point and your application will still work. - * It is strongly recommended that if you are redirecting to another action, you use this result rather than the + * + * This result uses the {@link ActionMapper} provided by the + * {@link ActionMapperFactory} to redirect the browser to a URL that invokes the + * specified action and (optional) namespace. This is better than the + * {@link ServletRedirectResult} because it does not require you to encode the + * URL patterns processed by the {@link ActionMapper} in to your struts.xml + * configuration files. This means you can change your URL patterns at any point + * and your application will still work. It is strongly recommended that if you + * are redirecting to another action, you use this result rather than the * standard redirect result. - * - * See examples below for an example of how request parameters could be passed in. - * + * + * See examples below for an example of how request parameters could be passed + * in. + * * - * + * * This result type takes the following parameters: - * + * * - * + * * - * + * * - * + * * Example: - * - *

+ * 
+ * 
+ * 
  * <package name="public" extends="struts-default">
  *     <action name="login" class="...">
  *         <!-- Redirect to another namespace -->
@@ -75,19 +84,19 @@ import java.util.List;
  *         </result>
  *     </action>
  * </package>
- *
+ * 
  * <package name="secure" extends="struts-default" namespace="/secure">
  *     <-- Redirect to an action in the same namespace -->
  *     <action name="dashboard" class="...">
  *         <result>dashboard.jsp</result>
  *         <result name="error" type="redirectAction">error</result>
  *     </action>
- *
+ * 
  *     <action name="error" class="...">
  *         <result>error.jsp</result>
  *     </action>
  * </package>
- *
+ * 
  * <package name="passingRequestParameters" extends="struts-default" namespace="/passingRequestParameters">
  *    <-- Pass parameters (reportType, width and height) -->
  *    <!--
@@ -102,14 +111,15 @@ import java.util.List;
  *          <param name="width">100</param>
  *          <param name="height">100</param>
  *          <param name="empty"></param>
- *          <param name="supressEmptyParameters">true</param>
+ *          <param name="suppressEmptyParameters">true</param>
  *       </result>
  *    </action>
  * </package>
- *
- *
- * 
- * + * + * + * + *
+ * * @see ActionMapper */ public class ServletActionRedirectResult extends ServletRedirectResult implements ReflectionExceptionHandler { @@ -137,7 +147,6 @@ public class ServletActionRedirectResult extends ServletRedirectResult implement this(null, actionName, method, null); } - public ServletActionRedirectResult(String namespace, String actionName, String method) { this(namespace, actionName, method, null); } @@ -149,21 +158,26 @@ public class ServletActionRedirectResult extends ServletRedirectResult implement this.method = method; } - /** * @see com.opensymphony.xwork2.Result#execute(com.opensymphony.xwork2.ActionInvocation) */ - public void execute(ActionInvocation invocation) throws Exception { + public void execute(ActionInvocation invocation) throws Exception + { actionName = conditionalParse(actionName, invocation); - if (namespace == null) { + if (namespace == null) + { namespace = invocation.getProxy().getNamespace(); - } else { + } + else + { namespace = conditionalParse(namespace, invocation); } - if (method == null) { + if (method == null) + { method = ""; } - else { + else + { method = conditionalParse(method, invocation); } @@ -176,34 +190,41 @@ public class ServletActionRedirectResult extends ServletRedirectResult implement /** * Sets the action name - * - * @param actionName The name + * + * @param actionName + * The name */ - public void setActionName(String actionName) { + public void setActionName(String actionName) + { this.actionName = actionName; } /** * Sets the namespace - * - * @param namespace The namespace + * + * @param namespace + * The namespace */ - public void setNamespace(String namespace) { + public void setNamespace(String namespace) + { this.namespace = namespace; } /** * Sets the method - * - * @param method The method + * + * @param method + * The method */ - public void setMethod(String method) { + public void setMethod(String method) + { this.method = method; } - protected List getProhibitedResultParams() { - return Arrays.asList(new String[]{ - DEFAULT_PARAM, "namespace", "method", "encode", "parse", "location", - "prependServletContext", "supressEmptyParameters", "anchor"}); + protected List getProhibitedResultParams() + { + return Arrays.asList(new String[] { + DEFAULT_PARAM, "namespace", "method", "encode", "parse", "location", "prependServletContext", "suppressEmptyParameters", "anchor" + }); } } diff --git a/core/src/main/java/org/apache/struts2/dispatcher/ServletRedirectResult.java b/core/src/main/java/org/apache/struts2/dispatcher/ServletRedirectResult.java index 8bb59c8c1..f779aa1c4 100644 --- a/core/src/main/java/org/apache/struts2/dispatcher/ServletRedirectResult.java +++ b/core/src/main/java/org/apache/struts2/dispatcher/ServletRedirectResult.java @@ -21,6 +21,23 @@ package org.apache.struts2.dispatcher; +import static javax.servlet.http.HttpServletResponse.SC_FOUND; + +import java.io.IOException; +import java.util.Arrays; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.struts2.ServletActionContext; +import org.apache.struts2.dispatcher.mapper.ActionMapper; +import org.apache.struts2.dispatcher.mapper.ActionMapping; +import org.apache.struts2.views.util.UrlHelper; + import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.config.entities.ResultConfig; @@ -29,26 +46,10 @@ import com.opensymphony.xwork2.util.logging.Logger; import com.opensymphony.xwork2.util.logging.LoggerFactory; import com.opensymphony.xwork2.util.reflection.ReflectionException; import com.opensymphony.xwork2.util.reflection.ReflectionExceptionHandler; -import org.apache.struts2.ServletActionContext; -import org.apache.struts2.dispatcher.mapper.ActionMapper; -import org.apache.struts2.dispatcher.mapper.ActionMapping; -import org.apache.struts2.views.util.UrlHelper; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; -import java.util.Arrays; -import java.util.Iterator; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - -import static javax.servlet.http.HttpServletResponse.SC_FOUND; - /** * - * + * * Calls the {@link HttpServletResponse#sendRedirect(String) sendRedirect} * method to the location specified. The response is told to redirect the * browser to the specified location (a new request from the client). The @@ -57,40 +58,42 @@ import static javax.servlet.http.HttpServletResponse.SC_FOUND; * available. This is because actions are built on a single-thread model. The * only way to pass data is through the session or with web parameters * (url?name=value) which can be OGNL expressions. - * + * * *

* This result type takes the following parameters: - * + * * - * + * *

- * + * *

* This result follows the same rules from {@link StrutsResultSupport}. *

- * + * * - * + * * Example: - * - *

+ * 
+ * 
+ * 
  * <result name="success" type="redirect">
  *   <param name="location">foo.jsp</param>
  *   <param name="parse">false</param>
  *   <param name="anchor">FRAGMENT</param>
  * </result>
- * 
- * + * + *
+ * */ public class ServletRedirectResult extends StrutsResultSupport implements ReflectionExceptionHandler { @@ -104,10 +107,10 @@ public class ServletRedirectResult extends StrutsResultSupport implements Reflec protected int statusCode = SC_FOUND; - protected boolean supressEmptyParameters = false; + protected boolean suppressEmptyParameters = false; protected Map requestParameters = new LinkedHashMap(); - + protected String anchor; public ServletRedirectResult() { @@ -117,41 +120,50 @@ public class ServletRedirectResult extends StrutsResultSupport implements Reflec public ServletRedirectResult(String location) { this(location, null); } - + public ServletRedirectResult(String location, String anchor) { super(location); this.anchor = anchor; } - + @Inject - public void setActionMapper(ActionMapper mapper) { + public void setActionMapper(ActionMapper mapper) + { this.actionMapper = mapper; } - public void setStatusCode(int code) { + public void setStatusCode(int code) + { this.statusCode = code; } /** * Set the optional anchor value. + * * @param anchor */ - public void setAnchor(String anchor) { + public void setAnchor(String anchor) + { this.anchor = anchor; } /** - * Sets whether or not to prepend the servlet context path to the redirected URL. - * - * @param prependServletContext true to prepend the location with the servlet context path, - * false otherwise. + * Sets whether or not to prepend the servlet context path to the redirected + * URL. + * + * @param prependServletContext + * true to prepend the location with the servlet context + * path, false otherwise. */ - public void setPrependServletContext(boolean prependServletContext) { + public void setPrependServletContext(boolean prependServletContext) + { this.prependServletContext = prependServletContext; } - - public void execute(ActionInvocation invocation) throws Exception { - if (anchor != null) { + + public void execute(ActionInvocation invocation) throws Exception + { + if (anchor != null) + { anchor = conditionalParse(anchor, invocation); } @@ -159,47 +171,62 @@ public class ServletRedirectResult extends StrutsResultSupport implements Reflec } /** - * Redirects to the location specified by calling {@link HttpServletResponse#sendRedirect(String)}. - * - * @param finalLocation the location to redirect to. - * @param invocation an encapsulation of the action execution state. - * @throws Exception if an error occurs when redirecting. + * Redirects to the location specified by calling + * {@link HttpServletResponse#sendRedirect(String)}. + * + * @param finalLocation + * the location to redirect to. + * @param invocation + * an encapsulation of the action execution state. + * @throws Exception + * if an error occurs when redirecting. */ - protected void doExecute(String finalLocation, ActionInvocation invocation) throws Exception { + protected void doExecute(String finalLocation, ActionInvocation invocation) throws Exception + { ActionContext ctx = invocation.getInvocationContext(); HttpServletRequest request = (HttpServletRequest) ctx.get(ServletActionContext.HTTP_REQUEST); HttpServletResponse response = (HttpServletResponse) ctx.get(ServletActionContext.HTTP_RESPONSE); - if (isPathUrl(finalLocation)) { - if (!finalLocation.startsWith("/")) { - ActionMapping mapping = actionMapper.getMapping( - request, Dispatcher.getInstance().getConfigurationManager()); + if (isPathUrl(finalLocation)) + { + if (!finalLocation.startsWith("/")) + { + ActionMapping mapping = actionMapper.getMapping(request, Dispatcher.getInstance().getConfigurationManager()); String namespace = null; - if (mapping != null) { + if (mapping != null) + { namespace = mapping.getNamespace(); } - if ((namespace != null) && (namespace.length() > 0) && (!"/".equals(namespace))) { + if ((namespace != null) && (namespace.length() > 0) && (!"/".equals(namespace))) + { finalLocation = namespace + "/" + finalLocation; - } else { + } + else + { finalLocation = "/" + finalLocation; } } // if the URL's are relative to the servlet context, append the servlet context path - if (prependServletContext && (request.getContextPath() != null) && (request.getContextPath().length() > 0)) { + if (prependServletContext && (request.getContextPath() != null) && (request.getContextPath().length() > 0)) + { finalLocation = request.getContextPath() + finalLocation; } ResultConfig resultConfig = invocation.getProxy().getConfig().getResults().get(invocation.getResultCode()); - if (resultConfig != null ) { + if (resultConfig != null) + { Map resultConfigParams = resultConfig.getParams(); - for (Iterator i = resultConfigParams.entrySet().iterator(); i.hasNext();) { + for (Iterator i = resultConfigParams.entrySet().iterator(); i.hasNext();) + { Map.Entry e = (Map.Entry) i.next(); - if (!getProhibitedResultParams().contains(e.getKey())) { + if (!getProhibitedResultParams().contains(e.getKey())) + { String potentialValue = e.getValue() == null ? "" : conditionalParse(e.getValue().toString(), invocation); - if (!supressEmptyParameters || ((potentialValue != null) && (potentialValue.length() > 0))) { + if (!suppressEmptyParameters || ((potentialValue != null) && (potentialValue.length() > 0))) + { requestParameters.put(e.getKey().toString(), potentialValue); } } @@ -210,39 +237,47 @@ public class ServletRedirectResult extends StrutsResultSupport implements Reflec UrlHelper.buildParametersString(requestParameters, tmpLocation, "&"); // add the anchor - if (anchor != null) { + if (anchor != null) + { tmpLocation.append('#').append(anchor); } finalLocation = response.encodeRedirectURL(tmpLocation.toString()); } - if (LOG.isDebugEnabled()) { + if (LOG.isDebugEnabled()) + { LOG.debug("Redirecting to finalLocation " + finalLocation); } sendRedirect(response, finalLocation); } - protected List getProhibitedResultParams() { - return Arrays.asList(new String[]{ - DEFAULT_PARAM, "namespace", "method", "encode", "parse", "location", - "prependServletContext", "supressEmptyParameters", "anchor"}); + protected List getProhibitedResultParams() + { + return Arrays.asList(new String[] { + DEFAULT_PARAM, "namespace", "method", "encode", "parse", "location", "prependServletContext", "suppressEmptyParameters", "anchor" + }); } - /** - * Sends the redirection. Can be overridden to customize how the redirect is handled (i.e. to use a different - * status code) - * - * @param response The response - * @param finalLocation The location URI + * Sends the redirection. Can be overridden to customize how the redirect is + * handled (i.e. to use a different status code) + * + * @param response + * The response + * @param finalLocation + * The location URI * @throws IOException */ - protected void sendRedirect(HttpServletResponse response, String finalLocation) throws IOException { - if (SC_FOUND == statusCode) { + protected void sendRedirect(HttpServletResponse response, String finalLocation) throws IOException + { + if (SC_FOUND == statusCode) + { response.sendRedirect(finalLocation); - } else { + } + else + { response.setStatus(statusCode); response.setHeader("Location", finalLocation); response.getWriter().write(finalLocation); @@ -251,7 +286,8 @@ public class ServletRedirectResult extends StrutsResultSupport implements Reflec } - private static boolean isPathUrl(String url) { + private static boolean isPathUrl(String url) + { // filter out "http:", "https:", "mailto:", "file:", "ftp:" // since the only valid places for : in URL's is before the path specification // either before the port, or after the protocol @@ -259,26 +295,32 @@ public class ServletRedirectResult extends StrutsResultSupport implements Reflec } /** - * Sets the supressEmptyParameters option - * - * @param supressEmptyParameters The new value for this option + * Sets the suppressEmptyParameters option + * + * @param suppressEmptyParameters + * The new value for this option */ - public void setSupressEmptyParameters(boolean supressEmptyParameters) { - this.supressEmptyParameters = supressEmptyParameters; + public void setSuppressEmptyParameters(boolean suppressEmptyParameters) + { + this.suppressEmptyParameters = suppressEmptyParameters; } /** * Adds a request parameter to be added to the redirect url - * - * @param key The parameter name - * @param value The parameter value + * + * @param key + * The parameter name + * @param value + * The parameter value */ - public ServletRedirectResult addParameter(String key, Object value) { + public ServletRedirectResult addParameter(String key, Object value) + { requestParameters.put(key, String.valueOf(value)); return this; } - public void handle(ReflectionException ex) { + public void handle(ReflectionException ex) + { // Only log as debug as they are probably parameters to be appended to the url LOG.debug(ex.getMessage(), ex); }