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