-
Notifications
You must be signed in to change notification settings - Fork 460
/
DotCmsWebSocketConfigurator.java
130 lines (98 loc) · 4.1 KB
/
DotCmsWebSocketConfigurator.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
package com.dotcms.rest.api.v1.system.websocket;
import com.dotcms.auth.providers.jwt.JsonWebTokenAuthCredentialProcessor;
import com.dotcms.auth.providers.jwt.services.JsonWebTokenAuthCredentialProcessorImpl;
import com.dotcms.business.LazyUserAPIWrapper;
import com.dotcms.repackage.com.google.common.annotations.VisibleForTesting;
import com.dotcms.repackage.org.glassfish.jersey.server.ContainerRequest;
import com.dotmarketing.business.APILocator;
import com.dotmarketing.business.UserAPI;
import com.dotmarketing.exception.DotDataException;
import com.dotmarketing.exception.DotSecurityException;
import com.dotmarketing.util.Logger;
import com.dotmarketing.util.UtilMethods;
import com.liferay.portal.model.User;
import javax.servlet.http.HttpSession;
import javax.websocket.HandshakeResponse;
import javax.websocket.server.HandshakeRequest;
import javax.websocket.server.ServerEndpointConfig;
import javax.websocket.server.ServerEndpointConfig.Configurator;
import java.util.List;
/**
* This {@link Configurator} is in charge of the single instantiation of the
* ServerSocket End points. It delegates the {@link WebSocketContainerAPI} the
* task to keep just one instance of the web sockets. Consequently, we can get
* the endpoint instance in any other place of the application.
*
* @author jsanca
* @version 3.7
*/
public class DotCmsWebSocketConfigurator extends Configurator {
private final WebSocketContainerAPI webSocketContainerAPI;
private final JsonWebTokenAuthCredentialProcessor authCredentialProcessor;
private final UserAPI userAPI;
public DotCmsWebSocketConfigurator() {
this(APILocator.getWebSocketContainerAPI(),
JsonWebTokenAuthCredentialProcessorImpl.getInstance(),
new LazyUserAPIWrapper());
}
@VisibleForTesting
protected DotCmsWebSocketConfigurator(final WebSocketContainerAPI webSocketContainerAPI,
final JsonWebTokenAuthCredentialProcessor authCredentialProcessor,
final UserAPI userAPI) {
this.webSocketContainerAPI = webSocketContainerAPI;
this.authCredentialProcessor = authCredentialProcessor;
this.userAPI = userAPI;
}
@Override
public <T> T getEndpointInstance(final Class<T> endpointClass) throws InstantiationException {
return this.webSocketContainerAPI.getEndpointInstance(endpointClass);
}
@Override
public void modifyHandshake(final ServerEndpointConfig serverEndpointConfig,
final HandshakeRequest request,
final HandshakeResponse response) {
super.modifyHandshake(serverEndpointConfig, request, response);
User user = null;
String authorizationHeader = null;
final List<String> headers = request.getHeaders().get(ContainerRequest.AUTHORIZATION);
final Object session = request.getHttpSession();
HttpSession httpSession = null;
if (UtilMethods.isSet(session) && session instanceof HttpSession) {
try {
httpSession = HttpSession.class.cast(session);
user = (User) httpSession.getAttribute(com.liferay.portal.util.WebKeys.USER);
if (!UtilMethods.isSet(user)) {
user = this.getUserFromId(httpSession);
}
} catch (Exception e) {
if (Logger.isErrorEnabled(this.getClass())) {
Logger.error(this.getClass(), e.getMessage(), e);
}
}
}
try {
if (!UtilMethods.isSet(user) && ((null != headers) && (headers.size() > 0))) {
authorizationHeader = headers.get(0);
user = this.authCredentialProcessor.processAuthCredentialsFromJWT
(authorizationHeader, httpSession);
}
if (UtilMethods.isSet(user)) {
serverEndpointConfig.getUserProperties().put
(SystemEventsWebSocketEndPoint.USER, user);
}
} catch (Exception e) {
if (Logger.isErrorEnabled(this.getClass())) {
Logger.error(this.getClass(), e.getMessage(), e);
}
}
} // modifyHandshake.
private User getUserFromId(final HttpSession httpSession) throws DotSecurityException, DotDataException {
User user = null;
final String userId = (String) httpSession.getAttribute
(com.liferay.portal.util.WebKeys.USER_ID);
if (UtilMethods.isSet(userId)) {
user = this.userAPI.loadUserById(userId);
}
return user;
} // getUserFromId.
} // DotCmsWebSocketConfigurator.