001: /*
002: * $Id: HttpHeaderResult.java 471756 2006-11-06 15:01:43Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.dispatcher;
022:
023: import java.util.HashMap;
024: import java.util.Iterator;
025: import java.util.Map;
026:
027: import javax.servlet.http.HttpServletResponse;
028:
029: import org.apache.struts2.ServletActionContext;
030:
031: import com.opensymphony.xwork2.ActionContext;
032: import com.opensymphony.xwork2.ActionInvocation;
033: import com.opensymphony.xwork2.Result;
034: import com.opensymphony.xwork2.util.TextParseUtil;
035: import com.opensymphony.xwork2.util.ValueStack;
036:
037: /**
038: * <!-- START SNIPPET: description -->
039: *
040: * A custom Result type for setting HTTP headers and status by optionally evaluating against the ValueStack.
041: *
042: * <!-- END SNIPPET: description -->
043: * <p/>
044: * <b>This result type takes the following parameters:</b>
045: *
046: * <!-- START SNIPPET: params -->
047: *
048: * <ul>
049: *
050: * <li><b>status</b> - the http servlet response status code that should be set on a response.</li>
051: *
052: * <li><b>parse</b> - true by default. If set to false, the headers param will not be parsed for Ognl expressions.</li>
053: *
054: * <li><b>headers</b> - header values.</li>
055: *
056: * </ul>
057: *
058: * <!-- END SNIPPET: params -->
059: *
060: * <b>Example:</b>
061: *
062: * <pre><!-- START SNIPPET: example -->
063: * <result name="success" type="httpheader">
064: * <param name="status">204</param>
065: * <param name="headers.a">a custom header value</param>
066: * <param name="headers.b">another custom header value</param>
067: * </result>
068: * <!-- END SNIPPET: example --></pre>
069: *
070: */
071: public class HttpHeaderResult implements Result {
072:
073: private static final long serialVersionUID = 195648957144219214L;
074:
075: /** The default parameter */
076: public static final String DEFAULT_PARAM = "status";
077:
078: private boolean parse = true;
079: private Map<String, String> headers;
080: private int status = -1;
081:
082: public HttpHeaderResult() {
083: super ();
084: headers = new HashMap<String, String>();
085: }
086:
087: public HttpHeaderResult(int status) {
088: this ();
089: this .status = status;
090: this .parse = false;
091: }
092:
093: /**
094: * Returns a Map of all HTTP headers.
095: *
096: * @return a Map of all HTTP headers.
097: */
098: public Map getHeaders() {
099: return headers;
100: }
101:
102: /**
103: * Sets whether or not the HTTP header values should be evaluated against the ValueStack (by default they are).
104: *
105: * @param parse <tt>true</tt> if HTTP header values should be evaluated agains the ValueStack, <tt>false</tt>
106: * otherwise.
107: */
108: public void setParse(boolean parse) {
109: this .parse = parse;
110: }
111:
112: /**
113: * Sets the http servlet response status code that should be set on a response.
114: *
115: * @param status the Http status code
116: * @see javax.servlet.http.HttpServletResponse#setStatus(int)
117: */
118: public void setStatus(int status) {
119: this .status = status;
120: }
121:
122: /**
123: * Adds an HTTP header to the response
124: * @param name
125: * @param value
126: */
127: public void addHeader(String name, String value) {
128: headers.put(name, value);
129: }
130:
131: /**
132: * Sets the optional HTTP response status code and also re-sets HTTP headers after they've
133: * been optionally evaluated against the ValueStack.
134: *
135: * @param invocation an encapsulation of the action execution state.
136: * @throws Exception if an error occurs when re-setting the headers.
137: */
138: public void execute(ActionInvocation invocation) throws Exception {
139: HttpServletResponse response = ServletActionContext
140: .getResponse();
141:
142: if (status != -1) {
143: response.setStatus(status);
144: }
145:
146: if (headers != null) {
147: ValueStack stack = ActionContext.getContext()
148: .getValueStack();
149:
150: for (Iterator iterator = headers.entrySet().iterator(); iterator
151: .hasNext();) {
152: Map.Entry entry = (Map.Entry) iterator.next();
153: String value = (String) entry.getValue();
154: String finalValue = parse ? TextParseUtil
155: .translateVariables(value, stack) : value;
156: response.addHeader((String) entry.getKey(), finalValue);
157: }
158: }
159: }
160: }
|