use of com.haleconnect.api.projectstore.v1.ApiCallback in project hale by halestudio.
the class HaleConnectServiceImpl method createUploadFileCallback.
private ApiCallback<Feedback> createUploadFileCallback(final SettableFuture<Boolean> future, final ProgressIndicator progress, final File file, final int totalWork) {
return new ApiCallback<Feedback>() {
AtomicLong chunkWritten = new AtomicLong(0);
AtomicLong bytesReported = new AtomicLong(0);
@Override
public void onDownloadProgress(long bytesRead, long contentLength, boolean done) {
// not required
}
@Override
public void onFailure(com.haleconnect.api.projectstore.v1.ApiException e, int statusCode, Map<String, List<String>> responseHeaders) {
progress.end();
future.setException(new HaleConnectException(e.getMessage(), e, statusCode, responseHeaders));
}
@Override
public void onSuccess(Feedback result, int statusCode, Map<String, List<String>> responseHeaders) {
if (result.getError()) {
log.error(MessageFormat.format("Error uploading project file \"{0}\": {1}", file.getAbsolutePath(), result.getMessage()));
future.set(false);
} else {
future.set(true);
}
progress.end();
}
@Override
public void onUploadProgress(long bytesWritten, long contentLength, boolean done) {
// bytesWritten contains the accumulated amount of bytes written
if (totalWork != ProgressIndicator.UNKNOWN) {
// Wait until at least 1 KiB was written
long chunk = chunkWritten.get();
chunk += bytesWritten - bytesReported.get();
if (chunk >= 1024) {
long workToReport = chunk >> 10;
// cannot overflow, total size in KiB
// is guaranteed to be < Integer.MAX_VALUE
progress.advance(Math.toIntExact(workToReport));
chunk -= workToReport << 10;
// chunkWritten now always < 1024
}
chunkWritten.set(chunk);
bytesReported.set(bytesWritten);
}
}
};
}
use of com.haleconnect.api.projectstore.v1.ApiCallback in project hale by halestudio.
the class HaleConnectServiceImpl method uploadProjectFile.
/**
* @see eu.esdihumboldt.hale.io.haleconnect.HaleConnectService#uploadProjectFile(java.lang.String,
* eu.esdihumboldt.hale.io.haleconnect.Owner, java.io.File,
* eu.esdihumboldt.hale.common.core.io.ProgressIndicator)
*/
@Override
public boolean uploadProjectFile(String projectId, Owner owner, File file, ProgressIndicator progress) throws HaleConnectException {
if (!this.isLoggedIn()) {
throw new HaleConnectException("Not logged in");
}
String apiKey = this.getSession().getToken();
SettableFuture<Boolean> future = SettableFuture.create();
try {
FilesApi filesApi = ProjectStoreHelper.getFilesApi(this, apiKey);
// POST /raw
int totalWork = computeTotalWork(file);
ApiCallback<Feedback> apiCallback = createUploadFileCallback(future, progress, file, totalWork);
progress.begin("Uploading project archive", totalWork);
filesApi.addFilesAsync(owner.getType().getJsonValue(), owner.getId(), projectId, file, apiCallback);
return future.get();
} catch (com.haleconnect.api.projectstore.v1.ApiException e1) {
throw new HaleConnectException(e1.getMessage(), e1, e1.getCode(), e1.getResponseHeaders());
} catch (ExecutionException e2) {
Throwable t = e2.getCause();
if (t instanceof HaleConnectException) {
throw (HaleConnectException) t;
} else {
throw new HaleConnectException(t.getMessage(), t);
}
} catch (InterruptedException e3) {
throw new HaleConnectException(e3.getMessage(), e3);
}
}
Aggregations