1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.web.context.request;
18
19 import javax.faces.context.FacesContext;
20
21 import org.springframework.core.NamedInheritableThreadLocal;
22 import org.springframework.core.NamedThreadLocal;
23 import org.springframework.util.ClassUtils;
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 public abstract class RequestContextHolder {
47
48 private static final boolean jsfPresent =
49 ClassUtils.isPresent("javax.faces.context.FacesContext", RequestContextHolder.class.getClassLoader());
50
51 private static final ThreadLocal<RequestAttributes> requestAttributesHolder =
52 new NamedThreadLocal<RequestAttributes>("Request attributes");
53
54 private static final ThreadLocal<RequestAttributes> inheritableRequestAttributesHolder =
55 new NamedInheritableThreadLocal<RequestAttributes>("Request context");
56
57
58
59
60
61 public static void resetRequestAttributes() {
62 requestAttributesHolder.remove();
63 inheritableRequestAttributesHolder.remove();
64 }
65
66
67
68
69
70
71
72 public static void setRequestAttributes(RequestAttributes attributes) {
73 setRequestAttributes(attributes, false);
74 }
75
76
77
78
79
80
81
82
83 public static void setRequestAttributes(RequestAttributes attributes, boolean inheritable) {
84 if (attributes == null) {
85 resetRequestAttributes();
86 }
87 else {
88 if (inheritable) {
89 inheritableRequestAttributesHolder.set(attributes);
90 requestAttributesHolder.remove();
91 }
92 else {
93 requestAttributesHolder.set(attributes);
94 inheritableRequestAttributesHolder.remove();
95 }
96 }
97 }
98
99
100
101
102
103
104 public static RequestAttributes getRequestAttributes() {
105 RequestAttributes attributes = requestAttributesHolder.get();
106 if (attributes == null) {
107 attributes = inheritableRequestAttributesHolder.get();
108 }
109 return attributes;
110 }
111
112
113
114
115
116
117
118
119
120
121
122
123
124 public static RequestAttributes currentRequestAttributes() throws IllegalStateException {
125 RequestAttributes attributes = getRequestAttributes();
126 if (attributes == null) {
127 if (jsfPresent) {
128 attributes = FacesRequestAttributesFactory.getFacesRequestAttributes();
129 }
130 if (attributes == null) {
131 throw new IllegalStateException("No thread-bound request found: " +
132 "Are you referring to request attributes outside of an actual web request, " +
133 "or processing a request outside of the originally receiving thread? " +
134 "If you are actually operating within a web request and still receive this message, " +
135 "your code is probably running outside of DispatcherServlet/DispatcherPortlet: " +
136 "In this case, use RequestContextListener or RequestContextFilter to expose the current request.");
137 }
138 }
139 return attributes;
140 }
141
142
143
144
145
146 private static class FacesRequestAttributesFactory {
147
148 public static RequestAttributes getFacesRequestAttributes() {
149 FacesContext facesContext = FacesContext.getCurrentInstance();
150 return (facesContext != null ? new FacesRequestAttributes(facesContext) : null);
151 }
152 }
153
154 }