Example usage for javax.websocket Session setMaxIdleTimeout

List of usage examples for javax.websocket Session setMaxIdleTimeout

Introduction

In this page you can find the example usage for javax.websocket Session setMaxIdleTimeout.

Prototype

void setMaxIdleTimeout(long timeout);

Source Link

Document

Set the idle timeout for this session.

Usage

From source file:com.ec2box.manage.socket.SecureShellWS.java

@OnOpen
public void onOpen(Session session, EndpointConfig config) {

    //set websocket timeout
    if (StringUtils.isNotEmpty(AppConfig.getProperty("websocketTimeout"))) {
        session.setMaxIdleTimeout(Long.parseLong(AppConfig.getProperty("websocketTimeout")) * 60000);
    } else {/*from ww  w . j a  v a 2s  . c  om*/
        session.setMaxIdleTimeout(0);
    }

    this.httpSession = (HttpSession) config.getUserProperties().get(HttpSession.class.getName());
    this.sessionId = AuthUtil.getSessionId(httpSession);
    this.session = session;

    Runnable run = new SentOutputTask(sessionId, session, UserDB.getUser(AuthUtil.getUserId(httpSession)));
    Thread thread = new Thread(run);
    thread.start();

}

From source file:org.apache.zeppelin.shell.terminal.service.TerminalService.java

public void onWebSocketConnect(Session webSocketSession) {
    this.webSocketSession = webSocketSession;
    webSocketSession.setMaxIdleTimeout(60 * 60 * 1000);
}

From source file:org.wso2.carbon.device.mgt.extensions.remote.session.RemoteSessionManagementServiceImpl.java

@Override
public void initializeSession(Session session, String deviceType, String deviceId, String operationId)
        throws RemoteSessionManagementException {

    // Check whether required configurations are enabled
    if (!RemoteSessionManagementDataHolder.getInstance().isEnabled()) {
        throw new RemoteSessionManagementException("Remote session feature is disabled.");
    } else if (RemoteSessionManagementDataHolder.getInstance().getServerUrl() == null) {
        throw new RemoteSessionManagementException("Server url has not been configured.");
    }/* w  w w  .ja v a2  s.  c  o m*/

    // Read Query Parameters for obtain the token
    Map<String, List<String>> sessionQueryParam = new HashMap();
    List<String> sessionQueryParamList = new LinkedList<>();
    sessionQueryParamList.add(session.getQueryString());
    sessionQueryParam.put(RemoteSessionConstants.QUERY_STRING, sessionQueryParamList);

    // Validate the token
    OAuthAuthenticator oAuthAuthenticator = RemoteSessionManagementDataHolder.getInstance()
            .getOauthAuthenticator();
    AuthenticationInfo authenticationInfo = oAuthAuthenticator.isAuthenticated(sessionQueryParam);

    if (authenticationInfo != null && authenticationInfo.isAuthenticated()) {
        try {
            PrivilegedCarbonContext.startTenantFlow();
            PrivilegedCarbonContext.getThreadLocalCarbonContext()
                    .setTenantDomain(authenticationInfo.getTenantDomain(), true);
            PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(authenticationInfo.getUsername());
            if (deviceId != null && !deviceId.isEmpty() && deviceType != null && !deviceType.isEmpty()) {
                DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
                deviceIdentifier.setId(deviceId);
                deviceIdentifier.setType(deviceType);

                // Check authorization of user for given device
                boolean userAuthorized = RemoteSessionManagementDataHolder.getInstance()
                        .getDeviceAccessAuthorizationService()
                        .isUserAuthorized(deviceIdentifier, authenticationInfo.getUsername());
                if (userAuthorized) {
                    // set common settings for session
                    session.setMaxBinaryMessageBufferSize(
                            RemoteSessionManagementDataHolder.getInstance().getMaxMessageBufferSize());
                    session.setMaxTextMessageBufferSize(
                            RemoteSessionManagementDataHolder.getInstance().getMaxMessageBufferSize());
                    session.setMaxIdleTimeout(
                            RemoteSessionManagementDataHolder.getInstance().getMaxIdleTimeout());

                    // if session initiated using operation id means request came from device
                    if (operationId != null) {
                        // create new device session
                        initializeDeviceSession(session, authenticationInfo.getTenantDomain(), deviceType,
                                deviceId, operationId);
                    } else {
                        // create new client session
                        initializeClientSession(session, authenticationInfo.getTenantDomain(), deviceType,
                                deviceId);
                    }
                    log.info("Current remote sessions count: "
                            + RemoteSessionManagementDataHolder.getInstance().getSessionMap().size());

                } else {
                    throw new RemoteSessionManagementException("Missing device Id or type ");
                }
            } else {
                throw new RemoteSessionManagementException("Unauthorized Access for the device Type : "
                        + deviceType + " , deviceId : " + deviceId);
            }
        } catch (OperationManagementException | InvalidDeviceException e) {
            throw new RemoteSessionManagementException("Error occurred while adding initial operation for the "
                    + "device Type : " + deviceType + " , deviceId : " + deviceId);
        } catch (DeviceAccessAuthorizationException e) {
            throw new RemoteSessionManagementException(
                    "Error occurred while device access authorization for the " + "device Type : " + deviceType
                            + " , " + "deviceId : " + deviceId);
        } finally {
            PrivilegedCarbonContext.endTenantFlow();
        }

    } else {
        throw new RemoteSessionManagementException("Invalid token");
    }
}