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.

ResourceReference.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright 2000-2018 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 static java.nio.charset.StandardCharsets.UTF_8;
  18. import java.io.UnsupportedEncodingException;
  19. import java.net.URLEncoder;
  20. import com.vaadin.shared.ApplicationConstants;
  21. import com.vaadin.shared.communication.URLReference;
  22. public class ResourceReference extends URLReference {
  23. private final Resource resource;
  24. private final ClientConnector connector;
  25. private final String key;
  26. public ResourceReference(Resource resource, ClientConnector connector,
  27. String key) {
  28. this.resource = resource;
  29. this.connector = connector;
  30. this.key = key;
  31. }
  32. public Resource getResource() {
  33. return resource;
  34. }
  35. @Override
  36. public String getURL() {
  37. if (resource instanceof ExternalResource) {
  38. return ((ExternalResource) resource).getURL();
  39. } else if (resource instanceof ConnectorResource) {
  40. ConnectorResource connectorResource = (ConnectorResource) resource;
  41. GlobalResourceHandler globalResourceHandler = connector.getUI()
  42. .getSession().getGlobalResourceHandler(false);
  43. if (globalResourceHandler != null) {
  44. String uri = globalResourceHandler.getUri(connector,
  45. connectorResource);
  46. if (uri != null && !uri.isEmpty()) {
  47. return uri;
  48. }
  49. }
  50. // app://APP/connector/[uiid]/[cid]/[key]/[filename]
  51. String prefix = key;
  52. String filename = connectorResource.getFilename();
  53. if (filename != null && !filename.isEmpty()) {
  54. prefix += '/' + filename;
  55. }
  56. String uri = getConnectorResourceBase(prefix, connector);
  57. return uri;
  58. } else if (resource instanceof ThemeResource) {
  59. final String uri = ApplicationConstants.THEME_PROTOCOL_PREFIX
  60. + ((ThemeResource) resource).getResourceId();
  61. return uri;
  62. } else if (resource instanceof FontIcon) {
  63. // fonticon://[font-family]/[codepoint]
  64. final FontIcon icon = (FontIcon) resource;
  65. final String uri = ApplicationConstants.FONTICON_PROTOCOL_PREFIX
  66. + urlEncode(icon.getFontFamily()) + "/"
  67. + Integer.toHexString(icon.getCodepoint());
  68. return uri;
  69. } else {
  70. throw new RuntimeException(getClass().getSimpleName()
  71. + " does not support resources of type: "
  72. + resource.getClass().getName());
  73. }
  74. }
  75. private static String getConnectorResourceBase(String filename,
  76. ClientConnector connector) {
  77. String uri = ApplicationConstants.APP_PROTOCOL_PREFIX
  78. + ApplicationConstants.APP_PATH + '/'
  79. + ConnectorResource.CONNECTOR_PATH + '/'
  80. + connector.getUI().getUIId() + '/' + connector.getConnectorId()
  81. + '/' + encodeFileName(filename);
  82. return uri;
  83. }
  84. public static String encodeFileName(String filename) {
  85. // #7738 At least Tomcat and JBoss refuses requests containing
  86. // encoded slashes or backslashes in URLs. Application resource URLs
  87. // should really be passed in another way than as part of the path
  88. // in the future.
  89. return urlEncode(filename).replace("%2F", "/").replace("%5C", "\\");
  90. }
  91. static String urlEncode(String filename) {
  92. try {
  93. return URLEncoder.encode(filename, UTF_8.name());
  94. } catch (UnsupportedEncodingException e) {
  95. throw new RuntimeException(
  96. "UTF-8 charset not available (\"this should never happen\")",
  97. e);
  98. }
  99. }
  100. public static ResourceReference create(Resource resource,
  101. ClientConnector connector, String key) {
  102. if (resource == null) {
  103. return null;
  104. } else {
  105. return new ResourceReference(resource, connector, key);
  106. }
  107. }
  108. public static Resource getResource(URLReference reference) {
  109. if (reference == null) {
  110. return null;
  111. }
  112. assert reference instanceof ResourceReference;
  113. return ((ResourceReference) reference).getResource();
  114. }
  115. }