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 javax.servlet.Servlet; 020import javax.websocket.server.ServerContainer; 021 022import org.apache.catalina.startup.Tomcat; 023import org.eclipse.jetty.websocket.jsr356.server.deploy.WebSocketServerContainerInitializer; 024 025import org.springframework.boot.autoconfigure.AutoConfigureBefore; 026import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 027import org.springframework.boot.autoconfigure.condition.ConditionalOnJava; 028import org.springframework.boot.autoconfigure.condition.ConditionalOnJava.JavaVersion; 029import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 030import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; 031import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration; 032import org.springframework.context.annotation.Bean; 033import org.springframework.context.annotation.Configuration; 034 035/** 036 * Auto configuration for websocket server in embedded Tomcat, Jetty or Undertow. Requires 037 * the appropriate WebSocket modules to be on the classpath. 038 * <p> 039 * If Tomcat's WebSocket support is detected on the classpath we add a customizer that 040 * installs the Tomcat Websocket initializer. In a non-embedded container it should 041 * already be there. 042 * <p> 043 * If Jetty's WebSocket support is detected on the classpath we add a configuration that 044 * configures the context with WebSocket support. In a non-embedded container it should 045 * already be there. 046 * <p> 047 * If Undertow's WebSocket support is detected on the classpath we add a customizer that 048 * installs the Undertow Websocket DeploymentInfo Customizer. In a non-embedded container 049 * it should already be there. 050 * 051 * @author Dave Syer 052 * @author Phillip Webb 053 * @author Andy Wilkinson 054 */ 055@Configuration 056@ConditionalOnClass({ Servlet.class, ServerContainer.class }) 057@ConditionalOnWebApplication 058@AutoConfigureBefore(EmbeddedServletContainerAutoConfiguration.class) 059public class WebSocketAutoConfiguration { 060 061 @Configuration 062 @ConditionalOnClass(name = "org.apache.tomcat.websocket.server.WsSci", value = Tomcat.class) 063 static class TomcatWebSocketConfiguration { 064 065 @Bean 066 @ConditionalOnMissingBean(name = "websocketContainerCustomizer") 067 @ConditionalOnJava(JavaVersion.SEVEN) 068 public TomcatWebSocketContainerCustomizer websocketContainerCustomizer() { 069 return new TomcatWebSocketContainerCustomizer(); 070 } 071 072 } 073 074 @Configuration 075 @ConditionalOnClass(WebSocketServerContainerInitializer.class) 076 static class JettyWebSocketConfiguration { 077 078 @Bean 079 @ConditionalOnMissingBean(name = "websocketContainerCustomizer") 080 public JettyWebSocketContainerCustomizer websocketContainerCustomizer() { 081 return new JettyWebSocketContainerCustomizer(); 082 } 083 084 } 085 086 @Configuration 087 @ConditionalOnClass(io.undertow.websockets.jsr.Bootstrap.class) 088 static class UndertowWebSocketConfiguration { 089 090 @Bean 091 @ConditionalOnMissingBean(name = "websocketContainerCustomizer") 092 public UndertowWebSocketContainerCustomizer websocketContainerCustomizer() { 093 return new UndertowWebSocketContainerCustomizer(); 094 } 095 096 } 097 098}