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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * Copyright 2000-2018 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. * Sets the URL to use for push requests.
  130. * <p>
  131. * This is only used when overriding the URL to use. Setting this to null
  132. * (the default) will use the default URL.
  133. *
  134. * @since 7.6
  135. * @param pushUrl
  136. * The push URL to use
  137. */
  138. public void setPushUrl(String pushUrl);
  139. /**
  140. * Returns the URL to use for push requests.
  141. * <p>
  142. * This is only used when overriding the URL to use. Returns null (the
  143. * default) when the default URL is used.
  144. *
  145. * @since 7.6
  146. * @return the URL to use for push requests, or null to use to default
  147. */
  148. public String getPushUrl();
  149. }
  150. class PushConfigurationImpl implements PushConfiguration {
  151. private final UI ui;
  152. public PushConfigurationImpl(UI ui) {
  153. this.ui = ui;
  154. }
  155. /*
  156. * (non-Javadoc)
  157. *
  158. * @see com.vaadin.ui.PushConfiguration#getPushMode()
  159. */
  160. @Override
  161. public PushMode getPushMode() {
  162. return getState(false).mode;
  163. }
  164. /*
  165. * (non-Javadoc)
  166. *
  167. * @see com.vaadin.ui.PushConfiguration#setPushMode(com.vaadin.shared.
  168. * communication .PushMode)
  169. */
  170. @Override
  171. public void setPushMode(PushMode pushMode) {
  172. if (pushMode == null) {
  173. throw new IllegalArgumentException("Push mode cannot be null");
  174. }
  175. VaadinSession session = ui.getSession();
  176. if (session == null) {
  177. throw new UIDetachedException(
  178. "Cannot set the push mode for a detached UI");
  179. }
  180. assert session.hasLock();
  181. if (pushMode.isEnabled()
  182. && !session.getService().ensurePushAvailable()) {
  183. throw new IllegalStateException(
  184. "Push is not available. See previous log messages for more information.");
  185. }
  186. PushMode oldMode = getState().mode;
  187. if (oldMode != pushMode) {
  188. getState().mode = pushMode;
  189. if (!oldMode.isEnabled() && pushMode.isEnabled()) {
  190. // The push connection is initially in a disconnected state;
  191. // the client will establish the connection
  192. ui.setPushConnection(new AtmospherePushConnection(ui));
  193. }
  194. // Nothing to do here if disabling push;
  195. // the client will close the connection
  196. }
  197. }
  198. @Override
  199. public void setPushUrl(String pushUrl) {
  200. getState().pushUrl = pushUrl;
  201. }
  202. @Override
  203. public String getPushUrl() {
  204. return getState(false).pushUrl;
  205. }
  206. /*
  207. * (non-Javadoc)
  208. *
  209. * @see com.vaadin.ui.PushConfiguration#getTransport()
  210. */
  211. @Override
  212. public Transport getTransport() {
  213. try {
  214. Transport tr = Transport.getByIdentifier(
  215. getParameter(PushConfigurationState.TRANSPORT_PARAM));
  216. if (tr == Transport.WEBSOCKET
  217. && getState(false).alwaysUseXhrForServerRequests) {
  218. return Transport.WEBSOCKET_XHR;
  219. } else {
  220. return tr;
  221. }
  222. } catch (IllegalArgumentException e) {
  223. return null;
  224. }
  225. }
  226. /*
  227. * (non-Javadoc)
  228. *
  229. * @see
  230. * com.vaadin.ui.PushConfiguration#setTransport(com.vaadin.shared.ui.ui.
  231. * Transport)
  232. */
  233. @Override
  234. public void setTransport(Transport transport) {
  235. if (transport == Transport.WEBSOCKET_XHR) {
  236. getState().alwaysUseXhrForServerRequests = true;
  237. // Atmosphere knows only about "websocket"
  238. setParameter(PushConfigurationState.TRANSPORT_PARAM,
  239. Transport.WEBSOCKET.getIdentifier());
  240. } else {
  241. getState().alwaysUseXhrForServerRequests = false;
  242. setParameter(PushConfigurationState.TRANSPORT_PARAM,
  243. transport.getIdentifier());
  244. }
  245. }
  246. /*
  247. * (non-Javadoc)
  248. *
  249. * @see com.vaadin.ui.PushConfiguration#getFallbackTransport()
  250. */
  251. @Override
  252. public Transport getFallbackTransport() {
  253. try {
  254. return Transport.valueOf(getParameter(
  255. PushConfigurationState.FALLBACK_TRANSPORT_PARAM));
  256. } catch (IllegalArgumentException e) {
  257. return null;
  258. }
  259. }
  260. /*
  261. * (non-Javadoc)
  262. *
  263. * @see
  264. * com.vaadin.ui.PushConfiguration#setFallbackTransport(com.vaadin.shared
  265. * .ui.ui.Transport)
  266. */
  267. @Override
  268. public void setFallbackTransport(Transport fallbackTransport) {
  269. if (fallbackTransport == Transport.WEBSOCKET_XHR) {
  270. throw new IllegalArgumentException(
  271. "WEBSOCKET_XHR can only be used as primary transport");
  272. }
  273. setParameter(PushConfigurationState.FALLBACK_TRANSPORT_PARAM,
  274. fallbackTransport.getIdentifier());
  275. }
  276. /*
  277. * (non-Javadoc)
  278. *
  279. * @see com.vaadin.ui.PushConfiguration#getParameter(java.lang.String)
  280. */
  281. @Override
  282. public String getParameter(String parameter) {
  283. return getState(false).parameters.get(parameter);
  284. }
  285. /*
  286. * (non-Javadoc)
  287. *
  288. * @see com.vaadin.ui.PushConfiguration#setParameter(java.lang.String,
  289. * java.lang.String)
  290. */
  291. @Override
  292. public void setParameter(String parameter, String value) {
  293. getState().parameters.put(parameter, value);
  294. }
  295. private PushConfigurationState getState() {
  296. return ui.getState().pushConfiguration;
  297. }
  298. private PushConfigurationState getState(boolean markAsDirty) {
  299. return ui.getState(markAsDirty).pushConfiguration;
  300. }
  301. @Override
  302. public Collection<String> getParameterNames() {
  303. return Collections
  304. .unmodifiableCollection(getState(false).parameters.keySet());
  305. }
  306. }