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.

ClassResource.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 com.vaadin.ui.LegacyWindow;
  18. import com.vaadin.ui.UI;
  19. import com.vaadin.util.FileTypeResolver;
  20. /**
  21. * <code>ClassResource</code> is a named resource accessed with the class
  22. * loader.
  23. *
  24. * This can be used to access resources such as icons, files, etc.
  25. *
  26. * @see java.lang.Class#getResource(java.lang.String)
  27. *
  28. * @author Vaadin Ltd.
  29. * @since 3.0
  30. */
  31. @SuppressWarnings("serial")
  32. public class ClassResource implements ConnectorResource {
  33. /**
  34. * Default buffer size for this stream resource.
  35. */
  36. private int bufferSize = 0;
  37. /**
  38. * Default cache time for this stream resource.
  39. */
  40. private long cacheTime = DownloadStream.DEFAULT_CACHETIME;
  41. /**
  42. * Associated class used for identifying the source of the resource.
  43. */
  44. private final Class<?> associatedClass;
  45. /**
  46. * Name of the resource is relative to the associated class.
  47. */
  48. private final String resourceName;
  49. /**
  50. * Creates a new application resource instance. The resource id is relative
  51. * to the location of the UI of the component using this resource (or the
  52. * Application if using LegacyWindow).
  53. *
  54. * @param resourceName
  55. * the Unique identifier of the resource within the application.
  56. */
  57. public ClassResource(String resourceName) {
  58. this(null, resourceName);
  59. }
  60. /**
  61. * Creates a new application resource instance.
  62. *
  63. * @param associatedClass
  64. * the class of the which the resource is associated.
  65. * @param resourceName
  66. * the Unique identifier of the resource within the application.
  67. */
  68. public ClassResource(Class<?> associatedClass, String resourceName) {
  69. this.associatedClass = associatedClass;
  70. this.resourceName = resourceName;
  71. if (resourceName == null) {
  72. throw new NullPointerException();
  73. }
  74. }
  75. /**
  76. * Gets the MIME type of this resource.
  77. *
  78. * @see com.vaadin.server.Resource#getMIMEType()
  79. */
  80. @Override
  81. public String getMIMEType() {
  82. return FileTypeResolver.getMIMEType(resourceName);
  83. }
  84. @Override
  85. public String getFilename() {
  86. String[] parts = resourceName.split("/");
  87. return parts[parts.length - 1];
  88. }
  89. @Override
  90. public DownloadStream getStream() {
  91. final DownloadStream ds = new DownloadStream(
  92. getAssociatedClass().getResourceAsStream(resourceName),
  93. getMIMEType(), getFilename());
  94. ds.setBufferSize(getBufferSize());
  95. ds.setCacheTime(getCacheTime());
  96. return ds;
  97. }
  98. protected Class<?> getAssociatedClass() {
  99. if (associatedClass == null) {
  100. UI current = UI.getCurrent();
  101. if (current instanceof LegacyWindow) {
  102. LegacyWindow legacyWindow = (LegacyWindow) current;
  103. return legacyWindow.getApplication().getClass();
  104. } else {
  105. return current.getClass();
  106. }
  107. }
  108. return associatedClass;
  109. }
  110. /**
  111. * Gets the size of the download buffer used for this resource.
  112. *
  113. * <p>
  114. * If the buffer size is 0, the buffer size is decided by the terminal
  115. * adapter. The default value is 0.
  116. * </p>
  117. *
  118. * @return the size of the buffer in bytes.
  119. */
  120. public int getBufferSize() {
  121. return bufferSize;
  122. }
  123. /**
  124. * Sets the size of the download buffer used for this resource.
  125. *
  126. * @param bufferSize
  127. * the size of the buffer in bytes.
  128. *
  129. * @see #getBufferSize()
  130. */
  131. public void setBufferSize(int bufferSize) {
  132. this.bufferSize = bufferSize;
  133. }
  134. /**
  135. * Gets the length of cache expiration time.
  136. *
  137. * <p>
  138. * This gives the adapter the possibility cache streams sent to the client.
  139. * The caching may be made in adapter or at the client if the client
  140. * supports caching. Default is {@link DownloadStream#DEFAULT_CACHETIME}.
  141. * </p>
  142. *
  143. * @return Cache time in milliseconds
  144. */
  145. public long getCacheTime() {
  146. return cacheTime;
  147. }
  148. /**
  149. * Sets the length of cache expiration time.
  150. *
  151. * <p>
  152. * This gives the adapter the possibility cache streams sent to the client.
  153. * The caching may be made in adapter or at the client if the client
  154. * supports caching. Zero or negative value disables the caching of this
  155. * stream.
  156. * </p>
  157. *
  158. * @param cacheTime
  159. * the cache time in milliseconds.
  160. *
  161. */
  162. public void setCacheTime(long cacheTime) {
  163. this.cacheTime = cacheTime;
  164. }
  165. }