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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. public String getMIMEType() {
  68. if (MIMEType != null) {
  69. return MIMEType;
  70. }
  71. return FileTypeResolver.getMIMEType(filename);
  72. }
  73. /**
  74. * Sets the mime type of the resource.
  75. *
  76. * @param MIMEType
  77. * the MIME type to be set.
  78. */
  79. public void setMIMEType(String MIMEType) {
  80. this.MIMEType = MIMEType;
  81. }
  82. /**
  83. * Returns the source for this <code>StreamResource</code>. StreamSource is
  84. * queried when the resource is about to be streamed to the client.
  85. *
  86. * @return Source of the StreamResource.
  87. */
  88. public StreamSource getStreamSource() {
  89. return streamSource;
  90. }
  91. /**
  92. * Sets the source for this <code>StreamResource</code>.
  93. * <code>StreamSource</code> is queried when the resource is about to be
  94. * streamed to the client.
  95. *
  96. * @param streamSource
  97. * the source to set.
  98. */
  99. public void setStreamSource(StreamSource streamSource) {
  100. this.streamSource = streamSource;
  101. }
  102. /**
  103. * Gets the filename.
  104. *
  105. * @return the filename.
  106. */
  107. public String getFilename() {
  108. return filename;
  109. }
  110. /**
  111. * Sets the filename.
  112. *
  113. * @param filename
  114. * the filename to set.
  115. */
  116. public void setFilename(String filename) {
  117. this.filename = filename;
  118. }
  119. /**
  120. * @see com.vaadin.terminal.ApplicationResource#getApplication()
  121. */
  122. public Application getApplication() {
  123. return application;
  124. }
  125. /**
  126. * @see com.vaadin.terminal.ApplicationResource#getStream()
  127. */
  128. public DownloadStream getStream() {
  129. final StreamSource ss = getStreamSource();
  130. if (ss == null) {
  131. return null;
  132. }
  133. final DownloadStream ds = new DownloadStream(ss.getStream(),
  134. getMIMEType(), getFilename());
  135. ds.setBufferSize(getBufferSize());
  136. ds.setCacheTime(cacheTime);
  137. return ds;
  138. }
  139. /**
  140. * Interface implemented by the source of a StreamResource.
  141. *
  142. * @author Vaadin Ltd.
  143. * @version
  144. * @VERSION@
  145. * @since 3.0
  146. */
  147. public interface StreamSource extends Serializable {
  148. /**
  149. * Returns new input stream that is used for reading the resource.
  150. */
  151. public InputStream getStream();
  152. }
  153. /* documented in superclass */
  154. public int getBufferSize() {
  155. return bufferSize;
  156. }
  157. /**
  158. * Sets the size of the download buffer used for this resource.
  159. *
  160. * @param bufferSize
  161. * the size of the buffer in bytes.
  162. */
  163. public void setBufferSize(int bufferSize) {
  164. this.bufferSize = bufferSize;
  165. }
  166. /* documented in superclass */
  167. public long getCacheTime() {
  168. return cacheTime;
  169. }
  170. /**
  171. * Sets the length of cache expiration time.
  172. *
  173. * <p>
  174. * This gives the adapter the possibility cache streams sent to the client.
  175. * The caching may be made in adapter or at the client if the client
  176. * supports caching. Zero or negavive value disbales the caching of this
  177. * stream.
  178. * </p>
  179. *
  180. * @param cacheTime
  181. * the cache time in milliseconds.
  182. *
  183. */
  184. public void setCacheTime(long cacheTime) {
  185. this.cacheTime = cacheTime;
  186. }
  187. }