Author: musachy
Date: Mon Jun 11 18:46:27 2007
New Revision: 546352

URL: http://svn.apache.org/viewvc?view=rev&rev=546352
Log:
WW-1853 Extend HttpHeaderResult to support HttpServletResponse.sendError()

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?view=diff&rev=546352&r1=546351&r2=546352
==============================================================================
--- 
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
 Mon Jun 11 18:46:27 2007
@@ -39,6 +39,7 @@
  * <!-- START SNIPPET: description -->
  *
  * 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.
  *
  * <!-- END SNIPPET: description -->
  * <p/>
@@ -53,7 +54,10 @@
  * <li><b>parse</b> - true by default. If set to false, the headers param will 
not be parsed for Ognl expressions.</li>
  *
  * <li><b>headers</b> - header values.</li>
+ * 
+ * <li><b>error</b> - the http servlet response error code that should be set 
on a response.</li>
  *
+ * <li><b>errorMessage</b> - error message to be set on response if 'error' is 
set.</li>
  * </ul>
  *
  * <!-- END SNIPPET: params -->
@@ -66,6 +70,11 @@
  *   &lt;param name="headers.a"&gt;a custom header value&lt;/param&gt;
  *   &lt;param name="headers.b"&gt;another custom header value&lt;/param&gt;
  * &lt;/result&gt;
+ * 
+ * &lt;result name="proxyRequired" type="httpheader"&gt;
+ *   &lt;param name="error"&gt;305&lt;/param&gt;
+ *   &lt;param name="errorMessage"&gt;this action must be accessed through a 
prozy&lt;/param&gt;
+ * &lt;/result&gt;
  * <!-- END SNIPPET: example --></pre>
  *
  */
@@ -80,7 +89,9 @@
     private boolean parse = true;
     private Map<String,String> headers;
     private int status = -1;
-
+    private int error = -1;
+    private String errorMessage;
+    
     public HttpHeaderResult() {
         super();
         headers = new HashMap<String,String>();
@@ -94,6 +105,26 @@
 
 
     /**
+     * Sets the http servlet error code that should be set on the reponse
+     * 
+     * @param error the Http error code
+     * @see javax.servlet.http.HttpServletResponse#sendError(int)
+     */
+    public void setError(int 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)
+     */
+    public void setErrorMessage(String errorMessage) {
+        this.errorMessage = errorMessage;
+    }
+
+    /**
      * Returns a Map of all HTTP headers.
      *
      * @return a Map of all HTTP headers.
@@ -140,14 +171,20 @@
      */
     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) {
-            ValueStack stack = ActionContext.getContext().getValueStack();
-
             for (Iterator iterator = headers.entrySet().iterator();
                  iterator.hasNext();) {
                 Map.Entry entry = (Map.Entry) iterator.next();

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?view=diff&rev=546352&r1=546351&r2=546352
==============================================================================
--- 
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
 Mon Jun 11 18:46:27 2007
@@ -81,10 +81,38 @@
         result.execute(invocation);
         responseMock.verify();
     }
+    
+    public void testErrorMessageIsParsedAndSet() throws Exception {
+        ActionContext.getContext().getValueStack().set("errMsg", "abc");
+        result.setError(404);
+        result.setErrorMessage("${errMsg}");
+        
+        responseMock.expect("sendError", C.args(C.eq(404), C.eq("abc")));
+        result.execute(invocation);
+        responseMock.verify();
+    }
+    
+    public void testErrorMessageIsNotParsedAndSet() throws Exception {
+        ActionContext.getContext().getValueStack().set("errMsg", "abc");
+        result.setError(404);
+        result.setParse(false);
+        result.setErrorMessage("${errMsg}");
+        
+        responseMock.expect("sendError", C.args(C.eq(404), C.eq("${errMsg}")));
+        result.execute(invocation);
+        responseMock.verify();
+    }
 
     public void testStatusIsSet() throws Exception {
         responseMock.expect("setStatus", C.eq(123));
         result.setStatus(123);
+        result.execute(invocation);
+        responseMock.verify();
+    }
+    
+    public void testErrorIsSet() throws Exception {
+        responseMock.expect("sendError", C.eq(404));
+        result.setError(404);
         result.execute(invocation);
         responseMock.verify();
     }


Reply via email to