com.amazonaws.util.Md5Utils Java Examples

The following examples show how to use com.amazonaws.util.Md5Utils. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SqsExecutorTest.java    From spring-integration-aws with MIT License 6 votes vote down vote up
@Test
public void correctMD5Test() throws Exception {

	String payload = "Hello, World";
	String messageBody = messageMarshaller.serialize(MessageBuilder
			.withPayload(payload).build());
	com.amazonaws.services.sqs.model.Message sqsMessage = new com.amazonaws.services.sqs.model.Message();
	sqsMessage.setBody(messageBody);
	sqsMessage.setMD5OfBody(new String(Hex.encodeHex(Md5Utils
			.computeMD5Hash(messageBody.getBytes("UTF-8")))));

	ReceiveMessageResult result = new ReceiveMessageResult();
	result.setMessages(Collections.singletonList(sqsMessage));
	when(mockSQS.receiveMessage(any(ReceiveMessageRequest.class)))
			.thenReturn(result);

	Message<?> recvMessage = executor.poll();
	assertNotNull("message is not null", recvMessage);

	Message<?> enclosed = messageMarshaller
			.deserialize((String) recvMessage.getPayload());
	String recvPayload = (String) enclosed.getPayload();
	assertEquals("payload must match", payload, recvPayload);
}
 
Example #2
Source File: S3SinkStreamWriterUnitTest.java    From Scribengin with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void testUploadManyFiles() throws Exception {
  init("s3.default.properties");

  for (int i = 0; i < 8; i++) {
    sink.append(new Record(Integer.toString(i), Integer.toString(i).getBytes()));
  }
  List<String> md5s = new ArrayList<>();
  for (String file : sink.getBuffer().getFiles()) {
    md5s.add(new String(Md5Utils.md5AsBase64(new File(file))));
  }
  sink.prepareCommit();
  sink.commit();
  ObjectListing list = s3.listObjects(s3SinkConfig.getBucketName(), "10");
  assertTrue(list.getObjectSummaries().size() == 4);
  for (int i = 0; i < 8; i += 2) {
    S3Object s3object = s3.getObject(s3SinkConfig.getBucketName(), "10/" + i + "_" + (i + 1));
    assertNotNull(s3object);
  }
}
 
Example #3
Source File: AmazonS3Mock.java    From Scribengin with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public PutObjectResult putObject(String bucketName, String key, File file) throws AmazonClientException,
    AmazonServiceException {
  throwException(putObjectException);
  
  List<String> keys = files.get(bucketName);
  if (keys == null) {
    throw new AmazonClientException("Bucket do not exist");
  }
  keys.add(key);
  files.put(bucketName, keys);
  PutObjectResult result = new PutObjectResult();
  try {
    result.setContentMd5(new String(Md5Utils.md5AsBase64(file)));
  } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }
  return result;
}
 
Example #4
Source File: SQSExecutorService.java    From amazon-sqs-java-temporary-queues-client with Apache License 2.0 5 votes vote down vote up
private void addDeduplicationAttributes(MessageContent messageContent, Object task) {
    if (task instanceof Deduplicated) {
        String deduplicationID = ((Deduplicated)task).deduplicationID();
        if (deduplicationID == null) {
            String body = messageContent.getMessageBody();
            deduplicationID = BinaryUtils.toHex(Md5Utils.computeMD5Hash(body.getBytes(UTF8)));
        }
        messageContent.setMessageAttributesEntry(SQSFutureTask.DEDUPLICATION_ID_ATTRIBUTE_NAME,
                stringMessageAttributeValue(deduplicationID));
    }
    messageContent.setMessageAttributesEntry(SQSFutureTask.UUID_ATTRIBUTE_NAME,
            stringMessageAttributeValue(UUID.randomUUID().toString()));
}
 
Example #5
Source File: TransportAmazonLambdaS3.java    From github-bucket with ISC License 5 votes vote down vote up
@Override
void writeFile(final String path, final byte[] data) throws IOException {
    ObjectMetadata bucketMetadata = new ObjectMetadata();
    bucketMetadata.setContentMD5(Md5Utils.md5AsBase64(data));
    bucketMetadata.setContentLength(data.length);
    // Give Tika a few hints for the content detection
    Metadata tikaMetadata = new Metadata();
    tikaMetadata.set(Metadata.RESOURCE_NAME_KEY, FilenameUtils.getName(FilenameUtils.normalize(path)));
    // Fire!
    try (InputStream bis = TikaInputStream.get(data, tikaMetadata)) {
        bucketMetadata.setContentType(TIKA_DETECTOR.detect(bis, tikaMetadata).toString());
        s3.putObject(bucket, resolveKey(path), bis, bucketMetadata);
    }
}
 
Example #6
Source File: TransportAmazonLambdaS3.java    From github-bucket with ISC License 5 votes vote down vote up
@Override
void writeFile(final String path, final byte[] data) throws IOException {
    ObjectMetadata bucketMetadata = new ObjectMetadata();
    bucketMetadata.setContentMD5(Md5Utils.md5AsBase64(data));
    bucketMetadata.setContentLength(data.length);
    // Give Tika a few hints for the content detection
    Metadata tikaMetadata = new Metadata();
    tikaMetadata.set(Metadata.RESOURCE_NAME_KEY, FilenameUtils.getName(FilenameUtils.normalize(path)));
    // Fire!
    try (InputStream bis = TikaInputStream.get(data, tikaMetadata)) {
        bucketMetadata.setContentType(TIKA_DETECTOR.detect(bis, tikaMetadata).toString());
        s3.putObject(bucket, resolveKey(path), bis, bucketMetadata);
    }
}
 
Example #7
Source File: AmazonS3SinkMockTests.java    From spring-cloud-stream-app-starters with Apache License 2.0 5 votes vote down vote up
@Test
@Override
public void test() throws Exception {
	AmazonS3 amazonS3Client = TestUtils.getPropertyValue(this.s3MessageHandler, "transferManager.s3",
			AmazonS3.class);

	InputStream payload = new StringInputStream("a");
	Message<?> message = MessageBuilder.withPayload(payload)
			.setHeader("key", "myInputStream")
			.build();

	this.channels.input().send(message);

	ArgumentCaptor<PutObjectRequest> putObjectRequestArgumentCaptor =
			ArgumentCaptor.forClass(PutObjectRequest.class);
	verify(amazonS3Client, atLeastOnce()).putObject(putObjectRequestArgumentCaptor.capture());

	PutObjectRequest putObjectRequest = putObjectRequestArgumentCaptor.getValue();
	assertThat(putObjectRequest.getBucketName(), equalTo(S3_BUCKET));
	assertThat(putObjectRequest.getKey(), equalTo("myInputStream"));
	assertNull(putObjectRequest.getFile());
	assertNotNull(putObjectRequest.getInputStream());

	ObjectMetadata metadata = putObjectRequest.getMetadata();
	assertThat(metadata.getContentMD5(), equalTo(Md5Utils.md5AsBase64(payload)));
	assertThat(metadata.getContentLength(), equalTo(1L));
	assertThat(metadata.getContentType(), equalTo(MediaType.APPLICATION_JSON_VALUE));
	assertThat(metadata.getContentDisposition(), equalTo("test.json"));
}
 
Example #8
Source File: AmazonS3SinkMockTests.java    From spring-cloud-stream-app-starters with Apache License 2.0 4 votes vote down vote up
@Test
@Override
public void test() throws Exception {
	AmazonS3 amazonS3Client = TestUtils.getPropertyValue(this.s3MessageHandler, "transferManager.s3",
			AmazonS3.class);

	File file = this.temporaryFolder.newFile("foo.mp3");
	Message<?> message = MessageBuilder.withPayload(file)
			.build();

	this.channels.input().send(message);

	ArgumentCaptor<PutObjectRequest> putObjectRequestArgumentCaptor =
			ArgumentCaptor.forClass(PutObjectRequest.class);
	verify(amazonS3Client, atLeastOnce()).putObject(putObjectRequestArgumentCaptor.capture());

	PutObjectRequest putObjectRequest = putObjectRequestArgumentCaptor.getValue();
	assertThat(putObjectRequest.getBucketName(), equalTo(S3_BUCKET));
	assertThat(putObjectRequest.getKey(), equalTo("foo.mp3"));
	assertNotNull(putObjectRequest.getFile());
	assertNull(putObjectRequest.getInputStream());

	ObjectMetadata metadata = putObjectRequest.getMetadata();
	assertThat(metadata.getContentMD5(), equalTo(Md5Utils.md5AsBase64(file)));
	assertThat(metadata.getContentLength(), equalTo(0L));
	assertThat(metadata.getContentType(), equalTo("audio/mpeg"));

	ProgressListener listener = putObjectRequest.getGeneralProgressListener();
	S3ProgressPublisher.publishProgress(listener, ProgressEventType.TRANSFER_COMPLETED_EVENT);

	assertTrue(this.transferCompletedLatch.await(10, TimeUnit.SECONDS));
	assertTrue(this.aclLatch.await(10, TimeUnit.SECONDS));

	ArgumentCaptor<SetObjectAclRequest> setObjectAclRequestArgumentCaptor =
			ArgumentCaptor.forClass(SetObjectAclRequest.class);
	verify(amazonS3Client).setObjectAcl(setObjectAclRequestArgumentCaptor.capture());

	SetObjectAclRequest setObjectAclRequest = setObjectAclRequestArgumentCaptor.getValue();

	assertThat(setObjectAclRequest.getBucketName(), equalTo(S3_BUCKET));
	assertThat(setObjectAclRequest.getKey(), equalTo("foo.mp3"));
	assertNull(setObjectAclRequest.getAcl());
	assertThat(setObjectAclRequest.getCannedAcl(), equalTo(CannedAccessControlList.PublicReadWrite));
}