From d4dedef383edd3dc5ab01b4d612dcf6a3b428891 Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Thu, 12 Feb 2015 11:05:22 +0200 Subject: [PATCH] GH-3: Fix `BeanFactory` population for `AS3MH` Fixes https://github.com/spring-projects/spring-integration-aws/issues/3 **Cherry-pick to 0.5.x** --- .../aws/s3/AmazonS3MessageHandler.java | 24 +++++++++++++------ .../aws/s3/AmazonS3MessageHandlerTests.java | 4 +++- ...efaultFileNameGenerationStrategyTests.java | 13 +++++++++- 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/springframework/integration/aws/s3/AmazonS3MessageHandler.java b/src/main/java/org/springframework/integration/aws/s3/AmazonS3MessageHandler.java index 58326ac..dca08f5 100644 --- a/src/main/java/org/springframework/integration/aws/s3/AmazonS3MessageHandler.java +++ b/src/main/java/org/springframework/integration/aws/s3/AmazonS3MessageHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,6 +25,7 @@ import java.io.InputStream; import java.util.Collection; import java.util.Map; +import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.expression.Expression; import org.springframework.messaging.Message; import org.springframework.integration.aws.core.AWSCredentials; @@ -41,6 +42,7 @@ import org.springframework.util.Assert; * * @author Amol Nayak * @author Rob Harrop + * @author Artem Bilan * * @since 0.5 * @@ -60,25 +62,30 @@ public class AmazonS3MessageHandler extends AbstractMessageHandler { private volatile FileNameGenerationStrategy fileNameGenerator = new DefaultFileNameGenerationStrategy(); - + private volatile boolean fileNameGeneratorSet; @Override protected void onInit() throws Exception { super.onInit(); Assert.hasText(bucket,"Bucket not set'"); - Assert.notNull(remoteDirectoryProcessor, "Remote Directory processor should be present, set the remore directory expression"); + Assert.notNull(remoteDirectoryProcessor, + "Remote Directory processor should be present, set the remote directory expression"); + if (!this.fileNameGeneratorSet && this.fileNameGenerator instanceof BeanFactoryAware) { + ((BeanFactoryAware) this.fileNameGenerator).setBeanFactory(getBeanFactory()); + } + + this.remoteDirectoryProcessor.setBeanFactory(getBeanFactory()); } /** * The constructor that initializes {@link AmazonS3MessageHandler} with the provided * implementation of {@link AmazonS3Operations} and using the provided {@link AWSCredentials} - * * @param credentials * @param operations */ - public AmazonS3MessageHandler(AWSCredentials credentials,AmazonS3Operations operations) { + public AmazonS3MessageHandler(AWSCredentials credentials, AmazonS3Operations operations) { Assert.notNull(operations,"s3 operations is null"); Assert.notNull(credentials,"AWS Credentials are null"); this.credentials = credentials; @@ -86,13 +93,14 @@ public class AmazonS3MessageHandler extends AbstractMessageHandler { } + + /** * The handler implementation for the Amazon S3 used to put objects in the remote AWS S3 bucket * the message should contain a valid payload of type {@link File}, {@link InputStream}, * byte[] or {@link String}. Various predetermined headers as defined in {@link AmazonS3MessageHeaders} * are extracted from the message and an {@link AmazonS3Object} is constructed that is provided to * the {@link AmazonS3Operations} implementation to be uploaded in S3. - * * @param message */ @Override @@ -172,7 +180,7 @@ public class AmazonS3MessageHandler extends AbstractMessageHandler { return null; } if(expectedType.isAssignableFrom(genericHeader.getClass())) { - header = (T)genericHeader; + header = (T) genericHeader; } else { logger.warn("Found header " + USER_METADATA + " in the message but was not of required type"); @@ -216,5 +224,7 @@ public class AmazonS3MessageHandler extends AbstractMessageHandler { public void setFileNameGenerator(FileNameGenerationStrategy fileNameGenerator) { Assert.notNull(fileNameGenerator,"File name generator is null"); this.fileNameGenerator = fileNameGenerator; + this.fileNameGeneratorSet = true; } + } diff --git a/src/test/java/org/springframework/integration/aws/s3/AmazonS3MessageHandlerTests.java b/src/test/java/org/springframework/integration/aws/s3/AmazonS3MessageHandlerTests.java index 46ec7db..7439320 100644 --- a/src/test/java/org/springframework/integration/aws/s3/AmazonS3MessageHandlerTests.java +++ b/src/test/java/org/springframework/integration/aws/s3/AmazonS3MessageHandlerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -57,6 +57,7 @@ import org.springframework.messaging.MessageHandlingException; * * @author Amol Nayak * @author Rob Harrop + * @author Artem Bilan * * @since 0.5 * @@ -219,6 +220,7 @@ public class AmazonS3MessageHandlerTests { AmazonS3MessageHandler handler = getHandler(); SpelExpressionParser parser = new SpelExpressionParser(); handler.setRemoteDirectoryExpression(parser.parseExpression("headers['remoteDirectory']")); + handler.afterPropertiesSet(); handler.handleMessage(message); Assert.assertEquals("TestBucket", holder.getBucket()); Assert.assertEquals("TestFileName.txt", holder.getObjectName()); diff --git a/src/test/java/org/springframework/integration/aws/s3/DefaultFileNameGenerationStrategyTests.java b/src/test/java/org/springframework/integration/aws/s3/DefaultFileNameGenerationStrategyTests.java index f8e8a6e..60fbb1e 100644 --- a/src/test/java/org/springframework/integration/aws/s3/DefaultFileNameGenerationStrategyTests.java +++ b/src/test/java/org/springframework/integration/aws/s3/DefaultFileNameGenerationStrategyTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,6 +25,9 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; +import org.mockito.Mockito; + +import org.springframework.beans.factory.BeanFactory; import org.springframework.integration.support.MessageBuilder; import org.springframework.messaging.Message; @@ -34,6 +37,7 @@ import org.springframework.messaging.Message; * * @author Amol Nayak * @author Rob Harrop + * @author Artem Bilan * * @since 0.5 * @@ -52,6 +56,7 @@ public class DefaultFileNameGenerationStrategyTests { .setHeader(AmazonS3MessageHeaders.FILE_NAME, "FileName.txt") .build(); DefaultFileNameGenerationStrategy strategy = new DefaultFileNameGenerationStrategy(); + strategy.setBeanFactory(Mockito.mock(BeanFactory.class)); Assert.assertEquals("FileName.txt", strategy.generateFileName(message)); } @@ -65,6 +70,7 @@ public class DefaultFileNameGenerationStrategyTests { Message message = MessageBuilder.withPayload(file) .build(); DefaultFileNameGenerationStrategy strategy = new DefaultFileNameGenerationStrategy(); + strategy.setBeanFactory(Mockito.mock(BeanFactory.class)); Assert.assertEquals("TempFile.txt", strategy.generateFileName(message)); file.delete(); } @@ -79,6 +85,7 @@ public class DefaultFileNameGenerationStrategyTests { Message message = MessageBuilder.withPayload(file) .build(); DefaultFileNameGenerationStrategy strategy = new DefaultFileNameGenerationStrategy(); + strategy.setBeanFactory(Mockito.mock(BeanFactory.class)); Assert.assertEquals("TempFile.txt", strategy.generateFileName(message)); file.delete(); } @@ -91,6 +98,7 @@ public class DefaultFileNameGenerationStrategyTests { Message message = MessageBuilder.withPayload("String") .build(); DefaultFileNameGenerationStrategy strategy = new DefaultFileNameGenerationStrategy(); + strategy.setBeanFactory(Mockito.mock(BeanFactory.class)); UUID uid = message.getHeaders().getId(); Assert.assertEquals(uid.toString() + ".ext", strategy.generateFileName(message)); } @@ -101,6 +109,7 @@ public class DefaultFileNameGenerationStrategyTests { @Test(expected=IllegalArgumentException.class) public void withNullExprssion() { DefaultFileNameGenerationStrategy strategy = new DefaultFileNameGenerationStrategy(); + strategy.setBeanFactory(Mockito.mock(BeanFactory.class)); strategy.setFileNameExpression(null); } @@ -110,6 +119,8 @@ public class DefaultFileNameGenerationStrategyTests { @Test(expected=IllegalArgumentException.class) public void withNullTemporarySuffix() { DefaultFileNameGenerationStrategy strategy = new DefaultFileNameGenerationStrategy(); + strategy.setBeanFactory(Mockito.mock(BeanFactory.class)); strategy.setTemporarySuffix(null); } + }