Search in sources :

Example 1 with Feedback

use of com.haleconnect.api.projectstore.v1.model.Feedback 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 Feedback

use of com.haleconnect.api.projectstore.v1.model.Feedback in project hale by halestudio.

the class HaleConnectServiceImpl method setProjectSharingOptions.

@Override
public boolean setProjectSharingOptions(String projectId, Owner owner, SharingOptions options) throws HaleConnectException {
    BucketsApi bucketsApi = ProjectStoreHelper.getBucketsApi(this, this.getSession().getToken());
    Feedback feedback;
    try {
        feedback = bucketsApi.setBucketProperty(owner.getType().getJsonValue(), owner.getId(), projectId, "sharingOptions", options);
    } catch (com.haleconnect.api.projectstore.v1.ApiException e) {
        throw new HaleConnectException(e.getMessage(), e);
    }
    if (feedback.getError()) {
        log.error(MessageFormat.format("Error setting sharing options for hale connect project {0}", projectId));
        return false;
    }
    return true;
}
Also used : Feedback(com.haleconnect.api.projectstore.v1.model.Feedback) BucketsApi(com.haleconnect.api.projectstore.v1.api.BucketsApi) HaleConnectException(eu.esdihumboldt.hale.io.haleconnect.HaleConnectException)

Example 3 with Feedback

use of com.haleconnect.api.projectstore.v1.model.Feedback in project hale by halestudio.

the class HaleConnectServiceImpl method setProjectName.

/**
 * @see eu.esdihumboldt.hale.io.haleconnect.HaleConnectService#setProjectName(java.lang.String,
 *      eu.esdihumboldt.hale.io.haleconnect.Owner, java.lang.String)
 */
@Override
public boolean setProjectName(String projectId, Owner owner, String name) throws HaleConnectException {
    // Build custom call because BucketsApi.setProjectName() is broken
    // (does not support plain text body)
    String path = MessageFormat.format("/buckets/{0}/{1}/{2}/name", owner.getType().getJsonValue(), owner.getId(), projectId);
    Feedback feedback = ProjectStoreHelper.executePlainTextCallWithFeedback("PUT", path, name, this, this.getSession().getToken());
    if (feedback.getError()) {
        log.error(MessageFormat.format("Error setting name \"{0}\" for hale connect project {1}", name, projectId));
        return false;
    }
    return true;
}
Also used : Feedback(com.haleconnect.api.projectstore.v1.model.Feedback)

Example 4 with Feedback

use of com.haleconnect.api.projectstore.v1.model.Feedback 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)

Example 5 with Feedback

use of com.haleconnect.api.projectstore.v1.model.Feedback in project hale by halestudio.

the class ProjectStoreHelper method executePlainTextCallWithFeedback.

/**
 * Execute a REST call with the content type <code>text/plain; charset=utf-8
 * </code>
 *
 * @param method HTTP method (POST, PUT)
 * @param path REST path (without base path)
 * @param body The plain text bdoy
 * @param basePath The REST base path
 * @param apiKey The API key
 * @return the feedback
 * @throws HaleConnectException thrown on any API error
 */
public static Feedback executePlainTextCallWithFeedback(String method, String path, String body, BasePathResolver basePath, String apiKey) throws HaleConnectException {
    ApiClient apiClient = ProjectStoreHelper.getApiClient(basePath, apiKey);
    OkHttpClient httpClient = apiClient.getHttpClient();
    String url = apiClient.buildUrl(path, null);
    Request.Builder reqBuilder = new Request.Builder().url(url);
    Map<String, String> headerParams = new HashMap<String, String>();
    apiClient.updateParamsForAuth(new String[] { "bearer" }, null, headerParams);
    apiClient.processHeaderParams(headerParams, reqBuilder);
    RequestBody reqBody = RequestBody.create(MediaType.parse("text/plain; charset=utf-8"), body);
    Request request = reqBuilder.method(method, reqBody).build();
    Call call = httpClient.newCall(request);
    Feedback feedback;
    try {
        ApiResponse<Feedback> resp = apiClient.execute(call, new TypeToken<Feedback>() {
        }.getType());
        feedback = resp.getData();
    } catch (com.haleconnect.api.projectstore.v1.ApiException e) {
        throw new HaleConnectException(e.getMessage(), e);
    }
    return feedback;
}
Also used : Call(com.squareup.okhttp.Call) OkHttpClient(com.squareup.okhttp.OkHttpClient) HashMap(java.util.HashMap) Request(com.squareup.okhttp.Request) HaleConnectException(eu.esdihumboldt.hale.io.haleconnect.HaleConnectException) ApiClient(com.haleconnect.api.projectstore.v1.ApiClient) Feedback(com.haleconnect.api.projectstore.v1.model.Feedback) TypeToken(com.google.gson.reflect.TypeToken) RequestBody(com.squareup.okhttp.RequestBody)

Aggregations

Feedback (com.haleconnect.api.projectstore.v1.model.Feedback)5 HaleConnectException (eu.esdihumboldt.hale.io.haleconnect.HaleConnectException)4 TypeToken (com.google.gson.reflect.TypeToken)1 ApiCallback (com.haleconnect.api.projectstore.v1.ApiCallback)1 ApiClient (com.haleconnect.api.projectstore.v1.ApiClient)1 BucketsApi (com.haleconnect.api.projectstore.v1.api.BucketsApi)1 FilesApi (com.haleconnect.api.projectstore.v1.api.FilesApi)1 ApiException (com.haleconnect.api.user.v1.ApiException)1 Call (com.squareup.okhttp.Call)1 OkHttpClient (com.squareup.okhttp.OkHttpClient)1 Request (com.squareup.okhttp.Request)1 RequestBody (com.squareup.okhttp.RequestBody)1 HashMap (java.util.HashMap)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