1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.apache.struts2.impl;
24
25 import static org.apache.struts2.StrutsStatics.HTTP_REQUEST;
26 import static org.apache.struts2.StrutsStatics.HTTP_RESPONSE;
27 import static org.apache.struts2.StrutsStatics.SERVLET_CONTEXT;
28
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.Locale;
32 import java.util.Map;
33 import java.util.concurrent.Callable;
34
35 import javax.servlet.ServletContext;
36 import javax.servlet.http.Cookie;
37 import javax.servlet.http.HttpServletRequest;
38 import javax.servlet.http.HttpServletResponse;
39
40 import org.apache.struts2.Messages;
41 import org.apache.struts2.dispatcher.RequestMap;
42 import org.apache.struts2.spi.ActionContext;
43 import org.apache.struts2.spi.RequestContext;
44 import org.apache.struts2.spi.ValueStack;
45
46 import com.opensymphony.xwork2.ActionInvocation;
47
48 public class RequestContextImpl implements RequestContext {
49
50 com.opensymphony.xwork2.ActionContext xworkContext;
51 ActionContext actionContext;
52 Messages messages = new MessagesImpl();
53
54 public static final Callable<String> ILLEGAL_PROCEED = new Callable<String>() {
55 public String call() throws Exception {
56 throw new IllegalStateException();
57 }
58 };
59
60 public RequestContextImpl(com.opensymphony.xwork2.ActionContext xworkContext) {
61 this.xworkContext = xworkContext;
62 }
63
64 public ActionContext getActionContext() {
65 return actionContext;
66 }
67
68 public Object getAction() {
69 return getActionContext().getAction();
70 }
71
72 void setActionContext(ActionContext actionContext) {
73 this.actionContext = actionContext;
74 }
75
76 public Map<String, String[]> getParameterMap() {
77 return xworkContext.getParameters();
78 }
79
80 Map<String, Object> attributeMap;
81
82 public Map<String, Object> getAttributeMap() {
83 if (attributeMap == null) {
84 attributeMap = new RequestMap(getServletRequest());
85 }
86 return attributeMap;
87 }
88
89 public Map<String, Object> getSessionMap() {
90 return xworkContext.getSession();
91 }
92
93 public Map<String, Object> getApplicationMap() {
94 return xworkContext.getApplication();
95 }
96
97 public List<Cookie> findCookiesForName(String name) {
98 List<Cookie> cookies = new ArrayList<Cookie>();
99 for (Cookie cookie : getServletRequest().getCookies())
100 if (name.equals(cookie.getName()))
101 cookies.add(cookie);
102
103 return cookies;
104 }
105
106 public Locale getLocale() {
107 return xworkContext.getLocale();
108 }
109
110 public void setLocale(Locale locale) {
111 xworkContext.setLocale(locale);
112 }
113
114 public Messages getMessages() {
115 return messages;
116 }
117
118 public HttpServletRequest getServletRequest() {
119 return (HttpServletRequest) xworkContext.get(HTTP_REQUEST);
120 }
121
122 public HttpServletResponse getServletResponse() {
123 return (HttpServletResponse) xworkContext.get(HTTP_RESPONSE);
124 }
125
126 public ServletContext getServletContext() {
127 return (ServletContext) xworkContext.get(SERVLET_CONTEXT);
128 }
129
130 ValueStack valueStack;
131
132 public ValueStack getValueStack() {
133 if (valueStack == null) {
134 valueStack = new ValueStackAdapter(xworkContext.getValueStack());
135 }
136 return valueStack;
137 }
138
139 Callable<String> proceed = ILLEGAL_PROCEED;
140
141 public String proceed() throws Exception {
142 return proceed.call();
143 }
144
145 public void setProceed(Callable<String> proceed) {
146 this.proceed = proceed;
147 }
148
149 public Callable<String> getProceed() {
150 return proceed;
151 }
152
153 static ThreadLocal<Object[]> threadLocalRequestContext = new ThreadLocal<Object[]>() {
154 protected RequestContextImpl[] initialValue() {
155 return new RequestContextImpl[1];
156 }
157 };
158
159 /***
160 * Creates RequestContext if necessary. Always creates a new ActionContext and restores an existing ActionContext
161 * when finished.
162 */
163 public static String callInContext(ActionInvocation invocation, Callable<String> callable)
164 throws Exception {
165 RequestContextImpl[] reference = (RequestContextImpl[])threadLocalRequestContext.get();
166
167 if (reference[0] == null) {
168
169 reference[0] = new RequestContextImpl(invocation.getInvocationContext());
170 reference[0].setActionContext(new ActionContextImpl(invocation));
171 try {
172 return callable.call();
173 } finally {
174 reference[0] = null;
175 }
176 } else {
177
178 RequestContextImpl requestContext = reference[0];
179 ActionContext previous = requestContext.getActionContext();
180 requestContext.setActionContext(new ActionContextImpl(invocation));
181 try {
182 return callable.call();
183 } finally {
184 requestContext.setActionContext(previous);
185 }
186 }
187 }
188
189 public static RequestContextImpl get() {
190 RequestContextImpl requestContext = ((RequestContextImpl[])threadLocalRequestContext.get())[0];
191
192 if (requestContext == null)
193 throw new IllegalStateException("RequestContext has not been created.");
194
195 return requestContext;
196 }
197 }