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
This commit is contained in:
xfrancois
2020-04-06 17:42:43 +02:00
committed by GitHub
parent 6b82fde8a1
commit 94e4481e6f
3 changed files with 33 additions and 6 deletions

View File

@@ -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<S3ObjectSummary> {
@@ -54,6 +55,8 @@ public class S3Session implements Session<S3ObjectSummary> {
private final ResourceIdResolver resourceIdResolver;
private String endpoint;
public S3Session(AmazonS3 amazonS3) {
this(amazonS3, null);
}
@@ -64,6 +67,10 @@ public class S3Session implements Session<S3ObjectSummary> {
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<S3ObjectSummary> {
@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) {

View File

@@ -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<S3ObjectSummary>, SharedSessionCapable {
@@ -64,4 +65,7 @@ public class S3SessionFactory implements SessionFactory<S3ObjectSummary>, Shared
// No-op. The S3Session is stateless and can be used concurrently.
}
public void setEndpoint(String endpoint) {
this.s3Session.setEndpoint(endpoint);
}
}

View File

@@ -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);