|
| 1 | +/* |
| 2 | + * Copyright 2012-2013 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.autoconfigure.websocket; |
| 18 | + |
| 19 | +import java.util.Collection; |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.Map; |
| 22 | + |
| 23 | +import javax.servlet.ServletContainerInitializer; |
| 24 | + |
| 25 | +import org.apache.catalina.Context; |
| 26 | +import org.springframework.beans.BeanUtils; |
| 27 | +import org.springframework.beans.BeansException; |
| 28 | +import org.springframework.beans.factory.config.BeanPostProcessor; |
| 29 | +import org.springframework.boot.autoconfigure.AutoConfigureBefore; |
| 30 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
| 31 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
| 32 | +import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration; |
| 33 | +import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory; |
| 34 | +import org.springframework.context.annotation.Bean; |
| 35 | +import org.springframework.context.annotation.Configuration; |
| 36 | +import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; |
| 37 | +import org.springframework.util.ClassUtils; |
| 38 | +import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping; |
| 39 | +import org.springframework.web.socket.WebSocketHandler; |
| 40 | +import org.springframework.web.socket.sockjs.SockJsHttpRequestHandler; |
| 41 | +import org.springframework.web.socket.sockjs.SockJsService; |
| 42 | +import org.springframework.web.socket.sockjs.support.AbstractSockJsService; |
| 43 | +import org.springframework.web.socket.sockjs.transport.handler.DefaultSockJsService; |
| 44 | + |
| 45 | +/** |
| 46 | + * Auto configuration for websockets (and sockjs in particular). Users should be able to |
| 47 | + * just define beans of type {@link WebSocketHandler}. If <code>spring-websocket</code> is |
| 48 | + * detected on the classpath then we add a {@link DefaultSockJsService} and an MVC handler |
| 49 | + * mapping to <code>/<beanName>/**</code> for all of the |
| 50 | + * <code>WebSocketHandler</code> beans that have a bean name beginning with "/". |
| 51 | + * |
| 52 | + * @author Dave Syer |
| 53 | + */ |
| 54 | +@Configuration |
| 55 | +@ConditionalOnClass({ WebSocketHandler.class }) |
| 56 | +@AutoConfigureBefore(EmbeddedServletContainerAutoConfiguration.class) |
| 57 | +public class WebSocketAutoConfiguration { |
| 58 | + |
| 59 | + private static class WebSocketEndpointPostProcessor implements BeanPostProcessor { |
| 60 | + |
| 61 | + private Map<String, WebSocketHandler> prefixes = new HashMap<String, WebSocketHandler>(); |
| 62 | + |
| 63 | + @Override |
| 64 | + public Object postProcessBeforeInitialization(Object bean, String beanName) |
| 65 | + throws BeansException { |
| 66 | + return bean; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public Object postProcessAfterInitialization(Object bean, String beanName) |
| 71 | + throws BeansException { |
| 72 | + if (bean instanceof WebSocketHandler && beanName.startsWith("/")) { |
| 73 | + this.prefixes.put(beanName, (WebSocketHandler) bean); |
| 74 | + } |
| 75 | + return bean; |
| 76 | + } |
| 77 | + |
| 78 | + public WebSocketHandler getHandler(String prefix) { |
| 79 | + return this.prefixes.get(prefix); |
| 80 | + } |
| 81 | + |
| 82 | + public String[] getPrefixes() { |
| 83 | + return this.prefixes.keySet().toArray(new String[this.prefixes.size()]); |
| 84 | + } |
| 85 | + |
| 86 | + } |
| 87 | + |
| 88 | + @Bean |
| 89 | + public WebSocketEndpointPostProcessor webSocketEndpointPostProcessor() { |
| 90 | + return new WebSocketEndpointPostProcessor(); |
| 91 | + } |
| 92 | + |
| 93 | + @Bean |
| 94 | + @ConditionalOnMissingBean(SockJsService.class) |
| 95 | + public DefaultSockJsService sockJsService() { |
| 96 | + DefaultSockJsService service = new DefaultSockJsService(sockJsTaskScheduler()); |
| 97 | + service.setSockJsClientLibraryUrl("https://cdn.sockjs.org/sockjs-0.3.4.min.js"); |
| 98 | + service.setWebSocketsEnabled(true); |
| 99 | + return service; |
| 100 | + } |
| 101 | + |
| 102 | + @Bean |
| 103 | + public SimpleUrlHandlerMapping handlerMapping(SockJsService sockJsService, |
| 104 | + Collection<WebSocketHandler> handlers) { |
| 105 | + |
| 106 | + WebSocketEndpointPostProcessor processor = webSocketEndpointPostProcessor(); |
| 107 | + Map<String, Object> urlMap = new HashMap<String, Object>(); |
| 108 | + for (String prefix : webSocketEndpointPostProcessor().getPrefixes()) { |
| 109 | + urlMap.put(prefix + "/**", new SockJsHttpRequestHandler(sockJsService, |
| 110 | + processor.getHandler(prefix))); |
| 111 | + } |
| 112 | + |
| 113 | + if (sockJsService instanceof AbstractSockJsService) { |
| 114 | + ((AbstractSockJsService) sockJsService).setValidSockJsPrefixes(processor |
| 115 | + .getPrefixes()); |
| 116 | + } |
| 117 | + SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping(); |
| 118 | + handlerMapping.setOrder(-1); |
| 119 | + handlerMapping.setUrlMap(urlMap); |
| 120 | + |
| 121 | + return handlerMapping; |
| 122 | + } |
| 123 | + |
| 124 | + @Bean |
| 125 | + @ConditionalOnMissingBean(name = "sockJsTaskScheduler") |
| 126 | + public ThreadPoolTaskScheduler sockJsTaskScheduler() { |
| 127 | + ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler(); |
| 128 | + taskScheduler.setThreadNamePrefix("SockJS-"); |
| 129 | + return taskScheduler; |
| 130 | + } |
| 131 | + |
| 132 | + @Configuration |
| 133 | + @ConditionalOnClass(name = "org.apache.tomcat.websocket.server.WsSci") |
| 134 | + protected static class TomcatWebSocketConfiguration { |
| 135 | + @Bean |
| 136 | + public TomcatEmbeddedServletContainerFactory tomcatEmbeddedServletContainerFactory() { |
| 137 | + TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory() { |
| 138 | + @Override |
| 139 | + protected void postProcessContext(Context context) { |
| 140 | + context.addServletContainerInitializer( |
| 141 | + (ServletContainerInitializer) BeanUtils |
| 142 | + .instantiate(ClassUtils.resolveClassName( |
| 143 | + "org.apache.tomcat.websocket.server.WsSci", |
| 144 | + null)), null); |
| 145 | + } |
| 146 | + }; |
| 147 | + return factory; |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | +} |
0 commit comments