Example usage for com.amazonaws.services.sqs.model QueueAttributeName Policy

List of usage examples for com.amazonaws.services.sqs.model QueueAttributeName Policy

Introduction

In this page you can find the example usage for com.amazonaws.services.sqs.model QueueAttributeName Policy.

Prototype

QueueAttributeName Policy

To view the source code for com.amazonaws.services.sqs.model QueueAttributeName Policy.

Click Source Link

Usage

From source file:io.konig.maven.CreateAwsSqsQueueAction.java

License:Apache License

public AwsDeployment from(String path) throws Exception {
    String cfTemplatePresent = System.getProperty("cfTemplatePresent");
    if (cfTemplatePresent == null || cfTemplatePresent.equals("N")) {
        try {//from  w w w. jav  a2s .  com
            File file = deployment.file(path);
            ObjectMapper mapper = new ObjectMapper();
            S3Bucket bucket = mapper.readValue(file, S3Bucket.class);
            deployment.verifyAWSCredentials();

            QueueConfiguration queueConfig = bucket.getNotificationConfiguration().getQueueConfiguration();

            if (queueConfig != null && queueConfig.getQueue() != null) {
                String accountId = "";
                if (System.getProperty("aws-account-id") != null) {
                    accountId = System.getProperty("aws-account-id");
                }

                Queue queue = queueConfig.getQueue();
                Regions regions = Regions.fromName(queue.getRegion());
                AmazonSQS sqs = AmazonSQSClientBuilder.standard().withCredentials(deployment.getCredential())
                        .withRegion(regions).build();
                AmazonSNS sns = AmazonSNSClientBuilder.standard().withCredentials(deployment.getCredential())
                        .withRegion(regions).build();

                CreateQueueResult result = sqs.createQueue(queue.getResourceName());

                String topicArn = StringUtils.replaceOnce(
                        bucket.getNotificationConfiguration().getTopicConfiguration().getTopicArn(),
                        "${aws-account-id}", accountId);
                String queueArn = StringUtils.replaceOnce(
                        bucket.getNotificationConfiguration().getQueueConfiguration().getQueueArn(),
                        "${aws-account-id}", accountId);

                deployment.setResponse("Queue  " + queueArn + " is created");

                Policy policy = new Policy()
                        .withStatements(new Statement(Effect.Allow).withPrincipals(Principal.AllUsers)
                                .withActions(SQSActions.SendMessage).withResources(new Resource(queueArn))
                                .withConditions(ConditionFactory.newSourceArnCondition(topicArn)));

                Map<String, String> queueAttributes = new HashMap<String, String>();
                queueAttributes.put(QueueAttributeName.Policy.toString(), policy.toJson());

                deployment.setResponse("Queue Policy Configured : " + policy.toJson());

                sqs.setQueueAttributes(new SetQueueAttributesRequest(result.getQueueUrl(), queueAttributes));

                Topics.subscribeQueue(sns, sqs, topicArn, result.getQueueUrl());

                deployment.setResponse(
                        "Subscription is created : Topic [" + topicArn + "], Queue [" + queueArn + "]");
            } else {
                deployment.setResponse("Queue Configuration Failed");
            }

        } catch (Exception e) {
            throw e;
        }
    } else {
        deployment.setResponse("Queue will be created through cloud formation template");
    }
    return deployment;
}

From source file:org.apache.camel.component.aws.sqs.SqsEndpoint.java

License:Apache License

protected void createQueue(AmazonSQS client) {
    LOG.trace("Queue '{}' doesn't exist. Will create it...", configuration.getQueueName());

    // creates a new queue, or returns the URL of an existing one
    CreateQueueRequest request = new CreateQueueRequest(configuration.getQueueName());
    if (getConfiguration().getDefaultVisibilityTimeout() != null) {
        request.getAttributes().put(QueueAttributeName.VisibilityTimeout.name(),
                String.valueOf(getConfiguration().getDefaultVisibilityTimeout()));
    }//from  w w w  .  j  a  v a 2 s  . c o m
    if (getConfiguration().getMaximumMessageSize() != null) {
        request.getAttributes().put(QueueAttributeName.MaximumMessageSize.name(),
                String.valueOf(getConfiguration().getMaximumMessageSize()));
    }
    if (getConfiguration().getMessageRetentionPeriod() != null) {
        request.getAttributes().put(QueueAttributeName.MessageRetentionPeriod.name(),
                String.valueOf(getConfiguration().getMessageRetentionPeriod()));
    }
    if (getConfiguration().getPolicy() != null) {
        request.getAttributes().put(QueueAttributeName.Policy.name(),
                String.valueOf(getConfiguration().getPolicy()));
    }
    if (getConfiguration().getReceiveMessageWaitTimeSeconds() != null) {
        request.getAttributes().put(QueueAttributeName.ReceiveMessageWaitTimeSeconds.name(),
                String.valueOf(getConfiguration().getReceiveMessageWaitTimeSeconds()));
    }
    LOG.trace("Creating queue [{}] with request [{}]...", configuration.getQueueName(), request);

    CreateQueueResult queueResult = client.createQueue(request);
    queueUrl = queueResult.getQueueUrl();

    LOG.trace("Queue created and available at: {}", queueUrl);
}

From source file:org.apache.camel.component.aws.sqs.SqsEndpoint.java

License:Apache License

private void updateQueueAttributes(AmazonSQS client) {
    SetQueueAttributesRequest request = new SetQueueAttributesRequest();
    request.setQueueUrl(queueUrl);//from   w  ww.  j  a  v  a  2 s  . c o m
    if (getConfiguration().getDefaultVisibilityTimeout() != null) {
        request.getAttributes().put(QueueAttributeName.VisibilityTimeout.name(),
                String.valueOf(getConfiguration().getDefaultVisibilityTimeout()));
    }
    if (getConfiguration().getMaximumMessageSize() != null) {
        request.getAttributes().put(QueueAttributeName.MaximumMessageSize.name(),
                String.valueOf(getConfiguration().getMaximumMessageSize()));
    }
    if (getConfiguration().getMessageRetentionPeriod() != null) {
        request.getAttributes().put(QueueAttributeName.MessageRetentionPeriod.name(),
                String.valueOf(getConfiguration().getMessageRetentionPeriod()));
    }
    if (getConfiguration().getPolicy() != null) {
        request.getAttributes().put(QueueAttributeName.Policy.name(),
                String.valueOf(getConfiguration().getPolicy()));
    }
    if (getConfiguration().getReceiveMessageWaitTimeSeconds() != null) {
        request.getAttributes().put(QueueAttributeName.ReceiveMessageWaitTimeSeconds.name(),
                String.valueOf(getConfiguration().getReceiveMessageWaitTimeSeconds()));
    }
    if (!request.getAttributes().isEmpty()) {
        LOG.trace("Updating queue '{}' with the provided queue attributes...", configuration.getQueueName());
        client.setQueueAttributes(request);
        LOG.trace("Queue '{}' updated and available at {}'", configuration.getQueueName(), queueUrl);
    }
}