001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.dispatcher;
006:
007: import com.opensymphony.webwork.ServletActionContext;
008: import com.opensymphony.xwork.ActionContext;
009: import com.opensymphony.xwork.ActionInvocation;
010: import com.opensymphony.xwork.Result;
011: import com.opensymphony.xwork.util.OgnlValueStack;
012: import com.opensymphony.xwork.util.TextParseUtil;
013:
014: import javax.servlet.http.HttpServletResponse;
015: import java.util.HashMap;
016: import java.util.Iterator;
017: import java.util.Map;
018:
019: /**
020: * <!-- START SNIPPET: description -->
021: *
022: * A custom Result type for evaluating HTTP headers against the ValueStack.
023: *
024: * <!-- END SNIPPET: description -->
025: * <p/>
026: * <b>This result type takes the following parameters:</b>
027: *
028: * <!-- START SNIPPET: params -->
029: *
030: * <ul>
031: *
032: * <li><b>status</b> - the http servlet response status code that should be set on a response.</li>
033: *
034: * <li><b>parse</b> - true by default. If set to false, the headers param will not be parsed for Ognl expressions.</li>
035: *
036: * <li><b>headers</b> - header values.</li>
037: *
038: * </ul>
039: *
040: * <!-- END SNIPPET: params -->
041: *
042: * <b>Example:</b>
043: *
044: * <pre><!-- START SNIPPET: example -->
045: * <result name="success" type="httpheader">
046: * <param name="status">204</param>
047: * <param name="headers.a">a custom header value</param>
048: * <param name="headers.b">another custom header value</param>
049: * </result>
050: * <!-- END SNIPPET: example --></pre>
051: *
052: * @author Jason Carreira
053: */
054: public class HttpHeaderResult implements Result {
055:
056: public static final String DEFAULT_PARAM = "status";
057:
058: protected boolean parse = true;
059: private Map headers;
060: private int status = -1;
061:
062: /**
063: * Returns a Map of all HTTP headers.
064: *
065: * @return a Map of all HTTP headers.
066: */
067: public Map getHeaders() {
068: if (headers == null) {
069: headers = new HashMap();
070: }
071:
072: return headers;
073: }
074:
075: /**
076: * Sets whether or not the HTTP header values should be evaluated against the ValueStack (by default they are).
077: *
078: * @param parse <tt>true</tt> if HTTP header values should be evaluated agains the ValueStack, <tt>false</tt>
079: * otherwise.
080: */
081: public void setParse(boolean parse) {
082: this .parse = parse;
083: }
084:
085: /**
086: * Sets the http servlet response status code that should be set on a response.
087: *
088: * @param status the Http status code
089: * @see javax.servlet.http.HttpServletResponse#setStatus(int)
090: */
091: public void setStatus(int status) {
092: this .status = status;
093: }
094:
095: /**
096: * Sets the optional HTTP response status code and also re-sets HTTP headers after they've
097: * been optionally evaluated against the ValueStack.
098: *
099: * @param invocation an encapsulation of the action execution state.
100: * @throws Exception if an error occurs when re-setting the headers.
101: */
102: public void execute(ActionInvocation invocation) throws Exception {
103: HttpServletResponse response = ServletActionContext
104: .getResponse();
105:
106: if (status != -1) {
107: response.setStatus(status);
108: }
109:
110: if (headers != null) {
111: OgnlValueStack stack = ActionContext.getContext()
112: .getValueStack();
113:
114: for (Iterator iterator = headers.entrySet().iterator(); iterator
115: .hasNext();) {
116: Map.Entry entry = (Map.Entry) iterator.next();
117: String value = (String) entry.getValue();
118: String finalValue = parse ? TextParseUtil
119: .translateVariables(value, stack) : value;
120: response.addHeader((String) entry.getKey(), finalValue);
121: }
122: }
123: }
124: }
|