Search in sources :

Example 1 with ApiCallback

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);
            }
        }
    };
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) ApiCallback(com.haleconnect.api.projectstore.v1.ApiCallback) Feedback(com.haleconnect.api.projectstore.v1.model.Feedback) HaleConnectException(eu.esdihumboldt.hale.io.haleconnect.HaleConnectException) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ApiException(com.haleconnect.api.user.v1.ApiException)

Example 2 with ApiCallback

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);
    }
}
Also used : HaleConnectException(eu.esdihumboldt.hale.io.haleconnect.HaleConnectException) FilesApi(com.haleconnect.api.projectstore.v1.api.FilesApi) Feedback(com.haleconnect.api.projectstore.v1.model.Feedback) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

Feedback (com.haleconnect.api.projectstore.v1.model.Feedback)2 HaleConnectException (eu.esdihumboldt.hale.io.haleconnect.HaleConnectException)2 ApiCallback (com.haleconnect.api.projectstore.v1.ApiCallback)1 FilesApi (com.haleconnect.api.projectstore.v1.api.FilesApi)1 ApiException (com.haleconnect.api.user.v1.ApiException)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ExecutionException (java.util.concurrent.ExecutionException)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1