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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Copyright 2000-2018 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.InputStream;
  18. import java.io.Serializable;
  19. import java.util.Arrays;
  20. import com.vaadin.shared.util.SharedUtil;
  21. import com.vaadin.util.FileTypeResolver;
  22. /**
  23. * <code>StreamResource</code> is a resource provided to the client directly by
  24. * the application.
  25. *
  26. * @author Vaadin Ltd.
  27. * @since 3.0
  28. */
  29. @SuppressWarnings("serial")
  30. public class StreamResource implements ConnectorResource {
  31. /**
  32. * Source stream the downloaded content is fetched from.
  33. */
  34. private StreamSource streamSource = null;
  35. /**
  36. * Explicit mime-type.
  37. */
  38. private String mimeType = null;
  39. /**
  40. * Filename.
  41. */
  42. private String filename;
  43. /**
  44. * Default buffer size for this stream resource.
  45. */
  46. private int bufferSize = 0;
  47. /**
  48. * Default cache time for this stream resource.
  49. */
  50. private long cacheTime = DownloadStream.DEFAULT_CACHETIME;
  51. /**
  52. * Creates a new stream resource for downloading from stream.
  53. *
  54. * @param streamSource
  55. * the source Stream.
  56. * @param filename
  57. * the name of the file.
  58. */
  59. public StreamResource(StreamSource streamSource, String filename) {
  60. setFilename(filename);
  61. setStreamSource(streamSource);
  62. }
  63. /**
  64. * @see com.vaadin.server.Resource#getMIMEType()
  65. */
  66. @Override
  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. @Override
  108. public String getFilename() {
  109. return filename;
  110. }
  111. /**
  112. * Sets the filename.
  113. *
  114. * @param filename
  115. * the filename to set.
  116. */
  117. public void setFilename(String filename) {
  118. this.filename = filename;
  119. }
  120. @Override
  121. public DownloadStream getStream() {
  122. final StreamSource ss = getStreamSource();
  123. if (ss == null) {
  124. return null;
  125. }
  126. final DownloadStream ds = new DownloadStream(ss.getStream(),
  127. getMIMEType(), getFilename());
  128. ds.setBufferSize(getBufferSize());
  129. ds.setCacheTime(getCacheTime());
  130. return ds;
  131. }
  132. /**
  133. * Interface implemented by the source of a StreamResource.
  134. *
  135. * @author Vaadin Ltd.
  136. * @since 3.0
  137. */
  138. @FunctionalInterface
  139. public interface StreamSource extends Serializable {
  140. /**
  141. * Returns new input stream that is used for reading the resource.
  142. */
  143. public InputStream getStream();
  144. }
  145. /**
  146. * Gets the size of the download buffer used for this resource.
  147. *
  148. * <p>
  149. * If the buffer size is 0, the buffer size is decided by the terminal
  150. * adapter. The default value is 0.
  151. * </p>
  152. *
  153. * @return the size of the buffer in bytes.
  154. */
  155. public int getBufferSize() {
  156. return bufferSize;
  157. }
  158. /**
  159. * Sets the size of the download buffer used for this resource.
  160. *
  161. * @param bufferSize
  162. * the size of the buffer in bytes.
  163. */
  164. public void setBufferSize(int bufferSize) {
  165. this.bufferSize = bufferSize;
  166. }
  167. /**
  168. * Gets the length of cache expiration time. This gives the adapter the
  169. * possibility cache streams sent to the client. The caching may be made in
  170. * adapter or at the client if the client supports caching. Default is
  171. * <code>DownloadStream.DEFAULT_CACHETIME</code>.
  172. *
  173. * @return Cache time in milliseconds.
  174. */
  175. public long getCacheTime() {
  176. return cacheTime;
  177. }
  178. /**
  179. * Sets the length of cache expiration time.
  180. *
  181. * <p>
  182. * This gives the adapter the possibility cache streams sent to the client.
  183. * The caching may be made in adapter or at the client if the client
  184. * supports caching. Zero or negative value disables the caching of this
  185. * stream.
  186. * </p>
  187. *
  188. * @param cacheTime
  189. * the cache time in milliseconds.
  190. *
  191. */
  192. public void setCacheTime(long cacheTime) {
  193. this.cacheTime = cacheTime;
  194. }
  195. @Override
  196. public boolean equals(Object obj) {
  197. if (this == obj) {
  198. return true;
  199. } else if (obj instanceof StreamResource) {
  200. StreamResource that = (StreamResource) obj;
  201. return SharedUtil.equals(getStreamSource(), that.getStreamSource())
  202. && SharedUtil.equals(mimeType, that.mimeType)
  203. && SharedUtil.equals(getFilename(), that.getFilename())
  204. && getBufferSize() == that.getBufferSize()
  205. && getCacheTime() == that.getCacheTime();
  206. } else {
  207. return false;
  208. }
  209. }
  210. @Override
  211. public int hashCode() {
  212. return Arrays.hashCode(new Object[] { getStreamSource(), mimeType,
  213. getFilename(), getBufferSize(), getCacheTime() });
  214. }
  215. }