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.

LegacyCommunicationManager.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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.server;
  17. import java.io.Serializable;
  18. import java.net.URI;
  19. import java.net.URISyntaxException;
  20. import java.security.GeneralSecurityException;
  21. import java.util.HashMap;
  22. import java.util.HashSet;
  23. import java.util.Map;
  24. import java.util.Set;
  25. import java.util.logging.Level;
  26. import java.util.logging.Logger;
  27. import com.vaadin.server.ClientConnector.ConnectorErrorEvent;
  28. import com.vaadin.shared.ApplicationConstants;
  29. import com.vaadin.shared.JavaScriptConnectorState;
  30. import com.vaadin.shared.communication.SharedState;
  31. import com.vaadin.ui.Component;
  32. import com.vaadin.ui.ConnectorTracker;
  33. import com.vaadin.ui.HasComponents;
  34. import com.vaadin.ui.SelectiveRenderer;
  35. import com.vaadin.ui.UI;
  36. import elemental.json.JsonObject;
  37. import elemental.json.JsonValue;
  38. /**
  39. * This is a common base class for the server-side implementations of the
  40. * communication system between the client code (compiled with GWT into
  41. * JavaScript) and the server side components. Its client side counterpart is
  42. * {@link com.vaadin.client.ApplicationConnection}.
  43. * <p>
  44. * TODO Document better!
  45. *
  46. * @deprecated As of 7.0. Will likely change or be removed in a future version
  47. */
  48. @Deprecated
  49. @SuppressWarnings("serial")
  50. public class LegacyCommunicationManager implements Serializable {
  51. // TODO Refactor (#11410)
  52. private final HashMap<Integer, ClientCache> uiToClientCache = new HashMap<Integer, ClientCache>();
  53. /**
  54. * The session this communication manager is used for
  55. */
  56. private final VaadinSession session;
  57. // TODO Refactor (#11412)
  58. private String requestThemeName;
  59. // TODO Refactor (#11413)
  60. private Map<String, Class<?>> publishedFileContexts = new HashMap<String, Class<?>>();
  61. /**
  62. * TODO New constructor - document me!
  63. *
  64. * @param session
  65. */
  66. public LegacyCommunicationManager(VaadinSession session) {
  67. this.session = session;
  68. }
  69. protected VaadinSession getSession() {
  70. return session;
  71. }
  72. /**
  73. * @deprecated As of 7.1. See #11411.
  74. */
  75. @Deprecated
  76. public static JsonObject encodeState(ClientConnector connector, SharedState state) {
  77. UI uI = connector.getUI();
  78. ConnectorTracker connectorTracker = uI.getConnectorTracker();
  79. Class<? extends SharedState> stateType = connector.getStateType();
  80. JsonValue diffState = connectorTracker.getDiffState(connector);
  81. boolean supportsDiffState = !JavaScriptConnectorState.class
  82. .isAssignableFrom(stateType);
  83. if (diffState == null && supportsDiffState) {
  84. // Use an empty state object as reference for full
  85. // repaints
  86. try {
  87. SharedState referenceState = stateType.newInstance();
  88. EncodeResult encodeResult = JsonCodec.encode(referenceState,
  89. null, stateType, uI.getConnectorTracker());
  90. diffState = encodeResult.getEncodedValue();
  91. } catch (Exception e) {
  92. getLogger()
  93. .log(Level.WARNING,
  94. "Error creating reference object for state of type {0}",
  95. stateType.getName());
  96. }
  97. }
  98. EncodeResult encodeResult = JsonCodec.encode(state, diffState,
  99. stateType, uI.getConnectorTracker());
  100. if (supportsDiffState) {
  101. connectorTracker.setDiffState(connector,
  102. (JsonObject) encodeResult.getEncodedValue());
  103. }
  104. return (JsonObject) encodeResult.getDiff();
  105. }
  106. /**
  107. * Resolves a dependency URI, registering the URI with this
  108. * {@code LegacyCommunicationManager} if needed and returns a fully
  109. * qualified URI.
  110. *
  111. * @deprecated As of 7.1. See #11413.
  112. */
  113. @Deprecated
  114. public String registerDependency(String resourceUri, Class<?> context) {
  115. try {
  116. URI uri = new URI(resourceUri);
  117. String protocol = uri.getScheme();
  118. if (ApplicationConstants.PUBLISHED_PROTOCOL_NAME.equals(protocol)) {
  119. // Strip initial slash
  120. String resourceName = uri.getPath().substring(1);
  121. return registerPublishedFile(resourceName, context);
  122. }
  123. if (protocol != null || uri.getHost() != null) {
  124. return resourceUri;
  125. }
  126. // Bare path interpreted as published file
  127. return registerPublishedFile(resourceUri, context);
  128. } catch (URISyntaxException e) {
  129. getLogger().log(Level.WARNING,
  130. "Could not parse resource url " + resourceUri, e);
  131. return resourceUri;
  132. }
  133. }
  134. /**
  135. * @deprecated As of 7.1. See #11413.
  136. */
  137. @Deprecated
  138. public Map<String, Class<?>> getDependencies() {
  139. return publishedFileContexts;
  140. }
  141. private String registerPublishedFile(String name, Class<?> context) {
  142. // Add to map of names accepted by servePublishedFile
  143. if (publishedFileContexts.containsKey(name)) {
  144. Class<?> oldContext = publishedFileContexts.get(name);
  145. if (oldContext != context) {
  146. getLogger()
  147. .log(Level.WARNING,
  148. "{0} published by both {1} and {2}. File from {2} will be used.",
  149. new Object[] { name, context, oldContext });
  150. }
  151. } else {
  152. publishedFileContexts.put(name, context);
  153. }
  154. return ApplicationConstants.PUBLISHED_PROTOCOL_PREFIX + "/" + name;
  155. }
  156. /**
  157. * @deprecated As of 7.1. See #11410.
  158. */
  159. @Deprecated
  160. public ClientCache getClientCache(UI uI) {
  161. Integer uiId = Integer.valueOf(uI.getUIId());
  162. ClientCache cache = uiToClientCache.get(uiId);
  163. if (cache == null) {
  164. cache = new ClientCache();
  165. uiToClientCache.put(uiId, cache);
  166. }
  167. return cache;
  168. }
  169. /**
  170. * Checks if the connector is visible in context. For Components,
  171. * {@link #isComponentVisibleToClient(Component)} is used. For other types
  172. * of connectors, the contextual visibility of its first Component ancestor
  173. * is used. If no Component ancestor is found, the connector is not visible.
  174. *
  175. * @deprecated As of 7.1. See #11411.
  176. *
  177. * @param connector
  178. * The connector to check
  179. * @return <code>true</code> if the connector is visible to the client,
  180. * <code>false</code> otherwise
  181. */
  182. @Deprecated
  183. public static boolean isConnectorVisibleToClient(ClientConnector connector) {
  184. if (connector instanceof Component) {
  185. return isComponentVisibleToClient((Component) connector);
  186. } else {
  187. ClientConnector parent = connector.getParent();
  188. if (parent == null) {
  189. return false;
  190. } else {
  191. return isConnectorVisibleToClient(parent);
  192. }
  193. }
  194. }
  195. /**
  196. * Checks if the component should be visible to the client. Returns false if
  197. * the child should not be sent to the client, true otherwise.
  198. *
  199. * @deprecated As of 7.1. See #11411.
  200. *
  201. * @param child
  202. * The child to check
  203. * @return true if the child is visible to the client, false otherwise
  204. */
  205. @Deprecated
  206. public static boolean isComponentVisibleToClient(Component child) {
  207. if (!child.isVisible()) {
  208. return false;
  209. }
  210. HasComponents parent = child.getParent();
  211. if (parent instanceof SelectiveRenderer) {
  212. if (!((SelectiveRenderer) parent).isRendered(child)) {
  213. return false;
  214. }
  215. }
  216. if (parent != null) {
  217. return isComponentVisibleToClient(parent);
  218. } else {
  219. if (child instanceof UI) {
  220. // UI has no parent and visibility was checked above
  221. return true;
  222. } else {
  223. // Component which is not attached to any UI
  224. return false;
  225. }
  226. }
  227. }
  228. /**
  229. * @deprecated As of 7.1. See #11412.
  230. */
  231. @Deprecated
  232. public String getTheme(UI uI) {
  233. String themeName = uI.getTheme();
  234. String requestThemeName = getRequestTheme();
  235. if (requestThemeName != null) {
  236. themeName = requestThemeName;
  237. }
  238. if (themeName == null) {
  239. themeName = VaadinServlet.getDefaultTheme();
  240. }
  241. return themeName;
  242. }
  243. private String getRequestTheme() {
  244. return requestThemeName;
  245. }
  246. /**
  247. * @deprecated As of 7.1. In 7.2 and later, use
  248. * {@link ConnectorTracker#getConnector(String)
  249. * uI.getConnectorTracker().getConnector(connectorId)} instead.
  250. * See ticket #11411.
  251. */
  252. @Deprecated
  253. public ClientConnector getConnector(UI uI, String connectorId) {
  254. return uI.getConnectorTracker().getConnector(connectorId);
  255. }
  256. /**
  257. * @deprecated As of 7.1. Will be removed in the future.
  258. */
  259. @Deprecated
  260. public static class InvalidUIDLSecurityKeyException extends
  261. GeneralSecurityException {
  262. public InvalidUIDLSecurityKeyException(String message) {
  263. super(message);
  264. }
  265. }
  266. private final HashMap<Class<? extends ClientConnector>, Integer> typeToKey = new HashMap<Class<? extends ClientConnector>, Integer>();
  267. private int nextTypeKey = 0;
  268. /**
  269. * @deprecated As of 7.1. Will be removed in the future.
  270. */
  271. @Deprecated
  272. public String getTagForType(Class<? extends ClientConnector> class1) {
  273. Integer id = typeToKey.get(class1);
  274. if (id == null) {
  275. id = nextTypeKey++;
  276. typeToKey.put(class1, id);
  277. if (getLogger().isLoggable(Level.FINE)) {
  278. getLogger().log(Level.FINE, "Mapping {0} to {1}",
  279. new Object[] { class1.getName(), id });
  280. }
  281. }
  282. return id.toString();
  283. }
  284. /**
  285. * Helper class for terminal to keep track of data that client is expected
  286. * to know.
  287. *
  288. * TODO make customlayout templates (from theme) to be cached here.
  289. *
  290. * @deprecated As of 7.1. See #11410.
  291. */
  292. @Deprecated
  293. public class ClientCache implements Serializable {
  294. private final Set<Object> res = new HashSet<Object>();
  295. /**
  296. *
  297. * @param paintable
  298. * @return true if the given class was added to cache
  299. */
  300. public boolean cache(Object object) {
  301. return res.add(object);
  302. }
  303. public void clear() {
  304. res.clear();
  305. }
  306. }
  307. /**
  308. * @deprecated As of 7.1. See #11411.
  309. */
  310. @Deprecated
  311. public String getStreamVariableTargetUrl(ClientConnector owner,
  312. String name, StreamVariable value) {
  313. /*
  314. * We will use the same APP/* URI space as ApplicationResources but
  315. * prefix url with UPLOAD
  316. *
  317. * eg. APP/UPLOAD/[UIID]/[PID]/[NAME]/[SECKEY]
  318. *
  319. * SECKEY is created on each paint to make URL's unpredictable (to
  320. * prevent CSRF attacks).
  321. *
  322. * NAME and PID from URI forms a key to fetch StreamVariable when
  323. * handling post
  324. */
  325. String paintableId = owner.getConnectorId();
  326. UI ui = owner.getUI();
  327. int uiId = ui.getUIId();
  328. String key = uiId + "/" + paintableId + "/" + name;
  329. ConnectorTracker connectorTracker = ui.getConnectorTracker();
  330. connectorTracker.addStreamVariable(paintableId, name, value);
  331. String seckey = connectorTracker.getSeckey(value);
  332. return ApplicationConstants.APP_PROTOCOL_PREFIX
  333. + ServletPortletHelper.UPLOAD_URL_PREFIX + key + "/" + seckey;
  334. }
  335. /**
  336. * Handles an exception that occurred when processing RPC calls or a file
  337. * upload.
  338. *
  339. * @deprecated As of 7.1. See #11411.
  340. *
  341. * @param ui
  342. * The UI where the exception occured
  343. * @param throwable
  344. * The exception
  345. * @param connector
  346. * The Rpc target
  347. */
  348. @Deprecated
  349. public void handleConnectorRelatedException(ClientConnector connector,
  350. Throwable throwable) {
  351. ErrorEvent errorEvent = new ConnectorErrorEvent(connector, throwable);
  352. ErrorHandler handler = ErrorEvent.findErrorHandler(connector);
  353. handler.error(errorEvent);
  354. }
  355. /**
  356. * Requests that the given UI should be fully re-rendered on the client
  357. * side.
  358. *
  359. * @since 7.1
  360. * @deprecated. As of 7.1. Should be refactored once locales are fixed
  361. * (#11378)
  362. */
  363. @Deprecated
  364. public void repaintAll(UI ui) {
  365. getClientCache(ui).clear();
  366. ui.getConnectorTracker().markAllConnectorsDirty();
  367. ui.getConnectorTracker().markAllClientSidesUninitialized();
  368. }
  369. private static final Logger getLogger() {
  370. return Logger.getLogger(LegacyCommunicationManager.class.getName());
  371. }
  372. }