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.

ConnectorHierarchyWriter.java 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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.communication;
  17. import java.io.IOException;
  18. import java.io.Serializable;
  19. import java.io.Writer;
  20. import java.util.Collection;
  21. import com.vaadin.server.AbstractClientConnector;
  22. import com.vaadin.server.ClientConnector;
  23. import com.vaadin.server.LegacyCommunicationManager;
  24. import com.vaadin.server.PaintException;
  25. import com.vaadin.ui.UI;
  26. import elemental.json.Json;
  27. import elemental.json.JsonArray;
  28. import elemental.json.JsonException;
  29. import elemental.json.JsonObject;
  30. import elemental.json.impl.JsonUtil;
  31. /**
  32. * Serializes a connector hierarchy to JSON.
  33. *
  34. * @author Vaadin Ltd
  35. * @since 7.1
  36. */
  37. public class ConnectorHierarchyWriter implements Serializable {
  38. /**
  39. * Writes a JSON object containing the connector hierarchy (parent-child
  40. * mappings) of the dirty connectors in the given UI.
  41. *
  42. * @param ui
  43. * The {@link UI} whose hierarchy to write.
  44. * @param writer
  45. * The {@link Writer} used to write the JSON.
  46. * @throws IOException
  47. * If the serialization fails.
  48. */
  49. public void write(UI ui, Writer writer) throws IOException {
  50. Collection<ClientConnector> dirtyVisibleConnectors = ui
  51. .getConnectorTracker().getDirtyVisibleConnectors();
  52. JsonObject hierarchyInfo = Json.createObject();
  53. for (ClientConnector connector : dirtyVisibleConnectors) {
  54. String connectorId = connector.getConnectorId();
  55. JsonArray children = Json.createArray();
  56. for (ClientConnector child : AbstractClientConnector
  57. .getAllChildrenIterable(connector)) {
  58. if (LegacyCommunicationManager
  59. .isConnectorVisibleToClient(child)) {
  60. children.set(children.length(), child.getConnectorId());
  61. }
  62. }
  63. try {
  64. hierarchyInfo.put(connectorId, children);
  65. } catch (JsonException e) {
  66. throw new PaintException(
  67. "Failed to send hierarchy information about "
  68. + connectorId + " to the client: "
  69. + e.getMessage(), e);
  70. }
  71. }
  72. writer.write(JsonUtil.stringify(hierarchyInfo));
  73. }
  74. }