From 94e4481e6fa52dccb61ff41bc9b2abc1a804362c Mon Sep 17 00:00:00 2001 From: xfrancois Date: Mon, 6 Apr 2020 17:42:43 +0200 Subject: [PATCH] GH-160: Custom endpoint for S3Session.getHostPort Fixes https://github.com/spring-projects/spring-integration-aws/issues/160 * Set endpoint with setter instead of constructor * Add unit test * Edit getHostPortWithEndpoint test * Add check to verify endpoint when receiving a message --- .../integration/aws/support/S3Session.java | 18 +++++++++++++++--- .../aws/support/S3SessionFactory.java | 6 +++++- .../inbound/S3InboundChannelAdapterTests.java | 15 +++++++++++++-- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/springframework/integration/aws/support/S3Session.java b/src/main/java/org/springframework/integration/aws/support/S3Session.java index e6dd849..efc4f4e 100644 --- a/src/main/java/org/springframework/integration/aws/support/S3Session.java +++ b/src/main/java/org/springframework/integration/aws/support/S3Session.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 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. @@ -47,6 +47,7 @@ import com.amazonaws.services.s3.model.S3ObjectSummary; * @author Artem Bilan * @author Jim Krygowski * @author Anwar Chirakkattil + * @author Xavier François */ public class S3Session implements Session { @@ -54,6 +55,8 @@ public class S3Session implements Session { private final ResourceIdResolver resourceIdResolver; + private String endpoint; + public S3Session(AmazonS3 amazonS3) { this(amazonS3, null); } @@ -64,6 +67,10 @@ public class S3Session implements Session { this.amazonS3 = amazonS3; } + public void setEndpoint(String endpoint) { + this.endpoint = endpoint; + } + @Override public S3ObjectSummary[] list(String path) { String[] bucketPrefix = splitPathToBucketAndKey(path, false); @@ -225,8 +232,13 @@ public class S3Session implements Session { @Override public String getHostPort() { - Region region = this.amazonS3.getRegion().toAWSRegion(); - return String.format("%s.%s.%s:%d", AmazonS3.ENDPOINT_PREFIX, region.getName(), region.getDomain(), 443); + if (this.endpoint != null) { + return this.endpoint; + } + else { + Region region = this.amazonS3.getRegion().toAWSRegion(); + return String.format("%s.%s.%s:%d", AmazonS3.ENDPOINT_PREFIX, region.getName(), region.getDomain(), 443); + } } public String normalizeBucketName(String path) { diff --git a/src/main/java/org/springframework/integration/aws/support/S3SessionFactory.java b/src/main/java/org/springframework/integration/aws/support/S3SessionFactory.java index 508babb..b0d25b6 100644 --- a/src/main/java/org/springframework/integration/aws/support/S3SessionFactory.java +++ b/src/main/java/org/springframework/integration/aws/support/S3SessionFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2019 the original author or authors. + * Copyright 2016-2020 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. @@ -31,6 +31,7 @@ import com.amazonaws.services.s3.model.S3ObjectSummary; * simple thread-safe wrapper for the {@link AmazonS3}. * * @author Artem Bilan + * @author Xavier François */ public class S3SessionFactory implements SessionFactory, SharedSessionCapable { @@ -64,4 +65,7 @@ public class S3SessionFactory implements SessionFactory, Shared // No-op. The S3Session is stateless and can be used concurrently. } + public void setEndpoint(String endpoint) { + this.s3Session.setEndpoint(endpoint); + } } diff --git a/src/test/java/org/springframework/integration/aws/inbound/S3InboundChannelAdapterTests.java b/src/test/java/org/springframework/integration/aws/inbound/S3InboundChannelAdapterTests.java index 613ac02..3f03d7f 100644 --- a/src/test/java/org/springframework/integration/aws/inbound/S3InboundChannelAdapterTests.java +++ b/src/test/java/org/springframework/integration/aws/inbound/S3InboundChannelAdapterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2019 the original author or authors. + * Copyright 2016-2020 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. @@ -43,6 +43,7 @@ import org.springframework.expression.ExpressionParser; import org.springframework.expression.spel.standard.SpelExpressionParser; import org.springframework.integration.annotation.InboundChannelAdapter; import org.springframework.integration.annotation.Poller; +import org.springframework.integration.aws.support.S3SessionFactory; import org.springframework.integration.aws.support.filters.S3RegexPatternFileListFilter; import org.springframework.integration.channel.QueueChannel; import org.springframework.integration.config.EnableIntegration; @@ -64,6 +65,7 @@ import com.amazonaws.services.s3.model.S3ObjectSummary; /** * @author Artem Bilan * @author Jim Krygowski + * @author Xavier François */ @SpringJUnitConfig @DirtiesContext @@ -135,6 +137,8 @@ public class S3InboundChannelAdapterTests { assertThat(message.getHeaders()) .containsKeys(FileHeaders.REMOTE_DIRECTORY, FileHeaders.REMOTE_HOST_PORT, FileHeaders.REMOTE_FILE); + assertThat(message.getHeaders().get(FileHeaders.REMOTE_HOST_PORT)).isEqualTo("s3-url.com:8000"); + assertThat(this.s3FilesChannel.receive(10)).isNull(); File file = new File(LOCAL_FOLDER, "A.TEST.a"); @@ -182,9 +186,16 @@ public class S3InboundChannelAdapterTests { return amazonS3; } + @Bean + public S3SessionFactory s3SessionFactory() { + S3SessionFactory s3SessionFactory = new S3SessionFactory(amazonS3()); + s3SessionFactory.setEndpoint("s3-url.com:8000"); + return s3SessionFactory; + } + @Bean public S3InboundFileSynchronizer s3InboundFileSynchronizer() { - S3InboundFileSynchronizer synchronizer = new S3InboundFileSynchronizer(amazonS3()); + S3InboundFileSynchronizer synchronizer = new S3InboundFileSynchronizer(s3SessionFactory()); synchronizer.setDeleteRemoteFiles(true); synchronizer.setPreserveTimestamp(true); synchronizer.setRemoteDirectory(S3_BUCKET);