Search in sources :

Example 1 with NotFoundException

use of org.sagebionetworks.bridge.exceptions.NotFoundException in project BridgeServer2 by Sage-Bionetworks.

the class UploadService method uploadComplete.

public void uploadComplete(String appId, UploadCompletionClient completedBy, Upload upload, boolean redrive) {
    String uploadId = upload.getUploadId();
    // We don't want to kick off upload validation on an upload that already has upload validation.
    if (!upload.canBeValidated() && !redrive) {
        logger.info(String.format("uploadComplete called for upload %s, which is already complete", uploadId));
        return;
    }
    final String objectId = upload.getObjectId();
    ObjectMetadata obj;
    try {
        Stopwatch stopwatch = Stopwatch.createStarted();
        obj = s3Client.getObjectMetadata(uploadBucket, objectId);
        logger.info("Finished getting S3 metadata for bucket " + uploadBucket + " key " + objectId + " in " + stopwatch.elapsed(TimeUnit.MILLISECONDS) + " ms");
    } catch (AmazonS3Exception ex) {
        if (ex.getStatusCode() == 404) {
            throw new NotFoundException(ex);
        } else {
            // Only S3 404s are mapped to 404s. Everything else is an internal server error.
            throw new BridgeServiceException(ex);
        }
    }
    String sse = obj.getSSEAlgorithm();
    if (!AES_256_SERVER_SIDE_ENCRYPTION.equals(sse)) {
        logger.error("Missing S3 server-side encryption (SSE) for presigned upload " + uploadId + ".");
    }
    try {
        uploadDao.uploadComplete(completedBy, upload);
    } catch (ConcurrentModificationException ex) {
        // The old workflow is the app calls uploadComplete. The new workflow has an S3 trigger to call
        // uploadComplete. During the transition, it's very likely that this will be called twice, sometimes
        // concurrently. As such, we should log and squelch the ConcurrentModificationException.
        logger.info("Concurrent modification of upload " + uploadId + " while marking upload complete");
        // and duplicate records.
        return;
    }
    // kick off upload validation
    App app = appService.getApp(appId);
    if (BridgeUtils.isExporter3Configured(app)) {
        exporter3Service.completeUpload(app, upload);
    }
    // For backwards compatibility, always call Legacy Exporter 2.0. In the future, we may introduce a setting to
    // disable this for new apps.
    uploadValidationService.validateUpload(appId, upload);
}
Also used : App(org.sagebionetworks.bridge.models.apps.App) ConcurrentModificationException(org.sagebionetworks.bridge.exceptions.ConcurrentModificationException) Stopwatch(com.google.common.base.Stopwatch) BridgeServiceException(org.sagebionetworks.bridge.exceptions.BridgeServiceException) NotFoundException(org.sagebionetworks.bridge.exceptions.NotFoundException) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata)

Example 2 with NotFoundException

use of org.sagebionetworks.bridge.exceptions.NotFoundException in project BridgeServer2 by Sage-Bionetworks.

the class HealthDataService method updateRecordsWithExporterStatus.

/**
 * returns received list of record Ids after updating
 * @param recordExportStatusRequest
 *         POJO contains: a lit of health record ids, not upload ids and
 *         an Synapse Exporter Status with value either NOT_EXPORTED or SUCCEEDED
 * @return updated health record ids list
 */
public List<String> updateRecordsWithExporterStatus(RecordExportStatusRequest recordExportStatusRequest) {
    Validate.entityThrowingException(exporterStatusValidator, recordExportStatusRequest);
    List<String> healthRecordIds = recordExportStatusRequest.getRecordIds();
    HealthDataRecord.ExporterStatus synapseExporterStatus = recordExportStatusRequest.getSynapseExporterStatus();
    if (healthRecordIds.size() > MAX_NUM_RECORD_IDS) {
        throw new BadRequestException("Size of the record ids list exceeds the limit.");
    }
    List<String> updatedRecordIds = healthRecordIds.stream().map(id -> {
        HealthDataRecord record = getRecordById(id);
        if (record == null) {
            throw new NotFoundException("The record: " + id + " cannot be found in our database.");
        }
        record.setSynapseExporterStatus(synapseExporterStatus);
        return createOrUpdateRecord(record);
    }).collect(Collectors.toList());
    return updatedRecordIds;
}
Also used : StudyParticipant(org.sagebionetworks.bridge.models.accounts.StudyParticipant) BadRequestException(org.sagebionetworks.bridge.exceptions.BadRequestException) Validator(org.springframework.validation.Validator) Validate(org.sagebionetworks.bridge.validators.Validate) RecordExportStatusRequestValidator(org.sagebionetworks.bridge.validators.RecordExportStatusRequestValidator) CANNOT_BE_BLANK(org.sagebionetworks.bridge.BridgeConstants.CANNOT_BE_BLANK) HealthDataRecord(org.sagebionetworks.bridge.models.healthdata.HealthDataRecord) Autowired(org.springframework.beans.factory.annotation.Autowired) UploadSchema(org.sagebionetworks.bridge.models.upload.UploadSchema) UploadUtil(org.sagebionetworks.bridge.upload.UploadUtil) StringUtils(org.apache.commons.lang3.StringUtils) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) BridgeObjectMapper(org.sagebionetworks.bridge.json.BridgeObjectMapper) HealthDataDao(org.sagebionetworks.bridge.dao.HealthDataDao) UploadFieldType(org.sagebionetworks.bridge.models.upload.UploadFieldType) Map(java.util.Map) JsonNode(com.fasterxml.jackson.databind.JsonNode) CANNOT_BE_NULL(org.sagebionetworks.bridge.BridgeConstants.CANNOT_BE_NULL) GuidCreatedOnVersionHolder(org.sagebionetworks.bridge.models.GuidCreatedOnVersionHolder) StrictValidationHandler(org.sagebionetworks.bridge.upload.StrictValidationHandler) Charsets(com.google.common.base.Charsets) NotFoundException(org.sagebionetworks.bridge.exceptions.NotFoundException) Survey(org.sagebionetworks.bridge.models.surveys.Survey) Iterator(java.util.Iterator) UploadValidationContext(org.sagebionetworks.bridge.upload.UploadValidationContext) DateTime(org.joda.time.DateTime) IOException(java.io.IOException) HealthDataRecordValidator(org.sagebionetworks.bridge.validators.HealthDataRecordValidator) TranscribeConsentHandler(org.sagebionetworks.bridge.upload.TranscribeConsentHandler) Collectors(java.util.stream.Collectors) Upload(org.sagebionetworks.bridge.models.upload.Upload) SchemaUtils(org.sagebionetworks.bridge.schema.SchemaUtils) UploadArtifactsHandler(org.sagebionetworks.bridge.upload.UploadArtifactsHandler) List(java.util.List) Component(org.springframework.stereotype.Component) InvalidEntityException(org.sagebionetworks.bridge.exceptions.InvalidEntityException) UploadFieldDefinition(org.sagebionetworks.bridge.models.upload.UploadFieldDefinition) UploadFileHelper(org.sagebionetworks.bridge.upload.UploadFileHelper) DateUtils(org.sagebionetworks.bridge.time.DateUtils) BridgeUtils(org.sagebionetworks.bridge.BridgeUtils) GuidCreatedOnVersionHolderImpl(org.sagebionetworks.bridge.models.GuidCreatedOnVersionHolderImpl) Preconditions(com.google.common.base.Preconditions) HealthDataSubmission(org.sagebionetworks.bridge.models.healthdata.HealthDataSubmission) RecordExportStatusRequest(org.sagebionetworks.bridge.models.healthdata.RecordExportStatusRequest) UploadValidationException(org.sagebionetworks.bridge.upload.UploadValidationException) HealthDataSubmissionValidator(org.sagebionetworks.bridge.validators.HealthDataSubmissionValidator) HealthDataRecord(org.sagebionetworks.bridge.models.healthdata.HealthDataRecord) BadRequestException(org.sagebionetworks.bridge.exceptions.BadRequestException) NotFoundException(org.sagebionetworks.bridge.exceptions.NotFoundException)

Example 3 with NotFoundException

use of org.sagebionetworks.bridge.exceptions.NotFoundException in project BridgeServer2 by Sage-Bionetworks.

the class DynamoUploadDaoTest method getUploadNotFound.

@Test
public void getUploadNotFound() {
    when(mockMapper.load(uploadCaptor.capture())).thenReturn(null);
    // execute
    Exception thrown = null;
    try {
        dao.getUpload("test-get-404");
        fail();
    } catch (NotFoundException ex) {
        thrown = ex;
    }
    assertNotNull(thrown);
    // validate we passed in the expected key
    assertEquals(uploadCaptor.getValue().getUploadId(), "test-get-404");
}
Also used : NotFoundException(org.sagebionetworks.bridge.exceptions.NotFoundException) EntityNotFoundException(org.sagebionetworks.bridge.exceptions.EntityNotFoundException) ConditionalCheckFailedException(com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException) BadRequestException(org.sagebionetworks.bridge.exceptions.BadRequestException) NotFoundException(org.sagebionetworks.bridge.exceptions.NotFoundException) ConcurrentModificationException(org.sagebionetworks.bridge.exceptions.ConcurrentModificationException) EntityNotFoundException(org.sagebionetworks.bridge.exceptions.EntityNotFoundException) Test(org.testng.annotations.Test)

Example 4 with NotFoundException

use of org.sagebionetworks.bridge.exceptions.NotFoundException in project BridgeServer2 by Sage-Bionetworks.

the class UploadServiceUploadCompleteTest method notFoundInS3.

@Test
public void notFoundInS3() {
    // set up input
    DynamoUpload2 upload = new DynamoUpload2();
    upload.setUploadId(TEST_UPLOAD_ID);
    upload.setStatus(UploadStatus.REQUESTED);
    // mock S3
    AmazonS3Exception s3Ex = new AmazonS3Exception("not found");
    s3Ex.setStatusCode(404);
    when(mockS3Client.getObjectMetadata(TEST_BUCKET, TEST_UPLOAD_ID)).thenThrow(s3Ex);
    // execute
    try {
        svc.uploadComplete(TEST_APP_ID, APP, upload, false);
        fail("expected exception");
    } catch (NotFoundException ex) {
    // expected exception
    }
    // Verify upload DAO and validation aren't called.
    verifyZeroInteractions(mockUploadDao, mockUploadValidationService);
}
Also used : NotFoundException(org.sagebionetworks.bridge.exceptions.NotFoundException) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception) DynamoUpload2(org.sagebionetworks.bridge.dynamodb.DynamoUpload2) Test(org.testng.annotations.Test)

Example 5 with NotFoundException

use of org.sagebionetworks.bridge.exceptions.NotFoundException in project BridgeServer2 by Sage-Bionetworks.

the class UploadServiceUploadCompleteTest method s3InternalError.

@Test
public void s3InternalError() {
    // set up input
    DynamoUpload2 upload = new DynamoUpload2();
    upload.setUploadId(TEST_UPLOAD_ID);
    upload.setStatus(UploadStatus.REQUESTED);
    // mock S3
    AmazonS3Exception s3Ex = new AmazonS3Exception("internal server error");
    s3Ex.setStatusCode(500);
    when(mockS3Client.getObjectMetadata(TEST_BUCKET, TEST_UPLOAD_ID)).thenThrow(s3Ex);
    // execute
    try {
        svc.uploadComplete(TEST_APP_ID, APP, upload, false);
        fail("expected exception");
    } catch (BridgeServiceException ex) {
        // expected exception
        assertFalse(ex instanceof NotFoundException);
    }
    // Verify upload DAO and validation aren't called.
    verifyZeroInteractions(mockUploadDao, mockUploadValidationService);
}
Also used : BridgeServiceException(org.sagebionetworks.bridge.exceptions.BridgeServiceException) NotFoundException(org.sagebionetworks.bridge.exceptions.NotFoundException) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception) DynamoUpload2(org.sagebionetworks.bridge.dynamodb.DynamoUpload2) Test(org.testng.annotations.Test)

Aggregations

NotFoundException (org.sagebionetworks.bridge.exceptions.NotFoundException)5 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)3 Test (org.testng.annotations.Test)3 DynamoUpload2 (org.sagebionetworks.bridge.dynamodb.DynamoUpload2)2 BadRequestException (org.sagebionetworks.bridge.exceptions.BadRequestException)2 BridgeServiceException (org.sagebionetworks.bridge.exceptions.BridgeServiceException)2 ConcurrentModificationException (org.sagebionetworks.bridge.exceptions.ConcurrentModificationException)2 ConditionalCheckFailedException (com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException)1 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 Charsets (com.google.common.base.Charsets)1 Preconditions (com.google.common.base.Preconditions)1 Stopwatch (com.google.common.base.Stopwatch)1 IOException (java.io.IOException)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 StringUtils (org.apache.commons.lang3.StringUtils)1