use of com.vaadin.flow.server.UploadException in project flow by vaadin.
the class StreamReceiverHandler method handleStream.
private boolean handleStream(VaadinSession session, StreamReceiver streamReceiver, StateNode owner, long contentLength, FileItemStream item) throws IOException {
String name = item.getName();
InputStream stream = item.openStream();
try {
return handleFileUploadValidationAndData(session, stream, streamReceiver, name, item.getContentType(), contentLength, owner);
} catch (UploadException e) {
session.getErrorHandler().error(new ErrorEvent(e));
}
return false;
}
use of com.vaadin.flow.server.UploadException in project flow by vaadin.
the class StreamReceiverHandler method handleStream.
private boolean handleStream(VaadinSession session, StreamReceiver streamReceiver, StateNode owner, Part part) throws IOException {
String name = part.getSubmittedFileName();
InputStream stream = part.getInputStream();
try {
return handleFileUploadValidationAndData(session, stream, streamReceiver, name, part.getContentType(), part.getSize(), owner);
} catch (UploadException e) {
session.getErrorHandler().error(new ErrorEvent(e));
}
return false;
}
use of com.vaadin.flow.server.UploadException 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.UploadException in project flow by vaadin.
the class StreamReceiverHandler method doHandleXhrFilePost.
/**
* Used to stream plain file post (aka XHR2.post(File))
* <p>
* This method takes care of locking the session as needed and does not
* assume the caller has locked the session. This allows the session to be
* locked only when needed and not when handling the upload data.
* </p>
*
* @param session
* The session containing the stream variable
* @param request
* The upload request
* @param response
* The upload response
* @param streamReceiver
* the receiver containing the destination stream variable
* @param owner
* The owner of the stream
* @param contentLength
* The length of the request content
* @throws IOException
* If there is a problem reading the request or writing the
* response
*/
protected void doHandleXhrFilePost(VaadinSession session, VaadinRequest request, VaadinResponse response, StreamReceiver streamReceiver, StateNode owner, long contentLength) throws IOException {
// These are unknown in filexhr ATM, maybe add to Accept header that
// is accessible in portlets
final String filename = "unknown";
final String mimeType = filename;
final InputStream stream = request.getInputStream();
boolean success = false;
try {
success = handleFileUploadValidationAndData(session, stream, streamReceiver, filename, mimeType, contentLength, owner);
} catch (UploadException e) {
session.getErrorHandler().error(new ErrorEvent(e));
}
sendUploadResponse(response, success);
}
Aggregations