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.

ClientConnector.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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;
  17. import java.io.IOException;
  18. import java.lang.reflect.Method;
  19. import java.util.Collection;
  20. import java.util.List;
  21. import com.vaadin.event.ConnectorEvent;
  22. import com.vaadin.event.ConnectorEventListener;
  23. import com.vaadin.shared.Connector;
  24. import com.vaadin.shared.Registration;
  25. import com.vaadin.shared.communication.SharedState;
  26. import com.vaadin.ui.UI;
  27. import com.vaadin.util.ReflectTools;
  28. import elemental.json.JsonObject;
  29. /**
  30. * Interface implemented by all connectors that are capable of communicating
  31. * with the client side
  32. *
  33. * @author Vaadin Ltd
  34. * @since 7.0.0
  35. *
  36. */
  37. public interface ClientConnector extends Connector {
  38. /**
  39. * Event fired after a connector is attached to the application.
  40. */
  41. public static class AttachEvent extends ConnectorEvent {
  42. public static final String ATTACH_EVENT_IDENTIFIER = "clientConnectorAttach";
  43. public AttachEvent(ClientConnector source) {
  44. super(source);
  45. }
  46. }
  47. /**
  48. * Interface for listening {@link AttachEvent connector attach events}.
  49. *
  50. */
  51. @FunctionalInterface
  52. public static interface AttachListener extends ConnectorEventListener {
  53. public static final Method attachMethod = ReflectTools
  54. .findMethod(AttachListener.class, "attach", AttachEvent.class);
  55. /**
  56. * Called when a AttachListener is notified of a AttachEvent.
  57. *
  58. * @param event
  59. * The attach event that was fired.
  60. */
  61. public void attach(AttachEvent event);
  62. }
  63. /**
  64. * Event fired before a connector is detached from the application.
  65. */
  66. public static class DetachEvent extends ConnectorEvent {
  67. public static final String DETACH_EVENT_IDENTIFIER = "clientConnectorDetach";
  68. public DetachEvent(ClientConnector source) {
  69. super(source);
  70. }
  71. }
  72. /**
  73. * Interface for listening {@link DetachEvent connector detach events}.
  74. *
  75. */
  76. @FunctionalInterface
  77. public static interface DetachListener extends ConnectorEventListener {
  78. public static final Method detachMethod = ReflectTools
  79. .findMethod(DetachListener.class, "detach", DetachEvent.class);
  80. /**
  81. * Called when a DetachListener is notified of a DetachEvent.
  82. *
  83. * @param event
  84. * The detach event that was fired.
  85. */
  86. public void detach(DetachEvent event);
  87. }
  88. public Registration addAttachListener(AttachListener listener);
  89. @Deprecated
  90. public void removeAttachListener(AttachListener listener);
  91. public Registration addDetachListener(DetachListener listener);
  92. @Deprecated
  93. public void removeDetachListener(DetachListener listener);
  94. /**
  95. * An error event for connector related errors. Use {@link #getConnector()}
  96. * to find the connector where the error occurred or {@link #getComponent()}
  97. * to find the nearest parent component.
  98. */
  99. public static class ConnectorErrorEvent
  100. extends com.vaadin.server.ErrorEvent {
  101. private final Connector connector;
  102. public ConnectorErrorEvent(Connector connector, Throwable t) {
  103. super(t);
  104. this.connector = connector;
  105. }
  106. /**
  107. * Gets the connector for which this error occurred.
  108. *
  109. * @return The connector for which the error occurred
  110. */
  111. public Connector getConnector() {
  112. return connector;
  113. }
  114. }
  115. /**
  116. * Returns the list of pending server to client RPC calls and clears the
  117. * list.
  118. *
  119. * @return an unmodifiable ordered list of pending server to client method
  120. * calls (not null)
  121. */
  122. public List<ClientMethodInvocation> retrievePendingRpcCalls();
  123. /**
  124. * Checks if the communicator is enabled. An enabled communicator is allowed
  125. * to receive messages from its counter-part.
  126. *
  127. * @return true if the connector can receive messages, false otherwise
  128. */
  129. public boolean isConnectorEnabled();
  130. /**
  131. * Returns the type of the shared state for this connector
  132. *
  133. * @return The type of the state. Must never return null.
  134. */
  135. public Class<? extends SharedState> getStateType();
  136. @Override
  137. public ClientConnector getParent();
  138. /**
  139. * @deprecated As of 7.0, use {@link #markAsDirty()} instead
  140. */
  141. @Deprecated
  142. public void requestRepaint();
  143. /**
  144. * Marks that this connector's state might have changed. When the framework
  145. * is about to send new data to the client-side, it will run
  146. * {@link #beforeClientResponse(boolean)} followed by {@link #encodeState()}
  147. * for all connectors that are marked as dirty and send any updated state
  148. * info to the client.
  149. *
  150. * @since 7.0.0
  151. */
  152. public void markAsDirty();
  153. /**
  154. * @deprecated As of 7.0, use {@link #markAsDirtyRecursive()} instead
  155. */
  156. @Deprecated
  157. public void requestRepaintAll();
  158. /**
  159. * Causes this connector and all connectors below it to be marked as dirty.
  160. * <p>
  161. * This should only be used in special cases, e.g when the state of a
  162. * descendant depends on the state of an ancestor.
  163. *
  164. * @see #markAsDirty()
  165. *
  166. * @since 7.0.0
  167. */
  168. public void markAsDirtyRecursive();
  169. /**
  170. * Checks if the connector is attached to a VaadinSession.
  171. *
  172. * @since 7.1
  173. * @return true if the connector is attached to a session, false otherwise
  174. */
  175. public boolean isAttached();
  176. /**
  177. * Notifies the connector that it is connected to a VaadinSession (and
  178. * therefore also to a UI).
  179. * <p>
  180. * The caller of this method is {@link #setParent(ClientConnector)} if the
  181. * parent is itself already attached to the session. If not, the parent will
  182. * call the {@link #attach()} for all its children when it is attached to
  183. * the session. This method is always called before the connector's data is
  184. * sent to the client-side for the first time.
  185. * </p>
  186. *
  187. * <p>
  188. * The attachment logic is implemented in {@link AbstractClientConnector}.
  189. * </p>
  190. */
  191. public void attach();
  192. /**
  193. * Notifies the connector that it is detached from its VaadinSession.
  194. *
  195. * <p>
  196. * The caller of this method is {@link #setParent(ClientConnector)} if the
  197. * parent is in the session. When the parent is detached from the session it
  198. * is its responsibility to call {@link #detach()} for each of its children.
  199. *
  200. * </p>
  201. */
  202. public void detach();
  203. /**
  204. * Get a read-only collection of all extensions attached to this connector.
  205. *
  206. * @return a collection of extensions
  207. */
  208. public Collection<Extension> getExtensions();
  209. /**
  210. * Remove an extension from this connector.
  211. *
  212. * @param extension
  213. * the extension to remove.
  214. */
  215. public void removeExtension(Extension extension);
  216. /**
  217. * Returns the UI this connector is attached to
  218. *
  219. * @return The UI this connector is attached to or null if it is not
  220. * attached to any UI
  221. */
  222. public UI getUI();
  223. /**
  224. * Called before the shared state and RPC invocations are sent to the
  225. * client. Gives the connector an opportunity to set computed/dynamic state
  226. * values or to invoke last minute RPC methods depending on other component
  227. * features.
  228. *
  229. * @param initial
  230. * <code>true</code> if the client-side connector will be created
  231. * and initialized after this method has been invoked.
  232. * <code>false</code> if there is already an initialized
  233. * client-side connector.
  234. *
  235. * @since 7.0
  236. */
  237. public void beforeClientResponse(boolean initial);
  238. /**
  239. * Called by the framework to encode the state to a JSONObject. This is
  240. * typically done by calling the static method
  241. * {@link LegacyCommunicationManager#encodeState(ClientConnector, SharedState)}
  242. * .
  243. *
  244. * @return a JSON object with the encoded connector state
  245. */
  246. public JsonObject encodeState();
  247. /**
  248. * Handle a request directed to this connector. This can be used by
  249. * connectors to dynamically generate a response and it is also used
  250. * internally when serving {@link ConnectorResource}s.
  251. * <p>
  252. * Requests to <code>/APP/connector/[ui id]/[connector id]/</code> are
  253. * routed to this method with the remaining part of the requested path
  254. * available in the path parameter.
  255. * <p>
  256. * NOTE that the session is not locked when this method is called. It is the
  257. * responsibility of the connector to ensure that the session is locked
  258. * while handling state or other session related data. For best performance
  259. * the session should be unlocked before writing a large response to the
  260. * client.
  261. * </p>
  262. *
  263. * @param request
  264. * the request that should be handled
  265. * @param response
  266. * the response object to which the response should be written
  267. * @param path
  268. * the requested relative path
  269. * @return <code>true</code> if the request has been handled,
  270. * <code>false</code> if no response has been written.
  271. * @throws IOException
  272. * if there is a problem generating a response.
  273. */
  274. public boolean handleConnectorRequest(VaadinRequest request,
  275. VaadinResponse response, String path) throws IOException;
  276. /**
  277. * Returns the RPC manager instance to use when receiving calls for an RPC
  278. * interface.
  279. *
  280. * @param rpcInterfaceName
  281. * name of the interface for which the call was made
  282. * @return ServerRpcManager or null if none found for the interface
  283. */
  284. public ServerRpcManager<?> getRpcManager(String rpcInterfaceName);
  285. /**
  286. * Gets the error handler for the connector.
  287. *
  288. * The error handler is dispatched whenever there is an error processing the
  289. * data coming from the client to this connector.
  290. *
  291. * @return The error handler or null if not set
  292. */
  293. public ErrorHandler getErrorHandler();
  294. /**
  295. * Sets the error handler for the connector.
  296. *
  297. * The error handler is dispatched whenever there is an error processing the
  298. * data coming from the client for this connector.
  299. *
  300. * @param errorHandler
  301. * The error handler for this connector
  302. */
  303. public void setErrorHandler(ErrorHandler errorHandler);
  304. }