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

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