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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal;
  5. import java.io.Serializable;
  6. import com.vaadin.Application;
  7. import com.vaadin.service.FileTypeResolver;
  8. /**
  9. * <code>ClassResource</code> is a named resource accessed with the class
  10. * loader.
  11. *
  12. * This can be used to access resources such as icons, files, etc.
  13. *
  14. * @see java.lang.Class#getResource(java.lang.String)
  15. *
  16. * @author Vaadin Ltd.
  17. * @since 3.0
  18. */
  19. @SuppressWarnings("serial")
  20. public class ClassResource implements ApplicationResource, Serializable {
  21. /**
  22. * Default buffer size for this stream resource.
  23. */
  24. private int bufferSize = 0;
  25. /**
  26. * Default cache time for this stream resource.
  27. */
  28. private long cacheTime = DEFAULT_CACHETIME;
  29. /**
  30. * Associated class used for indetifying the source of the resource.
  31. */
  32. private final Class<?> associatedClass;
  33. /**
  34. * Name of the resource is relative to the associated class.
  35. */
  36. private final String resourceName;
  37. /**
  38. * Application used for serving the class.
  39. */
  40. private final Application application;
  41. /**
  42. * Creates a new application resource instance. The resource id is relative
  43. * to the location of the application class.
  44. *
  45. * @param resourceName
  46. * the Unique identifier of the resource within the application.
  47. * @param application
  48. * the application this resource will be added to.
  49. */
  50. public ClassResource(String resourceName, Application application) {
  51. this(application.getClass(), resourceName, application);
  52. }
  53. /**
  54. * Creates a new application resource instance.
  55. *
  56. * @param associatedClass
  57. * the class of the which the resource is associated.
  58. * @param resourceName
  59. * the Unique identifier of the resource within the application.
  60. * @param application
  61. * the application this resource will be added to.
  62. */
  63. public ClassResource(Class<?> associatedClass, String resourceName,
  64. Application application) {
  65. this.associatedClass = associatedClass;
  66. this.resourceName = resourceName;
  67. this.application = application;
  68. if (resourceName == null || associatedClass == null) {
  69. throw new NullPointerException();
  70. }
  71. application.addResource(this);
  72. }
  73. /**
  74. * Gets the MIME type of this resource.
  75. *
  76. * @see com.vaadin.terminal.Resource#getMIMEType()
  77. */
  78. @Override
  79. public String getMIMEType() {
  80. return FileTypeResolver.getMIMEType(resourceName);
  81. }
  82. /**
  83. * Gets the application of this resource.
  84. *
  85. * @see com.vaadin.terminal.ApplicationResource#getApplication()
  86. */
  87. @Override
  88. public Application getApplication() {
  89. return application;
  90. }
  91. /**
  92. * Gets the virtual filename for this resource.
  93. *
  94. * @return the file name associated to this resource.
  95. * @see com.vaadin.terminal.ApplicationResource#getFilename()
  96. */
  97. @Override
  98. public String getFilename() {
  99. int index = 0;
  100. int next = 0;
  101. while ((next = resourceName.indexOf('/', index)) > 0
  102. && next + 1 < resourceName.length()) {
  103. index = next + 1;
  104. }
  105. return resourceName.substring(index);
  106. }
  107. /**
  108. * Gets resource as stream.
  109. *
  110. * @see com.vaadin.terminal.ApplicationResource#getStream()
  111. */
  112. @Override
  113. public DownloadStream getStream() {
  114. final DownloadStream ds = new DownloadStream(
  115. associatedClass.getResourceAsStream(resourceName),
  116. getMIMEType(), getFilename());
  117. ds.setBufferSize(getBufferSize());
  118. ds.setCacheTime(cacheTime);
  119. return ds;
  120. }
  121. /* documented in superclass */
  122. @Override
  123. public int getBufferSize() {
  124. return bufferSize;
  125. }
  126. /**
  127. * Sets the size of the download buffer used for this resource.
  128. *
  129. * @param bufferSize
  130. * the size of the buffer in bytes.
  131. */
  132. public void setBufferSize(int bufferSize) {
  133. this.bufferSize = bufferSize;
  134. }
  135. /* documented in superclass */
  136. @Override
  137. public long getCacheTime() {
  138. return cacheTime;
  139. }
  140. /**
  141. * Sets the length of cache expiration time.
  142. *
  143. * <p>
  144. * This gives the adapter the possibility cache streams sent to the client.
  145. * The caching may be made in adapter or at the client if the client
  146. * supports caching. Zero or negavive value disbales the caching of this
  147. * stream.
  148. * </p>
  149. *
  150. * @param cacheTime
  151. * the cache time in milliseconds.
  152. *
  153. */
  154. public void setCacheTime(long cacheTime) {
  155. this.cacheTime = cacheTime;
  156. }
  157. }