Author: lukaszlenart Date: Tue Dec 18 20:27:39 2012 New Revision: 1423621 URL: http://svn.apache.org/viewvc?rev=1423621&view=rev Log: WW-3906 adds support to parse all parameters of HttpHeaderResult
Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/HttpHeaderResult.java struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/HttpHeaderResultTest.java Modified: struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/HttpHeaderResult.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/HttpHeaderResult.java?rev=1423621&r1=1423620&r2=1423621&view=diff ============================================================================== --- struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/HttpHeaderResult.java (original) +++ struts/struts2/trunk/core/src/main/java/org/apache/struts2/dispatcher/HttpHeaderResult.java Tue Dec 18 20:27:39 2012 @@ -26,6 +26,8 @@ import com.opensymphony.xwork2.ActionInv import com.opensymphony.xwork2.Result; import com.opensymphony.xwork2.util.TextParseUtil; import com.opensymphony.xwork2.util.ValueStack; +import com.opensymphony.xwork2.util.logging.Logger; +import com.opensymphony.xwork2.util.logging.LoggerFactory; import org.apache.struts2.ServletActionContext; import javax.servlet.http.HttpServletResponse; @@ -35,64 +37,60 @@ import java.util.Map; /** * <!-- START SNIPPET: description --> - * + * <p/> * A custom Result type for setting HTTP headers and status by optionally evaluating against the ValueStack. * This result can also be used to send and error to the client. - * + * <p/> * <!-- END SNIPPET: description --> * <p/> * <b>This result type takes the following parameters:</b> - * + * <p/> * <!-- START SNIPPET: params --> - * + * <p/> * <ul> - * + * <p/> * <li><b>status</b> - the http servlet response status code that should be set on a response.</li> - * + * <p/> * <li><b>parse</b> - true by default. If set to false, the headers param will not be parsed for Ognl expressions.</li> - * + * <p/> * <li><b>headers</b> - header values.</li> - * + * <p/> * <li><b>error</b> - the http servlet response error code that should be set on a response.</li> - * + * <p/> * <li><b>errorMessage</b> - error message to be set on response if 'error' is set.</li> * </ul> - * + * <p/> * <!-- END SNIPPET: params --> - * + * <p/> * <b>Example:</b> - * + * <p/> * <pre><!-- START SNIPPET: example --> * <result name="success" type="httpheader"> * <param name="status">204</param> * <param name="headers.a">a custom header value</param> * <param name="headers.b">another custom header value</param> * </result> - * + * <p/> * <result name="proxyRequired" type="httpheader"> * <param name="error">305</param> * <param name="errorMessage">this action must be accessed through a prozy</param> * </result> * <!-- END SNIPPET: example --></pre> - * */ public class HttpHeaderResult implements Result { private static final long serialVersionUID = 195648957144219214L; - - /** The default parameter */ - public static final String DEFAULT_PARAM = "status"; - + private static final Logger LOG = LoggerFactory.getLogger(HttpHeaderResult.class); private boolean parse = true; - private Map<String,String> headers; + private Map<String, String> headers; private int status = -1; - private int error = -1; + private String error = null; private String errorMessage; - + public HttpHeaderResult() { super(); - headers = new HashMap<String,String>(); + headers = new HashMap<String, String>(); } public HttpHeaderResult(int status) { @@ -101,20 +99,19 @@ public class HttpHeaderResult implements this.parse = false; } - /** - * Sets the http servlet error code that should be set on the reponse - * + * Sets the http servlet error code that should be set on the response + * * @param error the Http error code * @see javax.servlet.http.HttpServletResponse#sendError(int) */ - public void setError(int error) { + public void setError(String error) { this.error = error; } - + /** * Sets the error message that should be set on the reponse - * + * * @param errorMessage error message send to the client * @see javax.servlet.http.HttpServletResponse#sendError(int, String) */ @@ -134,7 +131,7 @@ public class HttpHeaderResult implements /** * Sets whether or not the HTTP header values should be evaluated against the ValueStack (by default they are). * - * @param parse <tt>true</tt> if HTTP header values should be evaluated agains the ValueStack, <tt>false</tt> + * @param parse <tt>true</tt> if HTTP header values should be evaluated against the ValueStack, <tt>false</tt> * otherwise. */ public void setParse(boolean parse) { @@ -157,7 +154,8 @@ public class HttpHeaderResult implements /** * Adds an HTTP header to the response - * @param name header name + * + * @param name header name * @param value header value */ public void addHeader(String name, String value) { @@ -174,24 +172,35 @@ public class HttpHeaderResult implements public void execute(ActionInvocation invocation) throws Exception { HttpServletResponse response = ServletActionContext.getResponse(); ValueStack stack = ActionContext.getContext().getValueStack(); - + if (status != -1) { response.setStatus(status); - } else if (error != -1) { - if (errorMessage != null) { - String finalMessage = parse ? TextParseUtil.translateVariables( - errorMessage, stack) : errorMessage; - response.sendError(error, finalMessage); - } else - response.sendError(error); } if (headers != null) { - for (Map.Entry<String, String> stringStringEntry : headers.entrySet()) { - Map.Entry entry = (Map.Entry) stringStringEntry; - String value = (String) entry.getValue(); + for (Map.Entry<String, String> entry : headers.entrySet()) { + String value = entry.getValue(); String finalValue = parse ? TextParseUtil.translateVariables(value, stack) : value; - response.addHeader((String) entry.getKey(), finalValue); + response.addHeader(entry.getKey(), finalValue); + } + } + + if (status == -1 && error != null) { + int errorCode = -1; + try { + errorCode = Integer.parseInt(parse ? TextParseUtil.translateVariables(error, stack) : error); + } catch (Exception e) { + if (LOG.isErrorEnabled()) { + LOG.error("Cannot parse errorCode [#0] value as Integer!", e, error); + } + } + if (errorCode != -1) { + if (errorMessage != null) { + String finalMessage = parse ? TextParseUtil.translateVariables(errorMessage, stack) : errorMessage; + response.sendError(errorCode, finalMessage); + } else { + response.sendError(errorCode); + } } } } Modified: struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/HttpHeaderResultTest.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/HttpHeaderResultTest.java?rev=1423621&r1=1423620&r2=1423621&view=diff ============================================================================== --- struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/HttpHeaderResultTest.java (original) +++ struts/struts2/trunk/core/src/test/java/org/apache/struts2/dispatcher/HttpHeaderResultTest.java Tue Dec 18 20:27:39 2012 @@ -21,25 +21,20 @@ package org.apache.struts2.dispatcher; -import java.util.HashMap; -import java.util.Map; - -import javax.servlet.http.HttpServletResponse; - -import org.apache.struts2.ServletActionContext; -import org.apache.struts2.StrutsTestCase; - import com.mockobjects.dynamic.C; import com.mockobjects.dynamic.Mock; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; -import com.opensymphony.xwork2.ognl.OgnlUtil; import com.opensymphony.xwork2.util.reflection.ReflectionProvider; +import org.apache.struts2.ServletActionContext; +import org.apache.struts2.StrutsTestCase; +import javax.servlet.http.HttpServletResponse; +import java.util.HashMap; +import java.util.Map; /** * HttpHeaderResultTest - * */ public class HttpHeaderResultTest extends StrutsTestCase { @@ -49,13 +44,12 @@ public class HttpHeaderResultTest extend Mock responseMock; ReflectionProvider reflectionProvider; - public void testHeaderValuesAreNotParsedWhenParseIsFalse() throws Exception { - Map params = new HashMap(); + Map<String, String> params = new HashMap<String, String>(); params.put("headers.foo", "${bar}"); params.put("headers.baz", "baz"); - Map values = new HashMap(); + Map<String, String> values = new HashMap<String, String>(); values.put("bar", "abc"); ActionContext.getContext().getValueStack().push(values); @@ -69,11 +63,11 @@ public class HttpHeaderResultTest extend } public void testHeaderValuesAreParsedAndSet() throws Exception { - Map params = new HashMap(); + Map<String, String> params = new HashMap<String, String>(); params.put("headers.foo", "${bar}"); params.put("headers.baz", "baz"); - Map values = new HashMap(); + Map<String, String> values = new HashMap<String, String>(); values.put("bar", "abc"); ActionContext.getContext().getValueStack().push(values); @@ -87,7 +81,8 @@ public class HttpHeaderResultTest extend public void testErrorMessageIsParsedAndSet() throws Exception { ActionContext.getContext().getValueStack().set("errMsg", "abc"); - result.setError(404); + ActionContext.getContext().getValueStack().set("errCode", "404"); + result.setError("${errCode}"); result.setErrorMessage("${errMsg}"); responseMock.expect("sendError", C.args(C.eq(404), C.eq("abc"))); @@ -97,7 +92,7 @@ public class HttpHeaderResultTest extend public void testErrorMessageIsNotParsedAndSet() throws Exception { ActionContext.getContext().getValueStack().set("errMsg", "abc"); - result.setError(404); + result.setError("404"); result.setParse(false); result.setErrorMessage("${errMsg}"); @@ -115,7 +110,7 @@ public class HttpHeaderResultTest extend public void testErrorIsSet() throws Exception { responseMock.expect("sendError", C.eq(404)); - result.setError(404); + result.setError("404"); result.execute(invocation); responseMock.verify(); } @@ -134,4 +129,5 @@ public class HttpHeaderResultTest extend super.tearDown(); ActionContext.setContext(null); } + }