You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PushRequestHandler.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * 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, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.server.communication;
  17. import java.io.IOException;
  18. import java.util.logging.Level;
  19. import java.util.logging.Logger;
  20. import javax.servlet.ServletConfig;
  21. import javax.servlet.ServletException;
  22. import org.atmosphere.cache.UUIDBroadcasterCache;
  23. import org.atmosphere.client.TrackMessageSizeInterceptor;
  24. import org.atmosphere.cpr.ApplicationConfig;
  25. import org.atmosphere.cpr.AtmosphereFramework;
  26. import org.atmosphere.cpr.AtmosphereFramework.AtmosphereHandlerWrapper;
  27. import org.atmosphere.cpr.AtmosphereHandler;
  28. import org.atmosphere.cpr.AtmosphereInterceptor;
  29. import org.atmosphere.cpr.AtmosphereRequestImpl;
  30. import org.atmosphere.cpr.AtmosphereResponseImpl;
  31. import org.atmosphere.interceptor.HeartbeatInterceptor;
  32. import org.atmosphere.util.VoidAnnotationProcessor;
  33. import com.vaadin.server.RequestHandler;
  34. import com.vaadin.server.ServiceDestroyEvent;
  35. import com.vaadin.server.ServiceException;
  36. import com.vaadin.server.ServletPortletHelper;
  37. import com.vaadin.server.SessionExpiredHandler;
  38. import com.vaadin.server.VaadinRequest;
  39. import com.vaadin.server.VaadinResponse;
  40. import com.vaadin.server.VaadinServletRequest;
  41. import com.vaadin.server.VaadinServletResponse;
  42. import com.vaadin.server.VaadinServletService;
  43. import com.vaadin.server.VaadinSession;
  44. import com.vaadin.shared.communication.PushConstants;
  45. /**
  46. * Handles requests to open a push (bidirectional) communication channel between
  47. * the client and the server. After the initial request, communication through
  48. * the push channel is managed by {@link PushAtmosphereHandler} and
  49. * {@link PushHandler}
  50. *
  51. * @author Vaadin Ltd
  52. * @since 7.1
  53. */
  54. public class PushRequestHandler
  55. implements RequestHandler, SessionExpiredHandler {
  56. private AtmosphereFramework atmosphere;
  57. private PushHandler pushHandler;
  58. public PushRequestHandler(VaadinServletService service)
  59. throws ServiceException {
  60. service.addServiceDestroyListener((ServiceDestroyEvent event) -> {
  61. destroy();
  62. });
  63. final ServletConfig vaadinServletConfig = service.getServlet()
  64. .getServletConfig();
  65. pushHandler = createPushHandler(service);
  66. atmosphere = getPreInitializedAtmosphere(vaadinServletConfig);
  67. if (atmosphere == null) {
  68. // Not initialized by JSR356WebsocketInitializer
  69. getLogger().fine("Initializing Atmosphere for servlet "
  70. + vaadinServletConfig.getServletName());
  71. try {
  72. atmosphere = initAtmosphere(vaadinServletConfig);
  73. } catch (Exception e) {
  74. getLogger().log(Level.WARNING,
  75. "Failed to initialize Atmosphere for "
  76. + service.getServlet().getServletName()
  77. + ". Push will not work.",
  78. e);
  79. return;
  80. }
  81. } else {
  82. getLogger().fine("Using pre-initialized Atmosphere for servlet "
  83. + vaadinServletConfig.getServletName());
  84. }
  85. pushHandler.setLongPollingSuspendTimeout(
  86. atmosphere.getAtmosphereConfig().getInitParameter(
  87. com.vaadin.server.Constants.SERVLET_PARAMETER_PUSH_SUSPEND_TIMEOUT_LONGPOLLING,
  88. -1));
  89. for (AtmosphereHandlerWrapper handlerWrapper : atmosphere
  90. .getAtmosphereHandlers().values()) {
  91. AtmosphereHandler handler = handlerWrapper.atmosphereHandler;
  92. if (handler instanceof PushAtmosphereHandler) {
  93. // Map the (possibly pre-initialized) handler to the actual push
  94. // handler
  95. ((PushAtmosphereHandler) handler).setPushHandler(pushHandler);
  96. }
  97. }
  98. }
  99. /**
  100. * Creates a push handler for this request handler.
  101. * <p>
  102. * Create your own request handler and override this method if you want to
  103. * customize the {@link PushHandler}, e.g. to dynamically decide the suspend
  104. * timeout.
  105. *
  106. * @since 7.6
  107. * @param service
  108. * the vaadin service
  109. * @return the push handler to use for this service
  110. */
  111. protected PushHandler createPushHandler(VaadinServletService service) {
  112. return new PushHandler(service);
  113. }
  114. private static final Logger getLogger() {
  115. return Logger.getLogger(PushRequestHandler.class.getName());
  116. }
  117. /**
  118. * Returns an AtmosphereFramework instance which was initialized in the
  119. * servlet context init phase by {@link JSR356WebsocketInitializer}, if such
  120. * exists
  121. */
  122. private AtmosphereFramework getPreInitializedAtmosphere(
  123. ServletConfig vaadinServletConfig) {
  124. String attributeName = JSR356WebsocketInitializer
  125. .getAttributeName(vaadinServletConfig.getServletName());
  126. Object framework = vaadinServletConfig.getServletContext()
  127. .getAttribute(attributeName);
  128. if (framework != null && framework instanceof AtmosphereFramework) {
  129. return (AtmosphereFramework) framework;
  130. }
  131. return null;
  132. }
  133. /**
  134. * Initializes Atmosphere for the given ServletConfiguration
  135. *
  136. * @since 7.5.0
  137. * @param vaadinServletConfig
  138. * The servlet configuration for the servlet which should have
  139. * Atmosphere support
  140. */
  141. static AtmosphereFramework initAtmosphere(
  142. final ServletConfig vaadinServletConfig) {
  143. AtmosphereFramework atmosphere = new AtmosphereFramework(false, false) {
  144. @Override
  145. protected void analytics() {
  146. // Overridden to disable version number check
  147. }
  148. @Override
  149. public AtmosphereFramework addInitParameter(String name,
  150. String value) {
  151. if (vaadinServletConfig.getInitParameter(name) == null) {
  152. super.addInitParameter(name, value);
  153. }
  154. return this;
  155. }
  156. };
  157. atmosphere.addAtmosphereHandler("/*", new PushAtmosphereHandler());
  158. atmosphere.addInitParameter(ApplicationConfig.BROADCASTER_CACHE,
  159. UUIDBroadcasterCache.class.getName());
  160. atmosphere.addInitParameter(ApplicationConfig.ANNOTATION_PROCESSOR,
  161. VoidAnnotationProcessor.class.getName());
  162. atmosphere.addInitParameter(ApplicationConfig.PROPERTY_SESSION_SUPPORT,
  163. "true");
  164. atmosphere.addInitParameter(ApplicationConfig.MESSAGE_DELIMITER,
  165. String.valueOf(PushConstants.MESSAGE_DELIMITER));
  166. atmosphere.addInitParameter(
  167. ApplicationConfig.DROP_ACCESS_CONTROL_ALLOW_ORIGIN_HEADER,
  168. "false");
  169. // Disable heartbeat (it does not emit correct events client side)
  170. // https://github.com/Atmosphere/atmosphere-javascript/issues/141
  171. atmosphere.addInitParameter(
  172. ApplicationConfig.DISABLE_ATMOSPHEREINTERCEPTORS,
  173. HeartbeatInterceptor.class.getName());
  174. final String bufferSize = String
  175. .valueOf(PushConstants.WEBSOCKET_BUFFER_SIZE);
  176. atmosphere.addInitParameter(ApplicationConfig.WEBSOCKET_BUFFER_SIZE,
  177. bufferSize);
  178. atmosphere.addInitParameter(ApplicationConfig.WEBSOCKET_MAXTEXTSIZE,
  179. bufferSize);
  180. atmosphere.addInitParameter(ApplicationConfig.WEBSOCKET_MAXBINARYSIZE,
  181. bufferSize);
  182. atmosphere.addInitParameter(
  183. ApplicationConfig.PROPERTY_ALLOW_SESSION_TIMEOUT_REMOVAL,
  184. "false");
  185. // This prevents Atmosphere from recreating a broadcaster after it has
  186. // already been destroyed when the servlet is being undeployed
  187. // (see #20026)
  188. atmosphere.addInitParameter(ApplicationConfig.RECOVER_DEAD_BROADCASTER,
  189. "false");
  190. // Disable Atmosphere's message about commercial support
  191. atmosphere.addInitParameter("org.atmosphere.cpr.showSupportMessage",
  192. "false");
  193. try {
  194. atmosphere.init(vaadinServletConfig);
  195. // Ensure the client-side knows how to split the message stream
  196. // into individual messages when using certain transports
  197. AtmosphereInterceptor trackMessageSize = new TrackMessageSizeInterceptor();
  198. trackMessageSize.configure(atmosphere.getAtmosphereConfig());
  199. atmosphere.interceptor(trackMessageSize);
  200. } catch (ServletException e) {
  201. throw new RuntimeException("Atmosphere init failed", e);
  202. }
  203. return atmosphere;
  204. }
  205. @Override
  206. public boolean handleRequest(VaadinSession session, VaadinRequest request,
  207. VaadinResponse response) throws IOException {
  208. if (!ServletPortletHelper.isPushRequest(request)) {
  209. return false;
  210. }
  211. if (request instanceof VaadinServletRequest) {
  212. if (atmosphere == null) {
  213. response.sendError(500,
  214. "Atmosphere initialization failed. No push available.");
  215. return true;
  216. }
  217. try {
  218. atmosphere.doCometSupport(
  219. AtmosphereRequestImpl
  220. .wrap((VaadinServletRequest) request),
  221. AtmosphereResponseImpl
  222. .wrap((VaadinServletResponse) response));
  223. } catch (ServletException e) {
  224. // TODO PUSH decide how to handle
  225. throw new RuntimeException(e);
  226. }
  227. } else {
  228. throw new IllegalArgumentException(
  229. "Portlets not currently supported");
  230. }
  231. return true;
  232. }
  233. public void destroy() {
  234. atmosphere.destroy();
  235. }
  236. /*
  237. * (non-Javadoc)
  238. *
  239. * @see
  240. * com.vaadin.server.SessionExpiredHandler#handleSessionExpired(com.vaadin
  241. * .server.VaadinRequest, com.vaadin.server.VaadinResponse)
  242. */
  243. @Override
  244. public boolean handleSessionExpired(VaadinRequest request,
  245. VaadinResponse response) throws IOException {
  246. // Websockets request must be handled by accepting the websocket
  247. // connection and then sending session expired so we let
  248. // PushRequestHandler handle it
  249. return handleRequest(null, request, response);
  250. }
  251. }