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.

StreamVariable.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal;
  5. import java.io.OutputStream;
  6. import java.io.Serializable;
  7. import com.vaadin.Application;
  8. import com.vaadin.terminal.StreamVariable.StreamingEndEvent;
  9. import com.vaadin.terminal.StreamVariable.StreamingErrorEvent;
  10. import com.vaadin.terminal.StreamVariable.StreamingStartEvent;
  11. /**
  12. * StreamVariable is a special kind of variable whose value is streamed to an
  13. * {@link OutputStream} provided by the {@link #getOutputStream()} method. E.g.
  14. * in web terminals {@link StreamVariable} can be used to send large files from
  15. * browsers to the server without consuming large amounts of memory.
  16. * <p>
  17. * Note, writing to the {@link OutputStream} is not synchronized by the terminal
  18. * (to avoid stalls in other operations when eg. streaming to a slow network
  19. * service or file system). If UI is changed as a side effect of writing to the
  20. * output stream, developer must handle synchronization manually.
  21. * <p>
  22. *
  23. * @author Vaadin Ltd.
  24. * @version
  25. * @VERSION@
  26. * @since 6.5
  27. * @see PaintTarget#addVariable(VariableOwner, String, StreamVariable)
  28. */
  29. public interface StreamVariable extends Serializable {
  30. /**
  31. * Invoked by the terminal when a new upload arrives, after
  32. * {@link #streamingStarted(StreamingStartEvent)} method has been called.
  33. * The terminal implementation will write the streamed variable to the
  34. * returned output stream.
  35. *
  36. * @return Stream to which the uploaded file should be written.
  37. */
  38. public OutputStream getOutputStream();
  39. /**
  40. * Whether the {@link #onProgress(long, long)} method should be called
  41. * during the upload.
  42. * <p>
  43. * {@link #onProgress(long, long)} is called in a synchronized block when
  44. * the content is being received. This is potentially bit slow, so we are
  45. * calling that method only if requested. The value is requested after the
  46. * {@link #uploadStarted(StreamingStartEvent)} event, but not after reading
  47. * each buffer.
  48. *
  49. * @return true if this {@link StreamVariable} wants to by notified during
  50. * the upload of the progress of streaming.
  51. * @see #onProgress(StreamingProgressEvent)
  52. */
  53. public boolean listenProgress();
  54. /**
  55. * This method is called by the terminal if {@link #listenProgress()}
  56. * returns true when the streaming starts.
  57. */
  58. public void onProgress(StreamingProgressEvent event);
  59. public void streamingStarted(StreamingStartEvent event);
  60. public void streamingFinished(StreamingEndEvent event);
  61. public void streamingFailed(StreamingErrorEvent event);
  62. /*
  63. * Not synchronized to avoid stalls (caused by UIDL requests) while
  64. * streaming the content. Implementations also most commonly atomic even
  65. * without the restriction.
  66. */
  67. /**
  68. * If this method returns true while the content is being streamed the
  69. * Terminal to stop receiving current upload.
  70. * <p>
  71. * Note, the usage of this method is not synchronized over the Application
  72. * instance by the terminal like other methods. The implementation should
  73. * only return a boolean field and especially not modify UI or implement a
  74. * synchronization by itself.
  75. *
  76. * @return true if the streaming should be interrupted as soon as possible.
  77. */
  78. public boolean isInterrupted();
  79. public interface StreamingEvent extends Serializable {
  80. /**
  81. * @return the file name of the streamed file if known
  82. */
  83. public String getFileName();
  84. /**
  85. * @return the mime type of the streamed file if known
  86. */
  87. public String getMimeType();
  88. /**
  89. * @return the length of the stream (in bytes) if known, else -1
  90. */
  91. public long getContentLength();
  92. /**
  93. * @return then number of bytes streamed to StreamVariable
  94. */
  95. public long getBytesReceived();
  96. }
  97. /**
  98. * Event passed to {@link #uploadStarted(StreamingStartEvent)} method before
  99. * the streaming of the content to {@link StreamVariable} starts.
  100. */
  101. public interface StreamingStartEvent extends StreamingEvent {
  102. /**
  103. * The owner of the StreamVariable can call this method to inform the
  104. * terminal implementation that this StreamVariable will not be used to
  105. * accept more post.
  106. */
  107. public void disposeStreamVariable();
  108. }
  109. /**
  110. * Event passed to {@link #onProgress(StreamingProgressEvent)} method during
  111. * the streaming progresses.
  112. */
  113. public interface StreamingProgressEvent extends StreamingEvent {
  114. }
  115. /**
  116. * Event passed to {@link #uploadFinished(StreamingEndEvent)} method the
  117. * contents have been streamed to StreamVariable successfully.
  118. */
  119. public interface StreamingEndEvent extends StreamingEvent {
  120. }
  121. /**
  122. * Event passed to {@link #uploadFailed(StreamingErrorEvent)} method when
  123. * the streaming ended before the end of the input. The streaming may fail
  124. * due an interruption by {@link } or due an other unknown exception in
  125. * communication. In the latter case the exception is also passed to
  126. * {@link Application#terminalError(com.vaadin.terminal.Terminal.ErrorEvent)}
  127. * .
  128. */
  129. public interface StreamingErrorEvent extends StreamingEvent {
  130. /**
  131. * @return the exception that caused the receiving not to finish cleanly
  132. */
  133. public Exception getException();
  134. }
  135. }