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

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