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.

FileResource.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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.File;
  18. import java.io.FileInputStream;
  19. import java.io.FileNotFoundException;
  20. import com.vaadin.util.FileTypeResolver;
  21. /**
  22. * <code>FileResources</code> are files or directories on local filesystem. The
  23. * files and directories are served through URI:s to the client terminal and
  24. * thus must be registered to an URI context before they can be used. The
  25. * resource is automatically registered to the application when it is created.
  26. *
  27. * @author Vaadin Ltd.
  28. * @since 3.0
  29. */
  30. @SuppressWarnings("serial")
  31. public class FileResource implements ConnectorResource {
  32. /**
  33. * Default buffer size for this stream resource.
  34. */
  35. private int bufferSize = 0;
  36. /**
  37. * File where the downloaded content is fetched from.
  38. */
  39. private File sourceFile;
  40. /**
  41. * Default cache time for this stream resource.
  42. */
  43. private long cacheTime = DownloadStream.DEFAULT_CACHETIME;
  44. /**
  45. * Creates a new file resource for providing given file for client
  46. * terminals.
  47. *
  48. * @param sourceFile
  49. * the file that should be served.
  50. */
  51. public FileResource(File sourceFile) {
  52. if (sourceFile == null) {
  53. throw new IllegalArgumentException("File cannot be null");
  54. }
  55. setSourceFile(sourceFile);
  56. }
  57. @Override
  58. public DownloadStream getStream() {
  59. try {
  60. final DownloadStream ds = new DownloadStream(
  61. new FileInputStream(sourceFile), getMIMEType(),
  62. getFilename());
  63. ds.setParameter("Content-Length",
  64. String.valueOf(sourceFile.length()));
  65. ds.setBufferSize(getBufferSize());
  66. ds.setCacheTime(getCacheTime());
  67. return ds;
  68. } catch (final FileNotFoundException e) {
  69. throw new RuntimeException(
  70. "File not found: " + sourceFile.getName(), e);
  71. }
  72. }
  73. /**
  74. * Gets the source file.
  75. *
  76. * @return the source File.
  77. */
  78. public File getSourceFile() {
  79. return sourceFile;
  80. }
  81. /**
  82. * Sets the source file.
  83. *
  84. * @param sourceFile
  85. * the source file to set.
  86. */
  87. private void setSourceFile(File sourceFile) {
  88. this.sourceFile = sourceFile;
  89. }
  90. @Override
  91. public String getFilename() {
  92. return sourceFile.getName();
  93. }
  94. @Override
  95. public String getMIMEType() {
  96. return FileTypeResolver.getMIMEType(sourceFile);
  97. }
  98. /**
  99. * Gets the length of cache expiration time. This gives the adapter the
  100. * possibility cache streams sent to the client. The caching may be made in
  101. * adapter or at the client if the client supports caching. Default is
  102. * <code>DownloadStream.DEFAULT_CACHETIME</code>.
  103. *
  104. * @return Cache time in milliseconds.
  105. */
  106. public long getCacheTime() {
  107. return cacheTime;
  108. }
  109. /**
  110. * Sets the length of cache expiration time. This gives the adapter the
  111. * possibility cache streams sent to the client. The caching may be made in
  112. * adapter or at the client if the client supports caching. Zero or negative
  113. * value disables the caching of this stream.
  114. *
  115. * @param cacheTime
  116. * the cache time in milliseconds.
  117. */
  118. public void setCacheTime(long cacheTime) {
  119. this.cacheTime = cacheTime;
  120. }
  121. /**
  122. * Gets the size of the download buffer used for this resource.
  123. *
  124. * <p>
  125. * If the buffer size is 0, the buffer size is decided by the terminal
  126. * adapter. The default value is 0.
  127. * </p>
  128. *
  129. * @return the size of the buffer in bytes.
  130. */
  131. public int getBufferSize() {
  132. return bufferSize;
  133. }
  134. /**
  135. * Sets the size of the download buffer used for this resource.
  136. *
  137. * @param bufferSize
  138. * the size of the buffer in bytes.
  139. */
  140. public void setBufferSize(int bufferSize) {
  141. this.bufferSize = bufferSize;
  142. }
  143. }