package org.araymond.joal.web.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.simp.config.ChannelRegistration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.messaging.simp.stomp.StompCommand;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.messaging.support.ChannelInterceptorAdapter;
import org.springframework.messaging.support.MessageHeaderAccessor;
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketTransportRegistration;

/**
 * Created by raymo on 22/06/2017.
 */
@ConditionalOnProperty(name = "spring.main.web-environment", havingValue = "true")
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

    private final String webSocketPathPrefix;

    public WebSocketConfig(@Value("${joal.ui.path.prefix}")final String webSocketPathPrefix) {
        this.webSocketPathPrefix = webSocketPathPrefix;
    }

    @Override
    public void configureWebSocketTransport(final WebSocketTransportRegistration registration) {
        registration
                .setMessageSizeLimit(1000 * 1024) // Max outgoing message size => 1Mo
                .setSendBufferSizeLimit(5000 * 1024); // Max incoming message size => 5Mo
    }

    @Override
    public void configureMessageBroker(final MessageBrokerRegistry config) {
        config.enableSimpleBroker(
                "/global",
                "/announce",
                "/config",
                "/torrents"
        );
        config.setApplicationDestinationPrefixes("/joal");
    }

    @Override
    public void configureClientInboundChannel(final ChannelRegistration registration) {
        // TODO : see http://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html#websocket-stomp-authentication-token-based
        // TODO: about @Order(Ordered.HIGHEST_PRECEDENCE + 99)
        registration.setInterceptors(new ChannelInterceptorAdapter() {
            @Override
            public Message<?> preSend(final Message<?> message, final MessageChannel channel) {

                final StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);
                if (StompCommand.CONNECT == accessor.getCommand()) {
                    //Principal user = ... ; // access authentication header(s)
                    //accessor.setUser(user);
                }
                return message;
            }
        });
    }

    @Override
    public void registerStompEndpoints(final StompEndpointRegistry registry) {
        registry.addEndpoint(this.webSocketPathPrefix)
                .setAllowedOrigins("*");
    }

}

@@ -272,481 +272,41 @@
 ing.
-Message;%0Aimport org.springframework.messaging.MessageChannel;%0Aimport org.springframework.messaging.simp.config.ChannelRegistration;%0Aimport org.springframework.messaging.simp.config.MessageBrokerRegistry;%0Aimport org.springframework.messaging.simp.stomp.StompCommand;%0Aimport org.springframework.messaging.simp.stomp.StompHeaderAccessor;%0Aimport org.springframework.messaging.support.ChannelInterceptorAdapter;%0Aimport org.springframework.messaging.support.MessageHeaderAccessor
+simp.config.MessageBrokerRegistry
 ;%0Aim
@@ -1745,919 +1745,8 @@
  %7D%0A%0A
-    @Override%0A    public void configureClientInboundChannel(final ChannelRegistration registration) %7B%0A        // TODO : see http://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html#websocket-stomp-authentication-token-based%0A        // TODO: about @Order(Ordered.HIGHEST_PRECEDENCE + 99)%0A        registration.setInterceptors(new ChannelInterceptorAdapter() %7B%0A            @Override%0A            public Message%3C?%3E preSend(final Message%3C?%3E message, final MessageChannel channel) %7B%0A%0A                final StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);%0A                if (StompCommand.CONNECT == accessor.getCommand()) %7B%0A                    //Principal user = ... ; // access authentication header(s)%0A                    //accessor.setUser(user);%0A                %7D%0A                return message;%0A            %7D%0A        %7D);%0A    %7D%0A%0A
     

Fix WebSocketConfig