001/* 002 * Copyright 2012-2016 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; 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.jackson.JacksonAutoConfiguration; 029import org.springframework.context.annotation.Configuration; 030import org.springframework.messaging.converter.ByteArrayMessageConverter; 031import org.springframework.messaging.converter.DefaultContentTypeResolver; 032import org.springframework.messaging.converter.MappingJackson2MessageConverter; 033import org.springframework.messaging.converter.MessageConverter; 034import org.springframework.messaging.converter.StringMessageConverter; 035import org.springframework.messaging.simp.config.AbstractMessageBrokerConfiguration; 036import org.springframework.util.MimeTypeUtils; 037import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer; 038import org.springframework.web.socket.config.annotation.DelegatingWebSocketMessageBrokerConfiguration; 039import org.springframework.web.socket.config.annotation.StompEndpointRegistry; 040import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer; 041 042/** 043 * {@link EnableAutoConfiguration Auto-configuration} for WebSocket-based messaging. 044 * 045 * @author Andy Wilkinson 046 * @since 1.3.0 047 */ 048@ConditionalOnWebApplication 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 extends AbstractWebSocketMessageBrokerConfigurer { 059 060 private final ObjectMapper objectMapper; 061 062 WebSocketMessageConverterConfiguration(ObjectMapper objectMapper) { 063 this.objectMapper = objectMapper; 064 } 065 066 @Override 067 public void registerStompEndpoints(StompEndpointRegistry registry) { 068 // The user must register their own endpoints 069 } 070 071 @Override 072 public boolean configureMessageConverters( 073 List<MessageConverter> messageConverters) { 074 MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter(); 075 converter.setObjectMapper(this.objectMapper); 076 DefaultContentTypeResolver resolver = new DefaultContentTypeResolver(); 077 resolver.setDefaultMimeType(MimeTypeUtils.APPLICATION_JSON); 078 converter.setContentTypeResolver(resolver); 079 messageConverters.add(new StringMessageConverter()); 080 messageConverters.add(new ByteArrayMessageConverter()); 081 messageConverters.add(converter); 082 return false; 083 } 084 085 } 086 087}