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);
}
}
}
Aggregations