Search in sources :

Example 1 with TelemetryWebSocketSessionRef

use of org.thingsboard.server.service.telemetry.TelemetryWebSocketSessionRef in project thingsboard by thingsboard.

the class TbWebSocketHandler method toRef.

private TelemetryWebSocketSessionRef toRef(WebSocketSession session) throws IOException {
    URI sessionUri = session.getUri();
    String path = sessionUri.getPath();
    path = path.substring(WebSocketConfiguration.WS_PLUGIN_PREFIX.length());
    if (path.length() == 0) {
        throw new IllegalArgumentException("URL should contain plugin token!");
    }
    String[] pathElements = path.split("/");
    String serviceToken = pathElements[0];
    if (!"telemetry".equalsIgnoreCase(serviceToken)) {
        throw new InvalidParameterException("Can't find plugin with specified token!");
    } else {
        SecurityUser currentUser = (SecurityUser) ((Authentication) session.getPrincipal()).getPrincipal();
        return new TelemetryWebSocketSessionRef(UUID.randomUUID().toString(), currentUser, session.getLocalAddress(), session.getRemoteAddress());
    }
}
Also used : InvalidParameterException(java.security.InvalidParameterException) SecurityUser(org.thingsboard.server.service.security.model.SecurityUser) TelemetryWebSocketSessionRef(org.thingsboard.server.service.telemetry.TelemetryWebSocketSessionRef) URI(java.net.URI)

Example 2 with TelemetryWebSocketSessionRef

use of org.thingsboard.server.service.telemetry.TelemetryWebSocketSessionRef in project thingsboard by thingsboard.

the class TbWebSocketHandler method afterConnectionEstablished.

@Override
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.getAsyncRemote().setSendTimeout(sendTimeout);
            }
        }
        String internalSessionId = session.getId();
        TelemetryWebSocketSessionRef sessionRef = toRef(session);
        String externalSessionId = sessionRef.getSessionId();
        if (!checkLimits(session, sessionRef)) {
            return;
        }
        internalSessionMap.put(internalSessionId, new SessionMetaData(session, sessionRef, maxMsgQueuePerSession));
        externalSessionMap.put(externalSessionId, internalSessionId);
        processInWebSocketService(sessionRef, SessionEvent.onEstablished());
        log.info("[{}][{}][{}] Session is opened", sessionRef.getSecurityCtx().getTenantId(), externalSessionId, session.getId());
    } catch (InvalidParameterException e) {
        log.warn("[{}] Failed to start session", session.getId(), e);
        session.close(CloseStatus.BAD_DATA.withReason(e.getMessage()));
    } catch (Exception e) {
        log.warn("[{}] Failed to start session", session.getId(), e);
        session.close(CloseStatus.SERVER_ERROR.withReason(e.getMessage()));
    }
}
Also used : InvalidParameterException(java.security.InvalidParameterException) TelemetryWebSocketSessionRef(org.thingsboard.server.service.telemetry.TelemetryWebSocketSessionRef) NativeWebSocketSession(org.springframework.web.socket.adapter.NativeWebSocketSession) BeanCreationNotAllowedException(org.springframework.beans.factory.BeanCreationNotAllowedException) InvalidParameterException(java.security.InvalidParameterException) IOException(java.io.IOException) WebSocketSession(org.springframework.web.socket.WebSocketSession) Session(javax.websocket.Session) NativeWebSocketSession(org.springframework.web.socket.adapter.NativeWebSocketSession)

Aggregations

InvalidParameterException (java.security.InvalidParameterException)2 TelemetryWebSocketSessionRef (org.thingsboard.server.service.telemetry.TelemetryWebSocketSessionRef)2 IOException (java.io.IOException)1 URI (java.net.URI)1 Session (javax.websocket.Session)1 BeanCreationNotAllowedException (org.springframework.beans.factory.BeanCreationNotAllowedException)1 WebSocketSession (org.springframework.web.socket.WebSocketSession)1 NativeWebSocketSession (org.springframework.web.socket.adapter.NativeWebSocketSession)1 SecurityUser (org.thingsboard.server.service.security.model.SecurityUser)1