Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

FileResource.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright 2000-2016 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.setCacheTime(cacheTime);
  66. return ds;
  67. } catch (final FileNotFoundException e) {
  68. throw new RuntimeException(
  69. "File not found: " + sourceFile.getName(), e);
  70. }
  71. }
  72. /**
  73. * Gets the source file.
  74. *
  75. * @return the source File.
  76. */
  77. public File getSourceFile() {
  78. return sourceFile;
  79. }
  80. /**
  81. * Sets the source file.
  82. *
  83. * @param sourceFile
  84. * the source file to set.
  85. */
  86. private void setSourceFile(File sourceFile) {
  87. this.sourceFile = sourceFile;
  88. }
  89. @Override
  90. public String getFilename() {
  91. return sourceFile.getName();
  92. }
  93. @Override
  94. public String getMIMEType() {
  95. return FileTypeResolver.getMIMEType(sourceFile);
  96. }
  97. /**
  98. * Gets the length of cache expiration time. This gives the adapter the
  99. * possibility cache streams sent to the client. The caching may be made in
  100. * adapter or at the client if the client supports caching. Default is
  101. * <code>DownloadStream.DEFAULT_CACHETIME</code>.
  102. *
  103. * @return Cache time in milliseconds.
  104. */
  105. public long getCacheTime() {
  106. return cacheTime;
  107. }
  108. /**
  109. * Sets the length of cache expiration time. This gives the adapter the
  110. * possibility cache streams sent to the client. The caching may be made in
  111. * adapter or at the client if the client supports caching. Zero or negative
  112. * value disables the caching of this stream.
  113. *
  114. * @param cacheTime
  115. * the cache time in milliseconds.
  116. */
  117. public void setCacheTime(long cacheTime) {
  118. this.cacheTime = cacheTime;
  119. }
  120. /**
  121. * Gets the size of the download buffer used for this resource.
  122. *
  123. * <p>
  124. * If the buffer size is 0, the buffer size is decided by the terminal
  125. * adapter. The default value is 0.
  126. * </p>
  127. *
  128. * @return the size of the buffer in bytes.
  129. */
  130. public int getBufferSize() {
  131. return bufferSize;
  132. }
  133. /**
  134. * Sets the size of the download buffer used for this resource.
  135. *
  136. * @param bufferSize
  137. * the size of the buffer in bytes.
  138. */
  139. public void setBufferSize(int bufferSize) {
  140. this.bufferSize = bufferSize;
  141. }
  142. }