Search in sources :

Example 11 with ObjectMetadata

use of com.ibm.watson.visual_recognition.v4.model.ObjectMetadata in project BridgeServer2 by Sage-Bionetworks.

the class ConsentServiceTest method resendConsentAgreementWithPhoneOK.

@Test
public void resendConsentAgreementWithPhoneOK() throws Exception {
    doReturn("asdf.pdf").when(consentService).getSignedConsentUrl();
    account.setConsentSignatureHistory(SUBPOP_GUID, ImmutableList.of(CONSENT_SIGNATURE));
    TemplateRevision revision = TemplateRevision.create();
    revision.setDocumentContent("some test content");
    when(templateService.getRevisionForUser(app, TemplateType.SMS_SIGNED_CONSENT)).thenReturn(revision);
    consentService.resendConsentAgreement(app, SUBPOP_GUID, PHONE_PARTICIPANT);
    verify(smsService).sendSmsMessage(eq(ID), smsProviderCaptor.capture());
    ArgumentCaptor<ObjectMetadata> metadataCaptor = ArgumentCaptor.forClass(ObjectMetadata.class);
    verify(s3Helper).writeBytesToS3(eq(ConsentService.USERSIGNED_CONSENTS_BUCKET), eq("asdf.pdf"), any(), metadataCaptor.capture());
    assertEquals(metadataCaptor.getValue().getSSEAlgorithm(), ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION);
    SmsMessageProvider provider = smsProviderCaptor.getValue();
    assertEquals(provider.getPhone(), PHONE_PARTICIPANT.getPhone());
    assertEquals(provider.getApp(), app);
    assertEquals(provider.getSmsType(), "Transactional");
    assertEquals(provider.getTokenMap().get("consentUrl"), SHORT_URL);
    assertEquals(provider.getTemplateRevision().getDocumentContent(), revision.getDocumentContent());
}
Also used : TemplateRevision(org.sagebionetworks.bridge.models.templates.TemplateRevision) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) SmsMessageProvider(org.sagebionetworks.bridge.sms.SmsMessageProvider) Test(org.testng.annotations.Test)

Example 12 with ObjectMetadata

use of com.ibm.watson.visual_recognition.v4.model.ObjectMetadata 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 13 with ObjectMetadata

use of com.ibm.watson.visual_recognition.v4.model.ObjectMetadata in project BridgeServer2 by Sage-Bionetworks.

the class UploadFileHelperFindValueTest method attachmentFile.

@Test
public void attachmentFile() throws Exception {
    // Make field def.
    UploadFieldDefinition fieldDef = new UploadFieldDefinition.Builder().withName(FIELD_NAME_FILE).withType(UploadFieldType.ATTACHMENT_V2).build();
    // Make file map.
    File recordJsonFile = makeFileWithContent(FIELD_NAME_FILE, "dummy content");
    Map<String, File> fileMap = ImmutableMap.of(FIELD_NAME_FILE, recordJsonFile);
    // Execute
    String expectedAttachmentFilename = UPLOAD_ID + '-' + FIELD_NAME_FILE;
    JsonNode result = uploadFileHelper.findValueForField(UPLOAD_ID, fileMap, fieldDef, new HashMap<>());
    assertEquals(result.textValue(), expectedAttachmentFilename);
    // Verify uploaded file
    verify(mockS3Helper).writeFileToS3(eq(UploadFileHelper.ATTACHMENT_BUCKET), eq(expectedAttachmentFilename), eq(recordJsonFile), metadataCaptor.capture());
    ObjectMetadata metadata = metadataCaptor.getValue();
    assertEquals(metadata.getUserMetaDataOf(UploadFileHelper.KEY_CUSTOM_CONTENT_MD5), TestConstants.MOCK_MD5_HEX_ENCODED);
    assertEquals(metadata.getSSEAlgorithm(), ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION);
}
Also used : UploadFieldDefinition(org.sagebionetworks.bridge.models.upload.UploadFieldDefinition) JsonNode(com.fasterxml.jackson.databind.JsonNode) File(java.io.File) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) Test(org.testng.annotations.Test)

Example 14 with ObjectMetadata

use of com.ibm.watson.visual_recognition.v4.model.ObjectMetadata in project BridgeServer2 by Sage-Bionetworks.

the class UploadFileHelperTest method uploadBytesAsAttachment.

@Test
public void uploadBytesAsAttachment() throws Exception {
    // Execute.
    helper.uploadBytesAsAttachment(FILENAME, CONTENT);
    // Verify.
    ArgumentCaptor<ObjectMetadata> metadataCaptor = ArgumentCaptor.forClass(ObjectMetadata.class);
    verify(mockS3Helper).writeBytesToS3(eq(UploadFileHelper.ATTACHMENT_BUCKET), eq(FILENAME), eq(CONTENT), metadataCaptor.capture());
    ObjectMetadata metadata = metadataCaptor.getValue();
    assertEquals(metadata.getUserMetaDataOf(UploadFileHelper.KEY_CUSTOM_CONTENT_MD5), TestConstants.MOCK_MD5_HEX_ENCODED);
    assertEquals(metadata.getSSEAlgorithm(), ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION);
}
Also used : ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) Test(org.testng.annotations.Test)

Example 15 with ObjectMetadata

use of com.ibm.watson.visual_recognition.v4.model.ObjectMetadata in project BridgeServer2 by Sage-Bionetworks.

the class ConsentService method sendConsentViaSMS.

private void sendConsentViaSMS(App app, Subpopulation subpop, StudyParticipant participant, ConsentPdf consentPdf) {
    String shortUrl;
    try {
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setSSEAlgorithm(ObjectMetadata.AES_256_SERVER_SIDE_ENCRYPTION);
        String fileName = getSignedConsentUrl();
        DateTime expiresOn = getDownloadExpiration();
        s3Helper.writeBytesToS3(USERSIGNED_CONSENTS_BUCKET, fileName, consentPdf.getBytes(), metadata);
        URL url = s3Helper.generatePresignedUrl(USERSIGNED_CONSENTS_BUCKET, fileName, expiresOn, HttpMethod.GET);
        shortUrl = urlShortenerService.shortenUrl(url.toString(), SIGNED_CONSENT_DOWNLOAD_EXPIRE_IN_SECONDS);
    } catch (IOException e) {
        throw new BridgeServiceException(e);
    }
    TemplateRevision revision = templateService.getRevisionForUser(app, SMS_SIGNED_CONSENT);
    SmsMessageProvider provider = new SmsMessageProvider.Builder().withApp(app).withPhone(participant.getPhone()).withExpirationPeriod(EXPIRATION_PERIOD_KEY, SIGNED_CONSENT_DOWNLOAD_EXPIRE_IN_SECONDS).withTransactionType().withTemplateRevision(revision).withToken(BridgeConstants.CONSENT_URL, shortUrl).build();
    smsService.sendSmsMessage(participant.getId(), provider);
}
Also used : BridgeServiceException(org.sagebionetworks.bridge.exceptions.BridgeServiceException) TemplateRevision(org.sagebionetworks.bridge.models.templates.TemplateRevision) IOException(java.io.IOException) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) DateTime(org.joda.time.DateTime) URL(java.net.URL) SmsMessageProvider(org.sagebionetworks.bridge.sms.SmsMessageProvider)

Aggregations

ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)566 PutObjectRequest (com.amazonaws.services.s3.model.PutObjectRequest)191 ByteArrayInputStream (java.io.ByteArrayInputStream)157 Test (org.junit.Test)143 IOException (java.io.IOException)101 InputStream (java.io.InputStream)80 File (java.io.File)62 AmazonClientException (com.amazonaws.AmazonClientException)61 AmazonServiceException (com.amazonaws.AmazonServiceException)61 S3Object (com.amazonaws.services.s3.model.S3Object)59 AmazonS3 (com.amazonaws.services.s3.AmazonS3)54 Date (java.util.Date)46 S3FileTransferRequestParamsDto (org.finra.herd.model.dto.S3FileTransferRequestParamsDto)34 GetObjectMetadataRequest (com.amazonaws.services.s3.model.GetObjectMetadataRequest)33 PutObjectResult (com.amazonaws.services.s3.model.PutObjectResult)32 GetObjectRequest (com.amazonaws.services.s3.model.GetObjectRequest)30 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)29 Upload (com.amazonaws.services.s3.transfer.Upload)26 SdkClientException (com.amazonaws.SdkClientException)24 CopyObjectRequest (com.amazonaws.services.s3.model.CopyObjectRequest)24