|
|
|
@ -16,14 +16,17 @@ |
|
|
|
package org.thingsboard.server.controller.plugin; |
|
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.apache.tomcat.websocket.Constants; |
|
|
|
import org.springframework.beans.factory.BeanCreationNotAllowedException; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
import org.springframework.security.core.Authentication; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.util.StringUtils; |
|
|
|
import org.springframework.web.socket.CloseStatus; |
|
|
|
import org.springframework.web.socket.TextMessage; |
|
|
|
import org.springframework.web.socket.WebSocketSession; |
|
|
|
import org.springframework.web.socket.adapter.NativeWebSocketSession; |
|
|
|
import org.springframework.web.socket.handler.TextWebSocketHandler; |
|
|
|
import org.thingsboard.server.common.data.exception.ThingsboardErrorCode; |
|
|
|
import org.thingsboard.server.common.data.id.CustomerId; |
|
|
|
@ -38,6 +41,7 @@ import org.thingsboard.server.service.telemetry.TelemetryWebSocketMsgEndpoint; |
|
|
|
import org.thingsboard.server.service.telemetry.TelemetryWebSocketService; |
|
|
|
import org.thingsboard.server.service.telemetry.TelemetryWebSocketSessionRef; |
|
|
|
|
|
|
|
import javax.websocket.Session; |
|
|
|
import java.io.IOException; |
|
|
|
import java.net.URI; |
|
|
|
import java.security.InvalidParameterException; |
|
|
|
@ -56,6 +60,9 @@ public class TbWebSocketHandler extends TextWebSocketHandler implements Telemetr |
|
|
|
@Autowired |
|
|
|
private TelemetryWebSocketService webSocketService; |
|
|
|
|
|
|
|
@Value("${server.ws.blocking_send_timeout:5000}") |
|
|
|
private long blockingSendTimeout; |
|
|
|
|
|
|
|
@Value("${server.ws.limits.max_sessions_per_tenant:0}") |
|
|
|
private int maxSessionsPerTenant; |
|
|
|
@Value("${server.ws.limits.max_sessions_per_customer:0}") |
|
|
|
@ -96,6 +103,12 @@ public class TbWebSocketHandler extends TextWebSocketHandler implements Telemetr |
|
|
|
public void afterConnectionEstablished(WebSocketSession session) throws Exception { |
|
|
|
super.afterConnectionEstablished(session); |
|
|
|
try { |
|
|
|
if (session instanceof NativeWebSocketSession) { |
|
|
|
Session nativeSession = ((NativeWebSocketSession)session).getNativeSession(Session.class); |
|
|
|
if (nativeSession != null) { |
|
|
|
nativeSession.getUserProperties().put(Constants.BLOCKING_SEND_TIMEOUT_PROPERTY, new Long(blockingSendTimeout)); |
|
|
|
} |
|
|
|
} |
|
|
|
String internalSessionId = session.getId(); |
|
|
|
TelemetryWebSocketSessionRef sessionRef = toRef(session); |
|
|
|
String externalSessionId = sessionRef.getSessionId(); |
|
|
|
@ -159,7 +172,7 @@ public class TbWebSocketHandler extends TextWebSocketHandler implements Telemetr |
|
|
|
if (!"telemetry".equalsIgnoreCase(serviceToken)) { |
|
|
|
throw new InvalidParameterException("Can't find plugin with specified token!"); |
|
|
|
} else { |
|
|
|
SecurityUser currentUser = (SecurityUser) session.getAttributes().get(WebSocketConfiguration.WS_SECURITY_USER_ATTRIBUTE); |
|
|
|
SecurityUser currentUser = (SecurityUser) ((Authentication)session.getPrincipal()).getPrincipal(); |
|
|
|
return new TelemetryWebSocketSessionRef(UUID.randomUUID().toString(), currentUser, session.getLocalAddress(), session.getRemoteAddress()); |
|
|
|
} |
|
|
|
} |
|
|
|
|