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.

StreamResource.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal;
  5. import java.io.InputStream;
  6. import java.io.Serializable;
  7. import com.vaadin.Application;
  8. import com.vaadin.service.FileTypeResolver;
  9. /**
  10. * <code>StreamResource</code> is a resource provided to the client directly by
  11. * the application. The strean resource is fetched from URI that is most often
  12. * in the context of the application or window. The resource is automatically
  13. * registered to window in creation.
  14. *
  15. * @author Vaadin Ltd.
  16. * @version
  17. * @VERSION@
  18. * @since 3.0
  19. */
  20. @SuppressWarnings("serial")
  21. public class StreamResource implements ApplicationResource {
  22. /**
  23. * Source stream the downloaded content is fetched from.
  24. */
  25. private StreamSource streamSource = null;
  26. /**
  27. * Explicit mime-type.
  28. */
  29. private String MIMEType = null;
  30. /**
  31. * Filename.
  32. */
  33. private String filename;
  34. /**
  35. * Application.
  36. */
  37. private final Application application;
  38. /**
  39. * Default buffer size for this stream resource.
  40. */
  41. private int bufferSize = 0;
  42. /**
  43. * Default cache time for this stream resource.
  44. */
  45. private long cacheTime = DEFAULT_CACHETIME;
  46. /**
  47. * Creates a new stream resource for downloading from stream.
  48. *
  49. * @param streamSource
  50. * the source Stream.
  51. * @param filename
  52. * the name of the file.
  53. * @param application
  54. * the Application object.
  55. */
  56. public StreamResource(StreamSource streamSource, String filename,
  57. Application application) {
  58. this.application = application;
  59. setFilename(filename);
  60. setStreamSource(streamSource);
  61. // Register to application
  62. application.addResource(this);
  63. }
  64. /**
  65. * @see com.vaadin.terminal.Resource#getMIMEType()
  66. */
  67. @Override
  68. public String getMIMEType() {
  69. if (MIMEType != null) {
  70. return MIMEType;
  71. }
  72. return FileTypeResolver.getMIMEType(filename);
  73. }
  74. /**
  75. * Sets the mime type of the resource.
  76. *
  77. * @param MIMEType
  78. * the MIME type to be set.
  79. */
  80. public void setMIMEType(String MIMEType) {
  81. this.MIMEType = MIMEType;
  82. }
  83. /**
  84. * Returns the source for this <code>StreamResource</code>. StreamSource is
  85. * queried when the resource is about to be streamed to the client.
  86. *
  87. * @return Source of the StreamResource.
  88. */
  89. public StreamSource getStreamSource() {
  90. return streamSource;
  91. }
  92. /**
  93. * Sets the source for this <code>StreamResource</code>.
  94. * <code>StreamSource</code> is queried when the resource is about to be
  95. * streamed to the client.
  96. *
  97. * @param streamSource
  98. * the source to set.
  99. */
  100. public void setStreamSource(StreamSource streamSource) {
  101. this.streamSource = streamSource;
  102. }
  103. /**
  104. * Gets the filename.
  105. *
  106. * @return the filename.
  107. */
  108. @Override
  109. public String getFilename() {
  110. return filename;
  111. }
  112. /**
  113. * Sets the filename.
  114. *
  115. * @param filename
  116. * the filename to set.
  117. */
  118. public void setFilename(String filename) {
  119. this.filename = filename;
  120. }
  121. /**
  122. * @see com.vaadin.terminal.ApplicationResource#getApplication()
  123. */
  124. @Override
  125. public Application getApplication() {
  126. return application;
  127. }
  128. /**
  129. * @see com.vaadin.terminal.ApplicationResource#getStream()
  130. */
  131. @Override
  132. public DownloadStream getStream() {
  133. final StreamSource ss = getStreamSource();
  134. if (ss == null) {
  135. return null;
  136. }
  137. final DownloadStream ds = new DownloadStream(ss.getStream(),
  138. getMIMEType(), getFilename());
  139. ds.setBufferSize(getBufferSize());
  140. ds.setCacheTime(cacheTime);
  141. return ds;
  142. }
  143. /**
  144. * Interface implemented by the source of a StreamResource.
  145. *
  146. * @author Vaadin Ltd.
  147. * @version
  148. * @VERSION@
  149. * @since 3.0
  150. */
  151. public interface StreamSource extends Serializable {
  152. /**
  153. * Returns new input stream that is used for reading the resource.
  154. */
  155. public InputStream getStream();
  156. }
  157. /* documented in superclass */
  158. @Override
  159. public int getBufferSize() {
  160. return bufferSize;
  161. }
  162. /**
  163. * Sets the size of the download buffer used for this resource.
  164. *
  165. * @param bufferSize
  166. * the size of the buffer in bytes.
  167. */
  168. public void setBufferSize(int bufferSize) {
  169. this.bufferSize = bufferSize;
  170. }
  171. /* documented in superclass */
  172. @Override
  173. public long getCacheTime() {
  174. return cacheTime;
  175. }
  176. /**
  177. * Sets the length of cache expiration time.
  178. *
  179. * <p>
  180. * This gives the adapter the possibility cache streams sent to the client.
  181. * The caching may be made in adapter or at the client if the client
  182. * supports caching. Zero or negavive value disbales the caching of this
  183. * stream.
  184. * </p>
  185. *
  186. * @param cacheTime
  187. * the cache time in milliseconds.
  188. *
  189. */
  190. public void setCacheTime(long cacheTime) {
  191. this.cacheTime = cacheTime;
  192. }
  193. }