Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

FileResource.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal;
  5. import java.io.File;
  6. import java.io.FileInputStream;
  7. import java.io.FileNotFoundException;
  8. import com.vaadin.Application;
  9. import com.vaadin.service.FileTypeResolver;
  10. import com.vaadin.terminal.Terminal.ErrorEvent;
  11. /**
  12. * <code>FileResources</code> are files or directories on local filesystem. The
  13. * files and directories are served through URI:s to the client terminal and
  14. * thus must be registered to an URI context before they can be used. The
  15. * resource is automatically registered to the application when it is created.
  16. *
  17. * @author Vaadin Ltd.
  18. * @version
  19. * @VERSION@
  20. * @since 3.0
  21. */
  22. @SuppressWarnings("serial")
  23. public class FileResource implements ApplicationResource {
  24. /**
  25. * Default buffer size for this stream resource.
  26. */
  27. private int bufferSize = 0;
  28. /**
  29. * File where the downloaded content is fetched from.
  30. */
  31. private File sourceFile;
  32. /**
  33. * Application.
  34. */
  35. private final Application application;
  36. /**
  37. * Default cache time for this stream resource.
  38. */
  39. private long cacheTime = DownloadStream.DEFAULT_CACHETIME;
  40. /**
  41. * Creates a new file resource for providing given file for client
  42. * terminals.
  43. */
  44. public FileResource(File sourceFile, Application application) {
  45. this.application = application;
  46. setSourceFile(sourceFile);
  47. application.addResource(this);
  48. }
  49. /**
  50. * Gets the resource as stream.
  51. *
  52. * @see com.vaadin.terminal.ApplicationResource#getStream()
  53. */
  54. @Override
  55. public DownloadStream getStream() {
  56. try {
  57. final DownloadStream ds = new DownloadStream(new FileInputStream(
  58. sourceFile), getMIMEType(), getFilename());
  59. ds.setParameter("Content-Length",
  60. String.valueOf(sourceFile.length()));
  61. ds.setCacheTime(cacheTime);
  62. return ds;
  63. } catch (final FileNotFoundException e) {
  64. // Log the exception using the application error handler
  65. getApplication().getErrorHandler().terminalError(new ErrorEvent() {
  66. @Override
  67. public Throwable getThrowable() {
  68. return e;
  69. }
  70. });
  71. return null;
  72. }
  73. }
  74. /**
  75. * Gets the source file.
  76. *
  77. * @return the source File.
  78. */
  79. public File getSourceFile() {
  80. return sourceFile;
  81. }
  82. /**
  83. * Sets the source file.
  84. *
  85. * @param sourceFile
  86. * the source file to set.
  87. */
  88. public void setSourceFile(File sourceFile) {
  89. this.sourceFile = sourceFile;
  90. }
  91. /**
  92. * @see com.vaadin.terminal.ApplicationResource#getApplication()
  93. */
  94. @Override
  95. public Application getApplication() {
  96. return application;
  97. }
  98. /**
  99. * @see com.vaadin.terminal.ApplicationResource#getFilename()
  100. */
  101. @Override
  102. public String getFilename() {
  103. return sourceFile.getName();
  104. }
  105. /**
  106. * @see com.vaadin.terminal.Resource#getMIMEType()
  107. */
  108. @Override
  109. public String getMIMEType() {
  110. return FileTypeResolver.getMIMEType(sourceFile);
  111. }
  112. /**
  113. * Gets the length of cache expiration time. This gives the adapter the
  114. * possibility cache streams sent to the client. The caching may be made in
  115. * adapter or at the client if the client supports caching. Default is
  116. * <code>DownloadStream.DEFAULT_CACHETIME</code>.
  117. *
  118. * @return Cache time in milliseconds.
  119. */
  120. @Override
  121. public long getCacheTime() {
  122. return cacheTime;
  123. }
  124. /**
  125. * Sets the length of cache expiration time. This gives the adapter the
  126. * possibility cache streams sent to the client. The caching may be made in
  127. * adapter or at the client if the client supports caching. Zero or negavive
  128. * value disbales the caching of this stream.
  129. *
  130. * @param cacheTime
  131. * the cache time in milliseconds.
  132. */
  133. public void setCacheTime(long cacheTime) {
  134. this.cacheTime = cacheTime;
  135. }
  136. /* documented in superclass */
  137. @Override
  138. public int getBufferSize() {
  139. return bufferSize;
  140. }
  141. /**
  142. * Sets the size of the download buffer used for this resource.
  143. *
  144. * @param bufferSize
  145. * the size of the buffer in bytes.
  146. */
  147. public void setBufferSize(int bufferSize) {
  148. this.bufferSize = bufferSize;
  149. }
  150. }