This is an automated email from the ASF dual-hosted git repository. acosentino pushed a commit to branch CAMEL-22786-complete in repository https://gitbox.apache.org/repos/asf/camel.git
commit 1ff9e4c5ee701528373d0b990952e5d44945ce17 Author: Andrea Cosentino <[email protected]> AuthorDate: Tue Dec 16 15:12:51 2025 +0100 CAMEL-22786 - Camel-AWS: Extract common logic for clients instantiation in a separated module - AWS MQ Signed-off-by: Andrea Cosentino <[email protected]> --- components/camel-aws/camel-aws2-mq/pom.xml | 4 + .../camel/component/aws2/mq/MQ2Configuration.java | 9 +- .../camel/component/aws2/mq/MQ2Endpoint.java | 2 +- .../component/aws2/mq/client/MQ2ClientFactory.java | 28 ++---- .../aws2/mq/client/MQ2InternalClient.java | 32 ------ .../mq/client/impl/MQ2ClientOptimizedImpl.java | 93 ----------------- .../client/impl/MQ2ClientProfileOptimizedImpl.java | 98 ------------------ .../mq/client/impl/MQ2ClientSessionTokenImpl.java | 111 --------------------- .../aws2/mq/client/impl/MQ2ClientStandardImpl.java | 109 -------------------- .../component/aws2/mq/MQ2ClientFactoryTest.java | 52 +++++----- 10 files changed, 47 insertions(+), 491 deletions(-) diff --git a/components/camel-aws/camel-aws2-mq/pom.xml b/components/camel-aws/camel-aws2-mq/pom.xml index 8767cd9f332a..f55d59b54d88 100644 --- a/components/camel-aws/camel-aws2-mq/pom.xml +++ b/components/camel-aws/camel-aws2-mq/pom.xml @@ -40,6 +40,10 @@ <groupId>org.apache.camel</groupId> <artifactId>camel-support</artifactId> </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-aws-common</artifactId> + </dependency> <dependency> <groupId>software.amazon.awssdk</groupId> <artifactId>mq</artifactId> diff --git a/components/camel-aws/camel-aws2-mq/src/main/java/org/apache/camel/component/aws2/mq/MQ2Configuration.java b/components/camel-aws/camel-aws2-mq/src/main/java/org/apache/camel/component/aws2/mq/MQ2Configuration.java index 763a74b1e6d3..11de90978f69 100644 --- a/components/camel-aws/camel-aws2-mq/src/main/java/org/apache/camel/component/aws2/mq/MQ2Configuration.java +++ b/components/camel-aws/camel-aws2-mq/src/main/java/org/apache/camel/component/aws2/mq/MQ2Configuration.java @@ -17,6 +17,7 @@ package org.apache.camel.component.aws2.mq; 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.mq.MqClient; @UriParams -public class MQ2Configuration implements Cloneable { +public class MQ2Configuration implements Cloneable, AwsCommonConfiguration { @UriPath(description = "Logical name") @Metadata(required = true) @@ -217,14 +218,16 @@ public class MQ2Configuration implements Cloneable { * Set whether the MQ client should expect to load credentials through a default credentials provider or to expect * static credentials to be passed in. */ - public void setUseDefaultCredentialsProvider(Boolean useDefaultCredentialsProvider) { + public void setUseDefaultCredentialsProvider(boolean useDefaultCredentialsProvider) { this.useDefaultCredentialsProvider = useDefaultCredentialsProvider; } - public Boolean isUseDefaultCredentialsProvider() { + @Override + public boolean isUseDefaultCredentialsProvider() { return useDefaultCredentialsProvider; } + @Override public boolean isUseProfileCredentialsProvider() { return useProfileCredentialsProvider; } diff --git a/components/camel-aws/camel-aws2-mq/src/main/java/org/apache/camel/component/aws2/mq/MQ2Endpoint.java b/components/camel-aws/camel-aws2-mq/src/main/java/org/apache/camel/component/aws2/mq/MQ2Endpoint.java index e81b9c56bca4..af3dfc01bcc0 100644 --- a/components/camel-aws/camel-aws2-mq/src/main/java/org/apache/camel/component/aws2/mq/MQ2Endpoint.java +++ b/components/camel-aws/camel-aws2-mq/src/main/java/org/apache/camel/component/aws2/mq/MQ2Endpoint.java @@ -67,7 +67,7 @@ public class MQ2Endpoint extends ScheduledPollEndpoint implements EndpointServic mqClient = configuration.getAmazonMqClient() != null ? configuration.getAmazonMqClient() - : MQ2ClientFactory.getMqClient(configuration).getMqClient(); + : MQ2ClientFactory.getMqClient(configuration); } @Override diff --git a/components/camel-aws/camel-aws2-mq/src/main/java/org/apache/camel/component/aws2/mq/client/MQ2ClientFactory.java b/components/camel-aws/camel-aws2-mq/src/main/java/org/apache/camel/component/aws2/mq/client/MQ2ClientFactory.java index 56df2b0cbd3d..70ba82c7adcd 100644 --- a/components/camel-aws/camel-aws2-mq/src/main/java/org/apache/camel/component/aws2/mq/client/MQ2ClientFactory.java +++ b/components/camel-aws/camel-aws2-mq/src/main/java/org/apache/camel/component/aws2/mq/client/MQ2ClientFactory.java @@ -16,14 +16,12 @@ */ package org.apache.camel.component.aws2.mq.client; +import org.apache.camel.component.aws.common.AwsClientBuilderUtil; import org.apache.camel.component.aws2.mq.MQ2Configuration; -import org.apache.camel.component.aws2.mq.client.impl.MQ2ClientOptimizedImpl; -import org.apache.camel.component.aws2.mq.client.impl.MQ2ClientProfileOptimizedImpl; -import org.apache.camel.component.aws2.mq.client.impl.MQ2ClientSessionTokenImpl; -import org.apache.camel.component.aws2.mq.client.impl.MQ2ClientStandardImpl; +import software.amazon.awssdk.services.mq.MqClient; /** - * Factory class to return the correct type of AWS MQ client. + * Factory class to create AWS MQ clients using common configuration. */ public final class MQ2ClientFactory { @@ -31,20 +29,14 @@ public final class MQ2ClientFactory { } /** - * Return the correct AWS Mq client (based on remote vs local). + * Create an MQ client based on configuration. * - * @param configuration configuration - * @return MqClient + * @param configuration The MQ configuration + * @return Configured MqClient */ - public static MQ2InternalClient getMqClient(MQ2Configuration configuration) { - if (Boolean.TRUE.equals(configuration.isUseDefaultCredentialsProvider())) { - return new MQ2ClientOptimizedImpl(configuration); - } else if (Boolean.TRUE.equals(configuration.isUseProfileCredentialsProvider())) { - return new MQ2ClientProfileOptimizedImpl(configuration); - } else if (Boolean.TRUE.equals(configuration.isUseSessionCredentials())) { - return new MQ2ClientSessionTokenImpl(configuration); - } else { - return new MQ2ClientStandardImpl(configuration); - } + public static MqClient getMqClient(MQ2Configuration configuration) { + return AwsClientBuilderUtil.buildClient( + configuration, + MqClient::builder); } } diff --git a/components/camel-aws/camel-aws2-mq/src/main/java/org/apache/camel/component/aws2/mq/client/MQ2InternalClient.java b/components/camel-aws/camel-aws2-mq/src/main/java/org/apache/camel/component/aws2/mq/client/MQ2InternalClient.java deleted file mode 100644 index 743fa57d80fd..000000000000 --- a/components/camel-aws/camel-aws2-mq/src/main/java/org/apache/camel/component/aws2/mq/client/MQ2InternalClient.java +++ /dev/null @@ -1,32 +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.mq.client; - -import software.amazon.awssdk.services.mq.MqClient; - -/** - * Manage the required actions of an MQ client for either local or remote. - */ -public interface MQ2InternalClient { - - /** - * Returns an MQ client after a factory method determines which one to return. - * - * @return MqClient MqClient - */ - MqClient getMqClient(); -} diff --git a/components/camel-aws/camel-aws2-mq/src/main/java/org/apache/camel/component/aws2/mq/client/impl/MQ2ClientOptimizedImpl.java b/components/camel-aws/camel-aws2-mq/src/main/java/org/apache/camel/component/aws2/mq/client/impl/MQ2ClientOptimizedImpl.java deleted file mode 100644 index 19abefef76bf..000000000000 --- a/components/camel-aws/camel-aws2-mq/src/main/java/org/apache/camel/component/aws2/mq/client/impl/MQ2ClientOptimizedImpl.java +++ /dev/null @@ -1,93 +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.mq.client.impl; - -import java.net.URI; - -import org.apache.camel.component.aws2.mq.MQ2Configuration; -import org.apache.camel.component.aws2.mq.client.MQ2InternalClient; -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.mq.MqClient; -import software.amazon.awssdk.services.mq.MqClientBuilder; -import software.amazon.awssdk.utils.AttributeMap; - -/** - * Manage an AWS MQ client for all users to use (enabling temporary creds). This implementation is for remote instances - * to manage the credentials on their own (eliminating credential rotations) - */ -public class MQ2ClientOptimizedImpl implements MQ2InternalClient { - private static final Logger LOG = LoggerFactory.getLogger(MQ2ClientOptimizedImpl.class); - private MQ2Configuration configuration; - - /** - * Constructor that uses the config file. - */ - public MQ2ClientOptimizedImpl(MQ2Configuration configuration) { - LOG.trace("Creating an AWS MQ client for an ec2 instance with IAM temporary credentials (normal for ec2s)."); - this.configuration = configuration; - } - - /** - * Getting the MQ aws client that is used. - * - * @return MQ Client. - */ - @Override - public MqClient getMqClient() { - MqClient client = null; - MqClientBuilder clientBuilder = MqClient.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-mq/src/main/java/org/apache/camel/component/aws2/mq/client/impl/MQ2ClientProfileOptimizedImpl.java b/components/camel-aws/camel-aws2-mq/src/main/java/org/apache/camel/component/aws2/mq/client/impl/MQ2ClientProfileOptimizedImpl.java deleted file mode 100644 index a9e75f5f2669..000000000000 --- a/components/camel-aws/camel-aws2-mq/src/main/java/org/apache/camel/component/aws2/mq/client/impl/MQ2ClientProfileOptimizedImpl.java +++ /dev/null @@ -1,98 +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.mq.client.impl; - -import java.net.URI; - -import org.apache.camel.component.aws2.mq.MQ2Configuration; -import org.apache.camel.component.aws2.mq.client.MQ2InternalClient; -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.mq.MqClient; -import software.amazon.awssdk.services.mq.MqClientBuilder; -import software.amazon.awssdk.utils.AttributeMap; - -/** - * Manage an AWS MQ client for all users to use (enabling temporary creds). This implementation is for remote instances - * to manage the credentials on their own (eliminating credential rotations) - */ -public class MQ2ClientProfileOptimizedImpl implements MQ2InternalClient { - private static final Logger LOG = LoggerFactory.getLogger(MQ2ClientProfileOptimizedImpl.class); - private MQ2Configuration configuration; - - /** - * Constructor that uses the config file. - */ - public MQ2ClientProfileOptimizedImpl(MQ2Configuration configuration) { - LOG.trace("Creating an AWS MQ client for an ec2 instance with IAM temporary credentials (normal for ec2s)."); - this.configuration = configuration; - } - - /** - * Getting the MQ aws client that is used. - * - * @return MQ Client. - */ - @Override - public MqClient getMqClient() { - MqClient client = null; - MqClientBuilder clientBuilder = MqClient.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.httpClientBuilder(httpClientBuilder) - .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-mq/src/main/java/org/apache/camel/component/aws2/mq/client/impl/MQ2ClientSessionTokenImpl.java b/components/camel-aws/camel-aws2-mq/src/main/java/org/apache/camel/component/aws2/mq/client/impl/MQ2ClientSessionTokenImpl.java deleted file mode 100644 index a447ec868738..000000000000 --- a/components/camel-aws/camel-aws2-mq/src/main/java/org/apache/camel/component/aws2/mq/client/impl/MQ2ClientSessionTokenImpl.java +++ /dev/null @@ -1,111 +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.mq.client.impl; - -import java.net.URI; - -import org.apache.camel.component.aws2.mq.MQ2Configuration; -import org.apache.camel.component.aws2.mq.client.MQ2InternalClient; -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.mq.MqClient; -import software.amazon.awssdk.services.mq.MqClientBuilder; -import software.amazon.awssdk.utils.AttributeMap; - -/** - * Manage an AWS MQ client for all users to use. This implementation is for local instances to use a static and solid - * credential set. - */ -public class MQ2ClientSessionTokenImpl implements MQ2InternalClient { - private static final Logger LOG = LoggerFactory.getLogger(MQ2ClientSessionTokenImpl.class); - private MQ2Configuration configuration; - - /** - * Constructor that uses the config file. - */ - public MQ2ClientSessionTokenImpl(MQ2Configuration configuration) { - LOG.trace("Creating an AWS MQ manager using static credentials."); - this.configuration = configuration; - } - - /** - * Getting the MQ AWS client that is used. - * - * @return Amazon MQ Client. - */ - @Override - public MqClient getMqClient() { - MqClient client = null; - MqClientBuilder clientBuilder = MqClient.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 - && configuration.getSessionToken() != 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-mq/src/main/java/org/apache/camel/component/aws2/mq/client/impl/MQ2ClientStandardImpl.java b/components/camel-aws/camel-aws2-mq/src/main/java/org/apache/camel/component/aws2/mq/client/impl/MQ2ClientStandardImpl.java deleted file mode 100644 index f9f9060bae6b..000000000000 --- a/components/camel-aws/camel-aws2-mq/src/main/java/org/apache/camel/component/aws2/mq/client/impl/MQ2ClientStandardImpl.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.mq.client.impl; - -import java.net.URI; - -import org.apache.camel.component.aws2.mq.MQ2Configuration; -import org.apache.camel.component.aws2.mq.client.MQ2InternalClient; -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.mq.MqClient; -import software.amazon.awssdk.services.mq.MqClientBuilder; -import software.amazon.awssdk.utils.AttributeMap; - -/** - * Manage an AWS MQ client for all users to use. This implementation is for local instances to use a static and solid - * credential set. - */ -public class MQ2ClientStandardImpl implements MQ2InternalClient { - private static final Logger LOG = LoggerFactory.getLogger(MQ2ClientStandardImpl.class); - private MQ2Configuration configuration; - - /** - * Constructor that uses the config file. - */ - public MQ2ClientStandardImpl(MQ2Configuration configuration) { - LOG.trace("Creating an AWS MQ manager using static credentials."); - this.configuration = configuration; - } - - /** - * Getting the MQ AWS client that is used. - * - * @return Amazon MQ Client. - */ - @Override - public MqClient getMqClient() { - MqClient client = null; - MqClientBuilder clientBuilder = MqClient.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; - } -} diff --git a/components/camel-aws/camel-aws2-mq/src/test/java/org/apache/camel/component/aws2/mq/MQ2ClientFactoryTest.java b/components/camel-aws/camel-aws2-mq/src/test/java/org/apache/camel/component/aws2/mq/MQ2ClientFactoryTest.java index 6841bf3ef631..925a6348f9ee 100644 --- a/components/camel-aws/camel-aws2-mq/src/test/java/org/apache/camel/component/aws2/mq/MQ2ClientFactoryTest.java +++ b/components/camel-aws/camel-aws2-mq/src/test/java/org/apache/camel/component/aws2/mq/MQ2ClientFactoryTest.java @@ -17,43 +17,43 @@ package org.apache.camel.component.aws2.mq; import org.apache.camel.component.aws2.mq.client.MQ2ClientFactory; -import org.apache.camel.component.aws2.mq.client.MQ2InternalClient; -import org.apache.camel.component.aws2.mq.client.impl.MQ2ClientOptimizedImpl; -import org.apache.camel.component.aws2.mq.client.impl.MQ2ClientStandardImpl; import org.junit.jupiter.api.Test; +import software.amazon.awssdk.services.mq.MqClient; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertNotNull; public class MQ2ClientFactoryTest { @Test - public void getStandardMQClientDefault() { - MQ2Configuration mq2Configuration = new MQ2Configuration(); - MQ2InternalClient mqClient = MQ2ClientFactory.getMqClient(mq2Configuration); - assertTrue(mqClient instanceof MQ2ClientStandardImpl); + public void getMqClientWithDefaultCredentials() { + MQ2Configuration configuration = new MQ2Configuration(); + configuration.setUseDefaultCredentialsProvider(true); + configuration.setRegion("eu-west-1"); + MqClient mqClient = MQ2ClientFactory.getMqClient(configuration); + assertNotNull(mqClient); + mqClient.close(); } @Test - public void getStandardMQClient() { - MQ2Configuration mq2Configuration = new MQ2Configuration(); - mq2Configuration.setUseDefaultCredentialsProvider(false); - MQ2InternalClient mqClient = MQ2ClientFactory.getMqClient(mq2Configuration); - assertTrue(mqClient instanceof MQ2ClientStandardImpl); + public void getMqClientWithStaticCredentials() { + MQ2Configuration configuration = new MQ2Configuration(); + configuration.setAccessKey("testAccessKey"); + configuration.setSecretKey("testSecretKey"); + configuration.setRegion("eu-west-1"); + MqClient mqClient = MQ2ClientFactory.getMqClient(configuration); + assertNotNull(mqClient); + mqClient.close(); } @Test - public void getIAMOptimizedMQClient() { - MQ2Configuration mq2Configuration = new MQ2Configuration(); - mq2Configuration.setUseDefaultCredentialsProvider(true); - MQ2InternalClient mqClient = MQ2ClientFactory.getMqClient(mq2Configuration); - assertTrue(mqClient instanceof MQ2ClientOptimizedImpl); - } - - @Test - public void getSessionTokenMQClient() { - MQ2Configuration mq2Configuration = new MQ2Configuration(); - mq2Configuration.setUseDefaultCredentialsProvider(true); - MQ2InternalClient mqClient = MQ2ClientFactory.getMqClient(mq2Configuration); - assertTrue(mqClient instanceof MQ2ClientOptimizedImpl); + public void getMqClientWithEndpointOverride() { + MQ2Configuration configuration = new MQ2Configuration(); + configuration.setUseDefaultCredentialsProvider(true); + configuration.setRegion("eu-west-1"); + configuration.setOverrideEndpoint(true); + configuration.setUriEndpointOverride("http://localhost:4566"); + MqClient mqClient = MQ2ClientFactory.getMqClient(configuration); + assertNotNull(mqClient); + mqClient.close(); } }
