KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > dispatcher > HttpHeaderResult


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.webwork.dispatcher;
6
7 import com.opensymphony.webwork.ServletActionContext;
8 import com.opensymphony.xwork.ActionContext;
9 import com.opensymphony.xwork.ActionInvocation;
10 import com.opensymphony.xwork.Result;
11 import com.opensymphony.xwork.util.OgnlValueStack;
12 import com.opensymphony.xwork.util.TextParseUtil;
13
14 import javax.servlet.http.HttpServletResponse JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.Map JavaDoc;
18
19
20 /**
21  * A custom Result type for evaluating HTTP headers against the ValueStack.
22  *
23  * @author Jason Carreira
24  */

25 public class HttpHeaderResult implements Result {
26     //~ Static fields/initializers /////////////////////////////////////////////
27

28     public static final String JavaDoc DEFAULT_PARAM = "status";
29
30     //~ Instance fields ////////////////////////////////////////////////////////
31

32     protected boolean parse = true;
33     private Map JavaDoc headers;
34     private int status = -1;
35
36     //~ Methods ////////////////////////////////////////////////////////////////
37

38     /**
39      * Returns a Map of all HTTP headers.
40      *
41      * @return a Map of all HTTP headers.
42      */

43     public Map JavaDoc getHeaders() {
44         if (headers == null) {
45             headers = new HashMap JavaDoc();
46         }
47
48         return headers;
49     }
50
51     /**
52      * Sets whether or not the HTTP header values should be evaluated against the ValueStack (by default they are).
53      *
54      * @param parse <tt>true</tt> if HTTP header values should be evaluated agains the ValueStack, <tt>false</tt>
55      * otherwise.
56      */

57     public void setParse(boolean parse) {
58         this.parse = parse;
59     }
60
61     /**
62      * Sets the http servlet response status code that should be set on a response.
63      *
64      * @param status the Http status code
65      * @see javax.servlet.http.HttpServletResponse#setStatus(int)
66      */

67     public void setStatus(int status) {
68         this.status = status;
69     }
70
71     /**
72      * Sets the optional HTTP response status code and also re-sets HTTP headers after they've
73      * been optionally evaluated against the ValueStack.
74      *
75      * @param invocation an encapsulation of the action execution state.
76      * @throws Exception if an error occurs when re-setting the headers.
77      */

78     public void execute(ActionInvocation invocation) throws Exception JavaDoc {
79         HttpServletResponse JavaDoc response = ServletActionContext.getResponse();
80
81         if (status != -1) {
82             response.setStatus(status);
83         }
84
85         if (headers != null) {
86             OgnlValueStack stack = ActionContext.getContext().getValueStack();
87
88             for (Iterator JavaDoc iterator = headers.entrySet().iterator();
89                  iterator.hasNext();) {
90                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
91                 String JavaDoc value = (String JavaDoc) entry.getValue();
92                 String JavaDoc finalValue = parse ? TextParseUtil.translateVariables(value, stack) : value;
93                 response.addHeader((String JavaDoc) entry.getKey(), finalValue);
94             }
95         }
96     }
97 }
98
Popular Tags