Search in sources :

Example 1 with UploadException

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;
}
Also used : InputStream(java.io.InputStream) UploadException(com.vaadin.flow.server.UploadException) FileUploadException(org.apache.commons.fileupload.FileUploadException) ErrorEvent(com.vaadin.flow.server.ErrorEvent)

Example 2 with UploadException

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;
}
Also used : InputStream(java.io.InputStream) UploadException(com.vaadin.flow.server.UploadException) FileUploadException(org.apache.commons.fileupload.FileUploadException) ErrorEvent(com.vaadin.flow.server.ErrorEvent)

Example 3 with UploadException

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);
}
Also used : OutputStream(java.io.OutputStream) UploadException(com.vaadin.flow.server.UploadException) FileUploadException(org.apache.commons.fileupload.FileUploadException) StreamingStartEventImpl(com.vaadin.flow.server.communication.streaming.StreamingStartEventImpl) NoOutputStreamException(com.vaadin.flow.server.NoOutputStreamException) IOException(java.io.IOException) NoOutputStreamException(com.vaadin.flow.server.NoOutputStreamException) IOException(java.io.IOException) NoInputStreamException(com.vaadin.flow.server.NoInputStreamException) UploadException(com.vaadin.flow.server.UploadException) FileUploadException(org.apache.commons.fileupload.FileUploadException) StreamVariable(com.vaadin.flow.server.StreamVariable) StreamingEndEventImpl(com.vaadin.flow.server.communication.streaming.StreamingEndEventImpl) NoInputStreamException(com.vaadin.flow.server.NoInputStreamException) StreamingProgressEventImpl(com.vaadin.flow.server.communication.streaming.StreamingProgressEventImpl) Pair(com.vaadin.flow.internal.Pair)

Example 4 with UploadException

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);
}
Also used : InputStream(java.io.InputStream) UploadException(com.vaadin.flow.server.UploadException) FileUploadException(org.apache.commons.fileupload.FileUploadException) ErrorEvent(com.vaadin.flow.server.ErrorEvent)

Aggregations

UploadException (com.vaadin.flow.server.UploadException)4 FileUploadException (org.apache.commons.fileupload.FileUploadException)4 ErrorEvent (com.vaadin.flow.server.ErrorEvent)3 InputStream (java.io.InputStream)3 Pair (com.vaadin.flow.internal.Pair)1 NoInputStreamException (com.vaadin.flow.server.NoInputStreamException)1 NoOutputStreamException (com.vaadin.flow.server.NoOutputStreamException)1 StreamVariable (com.vaadin.flow.server.StreamVariable)1 StreamingEndEventImpl (com.vaadin.flow.server.communication.streaming.StreamingEndEventImpl)1 StreamingProgressEventImpl (com.vaadin.flow.server.communication.streaming.StreamingProgressEventImpl)1 StreamingStartEventImpl (com.vaadin.flow.server.communication.streaming.StreamingStartEventImpl)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1