Search in sources :

Example 16 with HaleConnectException

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

the class HaleConnectServiceImpl method getUserInfo.

/**
 * @see eu.esdihumboldt.hale.io.haleconnect.HaleConnectService#getUserInfo(java.lang.String)
 */
@Override
public HaleConnectUserInfo getUserInfo(String userId) throws HaleConnectException {
    if (!this.isLoggedIn()) {
        return null;
    }
    if (!userInfoCache.containsKey(userId)) {
        UsersApi api = UserServiceHelper.getUsersApi(this, this.getSession().getToken());
        try {
            UserInfo info = api.getProfile(userId);
            userInfoCache.put(info.getId(), new HaleConnectUserInfo(info.getId(), info.getScreenName(), info.getFullName()));
        } catch (ApiException e) {
            throw new HaleConnectException(e.getMessage(), e);
        }
    }
    return userInfoCache.get(userId);
}
Also used : UsersApi(com.haleconnect.api.user.v1.api.UsersApi) HaleConnectUserInfo(eu.esdihumboldt.hale.io.haleconnect.HaleConnectUserInfo) UserInfo(com.haleconnect.api.user.v1.model.UserInfo) HaleConnectUserInfo(eu.esdihumboldt.hale.io.haleconnect.HaleConnectUserInfo) HaleConnectException(eu.esdihumboldt.hale.io.haleconnect.HaleConnectException) ApiException(com.haleconnect.api.user.v1.ApiException)

Example 17 with HaleConnectException

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

the class HaleConnectServiceImpl method testUserPermission.

@SuppressWarnings("unchecked")
@Override
public boolean testUserPermission(String resourceType, String role, String permission) throws HaleConnectException {
    com.haleconnect.api.user.v1.api.PermissionsApi api = UserServiceHelper.getPermissionsApi(this, this.getSession().getToken());
    try {
        Map<String, Object> permissions = (Map<String, Object>) api.getResourcePermissionInfo(resourceType, permission);
        if ("user".equals(role)) {
            Object userPermission = permissions.get("user");
            return !"false".equals(userPermission.toString());
        } else {
            // Interpret role as orgId
            Object orgPermission = permissions.get("organisations");
            if (orgPermission instanceof Map) {
                // keySet is set of organisation ids
                Map<String, Object> orgPermissions = (Map<String, Object>) orgPermission;
                Object conditions = Optional.ofNullable(orgPermissions.get(role)).orElse(Collections.EMPTY_LIST);
                if ("false".equals(conditions.toString())) {
                    return false;
                } else if (conditions instanceof List) {
                    return ((List<?>) conditions).stream().anyMatch(cond -> "organisation".equals(cond.toString()));
                }
            }
        }
    } catch (ApiException e) {
        throw new HaleConnectException(e.getMessage(), e);
    }
    return false;
}
Also used : HaleConnectException(eu.esdihumboldt.hale.io.haleconnect.HaleConnectException) StringUtils(org.apache.commons.lang.StringUtils) Credentials(com.haleconnect.api.user.v1.model.Credentials) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ALogger(de.fhg.igd.slf4jplus.ALogger) Feedback(com.haleconnect.api.projectstore.v1.model.Feedback) SettableFuture(com.google.common.util.concurrent.SettableFuture) HaleConnectOrganisationInfo(eu.esdihumboldt.hale.io.haleconnect.HaleConnectOrganisationInfo) MessageFormat(java.text.MessageFormat) ArrayList(java.util.ArrayList) ApiException(com.haleconnect.api.user.v1.ApiException) BucketIdent(com.haleconnect.api.projectstore.v1.model.BucketIdent) SharingOptions(eu.esdihumboldt.hale.io.haleconnect.project.SharingOptions) BucketDetail(com.haleconnect.api.projectstore.v1.model.BucketDetail) NewBucket(com.haleconnect.api.projectstore.v1.model.NewBucket) FilesApi(com.haleconnect.api.projectstore.v1.api.FilesApi) LocatableInputSupplier(eu.esdihumboldt.hale.common.core.io.supplier.LocatableInputSupplier) UsersApi(com.haleconnect.api.user.v1.api.UsersApi) Map(java.util.Map) Owner(eu.esdihumboldt.hale.io.haleconnect.Owner) PermissionsApi(com.haleconnect.api.projectstore.v1.api.PermissionsApi) URI(java.net.URI) OrganisationInfo(com.haleconnect.api.user.v1.model.OrganisationInfo) OrganisationsApi(com.haleconnect.api.user.v1.api.OrganisationsApi) ProgressIndicator(eu.esdihumboldt.hale.common.core.io.ProgressIndicator) ApiCallback(com.haleconnect.api.projectstore.v1.ApiCallback) HaleConnectSession(eu.esdihumboldt.hale.io.haleconnect.HaleConnectSession) UserInfo(com.haleconnect.api.user.v1.model.UserInfo) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Token(com.haleconnect.api.user.v1.model.Token) ALoggerFactory(de.fhg.igd.slf4jplus.ALoggerFactory) HaleConnectProjectInfo(eu.esdihumboldt.hale.io.haleconnect.HaleConnectProjectInfo) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) HaleConnectUserInfo(eu.esdihumboldt.hale.io.haleconnect.HaleConnectUserInfo) File(java.io.File) HaleConnectInputSupplier(eu.esdihumboldt.hale.io.haleconnect.HaleConnectInputSupplier) HaleConnectUrnBuilder(eu.esdihumboldt.hale.io.haleconnect.HaleConnectUrnBuilder) ExecutionException(java.util.concurrent.ExecutionException) AtomicLong(java.util.concurrent.atomic.AtomicLong) LoginApi(com.haleconnect.api.user.v1.api.LoginApi) List(java.util.List) HaleConnectService(eu.esdihumboldt.hale.io.haleconnect.HaleConnectService) HaleConnectServiceListener(eu.esdihumboldt.hale.io.haleconnect.HaleConnectServiceListener) Optional(java.util.Optional) BasePathManager(eu.esdihumboldt.hale.io.haleconnect.BasePathManager) Collections(java.util.Collections) BucketsApi(com.haleconnect.api.projectstore.v1.api.BucketsApi) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) List(java.util.List) HaleConnectException(eu.esdihumboldt.hale.io.haleconnect.HaleConnectException) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ApiException(com.haleconnect.api.user.v1.ApiException)

Example 18 with HaleConnectException

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

the class HaleConnectServiceImpl method processBucketDetail.

@SuppressWarnings("unchecked")
private HaleConnectProjectInfo processBucketDetail(BucketDetail bucket) {
    String author = null;
    Long lastModified = bucket.getLastModified();
    if (bucket.getProperties() instanceof Map<?, ?>) {
        Map<Object, Object> properties = (Map<Object, Object>) bucket.getProperties();
        if (properties.containsKey("author")) {
            author = properties.get("author").toString();
        }
    }
    HaleConnectUserInfo user = null;
    HaleConnectOrganisationInfo org = null;
    try {
        if (!StringUtils.isEmpty(bucket.getId().getUserId())) {
            user = this.getUserInfo(bucket.getId().getUserId());
        }
        if (!StringUtils.isEmpty(bucket.getId().getOrgId())) {
            org = this.getOrganisationInfo(bucket.getId().getOrgId());
        }
    } catch (HaleConnectException e) {
        log.error(e.getMessage(), e);
    }
    return new HaleConnectProjectInfo(bucket.getId().getTransformationproject(), user, org, bucket.getName(), author, lastModified);
}
Also used : HaleConnectOrganisationInfo(eu.esdihumboldt.hale.io.haleconnect.HaleConnectOrganisationInfo) AtomicLong(java.util.concurrent.atomic.AtomicLong) HaleConnectUserInfo(eu.esdihumboldt.hale.io.haleconnect.HaleConnectUserInfo) HaleConnectException(eu.esdihumboldt.hale.io.haleconnect.HaleConnectException) HaleConnectProjectInfo(eu.esdihumboldt.hale.io.haleconnect.HaleConnectProjectInfo) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 19 with HaleConnectException

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

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