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.

PushConfiguration.java 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * Copyright 2000-2014 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.ui;
  17. import java.io.Serializable;
  18. import java.util.Collection;
  19. import java.util.Collections;
  20. import com.vaadin.server.VaadinSession;
  21. import com.vaadin.server.communication.AtmospherePushConnection;
  22. import com.vaadin.shared.communication.PushMode;
  23. import com.vaadin.shared.ui.ui.Transport;
  24. import com.vaadin.shared.ui.ui.UIState.PushConfigurationState;
  25. /**
  26. * Provides method for configuring the push channel.
  27. *
  28. * @since 7.1
  29. * @author Vaadin Ltd
  30. */
  31. public interface PushConfiguration extends Serializable {
  32. /**
  33. * Returns the mode of bidirectional ("push") communication that is used.
  34. *
  35. * @return The push mode.
  36. */
  37. public PushMode getPushMode();
  38. /**
  39. * Sets the mode of bidirectional ("push") communication that should be
  40. * used.
  41. * <p>
  42. * Add-on developers should note that this method is only meant for the
  43. * application developer. An add-on should not set the push mode directly,
  44. * rather instruct the user to set it.
  45. * </p>
  46. *
  47. * @param pushMode
  48. * The push mode to use.
  49. *
  50. * @throws IllegalArgumentException
  51. * if the argument is null.
  52. * @throws IllegalStateException
  53. * if push support is not available.
  54. */
  55. public void setPushMode(PushMode pushMode);
  56. /**
  57. * Returns the primary transport type for push.
  58. * <p>
  59. * Note that if you set the transport type using
  60. * {@link #setParameter(String, String)} to an unsupported type this method
  61. * will return null. Supported types are defined by {@link Transport}.
  62. *
  63. * @return The primary transport type
  64. */
  65. public Transport getTransport();
  66. /**
  67. * Sets the primary transport type for push.
  68. * <p>
  69. * Note that the new transport type will not be used until the push channel
  70. * is disconnected and reconnected if already active.
  71. *
  72. * @param transport
  73. * The primary transport type
  74. */
  75. public void setTransport(Transport transport);
  76. /**
  77. * Returns the fallback transport type for push.
  78. * <p>
  79. * Note that if you set the transport type using
  80. * {@link #setParameter(String, String)} to an unsupported type this method
  81. * will return null. Supported types are defined by {@link Transport}.
  82. *
  83. * @return The fallback transport type
  84. */
  85. public Transport getFallbackTransport();
  86. /**
  87. * Sets the fallback transport type for push.
  88. * <p>
  89. * Note that the new transport type will not be used until the push channel
  90. * is disconnected and reconnected if already active.
  91. *
  92. * @param fallbackTransport
  93. * The fallback transport type
  94. */
  95. public void setFallbackTransport(Transport fallbackTransport);
  96. /**
  97. * Returns the given parameter, if set.
  98. * <p>
  99. * This method provides low level access to push parameters and is typically
  100. * not needed for normal application development.
  101. *
  102. * @since 7.1
  103. * @param parameter
  104. * The parameter name
  105. * @return The parameter value or null if not set
  106. */
  107. public String getParameter(String parameter);
  108. /**
  109. * Returns the parameters which have been defined.
  110. *
  111. * @since 7.1
  112. * @return A collection of parameter names
  113. */
  114. public Collection<String> getParameterNames();
  115. /**
  116. * Sets the given parameter.
  117. * <p>
  118. * This method provides low level access to push parameters and is typically
  119. * not needed for normal application development.
  120. *
  121. * @since 7.1
  122. * @param parameter
  123. * The parameter name
  124. * @param value
  125. * The value
  126. */
  127. public void setParameter(String parameter, String value);
  128. }
  129. class PushConfigurationImpl implements PushConfiguration {
  130. private UI ui;
  131. public PushConfigurationImpl(UI ui) {
  132. this.ui = ui;
  133. }
  134. /*
  135. * (non-Javadoc)
  136. *
  137. * @see com.vaadin.ui.PushConfiguration#getPushMode()
  138. */
  139. @Override
  140. public PushMode getPushMode() {
  141. return getState(false).mode;
  142. }
  143. /*
  144. * (non-Javadoc)
  145. *
  146. * @see
  147. * com.vaadin.ui.PushConfiguration#setPushMode(com.vaadin.shared.communication
  148. * .PushMode)
  149. */
  150. @Override
  151. public void setPushMode(PushMode pushMode) {
  152. if (pushMode == null) {
  153. throw new IllegalArgumentException("Push mode cannot be null");
  154. }
  155. VaadinSession session = ui.getSession();
  156. if (session == null) {
  157. throw new UIDetachedException(
  158. "Cannot set the push mode for a detached UI");
  159. }
  160. assert session.hasLock();
  161. if (pushMode.isEnabled() && !session.getService().ensurePushAvailable()) {
  162. throw new IllegalStateException(
  163. "Push is not available. See previous log messages for more information.");
  164. }
  165. PushMode oldMode = getState().mode;
  166. if (oldMode != pushMode) {
  167. getState().mode = pushMode;
  168. if (!oldMode.isEnabled() && pushMode.isEnabled()) {
  169. // The push connection is initially in a disconnected state;
  170. // the client will establish the connection
  171. ui.setPushConnection(new AtmospherePushConnection(ui));
  172. }
  173. // Nothing to do here if disabling push;
  174. // the client will close the connection
  175. }
  176. }
  177. /*
  178. * (non-Javadoc)
  179. *
  180. * @see com.vaadin.ui.PushConfiguration#getTransport()
  181. */
  182. @Override
  183. public Transport getTransport() {
  184. try {
  185. Transport tr = Transport
  186. .getByIdentifier(getParameter(PushConfigurationState.TRANSPORT_PARAM));
  187. if (tr == Transport.WEBSOCKET
  188. && getState(false).alwaysUseXhrForServerRequests) {
  189. return Transport.WEBSOCKET_XHR;
  190. } else {
  191. return tr;
  192. }
  193. } catch (IllegalArgumentException e) {
  194. return null;
  195. }
  196. }
  197. /*
  198. * (non-Javadoc)
  199. *
  200. * @see
  201. * com.vaadin.ui.PushConfiguration#setTransport(com.vaadin.shared.ui.ui.
  202. * Transport)
  203. */
  204. @Override
  205. public void setTransport(Transport transport) {
  206. if (transport == Transport.WEBSOCKET_XHR) {
  207. getState().alwaysUseXhrForServerRequests = true;
  208. // Atmosphere knows only about "websocket"
  209. setParameter(PushConfigurationState.TRANSPORT_PARAM,
  210. Transport.WEBSOCKET.getIdentifier());
  211. } else {
  212. getState().alwaysUseXhrForServerRequests = false;
  213. setParameter(PushConfigurationState.TRANSPORT_PARAM,
  214. transport.getIdentifier());
  215. }
  216. }
  217. /*
  218. * (non-Javadoc)
  219. *
  220. * @see com.vaadin.ui.PushConfiguration#getFallbackTransport()
  221. */
  222. @Override
  223. public Transport getFallbackTransport() {
  224. try {
  225. return Transport
  226. .valueOf(getParameter(PushConfigurationState.FALLBACK_TRANSPORT_PARAM));
  227. } catch (IllegalArgumentException e) {
  228. return null;
  229. }
  230. }
  231. /*
  232. * (non-Javadoc)
  233. *
  234. * @see
  235. * com.vaadin.ui.PushConfiguration#setFallbackTransport(com.vaadin.shared
  236. * .ui.ui.Transport)
  237. */
  238. @Override
  239. public void setFallbackTransport(Transport fallbackTransport) {
  240. if (fallbackTransport == Transport.WEBSOCKET_XHR) {
  241. throw new IllegalArgumentException(
  242. "WEBSOCKET_XHR can only be used as primary transport");
  243. }
  244. setParameter(PushConfigurationState.FALLBACK_TRANSPORT_PARAM,
  245. fallbackTransport.getIdentifier());
  246. }
  247. /*
  248. * (non-Javadoc)
  249. *
  250. * @see com.vaadin.ui.PushConfiguration#getParameter(java.lang.String)
  251. */
  252. @Override
  253. public String getParameter(String parameter) {
  254. return getState(false).parameters.get(parameter);
  255. }
  256. /*
  257. * (non-Javadoc)
  258. *
  259. * @see com.vaadin.ui.PushConfiguration#setParameter(java.lang.String,
  260. * java.lang.String)
  261. */
  262. @Override
  263. public void setParameter(String parameter, String value) {
  264. getState().parameters.put(parameter, value);
  265. }
  266. private PushConfigurationState getState() {
  267. return ui.getState().pushConfiguration;
  268. }
  269. private PushConfigurationState getState(boolean markAsDirty) {
  270. return ui.getState(markAsDirty).pushConfiguration;
  271. }
  272. @Override
  273. public Collection<String> getParameterNames() {
  274. return Collections.unmodifiableCollection(getState(false).parameters
  275. .keySet());
  276. }
  277. }