Java Code Examples for org.springframework.web.socket.WebSocketSession#getRemoteAddress()

The following examples show how to use org.springframework.web.socket.WebSocketSession#getRemoteAddress() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PluginWebSocketHandler.java    From iotplatform with Apache License 2.0 5 votes vote down vote up
private PluginWebsocketSessionRef 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 pluginToken = pathElements[0];
    // TODO: cache
    PluginMetaData pluginMd = pluginService.findPluginByApiToken(pluginToken);
    if (pluginMd == null) {
        throw new InvalidParameterException("Can't find plugin with specified token!");
    } else {
        SecurityUser currentUser = (SecurityUser) session.getAttributes().get(WebSocketConfiguration.WS_SECURITY_USER_ATTRIBUTE);
        TenantId tenantId = currentUser.getTenantId();
        CustomerId customerId = currentUser.getCustomerId();
        if (PluginApiController.validatePluginAccess(pluginMd, tenantId, customerId)) {
            PluginApiCallSecurityContext securityCtx = new PluginApiCallSecurityContext(pluginMd.getTenantId(), pluginMd.getId(), tenantId,
                    currentUser.getCustomerId());
            return new BasicPluginWebsocketSessionRef(UUID.randomUUID().toString(), securityCtx, session.getUri(), session.getAttributes(),
                    session.getLocalAddress(), session.getRemoteAddress());
        } else {
            throw new SecurityException("Current user is not allowed to use this plugin!");
        }
    }
}
 
Example 2
Source File: BrokerStomp.java    From WeEvent with Apache License 2.0 4 votes vote down vote up
/**
 * @param session stomp session
 * @param topic topic name
 * @param groupId group id
 * @param subEventId event id
 * @param continueSubscriptionId subscription id
 * @param tag weevent-tag
 * @param ephemeral is ephemeral
 * @return String consumer subscription id, return "" if error
 * @throws BrokerException Exception
 */
private String handleSubscribe(WebSocketSession session,
                               String topic,
                               String groupId,
                               String headerIdStr,
                               String subEventId,
                               String continueSubscriptionId,
                               String tag,
                               boolean ephemeral) throws BrokerException {
    log.info("topic: {} group id: {} header subscription id: {}", topic, groupId, headerIdStr);

    String[] curTopicList;
    if (topic.contains(WeEvent.MULTIPLE_TOPIC_SEPARATOR)) {
        log.info("subscribe topic list");
        curTopicList = topic.split(WeEvent.MULTIPLE_TOPIC_SEPARATOR);
    } else {
        curTopicList = new String[]{topic};
    }

    if (!this.iconsumer.isStarted()) {
        log.error("IConsumer not started");
        throw new BrokerException(ErrorCode.UNKNOWN_ERROR);
    }

    // external params
    Map<IConsumer.SubscribeExt, String> ext = new HashMap<>();
    ext.put(IConsumer.SubscribeExt.InterfaceType, WeEventConstants.STOMPTYPE);
    String remoteIp = "";
    if (session.getRemoteAddress() != null) {
        remoteIp = session.getRemoteAddress().getAddress().getHostAddress();
    }
    ext.put(IConsumer.SubscribeExt.RemoteIP, remoteIp);

    if (!StringUtils.isBlank(continueSubscriptionId)) {
        log.info("continueSubscriptionId: {}", continueSubscriptionId);
        ext.put(IConsumer.SubscribeExt.SubscriptionId, continueSubscriptionId);
    }

    if (!StringUtils.isBlank(tag)) {
        ext.put(IConsumer.SubscribeExt.TopicTag, tag);
    }

    if (ephemeral) {
        log.info("ephemeral subscription");
        ext.put(IConsumer.SubscribeExt.Ephemeral, "1");
    }

    IConsumer.ConsumerListener listener;
    listener = new IConsumer.ConsumerListener() {
        @Override
        public void onEvent(String subscriptionId, WeEvent event) {
            log.info("consumer onEvent, subscriptionId: {} event: {}", subscriptionId, event);
            handleOnEvent(headerIdStr, subscriptionId, event, session);
        }

        @Override
        public void onException(Throwable e) {
            log.error("consumer onException", e);
        }
    };

    // support both single/multiple topic
    String subscriptionId = this.iconsumer.subscribe(curTopicList, groupId, subEventId, ext, listener);
    log.info("bind context, session id: {} header subscription id: {} consumer subscription id: {} topic: {}",
            session.getId(), headerIdStr, subscriptionId, Arrays.toString(curTopicList));
    sessionContext.get(session.getId()).put(headerIdStr, Pair.of(subscriptionId, StringUtils.join(curTopicList, WeEvent.MULTIPLE_TOPIC_SEPARATOR)));

    log.info("consumer subscribe success, consumer subscriptionId: {}", subscriptionId);
    return subscriptionId;
}