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 14KB

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