All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bidib.wizard.server.config.UserInterceptor Maven / Gradle / Ivy

There is a newer version: 2.0.29
Show newest version
package org.bidib.wizard.server.config;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
import org.springframework.messaging.simp.stomp.StompCommand;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.messaging.support.ChannelInterceptor;
import org.springframework.messaging.support.MessageHeaderAccessor;

public class UserInterceptor implements ChannelInterceptor {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserInterceptor.class);


    @SuppressWarnings("rawtypes")
    @Override
    public Message preSend(Message message, MessageChannel channel) {

        StompHeaderAccessor headerAccessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);

        LOGGER.info(">>>> preSend is called, headerAccessor: {}", headerAccessor);

        boolean isConnect = StompCommand.CONNECT.equals(headerAccessor.getCommand());

        if (isConnect) {
            Object raw = message.getHeaders().get(SimpMessageHeaderAccessor.NATIVE_HEADERS);

            LOGGER.info("Fetched native headers: {}", raw);
            if (raw instanceof Map) {
                Object name = ((Map) raw).get("login");
                LOGGER.info("Current login name: {}", name);
                if (name instanceof List) {
                    LOGGER.info(">>> *** Set the user from the login name: {}", name);
                    headerAccessor.setUser(new User(((List) name).get(0).toString()));
                }
                else {
                    LOGGER.warn(">>> *** No valid user from the login header received.");
                }
            }
        }
        return message;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy