This is an automated email from the ASF dual-hosted git repository. acosentino pushed a commit to branch 22786-2 in repository https://gitbox.apache.org/repos/asf/camel.git
commit dc9b09d4949805c2b46e877a2994aea78b0800e5 Author: Andrea Cosentino <[email protected]> AuthorDate: Mon Dec 15 14:41:41 2025 +0100 CAMEL-22786 - Camel-AWS: Extract common logic for clients instantiation in a separated module - AWS SNS Signed-off-by: Andrea Cosentino <[email protected]> --- components/camel-aws/camel-aws2-sns/pom.xml | 4 + .../component/aws2/sns/Sns2Configuration.java | 3 +- .../camel/component/aws2/sns/Sns2Endpoint.java | 2 +- .../aws2/sns/client/Sns2ClientFactory.java | 28 ++---- .../aws2/sns/client/Sns2InternalClient.java | 30 ------ .../sns/client/impl/Sns2ClientIAMOptimized.java | 94 ------------------ .../client/impl/Sns2ClientIAMProfileOptimized.java | 99 ------------------- .../client/impl/Sns2ClientSessionTokenImpl.java | 110 --------------------- .../sns/client/impl/Sns2ClientStandardImpl.java | 109 -------------------- 9 files changed, 17 insertions(+), 462 deletions(-) diff --git a/components/camel-aws/camel-aws2-sns/pom.xml b/components/camel-aws/camel-aws2-sns/pom.xml index c0e71e731bc6..5b76ba28f83c 100644 --- a/components/camel-aws/camel-aws2-sns/pom.xml +++ b/components/camel-aws/camel-aws2-sns/pom.xml @@ -38,6 +38,10 @@ </properties> <dependencies> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-aws-common</artifactId> + </dependency> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-support</artifactId> diff --git a/components/camel-aws/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/Sns2Configuration.java b/components/camel-aws/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/Sns2Configuration.java index b58f5af58a80..d5f94a2ed24a 100644 --- a/components/camel-aws/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/Sns2Configuration.java +++ b/components/camel-aws/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/Sns2Configuration.java @@ -17,6 +17,7 @@ package org.apache.camel.component.aws2.sns; import org.apache.camel.RuntimeCamelException; +import org.apache.camel.component.aws.common.AwsCommonConfiguration; import org.apache.camel.spi.Metadata; import org.apache.camel.spi.UriParam; import org.apache.camel.spi.UriParams; @@ -25,7 +26,7 @@ import software.amazon.awssdk.core.Protocol; import software.amazon.awssdk.services.sns.SnsClient; @UriParams -public class Sns2Configuration implements Cloneable { +public class Sns2Configuration implements Cloneable, AwsCommonConfiguration { private String topicArn; diff --git a/components/camel-aws/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/Sns2Endpoint.java b/components/camel-aws/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/Sns2Endpoint.java index 52d9c1b26912..8d18e5df017c 100644 --- a/components/camel-aws/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/Sns2Endpoint.java +++ b/components/camel-aws/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/Sns2Endpoint.java @@ -102,7 +102,7 @@ public class Sns2Endpoint extends DefaultEndpoint implements HeaderFilterStrateg public void doInit() throws Exception { super.doInit(); snsClient = configuration.getAmazonSNSClient() != null - ? configuration.getAmazonSNSClient() : Sns2ClientFactory.getSnsClient(configuration).getSNSClient(); + ? configuration.getAmazonSNSClient() : Sns2ClientFactory.getSnsClient(configuration); // check the setting the headerFilterStrategy if (headerFilterStrategy == null) { diff --git a/components/camel-aws/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/client/Sns2ClientFactory.java b/components/camel-aws/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/client/Sns2ClientFactory.java index a146d0c82c7a..f42db5578608 100644 --- a/components/camel-aws/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/client/Sns2ClientFactory.java +++ b/components/camel-aws/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/client/Sns2ClientFactory.java @@ -16,14 +16,12 @@ */ package org.apache.camel.component.aws2.sns.client; +import org.apache.camel.component.aws.common.AwsClientBuilderUtil; import org.apache.camel.component.aws2.sns.Sns2Configuration; -import org.apache.camel.component.aws2.sns.client.impl.Sns2ClientIAMOptimized; -import org.apache.camel.component.aws2.sns.client.impl.Sns2ClientIAMProfileOptimized; -import org.apache.camel.component.aws2.sns.client.impl.Sns2ClientSessionTokenImpl; -import org.apache.camel.component.aws2.sns.client.impl.Sns2ClientStandardImpl; +import software.amazon.awssdk.services.sns.SnsClient; /** - * Factory class to return the correct type of AWS SNS aws. + * Factory class to create AWS SNS clients using common configuration. */ public final class Sns2ClientFactory { @@ -31,20 +29,14 @@ public final class Sns2ClientFactory { } /** - * Return the correct aws SNS client (based on remote vs local). + * Create an SNS client based on configuration. * - * @param configuration configuration - * @return SNSClient + * @param configuration The SNS configuration + * @return Configured SnsClient */ - public static Sns2InternalClient getSnsClient(Sns2Configuration configuration) { - if (Boolean.TRUE.equals(configuration.isUseDefaultCredentialsProvider())) { - return new Sns2ClientIAMOptimized(configuration); - } else if (Boolean.TRUE.equals(configuration.isUseProfileCredentialsProvider())) { - return new Sns2ClientIAMProfileOptimized(configuration); - } else if (Boolean.TRUE.equals(configuration.isUseSessionCredentials())) { - return new Sns2ClientSessionTokenImpl(configuration); - } else { - return new Sns2ClientStandardImpl(configuration); - } + public static SnsClient getSnsClient(Sns2Configuration configuration) { + return AwsClientBuilderUtil.buildClient( + configuration, + SnsClient::builder); } } diff --git a/components/camel-aws/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/client/Sns2InternalClient.java b/components/camel-aws/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/client/Sns2InternalClient.java deleted file mode 100644 index 5510ec597dc0..000000000000 --- a/components/camel-aws/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/client/Sns2InternalClient.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.camel.component.aws2.sns.client; - -import software.amazon.awssdk.services.sns.SnsClient; - -public interface Sns2InternalClient { - - /** - * Returns an sns client after a factory method determines which one to return. - * - * @return SnsClient snsClient - */ - SnsClient getSNSClient(); - -} diff --git a/components/camel-aws/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/client/impl/Sns2ClientIAMOptimized.java b/components/camel-aws/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/client/impl/Sns2ClientIAMOptimized.java deleted file mode 100644 index b00335e2efaa..000000000000 --- a/components/camel-aws/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/client/impl/Sns2ClientIAMOptimized.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.camel.component.aws2.sns.client.impl; - -import java.net.URI; - -import org.apache.camel.component.aws2.sns.Sns2Configuration; -import org.apache.camel.component.aws2.sns.client.Sns2InternalClient; -import org.apache.camel.util.ObjectHelper; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import software.amazon.awssdk.http.SdkHttpClient; -import software.amazon.awssdk.http.SdkHttpConfigurationOption; -import software.amazon.awssdk.http.apache.ApacheHttpClient; -import software.amazon.awssdk.http.apache.ProxyConfiguration; -import software.amazon.awssdk.regions.Region; -import software.amazon.awssdk.services.sns.SnsClient; -import software.amazon.awssdk.services.sns.SnsClientBuilder; -import software.amazon.awssdk.utils.AttributeMap; - -/** - * Manage an AWS SNS client for all users to use. This implementation is for remote instances to manage the credentials - * on their own (eliminating credential rotations) - */ -public class Sns2ClientIAMOptimized implements Sns2InternalClient { - private static final Logger LOG = LoggerFactory.getLogger(Sns2ClientIAMOptimized.class); - private Sns2Configuration configuration; - - /** - * Constructor that uses the config file. - */ - public Sns2ClientIAMOptimized(Sns2Configuration configuration) { - LOG.trace("Creating an AWS SNS client for working on AWS Services"); - this.configuration = configuration; - } - - /** - * Getting the SNS AWS client that is used. - * - * @return Amazon SNS Client. - */ - @Override - public SnsClient getSNSClient() { - SnsClient client = null; - SnsClientBuilder clientBuilder = SnsClient.builder(); - ProxyConfiguration.Builder proxyConfig = null; - ApacheHttpClient.Builder httpClientBuilder = null; - - if (ObjectHelper.isNotEmpty(configuration.getProxyHost()) && ObjectHelper.isNotEmpty(configuration.getProxyPort())) { - proxyConfig = ProxyConfiguration.builder(); - URI proxyEndpoint = URI.create(configuration.getProxyProtocol() + "://" + configuration.getProxyHost() + ":" - + configuration.getProxyPort()); - proxyConfig.endpoint(proxyEndpoint); - httpClientBuilder = ApacheHttpClient.builder().proxyConfiguration(proxyConfig.build()); - clientBuilder = clientBuilder.httpClientBuilder(httpClientBuilder); - } - if (ObjectHelper.isNotEmpty(configuration.getRegion())) { - clientBuilder = clientBuilder.region(Region.of(configuration.getRegion())); - } - if (configuration.isOverrideEndpoint()) { - clientBuilder.endpointOverride(URI.create(configuration.getUriEndpointOverride())); - } - if (configuration.isTrustAllCertificates()) { - if (httpClientBuilder == null) { - httpClientBuilder = ApacheHttpClient.builder(); - } - SdkHttpClient ahc = httpClientBuilder.buildWithDefaults(AttributeMap - .builder() - .put( - SdkHttpConfigurationOption.TRUST_ALL_CERTIFICATES, - Boolean.TRUE) - .build()); - // set created http client to use instead of builder - clientBuilder.httpClient(ahc); - clientBuilder.httpClientBuilder(null); - } - client = clientBuilder.build(); - return client; - } -} diff --git a/components/camel-aws/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/client/impl/Sns2ClientIAMProfileOptimized.java b/components/camel-aws/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/client/impl/Sns2ClientIAMProfileOptimized.java deleted file mode 100644 index 4ee52ba887ab..000000000000 --- a/components/camel-aws/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/client/impl/Sns2ClientIAMProfileOptimized.java +++ /dev/null @@ -1,99 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.camel.component.aws2.sns.client.impl; - -import java.net.URI; - -import org.apache.camel.component.aws2.sns.Sns2Configuration; -import org.apache.camel.component.aws2.sns.client.Sns2InternalClient; -import org.apache.camel.util.ObjectHelper; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider; -import software.amazon.awssdk.http.SdkHttpClient; -import software.amazon.awssdk.http.SdkHttpConfigurationOption; -import software.amazon.awssdk.http.apache.ApacheHttpClient; -import software.amazon.awssdk.http.apache.ProxyConfiguration; -import software.amazon.awssdk.regions.Region; -import software.amazon.awssdk.services.sns.SnsClient; -import software.amazon.awssdk.services.sns.SnsClientBuilder; -import software.amazon.awssdk.utils.AttributeMap; - -/** - * Manage an AWS SNS client for all users to use. This implementation is for remote instances to manage the credentials - * on their own (eliminating credential rotations) - */ -public class Sns2ClientIAMProfileOptimized implements Sns2InternalClient { - private static final Logger LOG = LoggerFactory.getLogger(Sns2ClientIAMProfileOptimized.class); - private Sns2Configuration configuration; - - /** - * Constructor that uses the config file. - */ - public Sns2ClientIAMProfileOptimized(Sns2Configuration configuration) { - LOG.trace("Creating an AWS SNS client for working on AWS Services"); - this.configuration = configuration; - } - - /** - * Getting the SNS AWS client that is used. - * - * @return Amazon SNS Client. - */ - @Override - public SnsClient getSNSClient() { - SnsClient client = null; - SnsClientBuilder clientBuilder = SnsClient.builder(); - ProxyConfiguration.Builder proxyConfig = null; - ApacheHttpClient.Builder httpClientBuilder = null; - - if (ObjectHelper.isNotEmpty(configuration.getProxyHost()) && ObjectHelper.isNotEmpty(configuration.getProxyPort())) { - proxyConfig = ProxyConfiguration.builder(); - URI proxyEndpoint = URI.create(configuration.getProxyProtocol() + "://" + configuration.getProxyHost() + ":" - + configuration.getProxyPort()); - proxyConfig.endpoint(proxyEndpoint); - httpClientBuilder = ApacheHttpClient.builder().proxyConfiguration(proxyConfig.build()); - clientBuilder = clientBuilder.httpClientBuilder(httpClientBuilder); - } - if (configuration.getProfileCredentialsName() != null) { - clientBuilder = clientBuilder - .credentialsProvider(ProfileCredentialsProvider.create(configuration.getProfileCredentialsName())); - } - if (ObjectHelper.isNotEmpty(configuration.getRegion())) { - clientBuilder = clientBuilder.region(Region.of(configuration.getRegion())); - } - if (configuration.isOverrideEndpoint()) { - clientBuilder.endpointOverride(URI.create(configuration.getUriEndpointOverride())); - } - if (configuration.isTrustAllCertificates()) { - if (httpClientBuilder == null) { - httpClientBuilder = ApacheHttpClient.builder(); - } - SdkHttpClient ahc = httpClientBuilder.buildWithDefaults(AttributeMap - .builder() - .put( - SdkHttpConfigurationOption.TRUST_ALL_CERTIFICATES, - Boolean.TRUE) - .build()); - // set created http client to use instead of builder - clientBuilder.httpClient(ahc); - clientBuilder.httpClientBuilder(null); - } - client = clientBuilder.build(); - return client; - } -} diff --git a/components/camel-aws/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/client/impl/Sns2ClientSessionTokenImpl.java b/components/camel-aws/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/client/impl/Sns2ClientSessionTokenImpl.java deleted file mode 100644 index 350f3e595438..000000000000 --- a/components/camel-aws/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/client/impl/Sns2ClientSessionTokenImpl.java +++ /dev/null @@ -1,110 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.camel.component.aws2.sns.client.impl; - -import java.net.URI; - -import org.apache.camel.component.aws2.sns.Sns2Configuration; -import org.apache.camel.component.aws2.sns.client.Sns2InternalClient; -import org.apache.camel.util.ObjectHelper; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import software.amazon.awssdk.auth.credentials.AwsSessionCredentials; -import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; -import software.amazon.awssdk.http.SdkHttpClient; -import software.amazon.awssdk.http.SdkHttpConfigurationOption; -import software.amazon.awssdk.http.apache.ApacheHttpClient; -import software.amazon.awssdk.http.apache.ProxyConfiguration; -import software.amazon.awssdk.regions.Region; -import software.amazon.awssdk.services.sns.SnsClient; -import software.amazon.awssdk.services.sns.SnsClientBuilder; -import software.amazon.awssdk.utils.AttributeMap; - -/** - * Manage an AWS SNS client for all users to use. This implementation is for local instances to use a static and solid - * credential set. - */ -public class Sns2ClientSessionTokenImpl implements Sns2InternalClient { - private static final Logger LOG = LoggerFactory.getLogger(Sns2ClientSessionTokenImpl.class); - private Sns2Configuration configuration; - - /** - * Constructor that uses the config file. - */ - public Sns2ClientSessionTokenImpl(Sns2Configuration configuration) { - LOG.trace("Creating an AWS SNS manager using static credentials."); - this.configuration = configuration; - } - - /** - * Getting the SNS aws client that is used. - * - * @return Amazon SNS Client. - */ - @Override - public SnsClient getSNSClient() { - SnsClient client = null; - SnsClientBuilder clientBuilder = SnsClient.builder(); - ProxyConfiguration.Builder proxyConfig = null; - ApacheHttpClient.Builder httpClientBuilder = null; - boolean isClientConfigFound = false; - if (ObjectHelper.isNotEmpty(configuration.getProxyHost()) && ObjectHelper.isNotEmpty(configuration.getProxyPort())) { - proxyConfig = ProxyConfiguration.builder(); - URI proxyEndpoint = URI.create(configuration.getProxyProtocol() + "://" + configuration.getProxyHost() + ":" - + configuration.getProxyPort()); - proxyConfig.endpoint(proxyEndpoint); - httpClientBuilder = ApacheHttpClient.builder().proxyConfiguration(proxyConfig.build()); - isClientConfigFound = true; - } - if (configuration.getAccessKey() != null && configuration.getSecretKey() != null) { - AwsSessionCredentials cred = AwsSessionCredentials.create(configuration.getAccessKey(), - configuration.getSecretKey(), configuration.getSessionToken()); - if (isClientConfigFound) { - clientBuilder = clientBuilder.httpClientBuilder(httpClientBuilder) - .credentialsProvider(StaticCredentialsProvider.create(cred)); - } else { - clientBuilder = clientBuilder.credentialsProvider(StaticCredentialsProvider.create(cred)); - } - } else { - if (!isClientConfigFound) { - clientBuilder = clientBuilder.httpClientBuilder(httpClientBuilder); - } - } - if (ObjectHelper.isNotEmpty(configuration.getRegion())) { - clientBuilder = clientBuilder.region(Region.of(configuration.getRegion())); - } - if (configuration.isOverrideEndpoint()) { - clientBuilder.endpointOverride(URI.create(configuration.getUriEndpointOverride())); - } - if (configuration.isTrustAllCertificates()) { - if (httpClientBuilder == null) { - httpClientBuilder = ApacheHttpClient.builder(); - } - SdkHttpClient ahc = httpClientBuilder.buildWithDefaults(AttributeMap - .builder() - .put( - SdkHttpConfigurationOption.TRUST_ALL_CERTIFICATES, - Boolean.TRUE) - .build()); - // set created http client to use instead of builder - clientBuilder.httpClient(ahc); - clientBuilder.httpClientBuilder(null); - } - client = clientBuilder.build(); - return client; - } -} diff --git a/components/camel-aws/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/client/impl/Sns2ClientStandardImpl.java b/components/camel-aws/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/client/impl/Sns2ClientStandardImpl.java deleted file mode 100644 index b70259618f3f..000000000000 --- a/components/camel-aws/camel-aws2-sns/src/main/java/org/apache/camel/component/aws2/sns/client/impl/Sns2ClientStandardImpl.java +++ /dev/null @@ -1,109 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.camel.component.aws2.sns.client.impl; - -import java.net.URI; - -import org.apache.camel.component.aws2.sns.Sns2Configuration; -import org.apache.camel.component.aws2.sns.client.Sns2InternalClient; -import org.apache.camel.util.ObjectHelper; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; -import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; -import software.amazon.awssdk.http.SdkHttpClient; -import software.amazon.awssdk.http.SdkHttpConfigurationOption; -import software.amazon.awssdk.http.apache.ApacheHttpClient; -import software.amazon.awssdk.http.apache.ProxyConfiguration; -import software.amazon.awssdk.regions.Region; -import software.amazon.awssdk.services.sns.SnsClient; -import software.amazon.awssdk.services.sns.SnsClientBuilder; -import software.amazon.awssdk.utils.AttributeMap; - -/** - * Manage an AWS SNS client for all users to use. This implementation is for local instances to use a static and solid - * credential set. - */ -public class Sns2ClientStandardImpl implements Sns2InternalClient { - private static final Logger LOG = LoggerFactory.getLogger(Sns2ClientStandardImpl.class); - private Sns2Configuration configuration; - - /** - * Constructor that uses the config file. - */ - public Sns2ClientStandardImpl(Sns2Configuration configuration) { - LOG.trace("Creating an AWS SNS manager using static credentials."); - this.configuration = configuration; - } - - /** - * Getting the SNS aws client that is used. - * - * @return Amazon SNS Client. - */ - @Override - public SnsClient getSNSClient() { - SnsClient client = null; - SnsClientBuilder clientBuilder = SnsClient.builder(); - ProxyConfiguration.Builder proxyConfig = null; - ApacheHttpClient.Builder httpClientBuilder = null; - boolean isClientConfigFound = false; - if (ObjectHelper.isNotEmpty(configuration.getProxyHost()) && ObjectHelper.isNotEmpty(configuration.getProxyPort())) { - proxyConfig = ProxyConfiguration.builder(); - URI proxyEndpoint = URI.create(configuration.getProxyProtocol() + "://" + configuration.getProxyHost() + ":" - + configuration.getProxyPort()); - proxyConfig.endpoint(proxyEndpoint); - httpClientBuilder = ApacheHttpClient.builder().proxyConfiguration(proxyConfig.build()); - isClientConfigFound = true; - } - if (configuration.getAccessKey() != null && configuration.getSecretKey() != null) { - AwsBasicCredentials cred = AwsBasicCredentials.create(configuration.getAccessKey(), configuration.getSecretKey()); - if (isClientConfigFound) { - clientBuilder = clientBuilder.httpClientBuilder(httpClientBuilder) - .credentialsProvider(StaticCredentialsProvider.create(cred)); - } else { - clientBuilder = clientBuilder.credentialsProvider(StaticCredentialsProvider.create(cred)); - } - } else { - if (!isClientConfigFound) { - clientBuilder = clientBuilder.httpClientBuilder(httpClientBuilder); - } - } - if (ObjectHelper.isNotEmpty(configuration.getRegion())) { - clientBuilder = clientBuilder.region(Region.of(configuration.getRegion())); - } - if (configuration.isOverrideEndpoint()) { - clientBuilder.endpointOverride(URI.create(configuration.getUriEndpointOverride())); - } - if (configuration.isTrustAllCertificates()) { - if (httpClientBuilder == null) { - httpClientBuilder = ApacheHttpClient.builder(); - } - SdkHttpClient ahc = httpClientBuilder.buildWithDefaults(AttributeMap - .builder() - .put( - SdkHttpConfigurationOption.TRUST_ALL_CERTIFICATES, - Boolean.TRUE) - .build()); - // set created http client to use instead of builder - clientBuilder.httpClient(ahc); - clientBuilder.httpClientBuilder(null); - } - client = clientBuilder.build(); - return client; - } -}
