/
ConversationContextActivator.java
205 lines (177 loc) · 8.52 KB
/
ConversationContextActivator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
* JBoss, Home of Professional Open Source
* Copyright 2013, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.weld.servlet;
import java.util.function.Consumer;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.jboss.weld.context.AbstractConversationContext;
import org.jboss.weld.context.ConversationContext;
import org.jboss.weld.context.http.HttpConversationContext;
import org.jboss.weld.context.http.LazyHttpConversationContextImpl;
import org.jboss.weld.event.FastEvent;
import org.jboss.weld.literal.DestroyedLiteral;
import org.jboss.weld.literal.InitializedLiteral;
import org.jboss.weld.logging.ConversationLogger;
import org.jboss.weld.logging.ServletLogger;
import org.jboss.weld.manager.BeanManagerImpl;
/**
* This component takes care of activation/deactivation of the conversation context for a servlet request.
*
* @see ConversationFilter
* @see org.jboss.weld.servlet.WeldInitialListener
*
* @author Jozef Hartinger
* @author Marko Luksa
*
*/
public class ConversationContextActivator {
private static final String NO_CID = "nocid";
private static final String CONVERSATION_PROPAGATION = "conversationPropagation";
private static final String CONVERSATION_PROPAGATION_NONE = "none";
private static final String CONTEXT_ACTIVATED_IN_REQUEST = ConversationContextActivator.class.getName() + "CONTEXT_ACTIVATED_IN_REQUEST";
private final BeanManagerImpl beanManager;
private HttpConversationContext httpConversationContextCache;
private final FastEvent<HttpServletRequest> conversationInitializedEvent;
private final FastEvent<HttpServletRequest> conversationDestroyedEvent;
private final Consumer<HttpServletRequest> lazyInitializationCallback;
private final boolean lazy;
protected ConversationContextActivator(BeanManagerImpl beanManager, boolean lazy) {
this.beanManager = beanManager;
conversationInitializedEvent = FastEvent.of(HttpServletRequest.class, beanManager, InitializedLiteral.CONVERSATION);
conversationDestroyedEvent = FastEvent.of(HttpServletRequest.class, beanManager, DestroyedLiteral.CONVERSATION);
lazyInitializationCallback = lazy ? conversationInitializedEvent::fire : null;
this.lazy = lazy;
}
private HttpConversationContext httpConversationContext() {
if (httpConversationContextCache == null) {
this.httpConversationContextCache = beanManager.instance().select(HttpConversationContext.class).get();
}
return httpConversationContextCache;
}
public void startConversationContext(HttpServletRequest request) {
associateConversationContext(request);
activateConversationContext(request);
}
public void stopConversationContext(HttpServletRequest request) {
deactivateConversationContext(request);
}
// Conversation handling
protected void activateConversationContext(HttpServletRequest request) {
HttpConversationContext conversationContext = httpConversationContext();
/*
* Don't try to reactivate the ConversationContext if we have already activated it for this request WELD-877
*/
if (!isContextActivatedInRequest(request)) {
setContextActivatedInRequest(request);
activate(conversationContext, request);
} else {
/*
* We may have previously been associated with a ConversationContext, but the reference to that context may have been lost during a Servlet forward
* WELD-877
*/
conversationContext.dissociate(request);
conversationContext.associate(request);
activate(conversationContext, request);
}
}
private void activate(HttpConversationContext conversationContext, final HttpServletRequest request) {
if (lazy) {
conversationContext.activateLazily(lazyInitializationCallback);
} else {
String cid = determineConversationId(request, conversationContext.getParameterName());
conversationContext.activate(cid);
if (cid == null) { // transient conversation
conversationInitializedEvent.fire(request);
}
}
}
protected void associateConversationContext(HttpServletRequest request) {
httpConversationContext().associate(request);
}
private void setContextActivatedInRequest(HttpServletRequest request) {
request.setAttribute(CONTEXT_ACTIVATED_IN_REQUEST, true);
}
private boolean isContextActivatedInRequest(HttpServletRequest request) {
Object result = request.getAttribute(CONTEXT_ACTIVATED_IN_REQUEST);
if (result == null) {
return false;
}
return (Boolean) result;
}
protected void deactivateConversationContext(HttpServletRequest request) {
try {
ConversationContext conversationContext = httpConversationContext();
if (conversationContext.isActive()) {
// Only deactivate the context if one is already active, otherwise we get Exceptions
if (conversationContext instanceof LazyHttpConversationContextImpl) {
LazyHttpConversationContextImpl lazyConversationContext = (LazyHttpConversationContextImpl) conversationContext;
if (!lazyConversationContext.isInitialized()) {
// if this lazy conversation has not been touched yet, just deactivate it
lazyConversationContext.deactivate();
return;
}
}
boolean isTransient = conversationContext.getCurrentConversation().isTransient();
if (ConversationLogger.LOG.isTraceEnabled()) {
if (isTransient) {
ConversationLogger.LOG.cleaningUpTransientConversation();
} else {
ConversationLogger.LOG.cleaningUpConversation(conversationContext.getCurrentConversation().getId());
}
}
conversationContext.invalidate();
conversationContext.deactivate();
if (isTransient) {
conversationDestroyedEvent.fire(request);
}
}
} catch (Exception e) {
ServletLogger.LOG.unableToDeactivateContext(httpConversationContext(), request);
ServletLogger.LOG.catchingDebug(e);
}
}
protected void disassociateConversationContext(HttpServletRequest request) {
try {
httpConversationContext().dissociate(request);
} catch (Exception e) {
ServletLogger.LOG.unableToDissociateContext(httpConversationContext(), request);
ServletLogger.LOG.catchingDebug(e);
}
}
public void sessionCreated(HttpSession session) {
HttpConversationContext httpConversationContext = httpConversationContext();
if (httpConversationContext instanceof AbstractConversationContext) {
AbstractConversationContext<?, ?> abstractConversationContext = (AbstractConversationContext<?, ?>) httpConversationContext;
abstractConversationContext.sessionCreated();
}
}
public static String determineConversationId(HttpServletRequest request, String parameterName) {
if (request == null) {
throw ConversationLogger.LOG.mustCallAssociateBeforeActivate();
}
if (request.getParameter(NO_CID) != null) {
return null; // ignore cid; WELD-919
}
if (CONVERSATION_PROPAGATION_NONE.equals(request.getParameter(CONVERSATION_PROPAGATION))) {
return null; // conversationPropagation=none (CDI-135)
}
String cidName = parameterName;
String cid = request.getParameter(cidName);
ConversationLogger.LOG.foundConversationFromRequest(cid);
return cid;
}
}