Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

StreamVariable.java 5.7KB

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