use of com.ibm.watson.visual_recognition.v4.model.ObjectMetadata in project BridgeServer2 by Sage-Bionetworks.
the class StudyConsentService method writeBytesToPublicS3.
/**
* Write the byte array to a bucket at S3. The bucket will be given world read privileges, and the request
* will be returned with the appropriate content type header for the document's MimeType.
*/
void writeBytesToPublicS3(@Nonnull String bucket, @Nonnull String key, @Nonnull byte[] data, @Nonnull MimeType type) throws IOException {
try (InputStream dataInputStream = new ByteArrayInputStream(data)) {
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType(type.toString());
PutObjectRequest request = new PutObjectRequest(bucket, key, dataInputStream, metadata).withCannedAcl(CannedAccessControlList.PublicRead);
Stopwatch stopwatch = Stopwatch.createStarted();
s3Client.putObject(request);
logger.info("Finished writing to bucket " + bucket + " key " + key + " (" + data.length + " bytes) in " + stopwatch.elapsed(TimeUnit.MILLISECONDS) + " ms");
}
}
use of com.ibm.watson.visual_recognition.v4.model.ObjectMetadata in project BridgeServer2 by Sage-Bionetworks.
the class StudyConsentServiceTest method publishConsent.
@Test
public void publishConsent() throws Exception {
// This is the document with the footer, where all the template variables have been removed
String transformedDoc = "<doc><p>Document</p><table class=\"bridge-sig-block\"><tbody><tr>" + "<td><div class=\"label\">Name of Adult Participant</div></td><td><img brimg=\"\" " + "alt=\"\" onerror=\"this.style.display='none'\" src=\"cid:consentSignature\" /><div " + "class=\"label\">Signature of Adult Participant</div></td><td><div class=\"label\">" + "Date</div></td></tr><tr><td><div class=\"label\">Email, Phone, or ID</div></td><td>" + "<div class=\"label\">Sharing Option</div></td></tr></tbody></table></doc>";
StudyConsent consent = StudyConsent.create();
consent.setCreatedOn(CREATED_ON);
consent.setSubpopulationGuid(SUBPOP_GUID.getGuid());
when(mockDao.getConsent(SUBPOP_GUID, CREATED_ON)).thenReturn(consent);
when(mockS3Helper.readS3FileAsString(CONSENT_BUCKET, consent.getStoragePath())).thenReturn(DOCUMENT);
App app = App.create();
Subpopulation subpop = Subpopulation.create();
subpop.setGuid(SUBPOP_GUID);
service.setConsentTemplate(new ByteArrayResource("<doc>${consent.body}</doc>".getBytes()));
StudyConsentView result = service.publishConsent(app, subpop, CREATED_ON);
assertEquals(result.getDocumentContent(), DOCUMENT + SIGNATURE_BLOCK);
assertEquals(result.getCreatedOn(), CREATED_ON);
assertEquals(result.getSubpopulationGuid(), SUBPOP_GUID.getGuid());
assertEquals(result.getStudyConsent(), consent);
verify(mockSubpopService).updateSubpopulation(eq(app), subpopCaptor.capture());
assertEquals(subpopCaptor.getValue().getPublishedConsentCreatedOn(), CREATED_ON);
verify(mockS3Client, times(2)).putObject(requestCaptor.capture());
PutObjectRequest request = requestCaptor.getAllValues().get(0);
assertEquals(request.getBucketName(), PUBLICATION_BUCKET);
assertEquals(request.getCannedAcl(), PublicRead);
assertEquals(IOUtils.toString(request.getInputStream(), UTF_8), transformedDoc);
ObjectMetadata metadata = request.getMetadata();
assertEquals(metadata.getContentType(), MimeType.HTML.toString());
request = requestCaptor.getAllValues().get(1);
assertEquals(request.getBucketName(), PUBLICATION_BUCKET);
assertEquals(request.getCannedAcl(), PublicRead);
// The PDF output isn't easily verified...
metadata = request.getMetadata();
assertEquals(metadata.getContentType(), MimeType.PDF.toString());
}
use of com.ibm.watson.visual_recognition.v4.model.ObjectMetadata in project proxima-platform by O2-Czech-Republic.
the class S3BlobPathTest method testRead.
@Test
public void testRead() throws IOException {
final String message = "one two three four five six seven eight nine ten";
final S3FileSystem fs = Mockito.mock(S3FileSystem.class);
final AmazonS3 client = Mockito.mock(AmazonS3.class);
Mockito.when(fs.client()).thenReturn(client);
Mockito.when(fs.getSseCustomerKey()).thenReturn(new SSECustomerKey(SSE_KEY));
final S3Object object = new S3Object();
final byte[] messageBytes = message.getBytes(StandardCharsets.UTF_8);
object.setObjectContent(new ByteArrayInputStream(messageBytes));
final ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentLength(message.length());
object.setObjectMetadata(objectMetadata);
Mockito.when(client.getObject(Mockito.any(GetObjectRequest.class))).thenAnswer((Answer<S3Object>) invocationOnMock -> {
final GetObjectRequest request = invocationOnMock.getArgument(0, GetObjectRequest.class);
Assert.assertEquals(SSE_KEY, request.getSSECustomerKey().getKey());
if (request.getRange() != null) {
object.setObjectContent(new ByteArrayInputStream(Arrays.copyOfRange(messageBytes, (int) request.getRange()[0], (int) request.getRange()[1])));
} else {
object.setObjectContent(new ByteArrayInputStream(messageBytes));
}
return object;
});
// This is needed for file size lookup.
Mockito.when(fs.getObject(Mockito.eq("name"))).thenReturn(object);
final S3BlobPath blobPath = S3BlobPath.of(op.getContext(), fs, "name");
try (final ReadableByteChannel channel = blobPath.read()) {
Assert.assertTrue(channel instanceof SeekableByteChannel);
final SeekableByteChannel cast = (SeekableByteChannel) channel;
final ByteBuffer readBuffer = ByteBuffer.allocate(4096);
readAll(cast, readBuffer);
Assert.assertEquals(message, toString(readBuffer));
Assert.assertEquals(messageBytes.length, cast.position());
// Reset read buffer.
readBuffer.rewind();
// Seek to 'five' and re-read rest of the message.
final String remaining = message.substring(message.indexOf("five"));
cast.position(messageBytes.length - remaining.getBytes(StandardCharsets.UTF_8).length);
readAll(cast, readBuffer);
Assert.assertEquals(remaining, toString(readBuffer));
// Read partial.
final int oneLength = "one".getBytes(StandardCharsets.UTF_8).length;
final ByteBuffer smallReadBuffer = ByteBuffer.allocate(oneLength);
cast.position(0L);
Assert.assertEquals(oneLength, cast.read(smallReadBuffer));
// There are still bytes we can read, but there is no free space in receiving buffer left.
Assert.assertEquals(0, cast.read(smallReadBuffer));
Assert.assertEquals("one", toString(smallReadBuffer));
// Common checks.
Assert.assertTrue(cast.isOpen());
Assert.assertEquals(messageBytes.length, cast.size());
}
}
use of com.ibm.watson.visual_recognition.v4.model.ObjectMetadata in project aws-java-reference-pocs by hardikSinghBehl.
the class StorageService method save.
/**
* @param file: represents an object to be saved in configured S3 Bucket
* @return HttpStatus 200 OK if file was saved, HttpStatus 417
* EXPECTATION_FAILED if file was not saved.
*/
public HttpStatus save(final MultipartFile file) {
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(file.getSize());
metadata.setContentType(file.getContentType());
metadata.setContentDisposition(file.getOriginalFilename());
try {
amazonS3.putObject(awsProperties.getS3().getBucketName(), file.getOriginalFilename(), file.getInputStream(), metadata);
} catch (SdkClientException | IOException e) {
log.error("UNABLE TO STORE {} IN S3: {} ", file.getOriginalFilename(), LocalDateTime.now());
return HttpStatus.EXPECTATION_FAILED;
}
return HttpStatus.OK;
}
use of com.ibm.watson.visual_recognition.v4.model.ObjectMetadata in project aws-java-reference-pocs by hardikSinghBehl.
the class StorageService method save.
/**
* @param file: represents an object to be saved in configured S3 Bucket
* @return HttpStatus 200 OK if file was saved, HttpStatus 417
* EXPECTATION_FAILED if file was not saved.
*/
public HttpStatus save(final MultipartFile file) {
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(file.getSize());
metadata.setContentType(file.getContentType());
metadata.setContentDisposition(file.getOriginalFilename());
try {
amazonS3.putObject(awsS3ConfigurationProperties.getS3().getInputBucketName(), file.getOriginalFilename(), file.getInputStream(), metadata);
} catch (SdkClientException | IOException e) {
log.error("UNABLE TO STORE {} IN S3: {} ", file.getOriginalFilename(), LocalDateTime.now());
return HttpStatus.EXPECTATION_FAILED;
}
return HttpStatus.OK;
}
Aggregations