org.apache.camel.component.aws2.sns.Sns2Endpoint Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of camel-aws2-sns Show documentation
Show all versions of camel-aws2-sns Show documentation
A Camel Amazon SNS Web Service Component Version 2
/*
* 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;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import org.apache.camel.Category;
import org.apache.camel.Component;
import org.apache.camel.Consumer;
import org.apache.camel.Processor;
import org.apache.camel.Producer;
import org.apache.camel.component.aws2.sns.client.Sns2ClientFactory;
import org.apache.camel.health.HealthCheckHelper;
import org.apache.camel.impl.health.ComponentsHealthCheckRepository;
import org.apache.camel.spi.HeaderFilterStrategy;
import org.apache.camel.spi.HeaderFilterStrategyAware;
import org.apache.camel.spi.Metadata;
import org.apache.camel.spi.UriEndpoint;
import org.apache.camel.spi.UriParam;
import org.apache.camel.spi.UriPath;
import org.apache.camel.support.DefaultEndpoint;
import org.apache.camel.support.ResourceHelper;
import org.apache.camel.util.ObjectHelper;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import software.amazon.awssdk.awscore.exception.AwsServiceException;
import software.amazon.awssdk.services.sns.SnsClient;
import software.amazon.awssdk.services.sns.model.CreateTopicRequest;
import software.amazon.awssdk.services.sns.model.CreateTopicResponse;
import software.amazon.awssdk.services.sns.model.ListTopicsRequest;
import software.amazon.awssdk.services.sns.model.ListTopicsResponse;
import software.amazon.awssdk.services.sns.model.SetTopicAttributesRequest;
import software.amazon.awssdk.services.sns.model.SubscribeRequest;
import software.amazon.awssdk.services.sns.model.SubscribeResponse;
import software.amazon.awssdk.services.sns.model.Topic;
/**
* Send messages to an AWS Simple Notification Topic using AWS SDK version 2.x.
*/
@UriEndpoint(firstVersion = "3.1.0", scheme = "aws2-sns", title = "AWS Simple Notification System (SNS)",
syntax = "aws2-sns:topicNameOrArn", producerOnly = true,
category = { Category.CLOUD, Category.MESSAGING, Category.MOBILE }, headersClass = Sns2Constants.class)
public class Sns2Endpoint extends DefaultEndpoint implements HeaderFilterStrategyAware {
private static final Logger LOG = LoggerFactory.getLogger(Sns2Endpoint.class);
private ComponentsHealthCheckRepository healthCheckRepository;
private Sns2HealthCheck clientHealthCheck;
private SnsClient snsClient;
@UriPath(description = "Topic name or ARN")
@Metadata(required = true)
private String topicNameOrArn; // to support component docs
@UriParam
private final Sns2Configuration configuration;
@UriParam
private HeaderFilterStrategy headerFilterStrategy;
public Sns2Endpoint(String uri, Component component, Sns2Configuration configuration) {
super(uri, component);
this.configuration = configuration;
}
@Override
public HeaderFilterStrategy getHeaderFilterStrategy() {
return headerFilterStrategy;
}
/**
* To use a custom HeaderFilterStrategy to map headers to/from Camel.
*/
@Override
public void setHeaderFilterStrategy(HeaderFilterStrategy strategy) {
this.headerFilterStrategy = strategy;
}
@Override
public Consumer createConsumer(Processor processor) throws Exception {
throw new UnsupportedOperationException("You cannot receive messages from this endpoint");
}
@Override
public Producer createProducer() throws Exception {
return new Sns2Producer(this);
}
@Override
public void doInit() throws Exception {
super.doInit();
snsClient = configuration.getAmazonSNSClient() != null
? configuration.getAmazonSNSClient() : Sns2ClientFactory.getSnsClient(configuration).getSNSClient();
// check the setting the headerFilterStrategy
if (headerFilterStrategy == null) {
headerFilterStrategy = new Sns2HeaderFilterStrategy();
}
healthCheckRepository = HealthCheckHelper.getHealthCheckRepository(getCamelContext(),
ComponentsHealthCheckRepository.REPOSITORY_ID, ComponentsHealthCheckRepository.class);
if (healthCheckRepository != null) {
// Do not register the health check until we resolve CAMEL-18992
// clientHealthCheck = new Sns2HealthCheck(this, getId());
// healthCheckRepository.addHealthCheck(clientHealthCheck);
}
if (configuration.getTopicArn() == null) {
try {
String nextToken = null;
final String arnSuffix = ":" + configuration.getTopicName();
do {
ListTopicsRequest request = ListTopicsRequest.builder().nextToken(nextToken).build();
final ListTopicsResponse response = snsClient.listTopics(request);
nextToken = response.nextToken();
for (final Topic topic : response.topics()) {
if (topic.topicArn().endsWith(arnSuffix)) {
configuration.setTopicArn(topic.topicArn());
break;
}
}
} while (nextToken != null);
} catch (final AwsServiceException ase) {
LOG.trace("The list topics operation return the following error code {}", ase.awsErrorDetails().errorCode());
throw ase;
}
}
if (configuration.getTopicArn() == null && configuration.isAutoCreateTopic()) {
// creates a new topic, or returns the URL of an existing one
CreateTopicRequest.Builder builder = CreateTopicRequest.builder().name(configuration.getTopicName());
Map attributes = new HashMap<>();
if (configuration.isServerSideEncryptionEnabled()) {
if (ObjectHelper.isNotEmpty(configuration.getKmsMasterKeyId())) {
attributes.put("KmsMasterKeyId", configuration.getKmsMasterKeyId());
builder.attributes(attributes);
}
}
if (configuration.isFifoTopic()) {
attributes.put("FifoTopic", "true");
builder.attributes(attributes);
}
LOG.trace("Creating topic [{}] with request [{}]...", configuration.getTopicName(), builder);
CreateTopicResponse result = snsClient.createTopic(builder.build());
configuration.setTopicArn(result.topicArn());
LOG.trace("Topic created with Amazon resource name: {}", configuration.getTopicArn());
}
if (ObjectHelper.isNotEmpty(configuration.getPolicy())) {
LOG.trace("Updating topic [{}] with policy [{}]", configuration.getTopicArn(), configuration.getPolicy());
try (InputStream s = ResourceHelper.resolveMandatoryResourceAsInputStream(this.getCamelContext(),
getConfiguration().getPolicy())) {
String policy = IOUtils.toString(s, Charset.defaultCharset());
snsClient.setTopicAttributes(SetTopicAttributesRequest.builder().topicArn(configuration.getTopicArn())
.attributeName("Policy").attributeValue(policy)
.build());
LOG.trace("Topic policy updated");
}
}
if (configuration.isSubscribeSNStoSQS()) {
if (ObjectHelper.isNotEmpty(ObjectHelper.isNotEmpty(configuration.getQueueUrl()))) {
SubscribeResponse resp = snsClient.subscribe(SubscribeRequest.builder().topicArn(configuration.getTopicArn())
.protocol("sqs").endpoint(configuration.getQueueUrl())
.returnSubscriptionArn(true).build());
LOG.trace("Subscription of SQS Queue to SNS Topic done with Amazon resource name: {}", resp.subscriptionArn());
} else {
throw new IllegalArgumentException(
"Using the SubscribeSNStoSQS option require both AmazonSQSClient and Queue URL options");
}
}
}
@Override
public void doStop() throws Exception {
if (ObjectHelper.isEmpty(configuration.getAmazonSNSClient())) {
if (snsClient != null) {
snsClient.close();
}
}
if (healthCheckRepository != null && clientHealthCheck != null) {
healthCheckRepository.removeHealthCheck(clientHealthCheck);
clientHealthCheck = null;
}
super.doStop();
}
public Sns2Configuration getConfiguration() {
return configuration;
}
public void setSNSClient(SnsClient snsClient) {
this.snsClient = snsClient;
}
public SnsClient getSNSClient() {
return snsClient;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy