Search in sources :

Example 1 with SynapseException

use of org.sagebionetworks.client.exceptions.SynapseException in project BridgeServer2 by Sage-Bionetworks.

the class Exporter3Service method initExporter3.

/**
 * Initializes configs and Synapse resources for Exporter 3.0. Note that if any config already exists, this API
 * will simply ignore them. This allows for two notable scenarios
 * (a) Advanced users can use existing projects or data access teams for Exporter 3.0.
 * (b) If in the future, we add something new (like a notification queue, or a default view), we can re-run this
 * API to create the new stuff without affecting the old stuff.
 */
public Exporter3Configuration initExporter3(String appId) throws BridgeSynapseException, IOException, SynapseException {
    boolean isAppModified = false;
    App app = appService.getApp(appId);
    // Init the Exporter3Config object.
    Exporter3Configuration ex3Config = app.getExporter3Configuration();
    if (ex3Config == null) {
        ex3Config = new Exporter3Configuration();
        app.setExporter3Configuration(ex3Config);
        isAppModified = true;
    }
    // Name in Synapse are globally unique, so we add a random token to the name to ensure it
    // doesn't conflict with an existing name. Also, Synapse names can only contain a certain
    // subset of characters. We've verified this name is acceptable for this transformation.
    String synapseName = BridgeUtils.toSynapseFriendlyName(app.getName());
    String nameScopingToken = getNameScopingToken();
    // Create data access team.
    Long dataAccessTeamId = ex3Config.getDataAccessTeamId();
    if (dataAccessTeamId == null) {
        Team team = new Team();
        team.setName(synapseName + " Access Team " + nameScopingToken);
        team = synapseHelper.createTeamWithRetry(team);
        dataAccessTeamId = Long.parseLong(team.getId());
        LOG.info("Created Synapse team " + dataAccessTeamId);
        ex3Config.setDataAccessTeamId(dataAccessTeamId);
        isAppModified = true;
    }
    Set<Long> adminPrincipalIds = ImmutableSet.of(exporterSynapseId, bridgeAdminTeamId);
    Set<Long> readOnlyPrincipalIds = ImmutableSet.of(bridgeStaffTeamId, dataAccessTeamId);
    // Create project.
    String projectId = ex3Config.getProjectId();
    if (projectId == null) {
        Project project = new Project();
        project.setName(synapseName + " Project " + nameScopingToken);
        project = synapseHelper.createEntityWithRetry(project);
        projectId = project.getId();
        LOG.info("Created Synapse project " + projectId);
        // Create ACLs for project.
        synapseHelper.createAclWithRetry(projectId, adminPrincipalIds, readOnlyPrincipalIds);
        ex3Config.setProjectId(projectId);
        isAppModified = true;
        // We also need to add this project to the tracking view.
        if (StringUtils.isNotBlank(synapseTrackingViewId)) {
            try {
                EntityView view = synapseHelper.getEntityWithRetry(synapseTrackingViewId, EntityView.class);
                if (view != null) {
                    // For whatever reason, view.getScopes() doesn't include the "syn" prefix.
                    view.getScopeIds().add(projectId.substring(3));
                    synapseHelper.updateEntityWithRetry(view);
                }
            } catch (SynapseException ex) {
                LOG.error("Error adding new project " + projectId + " to tracking view " + synapseTrackingViewId + ": " + ex.getMessage(), ex);
            }
        }
    }
    // Create Participant Version Table.
    String participantVersionTableId = ex3Config.getParticipantVersionTableId();
    if (participantVersionTableId == null) {
        participantVersionTableId = synapseHelper.createTableWithColumnsAndAcls(PARTICIPANT_VERSION_COLUMN_MODELS, readOnlyPrincipalIds, adminPrincipalIds, projectId, TABLE_NAME_PARTICIPANT_VERSIONS);
        LOG.info("Created Synapse table " + participantVersionTableId);
        ex3Config.setParticipantVersionTableId(participantVersionTableId);
        isAppModified = true;
    }
    // Create Folder for raw data.
    String rawDataFolderId = ex3Config.getRawDataFolderId();
    if (rawDataFolderId == null) {
        Folder folder = new Folder();
        folder.setName(FOLDER_NAME_BRIDGE_RAW_DATA);
        folder.setParentId(projectId);
        folder = synapseHelper.createEntityWithRetry(folder);
        rawDataFolderId = folder.getId();
        LOG.info("Created Synapse folder " + rawDataFolderId);
        // Create ACLs for folder. This is a separate ACL because we don't want to allow people to modify the
        // raw data.
        synapseHelper.createAclWithRetry(rawDataFolderId, adminPrincipalIds, readOnlyPrincipalIds);
        ex3Config.setRawDataFolderId(rawDataFolderId);
        isAppModified = true;
    }
    // Create storage location.
    Long storageLocationId = ex3Config.getStorageLocationId();
    if (storageLocationId == null) {
        // Create owner.txt so that we can create the storage location.
        s3Helper.writeLinesToS3(rawHealthDataBucket, appId + "/owner.txt", ImmutableList.of(exporterSynapseUser));
        // Create storage location.
        ExternalS3StorageLocationSetting storageLocation = new ExternalS3StorageLocationSetting();
        storageLocation.setBaseKey(appId);
        storageLocation.setBucket(rawHealthDataBucket);
        storageLocation.setStsEnabled(true);
        storageLocation = synapseHelper.createStorageLocationForEntity(rawDataFolderId, storageLocation);
        storageLocationId = storageLocation.getStorageLocationId();
        LOG.info("Created Synapse storage location " + storageLocationId);
        ex3Config.setStorageLocationId(storageLocationId);
        isAppModified = true;
    }
    // Finally, enable Exporter 3.0 if it's not already enabled.
    if (!app.isExporter3Enabled()) {
        app.setExporter3Enabled(true);
        isAppModified = true;
    }
    // exporter3config and exporter3enabled.
    if (isAppModified) {
        appService.updateApp(app, true);
    }
    return ex3Config;
}
Also used : App(org.sagebionetworks.bridge.models.apps.App) Project(org.sagebionetworks.repo.model.Project) ExternalS3StorageLocationSetting(org.sagebionetworks.repo.model.project.ExternalS3StorageLocationSetting) BridgeSynapseException(org.sagebionetworks.bridge.exceptions.BridgeSynapseException) SynapseException(org.sagebionetworks.client.exceptions.SynapseException) EntityView(org.sagebionetworks.repo.model.table.EntityView) Team(org.sagebionetworks.repo.model.Team) Exporter3Configuration(org.sagebionetworks.bridge.models.apps.Exporter3Configuration) Folder(org.sagebionetworks.repo.model.Folder)

Example 2 with SynapseException

use of org.sagebionetworks.client.exceptions.SynapseException in project BridgeServer2 by Sage-Bionetworks.

the class AppAndUsersValidator method validate.

@Override
public void validate(Object object, Errors errors) {
    AppAndUsers appAndUsers = (AppAndUsers) object;
    App app = appAndUsers.getApp();
    if (app == null) {
        errors.rejectValue("app", "cannot be null");
        return;
    } else {
        errors.pushNestedPath("app");
        try {
            BridgeUtils.toSynapseFriendlyName(app.getName());
        } catch (NullPointerException | IllegalArgumentException e) {
            errors.rejectValue("name", "is an invalid Synapse project name");
        }
        if (StringUtils.isBlank(app.getSponsorName())) {
            errors.rejectValue("sponsorName", "is required");
        }
        if (StringUtils.isBlank(app.getIdentifier())) {
            errors.rejectValue("identifier", "is required");
        }
        errors.popNestedPath();
    }
    StudyParticipantValidator participantValidator = new StudyParticipantValidator(studyService, organizationService, app, true);
    List<String> adminIds = appAndUsers.getAdminIds();
    if (adminIds == null || adminIds.isEmpty()) {
        errors.rejectValue("adminIds", "are required");
    } else {
        for (int i = 0; i < adminIds.size(); i++) {
            String adminId = adminIds.get(i);
            errors.pushNestedPath("adminIds[" + i + "]");
            if (isBlank(adminId)) {
                errors.rejectValue("", "cannot be blank or null");
            }
            try {
                synapseClient.getUserProfile(adminId);
            } catch (SynapseNotFoundException e) {
                errors.rejectValue("", "is invalid");
            } catch (SynapseException se) {
                throw new RuntimeException(se);
            }
            errors.popNestedPath();
        }
    }
    List<StudyParticipant> users = appAndUsers.getUsers();
    if (users == null || users.isEmpty()) {
        errors.rejectValue("users", "are required");
    } else {
        for (int i = 0; i < users.size(); i++) {
            StudyParticipant user = users.get(i);
            errors.pushNestedPath("users[" + i + "]");
            if (isBlank(user.getSynapseUserId())) {
                errors.rejectValue("synapseUserId", "cannot be blank");
            } else {
                try {
                    synapseClient.getUserProfile(user.getSynapseUserId());
                } catch (SynapseNotFoundException e) {
                    errors.rejectValue("synapseUserId", "is invalid");
                } catch (SynapseException se) {
                    throw new RuntimeException(se);
                }
            }
            // validate roles for each user
            if (user.getRoles() == null || user.getRoles().isEmpty()) {
                errors.rejectValue("roles", "should have at least one role");
            } else if (!Collections.disjoint(user.getRoles(), ImmutableSet.of(ADMIN, WORKER, SUPERADMIN))) {
                errors.rejectValue("roles", "can only have roles developer and/or researcher");
            }
            // validate the user
            participantValidator.validate(user, errors);
            errors.popNestedPath();
        }
    }
}
Also used : App(org.sagebionetworks.bridge.models.apps.App) SynapseException(org.sagebionetworks.client.exceptions.SynapseException) StudyParticipant(org.sagebionetworks.bridge.models.accounts.StudyParticipant) AppAndUsers(org.sagebionetworks.bridge.models.apps.AppAndUsers) SynapseNotFoundException(org.sagebionetworks.client.exceptions.SynapseNotFoundException)

Example 3 with SynapseException

use of org.sagebionetworks.client.exceptions.SynapseException in project Synapse-Repository-Services by Sage-Bionetworks.

the class DataUploaderRESTDocumentationGenerator method uploadDataMultiPart.

@Override
public void uploadDataMultiPart(S3Token s3Token, File dataFile) throws SynapseException {
    byte[] encoded;
    String base64Md5;
    try {
        encoded = Base64.encodeBase64(Hex.decodeHex(s3Token.getMd5().toCharArray()));
        base64Md5 = new String(encoded, "ASCII");
    } catch (Exception e) {
        throw new SynapseException(e);
    }
    Map<String, String> headerMap = new HashMap<String, String>();
    headerMap.put("x-amz-acl", "bucket-owner-full-control");
    headerMap.put("Content-MD5", base64Md5);
    headerMap.put("Content-Type", s3Token.getContentType());
    String curl = "curl -i ";
    for (Entry<String, String> header : headerMap.entrySet()) {
        curl += " -H " + header.getKey() + ":" + header.getValue();
    }
    curl += " '" + s3Token.getPresignedUrl() + "'";
    if (markup.equals(SynapseRESTDocumentationGenerator.MARKUP.WIKI)) {
        log.info("*Request* {code}" + curl + "{code}");
        log.info("*Response* {code}");
    } else if (markup.equals(SynapseRESTDocumentationGenerator.MARKUP.HTML)) {
        log.info("<br><span class=\"request\">Request</span> <pre>" + curl + "</pre><br>");
        log.info("<span class=\"response\">Response</span> <pre>");
    } else {
        log.info("REQUEST " + curl + "");
        log.info("RESPONSE");
    }
    super.uploadDataSingle(s3Token, dataFile);
    String response = "";
    if (markup.equals(SynapseRESTDocumentationGenerator.MARKUP.WIKI)) {
        log.info(response + "{code}");
    } else if (markup.equals(SynapseRESTDocumentationGenerator.MARKUP.HTML)) {
        log.info(response + "</pre><br>");
    } else {
        log.info(response);
        log.info("");
    }
}
Also used : SynapseException(org.sagebionetworks.client.exceptions.SynapseException) HashMap(java.util.HashMap) SynapseException(org.sagebionetworks.client.exceptions.SynapseException)

Example 4 with SynapseException

use of org.sagebionetworks.client.exceptions.SynapseException in project Synapse-Repository-Services by Sage-Bionetworks.

the class Synapse method updateSynapseEntity.

/**
 * Update a dataset, layer, preview, annotations, etc...
 *
 * This convenience method first grabs a copy of the currently stored
 * entity, then overwrites fields from the entity passed in on top of the
 * stored entity we retrieved and then PUTs the entity. This essentially
 * does a partial update from the point of view of the user of this API.
 *
 * Note that users of this API may want to inspect what they are overwriting
 * before they do so. Another approach would be to do a GET, display the
 * field to the user, allow them to edit the fields, and then do a PUT.
 *
 * @param endpoint
 * @param uri
 * @param entity
 * @return the updated entity
 * @throws SynapseException
 */
@SuppressWarnings("unchecked")
public JSONObject updateSynapseEntity(String endpoint, String uri, JSONObject entity) throws SynapseException {
    JSONObject storedEntity = getSynapseEntity(endpoint, uri);
    boolean isAnnotation = uri.endsWith(ANNOTATION_URI_SUFFIX);
    try {
        Iterator<String> keyIter = entity.keys();
        while (keyIter.hasNext()) {
            String key = keyIter.next();
            if (isAnnotation) {
                // Annotations need to go one level deeper
                JSONObject storedAnnotations = storedEntity.getJSONObject(key);
                JSONObject entityAnnotations = entity.getJSONObject(key);
                Iterator<String> annotationIter = entity.getJSONObject(key).keys();
                while (annotationIter.hasNext()) {
                    String annotationKey = annotationIter.next();
                    storedAnnotations.put(annotationKey, entityAnnotations.get(annotationKey));
                }
            } else {
                storedEntity.put(key, entity.get(key));
            }
        }
        return putSynapseEntity(endpoint, uri, storedEntity);
    } catch (JSONException e) {
        throw new SynapseException(e);
    }
}
Also used : JSONObject(org.json.JSONObject) SynapseException(org.sagebionetworks.client.exceptions.SynapseException) JSONException(org.json.JSONException)

Example 5 with SynapseException

use of org.sagebionetworks.client.exceptions.SynapseException in project Synapse-Repository-Services by Sage-Bionetworks.

the class Synapse method downloadLocationableFromSynapse.

/**
 * Download the locationable to a tempfile
 *
 * @param locationable
 * @return destination file
 * @throws SynapseException
 * @throws SynapseUserException
 */
public File downloadLocationableFromSynapse(Locationable locationable) throws SynapseException {
    // TODO do the equivalent of the R client synapse cache and file naming
    // scheme
    File file;
    try {
        // from the Java doc
        // prefix - The prefix string to be used in generating the file's name; must be at least three characters long
        String prefix = locationable.getId();
        if (prefix.length() < 3) {
            prefix = "000".substring(prefix.length()) + prefix;
        }
        file = File.createTempFile(prefix, ".txt");
        return downloadLocationableFromSynapse(locationable, file);
    } catch (IOException e) {
        throw new SynapseException(e);
    }
}
Also used : SynapseException(org.sagebionetworks.client.exceptions.SynapseException) IOException(java.io.IOException) File(java.io.File)

Aggregations

SynapseException (org.sagebionetworks.client.exceptions.SynapseException)40 JSONObject (org.json.JSONObject)23 JSONObjectAdapterException (org.sagebionetworks.schema.adapter.JSONObjectAdapterException)19 JSONObjectAdapter (org.sagebionetworks.schema.adapter.JSONObjectAdapter)9 JSONObjectAdapterImpl (org.sagebionetworks.schema.adapter.org.json.JSONObjectAdapterImpl)9 HashMap (java.util.HashMap)6 JSONException (org.json.JSONException)6 PaginatedResults (org.sagebionetworks.repo.model.PaginatedResults)5 VariableContentPaginatedResults (org.sagebionetworks.repo.model.VariableContentPaginatedResults)5 UserProfile (org.sagebionetworks.repo.model.UserProfile)3 File (java.io.File)2 IOException (java.io.IOException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 Test (org.junit.Test)2 App (org.sagebionetworks.bridge.models.apps.App)2 AccessRequirement (org.sagebionetworks.repo.model.AccessRequirement)2 AWSCredentials (com.amazonaws.auth.AWSCredentials)1 BasicSessionCredentials (com.amazonaws.auth.BasicSessionCredentials)1 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)1 PutObjectRequest (com.amazonaws.services.s3.model.PutObjectRequest)1