FileDocCategorySizeDatePackage
HttpHeaderResult.javaAPI DocExample5144Mon Jul 23 13:26:38 BST 2007org.apache.struts2.dispatcher

HttpHeaderResult

public class HttpHeaderResult extends Object implements com.opensymphony.xwork2.Result
A custom Result type for setting HTTP headers and status by optionally evaluating against the ValueStack.

This result type takes the following parameters:

  • status - the http servlet response status code that should be set on a response.
  • parse - true by default. If set to false, the headers param will not be parsed for Ognl expressions.
  • headers - header values.
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>

Fields Summary
private static final long
serialVersionUID
public static final String
DEFAULT_PARAM
The default parameter
private boolean
parse
private Map
headers
private int
status
Constructors Summary
public HttpHeaderResult()


      
        super();
        headers = new HashMap<String,String>();
    
public HttpHeaderResult(int status)

        this();
        this.status = status;
        this.parse = false;
    
Methods Summary
public voidaddHeader(java.lang.String name, java.lang.String value)
Adds an HTTP header to the response

param
name
param
value

        headers.put(name, value);
    
public voidexecute(com.opensymphony.xwork2.ActionInvocation invocation)
Sets the optional HTTP response status code and also re-sets HTTP headers after they've been optionally evaluated against the ValueStack.

param
invocation an encapsulation of the action execution state.
throws
Exception if an error occurs when re-setting the headers.

        HttpServletResponse response = ServletActionContext.getResponse();

        if (status != -1) {
            response.setStatus(status);
        }

        if (headers != null) {
            ValueStack stack = ActionContext.getContext().getValueStack();

            for (Iterator iterator = headers.entrySet().iterator();
                 iterator.hasNext();) {
                Map.Entry entry = (Map.Entry) iterator.next();
                String value = (String) entry.getValue();
                String finalValue = parse ? TextParseUtil.translateVariables(value, stack) : value;
                response.addHeader((String) entry.getKey(), finalValue);
            }
        }
    
public java.util.MapgetHeaders()
Returns a Map of all HTTP headers.

return
a Map of all HTTP headers.

        return headers;
    
public voidsetParse(boolean parse)
Sets whether or not the HTTP header values should be evaluated against the ValueStack (by default they are).

param
parse true if HTTP header values should be evaluated agains the ValueStack, false otherwise.

        this.parse = parse;
    
public voidsetStatus(int status)
Sets the http servlet response status code that should be set on a response.

param
status the Http status code
see
javax.servlet.http.HttpServletResponse#setStatus(int)

        this.status = status;