001/*
002 * Copyright 2012-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.autoconfigure.websocket.servlet;
018
019import java.util.List;
020
021import com.fasterxml.jackson.databind.ObjectMapper;
022
023import org.springframework.boot.autoconfigure.AutoConfigureAfter;
024import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
025import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
026import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
027import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
028import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
029import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
030import org.springframework.context.annotation.Configuration;
031import org.springframework.messaging.converter.ByteArrayMessageConverter;
032import org.springframework.messaging.converter.DefaultContentTypeResolver;
033import org.springframework.messaging.converter.MappingJackson2MessageConverter;
034import org.springframework.messaging.converter.MessageConverter;
035import org.springframework.messaging.converter.StringMessageConverter;
036import org.springframework.messaging.simp.config.AbstractMessageBrokerConfiguration;
037import org.springframework.util.MimeTypeUtils;
038import org.springframework.web.socket.config.annotation.DelegatingWebSocketMessageBrokerConfiguration;
039import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
040
041/**
042 * {@link EnableAutoConfiguration Auto-configuration} for WebSocket-based messaging.
043 *
044 * @author Andy Wilkinson
045 * @since 1.3.0
046 */
047@Configuration
048@ConditionalOnWebApplication(type = Type.SERVLET)
049@ConditionalOnClass(WebSocketMessageBrokerConfigurer.class)
050@AutoConfigureAfter(JacksonAutoConfiguration.class)
051public class WebSocketMessagingAutoConfiguration {
052
053        @Configuration
054        @ConditionalOnBean({ DelegatingWebSocketMessageBrokerConfiguration.class,
055                        ObjectMapper.class })
056        @ConditionalOnClass({ ObjectMapper.class, AbstractMessageBrokerConfiguration.class })
057        static class WebSocketMessageConverterConfiguration
058                        implements WebSocketMessageBrokerConfigurer {
059
060                private final ObjectMapper objectMapper;
061
062                WebSocketMessageConverterConfiguration(ObjectMapper objectMapper) {
063                        this.objectMapper = objectMapper;
064                }
065
066                @Override
067                public boolean configureMessageConverters(
068                                List<MessageConverter> messageConverters) {
069                        MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
070                        converter.setObjectMapper(this.objectMapper);
071                        DefaultContentTypeResolver resolver = new DefaultContentTypeResolver();
072                        resolver.setDefaultMimeType(MimeTypeUtils.APPLICATION_JSON);
073                        converter.setContentTypeResolver(resolver);
074                        messageConverters.add(new StringMessageConverter());
075                        messageConverters.add(new ByteArrayMessageConverter());
076                        messageConverters.add(converter);
077                        return false;
078                }
079
080        }
081
082}