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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.itmill.toolkit.terminal;
  5. import java.io.File;
  6. import java.io.FileInputStream;
  7. import java.io.FileNotFoundException;
  8. import com.itmill.toolkit.Application;
  9. import com.itmill.toolkit.service.FileTypeResolver;
  10. /**
  11. * <code>FileResources</code> are files or directories on local filesystem.
  12. * The files and directories are served through URI:s to the client terminal and
  13. * thus must be registered to an URI context before they can be used. The
  14. * resource is automatically registered to the application when it is created.
  15. *
  16. * @author IT Mill Ltd.
  17. * @version
  18. * @VERSION@
  19. * @since 3.0
  20. */
  21. public class FileResource implements ApplicationResource {
  22. /**
  23. * Default buffer size for this stream resource.
  24. */
  25. private int bufferSize = 0;
  26. /**
  27. * File where the downloaded content is fetched from.
  28. */
  29. private File sourceFile;
  30. /**
  31. * Application.
  32. */
  33. private final Application application;
  34. /**
  35. * Default cache time for this stream resource.
  36. */
  37. private long cacheTime = DownloadStream.DEFAULT_CACHETIME;
  38. /**
  39. * Creates a new file resource for providing given file for client
  40. * terminals.
  41. */
  42. public FileResource(File sourceFile, Application application) {
  43. this.application = application;
  44. setSourceFile(sourceFile);
  45. application.addResource(this);
  46. }
  47. /**
  48. * Gets the resource as stream.
  49. *
  50. * @see com.itmill.toolkit.terminal.ApplicationResource#getStream()
  51. */
  52. public DownloadStream getStream() {
  53. try {
  54. final DownloadStream ds = new DownloadStream(new FileInputStream(
  55. sourceFile), getMIMEType(), getFilename());
  56. ds.setCacheTime(cacheTime);
  57. return ds;
  58. } catch (final FileNotFoundException e) {
  59. // No logging for non-existing files at this level.
  60. return null;
  61. }
  62. }
  63. /**
  64. * Gets the source file.
  65. *
  66. * @return the source File.
  67. */
  68. public File getSourceFile() {
  69. return sourceFile;
  70. }
  71. /**
  72. * Sets the source file.
  73. *
  74. * @param sourceFile
  75. * the source file to set.
  76. */
  77. public void setSourceFile(File sourceFile) {
  78. this.sourceFile = sourceFile;
  79. }
  80. /**
  81. * @see com.itmill.toolkit.terminal.ApplicationResource#getApplication()
  82. */
  83. public Application getApplication() {
  84. return application;
  85. }
  86. /**
  87. * @see com.itmill.toolkit.terminal.ApplicationResource#getFilename()
  88. */
  89. public String getFilename() {
  90. return sourceFile.getName();
  91. }
  92. /**
  93. * @see com.itmill.toolkit.terminal.Resource#getMIMEType()
  94. */
  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 negavive
  113. * value disbales 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. /* documented in superclass */
  122. public int getBufferSize() {
  123. return bufferSize;
  124. }
  125. /**
  126. * Sets the size of the download buffer used for this resource.
  127. *
  128. * @param bufferSize
  129. * the size of the buffer in bytes.
  130. */
  131. public void setBufferSize(int bufferSize) {
  132. this.bufferSize = bufferSize;
  133. }
  134. }