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.

ResourceWriter.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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.communication;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.io.InputStreamReader;
  20. import java.io.Serializable;
  21. import java.io.Writer;
  22. import java.nio.charset.StandardCharsets;
  23. import java.util.logging.Level;
  24. import java.util.logging.Logger;
  25. import com.vaadin.server.JsonPaintTarget;
  26. import com.vaadin.server.LegacyCommunicationManager;
  27. import com.vaadin.ui.CustomLayout;
  28. import com.vaadin.ui.UI;
  29. /**
  30. * Serializes resources to JSON. Currently only used for {@link CustomLayout}
  31. * templates.
  32. *
  33. * @author Vaadin Ltd
  34. * @since 7.1
  35. */
  36. public class ResourceWriter implements Serializable {
  37. /**
  38. * Writes a JSON object containing registered resources.
  39. *
  40. * @param ui
  41. * The {@link UI} whose resources to write.
  42. * @param writer
  43. * The {@link Writer} to use.
  44. * @param target
  45. * The {@link JsonPaintTarget} containing the resources.
  46. * @throws IOException
  47. */
  48. public void write(UI ui, Writer writer, JsonPaintTarget target)
  49. throws IOException {
  50. // TODO PUSH Refactor so that this is not needed
  51. LegacyCommunicationManager manager = ui.getSession()
  52. .getCommunicationManager();
  53. // Precache custom layouts
  54. // TODO We should only precache the layouts that are not
  55. // cached already (plagiate from usedPaintableTypes)
  56. writer.write("{");
  57. int resourceIndex = 0;
  58. for (Object o : target.getUsedResources()) {
  59. final String resource = (String) o;
  60. InputStream is = null;
  61. try {
  62. is = ui.getSession().getService().getThemeResourceAsStream(ui,
  63. ui.getTheme(), resource);
  64. } catch (final Exception e) {
  65. // FIXME: Handle exception
  66. getLogger().log(Level.FINER,
  67. "Failed to get theme resource stream.", e);
  68. }
  69. if (is != null) {
  70. writer.write((resourceIndex++ > 0 ? ", " : "") + "\"" + resource
  71. + "\" : ");
  72. final StringBuffer layout = new StringBuffer();
  73. try (InputStreamReader r = new InputStreamReader(is,
  74. StandardCharsets.UTF_8)) {
  75. final char[] buffer = new char[20000];
  76. int charsRead = 0;
  77. while ((charsRead = r.read(buffer)) > 0) {
  78. layout.append(buffer, 0, charsRead);
  79. }
  80. } catch (final IOException e) {
  81. // FIXME: Handle exception
  82. getLogger().log(Level.INFO, "Resource transfer failed", e);
  83. }
  84. writer.write("\""
  85. + JsonPaintTarget.escapeJSON(layout.toString()) + "\"");
  86. } else {
  87. // FIXME: Handle exception
  88. getLogger().severe("CustomLayout not found: " + resource);
  89. }
  90. }
  91. writer.write("}");
  92. }
  93. private static final Logger getLogger() {
  94. return Logger.getLogger(ResourceWriter.class.getName());
  95. }
  96. }