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

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