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