use of com.vaadin.flow.server.StreamVariable in project flow by vaadin.
the class StreamReceiverHandler method streamToReceiver.
private final Pair<Boolean, UploadStatus> streamToReceiver(VaadinSession session, final InputStream in, StreamReceiver streamReceiver, String filename, String type, long contentLength) throws UploadException {
StreamVariable streamVariable = streamReceiver.getStreamVariable();
if (streamVariable == null) {
throw new IllegalStateException("StreamVariable for the post not found");
}
OutputStream out = null;
long totalBytes = 0;
StreamingStartEventImpl startedEvent = new StreamingStartEventImpl(filename, type, contentLength);
boolean success = false;
try {
boolean listenProgress;
session.lock();
try {
streamVariable.streamingStarted(startedEvent);
out = streamVariable.getOutputStream();
listenProgress = streamVariable.listenProgress();
} finally {
session.unlock();
}
// Gets the output target stream
if (out == null) {
throw new NoOutputStreamException();
}
if (null == in) {
// No file, for instance non-existent filename in html upload
throw new NoInputStreamException();
}
final byte[] buffer = new byte[MAX_UPLOAD_BUFFER_SIZE];
long lastStreamingEvent = 0;
int bytesReadToBuffer;
do {
bytesReadToBuffer = in.read(buffer);
if (bytesReadToBuffer > 0) {
out.write(buffer, 0, bytesReadToBuffer);
totalBytes += bytesReadToBuffer;
}
if (listenProgress) {
StreamingProgressEventImpl progressEvent = new StreamingProgressEventImpl(filename, type, contentLength, totalBytes);
lastStreamingEvent = updateProgress(session, streamVariable, progressEvent, lastStreamingEvent, bytesReadToBuffer);
}
if (streamVariable.isInterrupted()) {
throw new UploadInterruptedException();
}
} while (bytesReadToBuffer > 0);
// upload successful
out.close();
StreamVariable.StreamingEndEvent event = new StreamingEndEventImpl(filename, type, totalBytes);
session.lock();
try {
streamVariable.streamingFinished(event);
} finally {
session.unlock();
}
success = true;
} catch (UploadInterruptedException | IOException e) {
// Download is either interrupted by application code or some
// IOException happens
onStreamingFailed(session, filename, type, contentLength, streamVariable, out, totalBytes, e);
// Interrupted exception and IOEXception are not thrown forward:
// it's enough to fire them via streamVariable
} catch (final Exception e) {
onStreamingFailed(session, filename, type, contentLength, streamVariable, out, totalBytes, e);
// I/O operations).
throw new UploadException(e);
}
return new Pair<>(startedEvent.isDisposed(), success ? UploadStatus.OK : UploadStatus.ERROR);
}
use of com.vaadin.flow.server.StreamVariable in project flow by vaadin.
the class StreamReceiverHandlerTest method partsAreUsedDirectlyIfPresentWithoutParsingInput.
// Vaadin Spring #381
@Test
public void partsAreUsedDirectlyIfPresentWithoutParsingInput() throws IOException {
contentType = "multipart/form-data; boundary=----WebKitFormBoundary7NsWHeCJVZNwi6ll";
inputStream = createInputStream("------WebKitFormBoundary7NsWHeCJVZNwi6ll\n" + "Content-Disposition: form-data; name=\"file\"; filename=\"EBookJP.txt\"\n" + "Content-Type: text/plain\n" + "\n" + "\n" + "------WebKitFormBoundary7NsWHeCJVZNwi6ll--");
outputStream = new ByteArrayOutputStream();
contentLength = "99";
final String fileName = "EBookJP.txt";
final String contentType = "text/plain";
parts = new ArrayList<>();
parts.add(createPart(createInputStream("foobar"), contentType, fileName, 6));
handler.handleRequest(session, request, response, streamReceiver, String.valueOf(uiId), expectedSecurityKey);
Mockito.verify(responseOutput).close();
ArgumentCaptor<StreamVariable.StreamingEndEvent> endEventArgumentCaptor = ArgumentCaptor.forClass(StreamVariable.StreamingEndEvent.class);
Mockito.verify(streamVariable).streamingFinished(endEventArgumentCaptor.capture());
Assert.assertEquals("foobar", new String(((ByteArrayOutputStream) outputStream).toByteArray()));
Assert.assertEquals(fileName, endEventArgumentCaptor.getValue().getFileName());
Assert.assertEquals(contentType, endEventArgumentCaptor.getValue().getMimeType());
Mockito.verify(response).setContentType(ApplicationConstants.CONTENT_TYPE_TEXT_HTML_UTF_8);
Mockito.verify(response, Mockito.times(0)).setStatus(Mockito.anyInt());
}
Aggregations