Search in sources :

Example 11 with HaleConnectException

use of eu.esdihumboldt.hale.io.haleconnect.HaleConnectException in project hale by halestudio.

the class HaleConnectLoginHandler method performLogin.

/**
 * Login to hale connect using the credentials entered in the given
 * {@link HaleConnectLoginDialog}.
 *
 * @param loginDialog Login dialog with credentials
 * @return true if successfully logged in
 */
public static boolean performLogin(HaleConnectLoginDialog loginDialog) {
    HaleConnectService hcs = HaleUI.getServiceProvider().getService(HaleConnectService.class);
    String username = loginDialog.getUsername();
    String password = loginDialog.getPassword();
    boolean saveCredentials = loginDialog.isSaveCredentials();
    try {
        if (hcs.login(username, password)) {
            loginDialog.close();
            if (saveCredentials) {
                try {
                    HaleConnectUIPlugin.storeUsername(username);
                    HaleConnectUIPlugin.storePassword(password);
                } catch (StorageException e) {
                    log.error("hale connect credentials could not be saved.", e);
                }
            }
            log.userInfo("Login to hale connect successful.");
            return true;
        } else {
            log.userWarn("Login to hale connect failed, please check the credentials.");
        }
    } catch (HaleConnectException e) {
        log.userError("An error occurred while trying to login to hale connect", e);
    }
    return false;
}
Also used : HaleConnectService(eu.esdihumboldt.hale.io.haleconnect.HaleConnectService) HaleConnectException(eu.esdihumboldt.hale.io.haleconnect.HaleConnectException) StorageException(org.eclipse.equinox.security.storage.StorageException)

Example 12 with HaleConnectException

use of eu.esdihumboldt.hale.io.haleconnect.HaleConnectException in project hale by halestudio.

the class HaleConnectTarget method updateLoginStatus.

private void updateLoginStatus() {
    HaleConnectService hcs = HaleUI.getServiceProvider().getService(HaleConnectService.class);
    boolean loggedIn = hcs.isLoggedIn();
    ownershipGroup.setEnabled(loggedIn);
    enableVersioning.setEnabled(loggedIn);
    publicAccess.setEnabled(loggedIn);
    ownerUser.setEnabled(loggedIn);
    includeWebResources.setEnabled(loggedIn);
    excludeData.setEnabled(loggedIn);
    excludeCachedResources.setEnabled(loggedIn);
    selectProjectButton.setEnabled(loggedIn);
    newProject.setEnabled(loggedIn);
    updateProject.setEnabled(loggedIn);
    if (loggedIn) {
        loginStatusLabel.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_DARK_GREEN));
        loginStatusLabel.setText(MessageFormat.format("Logged in as {0}", hcs.getSession().getUsername()));
        boolean orgAllowed;
        if (hcs.getSession().getOrganisationIds().isEmpty()) {
            orgAllowed = false;
        } else {
            try {
                orgAllowed = hcs.testUserPermission(RESOURCE_TRANSFORMATION_PROJECT, hcs.getSession().getOrganisationIds().iterator().next(), PERMISSION_CREATE);
            } catch (Throwable t) {
                log.userError("A problem occurred while contacting hale connect. Functionality may be limited.", t);
                orgAllowed = false;
            }
        }
        ownerOrg.setEnabled(false);
        orgSelector.getCombo().setEnabled(false);
        if (orgAllowed) {
            final SettableFuture<List<HaleConnectOrganisationInfo>> orgsFuture = SettableFuture.create();
            Futures.addCallback(orgsFuture, new FutureCallback<List<HaleConnectOrganisationInfo>>() {

                @Override
                public void onSuccess(List<HaleConnectOrganisationInfo> result) {
                    PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {

                        @Override
                        public void run() {
                            ownerOrg.setEnabled(true);
                            orgSelector.getCombo().setEnabled(true);
                            orgSelector.setInput(result);
                            if (!result.isEmpty()) {
                                orgSelector.setSelection(new StructuredSelection(result.get(0)));
                            }
                        }
                    });
                }

                @Override
                public void onFailure(Throwable t) {
                    log.userError("A problem occurred while contacting hale connect. Functionality may be limited.", t);
                }
            });
            Thread fetchOrgInfoThread = new Thread(new Runnable() {

                @Override
                public void run() {
                    boolean hadException = false;
                    List<HaleConnectOrganisationInfo> organisations = new ArrayList<>();
                    List<HaleConnectException> exceptions = new ArrayList<>();
                    for (String orgId : hcs.getSession().getOrganisationIds()) {
                        try {
                            HaleConnectOrganisationInfo orgInfo = hcs.getOrganisationInfo(orgId);
                            if (orgInfo != null) {
                                organisations.add(orgInfo);
                            }
                        } catch (HaleConnectException e) {
                            hadException = true;
                            log.error("Error fetching organization details from hale connect.", e);
                            exceptions.add(e);
                            // As a fallback, display dummy value that
                            // contains the
                            // orgId
                            organisations.add(new HaleConnectOrganisationInfo(orgId, MessageFormat.format("<Organisation {0}>", orgId)));
                        }
                    }
                    orgsFuture.set(organisations);
                    if (hadException) {
                        PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {

                            @Override
                            public void run() {
                                log.userError("A problem occurred while contacting hale connect. Functionality may be limited.");
                            }
                        });
                    }
                }
            });
            fetchOrgInfoThread.start();
        }
        boolean userAllowed;
        try {
            userAllowed = hcs.testUserPermission(RESOURCE_TRANSFORMATION_PROJECT, OwnerType.USER.getJsonValue(), PERMISSION_CREATE);
        } catch (Throwable t) {
            log.userError("A problem occurred while contacting hale connect. Functionality may be limited.", t);
            userAllowed = false;
        }
        ownerUser.setEnabled(userAllowed);
        ownerUser.setSelection(userAllowed);
        if (!userAllowed || !orgAllowed) {
            ownerOrg.setSelection(orgAllowed);
        }
        if (!userAllowed && !orgAllowed) {
            loginStatusLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1));
            loginStatusLabel.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_DARK_RED));
            loginStatusLabel.setText("You do not have sufficient permissions to upload transformation projects to hale connect.");
        }
    } else {
        loginStatusLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1));
        loginStatusLabel.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_DARK_RED));
        loginStatusLabel.setText("You are not logged in to hale connect. Please login before sharing a project.");
        ownerOrg.setEnabled(false);
    }
}
Also used : StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) HaleConnectException(eu.esdihumboldt.hale.io.haleconnect.HaleConnectException) HaleConnectOrganisationInfo(eu.esdihumboldt.hale.io.haleconnect.HaleConnectOrganisationInfo) HaleConnectService(eu.esdihumboldt.hale.io.haleconnect.HaleConnectService) GridData(org.eclipse.swt.layout.GridData) List(java.util.List) ArrayList(java.util.ArrayList)

Example 13 with HaleConnectException

use of eu.esdihumboldt.hale.io.haleconnect.HaleConnectException in project hale by halestudio.

the class HaleConnectServiceImpl method uploadProjectFileAsync.

@Override
public ListenableFuture<Boolean> uploadProjectFileAsync(String projectId, Owner owner, File file, ProgressIndicator progress) throws HaleConnectException {
    if (!this.isLoggedIn()) {
        throw new HaleConnectException("Not logged in");
    }
    String apiKey = this.getSession().getToken();
    // PUT /buckets/{ownerType}/{ownerId}/{bucketID}/name
    FilesApi filesApi = ProjectStoreHelper.getFilesApi(this, apiKey);
    // refactor to reuse code in both sync and async methods
    SettableFuture<Boolean> future = SettableFuture.create();
    try {
        // POST /raw
        int totalWork = computeTotalWork(file);
        progress.begin("Uploading project archive", totalWork);
        filesApi.addFilesAsync(owner.getType().getJsonValue(), owner.getId(), projectId, file, createUploadFileCallback(future, progress, file, totalWork));
    } catch (com.haleconnect.api.projectstore.v1.ApiException e) {
        throw new HaleConnectException(e.getMessage(), e);
    }
    return future;
}
Also used : FilesApi(com.haleconnect.api.projectstore.v1.api.FilesApi) HaleConnectException(eu.esdihumboldt.hale.io.haleconnect.HaleConnectException)

Example 14 with HaleConnectException

use of eu.esdihumboldt.hale.io.haleconnect.HaleConnectException 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 15 with HaleConnectException

use of eu.esdihumboldt.hale.io.haleconnect.HaleConnectException in project hale by halestudio.

the class HaleConnectServiceImpl method createProject.

/**
 * @see eu.esdihumboldt.hale.io.haleconnect.HaleConnectService#createProject(java.lang.String,
 *      java.lang.String, eu.esdihumboldt.hale.io.haleconnect.Owner,
 *      boolean)
 */
@Override
public String createProject(String name, String author, Owner owner, boolean versionControl) throws HaleConnectException {
    if (!this.isLoggedIn()) {
        throw new HaleConnectException("Not logged in");
    }
    String apiKey = this.getSession().getToken();
    NewBucket newBucket = new NewBucket();
    newBucket.setName(name);
    newBucket.setVersionControl(versionControl);
    final BucketIdent id;
    try {
        BucketsApi bucketsApi = ProjectStoreHelper.getBucketsApi(this, apiKey);
        // POST /buckets
        id = bucketsApi.createBucketWithOwner(owner.getType().getJsonValue(), owner.getId(), newBucket);
        Owner bucketOwner = UserServiceHelper.toOwner(id.getUserId(), id.getOrgId());
        // PUT /buckets/{ownerType}/{ownerId}/{bucketID}/p/author
        bucketsApi.setBucketProperty(bucketOwner.getType().getJsonValue(), bucketOwner.getId(), id.getTransformationproject(), "author", author);
    } catch (com.haleconnect.api.projectstore.v1.ApiException e) {
        throw new HaleConnectException(e.getMessage(), e);
    }
    return id.getTransformationproject();
}
Also used : BucketIdent(com.haleconnect.api.projectstore.v1.model.BucketIdent) Owner(eu.esdihumboldt.hale.io.haleconnect.Owner) BucketsApi(com.haleconnect.api.projectstore.v1.api.BucketsApi) HaleConnectException(eu.esdihumboldt.hale.io.haleconnect.HaleConnectException) NewBucket(com.haleconnect.api.projectstore.v1.model.NewBucket)

Aggregations

HaleConnectException (eu.esdihumboldt.hale.io.haleconnect.HaleConnectException)19 Feedback (com.haleconnect.api.projectstore.v1.model.Feedback)5 ApiException (com.haleconnect.api.user.v1.ApiException)5 HaleConnectProjectInfo (eu.esdihumboldt.hale.io.haleconnect.HaleConnectProjectInfo)5 HaleConnectService (eu.esdihumboldt.hale.io.haleconnect.HaleConnectService)5 HaleConnectUserInfo (eu.esdihumboldt.hale.io.haleconnect.HaleConnectUserInfo)5 HaleConnectOrganisationInfo (eu.esdihumboldt.hale.io.haleconnect.HaleConnectOrganisationInfo)4 Owner (eu.esdihumboldt.hale.io.haleconnect.Owner)4 BucketsApi (com.haleconnect.api.projectstore.v1.api.BucketsApi)3 FilesApi (com.haleconnect.api.projectstore.v1.api.FilesApi)3 UsersApi (com.haleconnect.api.user.v1.api.UsersApi)3 UserInfo (com.haleconnect.api.user.v1.model.UserInfo)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Map (java.util.Map)3 ApiCallback (com.haleconnect.api.projectstore.v1.ApiCallback)2 PermissionsApi (com.haleconnect.api.projectstore.v1.api.PermissionsApi)2 BucketIdent (com.haleconnect.api.projectstore.v1.model.BucketIdent)2 NewBucket (com.haleconnect.api.projectstore.v1.model.NewBucket)2 LoginApi (com.haleconnect.api.user.v1.api.LoginApi)2