001/* 002 * Copyright 2002-2017 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 * https://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.web.socket.server.standard; 018 019import javax.servlet.ServletContext; 020import javax.websocket.WebSocketContainer; 021import javax.websocket.server.ServerContainer; 022 023import org.springframework.beans.factory.FactoryBean; 024import org.springframework.beans.factory.InitializingBean; 025import org.springframework.lang.Nullable; 026import org.springframework.util.Assert; 027import org.springframework.web.context.ServletContextAware; 028 029/** 030 * A {@link FactoryBean} for configuring {@link javax.websocket.server.ServerContainer}. 031 * Since there is usually only one {@code ServerContainer} instance accessible under a 032 * well-known {@code javax.servlet.ServletContext} attribute, simply declaring this 033 * FactoryBean and using its setters allows for configuring the {@code ServerContainer} 034 * through Spring configuration. 035 * 036 * <p>This is useful even if the {@code ServerContainer} is not injected into any other 037 * bean within the Spring application context. For example, an application can configure 038 * a {@link org.springframework.web.socket.server.support.DefaultHandshakeHandler}, 039 * a {@link org.springframework.web.socket.sockjs.SockJsService}, or 040 * {@link ServerEndpointExporter}, and separately declare this FactoryBean in order 041 * to customize the properties of the (one and only) {@code ServerContainer} instance. 042 * 043 * @author Rossen Stoyanchev 044 * @author Sam Brannen 045 * @since 4.0 046 */ 047public class ServletServerContainerFactoryBean 048 implements FactoryBean<WebSocketContainer>, ServletContextAware, InitializingBean { 049 050 @Nullable 051 private Long asyncSendTimeout; 052 053 @Nullable 054 private Long maxSessionIdleTimeout; 055 056 @Nullable 057 private Integer maxTextMessageBufferSize; 058 059 @Nullable 060 private Integer maxBinaryMessageBufferSize; 061 062 @Nullable 063 private ServletContext servletContext; 064 065 @Nullable 066 private ServerContainer serverContainer; 067 068 069 public void setAsyncSendTimeout(Long timeoutInMillis) { 070 this.asyncSendTimeout = timeoutInMillis; 071 } 072 073 @Nullable 074 public Long getAsyncSendTimeout() { 075 return this.asyncSendTimeout; 076 } 077 078 public void setMaxSessionIdleTimeout(Long timeoutInMillis) { 079 this.maxSessionIdleTimeout = timeoutInMillis; 080 } 081 082 @Nullable 083 public Long getMaxSessionIdleTimeout() { 084 return this.maxSessionIdleTimeout; 085 } 086 087 public void setMaxTextMessageBufferSize(Integer bufferSize) { 088 this.maxTextMessageBufferSize = bufferSize; 089 } 090 091 @Nullable 092 public Integer getMaxTextMessageBufferSize() { 093 return this.maxTextMessageBufferSize; 094 } 095 096 public void setMaxBinaryMessageBufferSize(Integer bufferSize) { 097 this.maxBinaryMessageBufferSize = bufferSize; 098 } 099 100 @Nullable 101 public Integer getMaxBinaryMessageBufferSize() { 102 return this.maxBinaryMessageBufferSize; 103 } 104 105 @Override 106 public void setServletContext(ServletContext servletContext) { 107 this.servletContext = servletContext; 108 } 109 110 111 @Override 112 public void afterPropertiesSet() { 113 Assert.state(this.servletContext != null, 114 "A ServletContext is required to access the javax.websocket.server.ServerContainer instance"); 115 this.serverContainer = (ServerContainer) this.servletContext.getAttribute( 116 "javax.websocket.server.ServerContainer"); 117 Assert.state(this.serverContainer != null, 118 "Attribute 'javax.websocket.server.ServerContainer' not found in ServletContext"); 119 120 if (this.asyncSendTimeout != null) { 121 this.serverContainer.setAsyncSendTimeout(this.asyncSendTimeout); 122 } 123 if (this.maxSessionIdleTimeout != null) { 124 this.serverContainer.setDefaultMaxSessionIdleTimeout(this.maxSessionIdleTimeout); 125 } 126 if (this.maxTextMessageBufferSize != null) { 127 this.serverContainer.setDefaultMaxTextMessageBufferSize(this.maxTextMessageBufferSize); 128 } 129 if (this.maxBinaryMessageBufferSize != null) { 130 this.serverContainer.setDefaultMaxBinaryMessageBufferSize(this.maxBinaryMessageBufferSize); 131 } 132 } 133 134 135 @Override 136 @Nullable 137 public ServerContainer getObject() { 138 return this.serverContainer; 139 } 140 141 @Override 142 public Class<?> getObjectType() { 143 return (this.serverContainer != null ? this.serverContainer.getClass() : ServerContainer.class); 144 } 145 146 @Override 147 public boolean isSingleton() { 148 return true; 149 } 150 151}