Search in sources :

Example 1 with InputStreamProgressEntity

use of com.haulmont.cuba.client.sys.fileupload.InputStreamProgressEntity in project cuba by cuba-platform.

the class FileLoaderClientImpl method saveStreamWithServlet.

protected void saveStreamWithServlet(FileDescriptor fd, Supplier<InputStream> inputStreamSupplier, @Nullable StreamingProgressListener streamingListener) throws FileStorageException, InterruptedException {
    Object context = serverSelector.initContext();
    String selectedUrl = serverSelector.getUrl(context);
    if (selectedUrl == null) {
        throw new FileStorageException(FileStorageException.Type.IO_EXCEPTION, fd.getName());
    }
    ClientConfig clientConfig = configuration.getConfig(ClientConfig.class);
    String fileUploadContext = clientConfig.getFileUploadContext();
    while (true) {
        String url = selectedUrl + fileUploadContext + "?s=" + userSessionSource.getUserSession().getId() + "&f=" + fd.toUrlParam();
        try (InputStream inputStream = inputStreamSupplier.get()) {
            InputStreamProgressEntity.UploadProgressListener progressListener = null;
            if (streamingListener != null) {
                progressListener = streamingListener::onStreamingProgressChanged;
            }
            HttpPost method = new HttpPost(url);
            method.setEntity(new InputStreamProgressEntity(inputStream, ContentType.APPLICATION_OCTET_STREAM, progressListener));
            HttpClientConnectionManager connectionManager = new BasicHttpClientConnectionManager();
            HttpClient client = HttpClientBuilder.create().setConnectionManager(connectionManager).build();
            try {
                HttpResponse response = client.execute(method);
                int statusCode = response.getStatusLine().getStatusCode();
                if (statusCode == HttpStatus.SC_OK) {
                    break;
                } else {
                    log.debug("Unable to upload file to {}\n{}", url, response.getStatusLine());
                    selectedUrl = failAndGetNextUrl(context);
                    if (selectedUrl == null) {
                        throw new FileStorageException(FileStorageException.Type.fromHttpStatus(statusCode), fd.getName());
                    }
                }
            } catch (InterruptedIOException e) {
                log.trace("Uploading has been interrupted");
                throw new InterruptedException("File uploading is interrupted");
            } catch (IOException e) {
                log.debug("Unable to upload file to {}\n{}", url, e);
                selectedUrl = failAndGetNextUrl(context);
                if (selectedUrl == null) {
                    throw new FileStorageException(FileStorageException.Type.IO_EXCEPTION, fd.getName(), e);
                }
            } finally {
                connectionManager.shutdown();
            }
        } catch (IOException | RetryUnsupportedException e) {
            throw new FileStorageException(FileStorageException.Type.IO_EXCEPTION, fd.getName(), e);
        }
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) InterruptedIOException(java.io.InterruptedIOException) InputStream(java.io.InputStream) HttpResponse(org.apache.http.HttpResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) InputStreamProgressEntity(com.haulmont.cuba.client.sys.fileupload.InputStreamProgressEntity) HttpClient(org.apache.http.client.HttpClient) ClientConfig(com.haulmont.cuba.client.ClientConfig) HttpClientConnectionManager(org.apache.http.conn.HttpClientConnectionManager) BasicHttpClientConnectionManager(org.apache.http.impl.conn.BasicHttpClientConnectionManager) BasicHttpClientConnectionManager(org.apache.http.impl.conn.BasicHttpClientConnectionManager)

Aggregations

ClientConfig (com.haulmont.cuba.client.ClientConfig)1 InputStreamProgressEntity (com.haulmont.cuba.client.sys.fileupload.InputStreamProgressEntity)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InterruptedIOException (java.io.InterruptedIOException)1 HttpResponse (org.apache.http.HttpResponse)1 HttpClient (org.apache.http.client.HttpClient)1 HttpPost (org.apache.http.client.methods.HttpPost)1 HttpClientConnectionManager (org.apache.http.conn.HttpClientConnectionManager)1 BasicHttpClientConnectionManager (org.apache.http.impl.conn.BasicHttpClientConnectionManager)1