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

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