Search in sources :

Example 6 with HaleConnectException

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

the class HaleConnectServiceImpl method getOrganisationInfo.

/**
 * @see eu.esdihumboldt.hale.io.haleconnect.HaleConnectService#getOrganisationInfo(java.lang.String)
 */
@Override
public HaleConnectOrganisationInfo getOrganisationInfo(String orgId) throws HaleConnectException {
    if (!this.isLoggedIn()) {
        return null;
    }
    if (!orgInfoCache.containsKey(orgId)) {
        OrganisationsApi api = UserServiceHelper.getOrganisationsApi(this, this.getSession().getToken());
        try {
            OrganisationInfo org = api.getOrganisation(orgId);
            orgInfoCache.put(org.getId(), new HaleConnectOrganisationInfo(org.getId(), org.getName()));
        } catch (ApiException e) {
            throw new HaleConnectException(e.getMessage(), e);
        }
    }
    return orgInfoCache.get(orgId);
}
Also used : HaleConnectOrganisationInfo(eu.esdihumboldt.hale.io.haleconnect.HaleConnectOrganisationInfo) OrganisationInfo(com.haleconnect.api.user.v1.model.OrganisationInfo) HaleConnectOrganisationInfo(eu.esdihumboldt.hale.io.haleconnect.HaleConnectOrganisationInfo) HaleConnectException(eu.esdihumboldt.hale.io.haleconnect.HaleConnectException) OrganisationsApi(com.haleconnect.api.user.v1.api.OrganisationsApi) ApiException(com.haleconnect.api.user.v1.ApiException)

Example 7 with HaleConnectException

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

the class HaleConnectServiceImpl method loadProject.

/**
 * @see eu.esdihumboldt.hale.io.haleconnect.HaleConnectService#loadProject(Owner,
 *      String)
 */
@Override
public LocatableInputSupplier<InputStream> loadProject(Owner owner, String projectId) throws HaleConnectException {
    if (!isLoggedIn()) {
        throw new IllegalStateException("Not logged in.");
    }
    URI location = HaleConnectUrnBuilder.buildProjectUrn(owner, projectId);
    HaleConnectProjectInfo projectInfo = getProject(owner, projectId);
    if (projectInfo == null) {
        throw new HaleConnectException(MessageFormat.format("Project does not exist: {0}", location.toString()));
    }
    return new HaleConnectInputSupplier(location, this.getSession().getToken(), this);
}
Also used : HaleConnectInputSupplier(eu.esdihumboldt.hale.io.haleconnect.HaleConnectInputSupplier) HaleConnectProjectInfo(eu.esdihumboldt.hale.io.haleconnect.HaleConnectProjectInfo) HaleConnectException(eu.esdihumboldt.hale.io.haleconnect.HaleConnectException) URI(java.net.URI)

Example 8 with HaleConnectException

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

the class HaleConnectServiceImpl method login.

/**
 * @see eu.esdihumboldt.hale.io.haleconnect.HaleConnectService#login(java.lang.String,
 *      java.lang.String)
 */
@Override
public boolean login(String username, String password) throws HaleConnectException {
    LoginApi loginApi = UserServiceHelper.getLoginApi(this);
    Credentials credentials = UserServiceHelper.buildCredentials(username, password);
    try {
        Token token = loginApi.login(credentials);
        if (token != null) {
            UsersApi usersApi = UserServiceHelper.getUsersApi(this, token.getToken());
            // First get the current user's profile to obtain the user ID
            // required to fetch the extended profile (including the user's
            // roles/organisations) in the next step
            UserInfo shortProfile = usersApi.getProfileOfCurrentUser();
            session = new HaleConnectSessionImpl(username, token.getToken(), usersApi.getProfile(shortProfile.getId()));
            notifyLoginStateChanged();
        } else {
            clearSession();
        }
    } catch (ApiException e) {
        if (e.getCode() == 401) {
            clearSession();
        } else {
            throw new HaleConnectException(e.getMessage(), e);
        }
    }
    return isLoggedIn();
}
Also used : UsersApi(com.haleconnect.api.user.v1.api.UsersApi) Token(com.haleconnect.api.user.v1.model.Token) UserInfo(com.haleconnect.api.user.v1.model.UserInfo) HaleConnectUserInfo(eu.esdihumboldt.hale.io.haleconnect.HaleConnectUserInfo) HaleConnectException(eu.esdihumboldt.hale.io.haleconnect.HaleConnectException) LoginApi(com.haleconnect.api.user.v1.api.LoginApi) Credentials(com.haleconnect.api.user.v1.model.Credentials) ApiException(com.haleconnect.api.user.v1.ApiException)

Example 9 with HaleConnectException

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

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

the class HaleConnectProjectWriter method execute.

@Override
protected IOReport execute(ProgressIndicator progress, IOReporter reporter) throws IOProviderConfigurationException, IOException {
    if (!haleConnect.isLoggedIn()) {
        reporter.error("Must be logged in to hale connect to upload project.");
        reporter.setSuccess(false);
        return reporter;
    }
    URI location = null;
    if (getTarget().getLocation() != null) {
        location = getTarget().getLocation();
    }
    progress.begin("Saving project to hale connect", ProgressIndicator.UNKNOWN);
    Project project = getProject();
    URI projectUrn;
    if (location == null) {
        // was not shared before or creation of new project requested by
        // user
        boolean enableVersioning = getParameter(ENABLE_VERSIONING).as(Boolean.class);
        boolean publicAccess = getParameter(SHARING_PUBLIC).as(Boolean.class);
        String ownerTypeParameter = getParameter(OWNER_TYPE).as(String.class);
        OwnerType ownerType;
        try {
            ownerType = OwnerType.fromJsonValue(ownerTypeParameter);
        } catch (IllegalArgumentException e) {
            throw new IOProviderConfigurationException(MessageFormat.format("Invalid owner type: {0}", ownerTypeParameter), e);
        }
        String ownerId;
        switch(ownerType) {
            case USER:
                ownerId = haleConnect.getSession().getUserId();
                break;
            case ORGANISATION:
                if (haleConnect.getSession().getOrganisationIds().isEmpty()) {
                    throw new IOProviderConfigurationException(MessageFormat.format("Owner type is set to ORGANISATION but user \"{0}\" is not associated with any organisation", haleConnect.getSession().getUsername()));
                }
                ownerId = haleConnect.getSession().getOrganisationIds().iterator().next();
                break;
            default:
                throw new IOProviderConfigurationException(MessageFormat.format("Unknown owner type: {0}", ownerType));
        }
        Owner owner = new Owner(ownerType, ownerId);
        String projectId;
        try {
            projectId = haleConnect.createProject(project.getName(), project.getAuthor(), owner, enableVersioning);
            haleConnect.setProjectSharingOptions(projectId, owner, new SharingOptions(publicAccess));
        } catch (HaleConnectException e) {
            reporter.error("Error creating hale connect project", e);
            reporter.setSuccess(false);
            return reporter;
        }
        projectUrn = HaleConnectUrnBuilder.buildProjectUrn(owner, projectId);
        if (reporter instanceof MutableTargetIOReport) {
            ((MutableTargetIOReport) reporter).setTarget(new LocatableURI(prettifyTarget(projectUrn)));
        }
    } else if (!HaleConnectUrnBuilder.isValidProjectUrn(location)) {
        throw new IOProviderConfigurationException(MessageFormat.format("Cannot write to location: {0}", location.toString()));
    } else {
        projectUrn = location;
        writerMode = ProjectWriterMode.SAVE;
    }
    this.setTarget(new NoStreamOutputSupplier(projectUrn));
    // save the hale connect project URN in the project properties
    getProject().getProperties().put(HaleConnectProjectReader.HALECONNECT_URN_PROPERTY, Value.of(projectUrn.toString()));
    // redirect project archive to temporary local file
    File projectArchive = Files.createTempFile("hc-arc", ".zip").toFile();
    IOReport report;
    try (final FileOutputStream archiveStream = new FileOutputStream(projectArchive)) {
        report = createProjectArchive(archiveStream, reporter, progress);
    }
    if (!report.isSuccess()) {
        // exit when creating project archive failed
        return report;
    }
    String projectId = HaleConnectUrnBuilder.extractProjectId(projectUrn);
    Owner owner = HaleConnectUrnBuilder.extractProjectOwner(projectUrn);
    boolean result;
    try {
        result = haleConnect.uploadProjectFile(projectId, owner, projectArchive, progress);
    } catch (HaleConnectException e) {
        switch(e.getStatusCode()) {
            case 403:
                /* Forbidden */
                reporter.error(MessageFormat.format("You are not authorized to access project {0}", projectId), e);
                break;
            default:
                reporter.error(MessageFormat.format("Error uploading hale connect project: {0}", e.getMessage()), e);
        }
        reporter.setSuccess(false);
        return reporter;
    }
    // name
    try {
        haleConnect.setProjectName(projectId, owner, project.getName());
    } catch (HaleConnectException e) {
        // This is non-fatal
        log.warn(MessageFormat.format("Unable to update project bucket name for project {0}: {1}", HaleConnectUrnBuilder.buildProjectUrn(owner, projectId).toString(), e.getMessage()), e);
    }
    try {
        HaleConnectProjectInfo hcProjectInfo = haleConnect.getProject(owner, projectId);
        if (hcProjectInfo != null) {
            getProject().getProperties().put(HaleConnectProjectReader.HALECONNECT_LAST_MODIFIED_PROPERTY, Value.of(hcProjectInfo.getLastModified()));
        }
    } catch (HaleConnectException e) {
        // This is non-fatal
        log.warn(MessageFormat.format("Unable to get lastUpdated property for project {0}: {1}", HaleConnectUrnBuilder.buildProjectUrn(owner, projectId).toString(), e.getMessage()), e);
    }
    this.clientAccessUrl = HaleConnectUrnBuilder.buildClientAccessUrl(haleConnect.getBasePathManager().getBasePath(HaleConnectServices.WEB_CLIENT), owner, projectId);
    this.projectUri = HaleConnectUrnBuilder.buildProjectUrn(owner, projectId);
    reporter.setSuccess(result);
    return reporter;
}
Also used : Owner(eu.esdihumboldt.hale.io.haleconnect.Owner) LocatableURI(eu.esdihumboldt.hale.common.core.io.supplier.LocatableURI) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport) MutableTargetIOReport(eu.esdihumboldt.hale.common.core.io.report.MutableTargetIOReport) HaleConnectException(eu.esdihumboldt.hale.io.haleconnect.HaleConnectException) HaleConnectProjectInfo(eu.esdihumboldt.hale.io.haleconnect.HaleConnectProjectInfo) URI(java.net.URI) LocatableURI(eu.esdihumboldt.hale.common.core.io.supplier.LocatableURI) Project(eu.esdihumboldt.hale.common.core.io.project.model.Project) OwnerType(eu.esdihumboldt.hale.io.haleconnect.OwnerType) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) FileOutputStream(java.io.FileOutputStream) NoStreamOutputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.NoStreamOutputSupplier) File(java.io.File) MutableTargetIOReport(eu.esdihumboldt.hale.common.core.io.report.MutableTargetIOReport)

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