com.amazonaws.services.sns.AmazonSNSClient Java Examples
The following examples show how to use
com.amazonaws.services.sns.AmazonSNSClient.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SNSIntegrationTest.java From wildfly-camel with Apache License 2.0 | 6 votes |
public static void assertNoStaleTopic(AmazonSNSClient client, String when) { /* Remove the topic created by the the old version of this test. Note that this may break the old tests running * in parallel. */ client.listTopics().getTopics().stream() .filter(topic -> topic.getTopicArn().endsWith("MyNewTopic")) .forEach(t -> client.deleteTopic(t.getTopicArn())); List<String> staleInstances = client.listTopics().getTopics().stream() // .map(topic -> topic.getTopicArn().substring(topic.getTopicArn().lastIndexOf(':') + 1)) // extract the // topic name // from the ARN .filter(name -> !name.startsWith(SNSIntegrationTest.class.getSimpleName()) || System.currentTimeMillis() - AWSUtils.toEpochMillis(name) > AWSUtils.HOUR) // .collect(Collectors.toList()); Assert.assertEquals(String.format("Found stale SNS topics %s running the test: %s", when, staleInstances), 0, staleInstances.size()); }
Example #2
Source File: SNSInventoryUtilTest.java From pacbot with Apache License 2.0 | 6 votes |
/** * Fetch SNS topics test. * * @throws Exception the exception */ @SuppressWarnings("static-access") @Test public void fetchSNSTopicsTest() throws Exception { mockStatic(AmazonSNSClientBuilder.class); AmazonSNSClient snsClient = PowerMockito.mock(AmazonSNSClient.class); AmazonSNSClientBuilder amazonSNSClientBuilder = PowerMockito.mock(AmazonSNSClientBuilder.class); AWSStaticCredentialsProvider awsStaticCredentialsProvider = PowerMockito.mock(AWSStaticCredentialsProvider.class); PowerMockito.whenNew(AWSStaticCredentialsProvider.class).withAnyArguments().thenReturn(awsStaticCredentialsProvider); when(amazonSNSClientBuilder.standard()).thenReturn(amazonSNSClientBuilder); when(amazonSNSClientBuilder.withCredentials(anyObject())).thenReturn(amazonSNSClientBuilder); when(amazonSNSClientBuilder.withRegion(anyString())).thenReturn(amazonSNSClientBuilder); when(amazonSNSClientBuilder.build()).thenReturn(snsClient); ListSubscriptionsResult listSubscriptionDefinitionsResult = new ListSubscriptionsResult(); List<Subscription> subscriptionList = new ArrayList<>(); subscriptionList.add(new Subscription()); listSubscriptionDefinitionsResult.setSubscriptions(subscriptionList); when(snsClient.listSubscriptions( new ListSubscriptionsRequest())).thenReturn(listSubscriptionDefinitionsResult); assertThat(snsInventoryUtil.fetchSNSTopics(new BasicSessionCredentials("awsAccessKey", "awsSecretKey", "sessionToken"), "skipRegions", "account","accountName").size(), is(1)); }
Example #3
Source File: AwsGlacierInventoryRetriever.java From core with GNU General Public License v3.0 | 6 votes |
/********************** Member Functions **************************/ public AwsGlacierInventoryRetriever(String region) { // Get credentials from credentials file, environment variable, or // Java property. // See http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/auth/DefaultAWSCredentialsProviderChain.html AWSCredentialsProviderChain credentialsProvider = new DefaultAWSCredentialsProviderChain(); AWSCredentials credentials = credentialsProvider.getCredentials(); logger.debug("Read in credentials AWSAccessKeyId={} AWSSecretKey={}...", credentials.getAWSAccessKeyId(), credentials.getAWSSecretKey().substring(0, 4)); // Create the glacier client and set to specified region. glacierClient = new AmazonGlacierClient(credentials); glacierClient.setEndpoint("https://glacier." + region + ".amazonaws.com"); // Set up params needed for retrieving vault inventory sqsClient = new AmazonSQSClient(credentials); sqsClient.setEndpoint("https://sqs." + region + ".amazonaws.com"); snsClient = new AmazonSNSClient(credentials); snsClient.setEndpoint("https://sns." + region + ".amazonaws.com"); setupSQS(); setupSNS(); }
Example #4
Source File: SNSWebhookRunner.java From eplmp with Eclipse Public License 1.0 | 6 votes |
@Override public void run(Webhook webhook, String login, String email, String name, String subject, String content) { SNSWebhookApp webhookApp = (SNSWebhookApp) webhook.getWebhookApp(); String topicArn = webhookApp.getTopicArn(); String awsAccount = webhookApp.getAwsAccount(); String awsSecret = webhookApp.getAwsSecret(); String region = webhookApp.getRegion(); //AmazonSNSClient snsClient = new AmazonSNSClient(new BasicAWSCredentials(awsAccount, awsSecret)); //snsClient.setRegion(Region.getRegion(Regions.fromName(region))); AmazonSNS snsClient = AmazonSNSClient.builder() .withRegion(Regions.fromName(region)) .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(awsAccount, awsSecret))) .build(); try { PublishRequest publishReq = new PublishRequest() .withTopicArn(topicArn) .withMessage(getMessage(login, email, name, subject, content)); snsClient.publish(publishReq); } catch (Exception e) { LOGGER.log(Level.SEVERE, "Cannot send notification to SNS service", e); } finally { LOGGER.log(Level.INFO, "Webhook runner terminated"); } }
Example #5
Source File: S3SnsNotifier.java From bender with Apache License 2.0 | 6 votes |
public static boolean publish(String arn, String msg, AmazonSNSClient snsClient, String s3Key) { if (dryRun) { logger.warn("would have published " + s3Key + " S3 creation event to SNS"); return true; } logger.info("publishing " + s3Key + " S3 creation event to SNS"); try { snsClient.publish(arn, msg, "Amazon S3 Notification"); } catch (RuntimeException e) { logger.error("error publishing", e); return false; } return true; }
Example #6
Source File: AwsGlacier.java From core with GNU General Public License v3.0 | 5 votes |
/********************** Member Functions **************************/ public AwsGlacier(String region) { // Get credentials from credentials file, environment variable, or // Java property. // See http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/auth/DefaultAWSCredentialsProviderChain.html AWSCredentialsProviderChain credentialsProvider = new DefaultAWSCredentialsProviderChain(); credentials = credentialsProvider.getCredentials(); logger.debug("Read in credentials AWSAccessKeyId={} AWSSecretKey={}...", credentials.getAWSAccessKeyId(), credentials.getAWSSecretKey().substring(0, 4)); // Create the glacier client and set to specified region. glacierClient = new AmazonGlacierClient(credentials); glacierClient.setEndpoint("https://glacier." + region + ".amazonaws.com"); // Set up params needed for retrieving vault inventory sqsClient = new AmazonSQSClient(credentials); sqsClient.setEndpoint("https://sqs." + region + ".amazonaws.com"); snsClient = new AmazonSNSClient(credentials); snsClient.setEndpoint("https://sns." + region + ".amazonaws.com"); // Create the ArchiveTransferManager used for uploading and // downloading files. Need to use ArchiveTransferManager constructor // that allows one to specify sqsClient & snsClient so that they have // the proper region. If use ArchiveTransferManager without specifying // sqs and sns clients then default ones are constructed, but these // use the default Virginia region, which is wrong. atm = new ArchiveTransferManager(glacierClient, sqsClient, snsClient); }
Example #7
Source File: SNSQueueManagerImpl.java From usergrid with Apache License 2.0 | 5 votes |
/** * The Synchronous SNS client is used for creating topics and subscribing queues. */ private AmazonSNSClient createSNSClient( final Region region ) { final UsergridAwsCredentialsProvider ugProvider = new UsergridAwsCredentialsProvider(); final AmazonSNSClient sns = new AmazonSNSClient( ugProvider.getCredentials(), clientConfiguration ); sns.setRegion( region ); return sns; }
Example #8
Source File: SNSUtils.java From wildfly-camel with Apache License 2.0 | 5 votes |
public static AmazonSNSClient createNotificationClient() { BasicCredentialsProvider credentials = BasicCredentialsProvider.standard(); AmazonSNSClient client = !credentials.isValid() ? null : (AmazonSNSClient) AmazonSNSClientBuilder.standard() .withCredentials(credentials) .withRegion("eu-west-1") .build(); return client; }
Example #9
Source File: AbstractSNSProcessor.java From nifi with Apache License 2.0 | 5 votes |
/** * Create client using AWSCredentials * * @deprecated use {@link #createClient(ProcessContext, AWSCredentialsProvider, ClientConfiguration)} instead */ @Override protected AmazonSNSClient createClient(final ProcessContext context, final AWSCredentials credentials, final ClientConfiguration config) { getLogger().info("Creating client using aws credentials"); return new AmazonSNSClient(credentials, config); }
Example #10
Source File: AbstractSNSProcessor.java From nifi with Apache License 2.0 | 5 votes |
/** * Create client using aws credentials provider. This is the preferred way for creating clients */ @Override protected AmazonSNSClient createClient(final ProcessContext context, final AWSCredentialsProvider credentialsProvider, final ClientConfiguration config) { getLogger().info("Creating client using aws credentials provider"); return new AmazonSNSClient(credentialsProvider, config); }
Example #11
Source File: TestPutSNS.java From nifi with Apache License 2.0 | 5 votes |
@Before public void setUp() { mockSNSClient = Mockito.mock(AmazonSNSClient.class); mockPutSNS = new PutSNS() { @Override protected AmazonSNSClient getClient() { actualSNSClient = client; return mockSNSClient; } }; runner = TestRunners.newTestRunner(mockPutSNS); }
Example #12
Source File: ControlChannel.java From s3-bucket-loader with Apache License 2.0 | 5 votes |
public ControlChannel(boolean callerIsMaster, String awsAccessKey, String awsSecretKey, String snsControlTopicName, String userAccountPrincipalId, String userARN, CCPayloadHandler ccPayloadHandler) throws Exception { super(); try { this.mySourceIp = InetAddress.getLocalHost().getHostAddress(); } catch(Exception e) { logger.error("Error getting local inet address: " + e.getMessage()); } mySourceIdentifier = determineHostName() + "-" +uuid; this.snsControlTopicName = snsControlTopicName; if (callerIsMaster) { canDestroyTopic = true; this.snsControlTopicName += "-" + mySourceIdentifier; } this.ccPayloadHandler = ccPayloadHandler; sqsClient = new AmazonSQSClient(new BasicAWSCredentials(awsAccessKey, awsSecretKey)); snsClient = new AmazonSNSClient(new BasicAWSCredentials(awsAccessKey, awsSecretKey)); this.connectToTopic(callerIsMaster, 1000, userAccountPrincipalId, userARN); }
Example #13
Source File: ArnEndpointHandler.java From ETSMobile-Android2 with Apache License 2.0 | 5 votes |
public ArnEndpointHandler(Context context, String appToken, String data, String arn) { client = new AmazonSNSClient(new BasicAWSCredentials( context.getString(R.string.aws_access_key), context.getString(R.string.aws_secret_key) )); token = appToken; userData = data; applicationArn = arn; securePreferences = new SecurePreferences(context); }
Example #14
Source File: ScanUploadModule.java From emodb with Apache License 2.0 | 5 votes |
@Provides @Singleton protected AmazonSNS provideAmazonSNS(Region region, AWSCredentialsProvider credentialsProvider) { AmazonSNS amazonSNS = new AmazonSNSClient(credentialsProvider); amazonSNS.setRegion(region); return amazonSNS; }
Example #15
Source File: SNSS3HandlerTest.java From bender with Apache License 2.0 | 5 votes |
@Test public void testExceptionHandlingd() throws Throwable { BaseHandler.CONFIG_FILE = "/com/nextdoor/bender/handler/config_test_sns.json"; TestContext ctx = new TestContext(); ctx.setFunctionName("unittest"); ctx.setInvokedFunctionArn("arn:aws:lambda:us-east-1:123:function:test-function:staging"); /* * Invoke handler */ SNSS3Handler fhandler = (SNSS3Handler) getHandler(); fhandler.init(ctx); IpcSenderService ipcSpy = spy(fhandler.getIpcService()); doThrow(new TransportException("expected")).when(ipcSpy).flush(); fhandler.setIpcService(ipcSpy); AmazonSNSClient mockClient = mock(AmazonSNSClient.class); AmazonSNSClientFactory mockClientFactory = mock(AmazonSNSClientFactory.class); doReturn(mockClient).when(mockClientFactory).newInstance(); fhandler.snsClientFactory = mockClientFactory; SNSEvent event = getTestEvent(); try { fhandler.handler(event, ctx); } catch (Exception e) { } verify(mockClient, times(1)).publish("foo", "basic_input.log", "SNSS3Handler Failed"); }
Example #16
Source File: SNSS3Handler.java From bender with Apache License 2.0 | 5 votes |
@Override public void onException(Exception e) { /* * Always close the iterator to prevent connection leaking */ try { if (this.recordIterator != null) { this.recordIterator.close(); } } catch (IOException e1) { logger.error("unable to close record iterator", e); } if (this.config == null || this.config.getHandlerConfig() == null) { return; } /* * Notify SNS topic */ SNSS3HandlerConfig handlerConfig = (SNSS3HandlerConfig) this.config.getHandlerConfig(); if (handlerConfig.getSnsNotificationArn() != null) { AmazonSNSClient snsClient = this.snsClientFactory.newInstance(); snsClient.publish(handlerConfig.getSnsNotificationArn(), this.inputFiles.stream().map(Object::toString).collect(Collectors.joining(",")), "SNSS3Handler Failed"); } }
Example #17
Source File: SNSNotificationsConfig.java From metacat with Apache License 2.0 | 5 votes |
/** * If SNS notifications are desired and no existing client has been created elsewhere * in the application create a default client here. * * @return The configured SNS client */ //TODO: See what spring-cloud-aws would provide automatically... @Bean @ConditionalOnMissingBean(AmazonSNS.class) public AmazonSNS amazonSNS() { return new AmazonSNSClient(DefaultAWSCredentialsProviderChain.getInstance()); }
Example #18
Source File: SNSService.java From oneops with Apache License 2.0 | 5 votes |
/** * Sends using the sns publisher */ @Override public boolean postMessage(NotificationMessage nMsg, BasicSubscriber subscriber) { SNSSubscriber sub; if (subscriber instanceof SNSSubscriber) { sub = (SNSSubscriber) subscriber; } else { throw new ClassCastException("invalid subscriber " + subscriber.getClass().getName()); } SNSMessage msg = buildSNSMessage(nMsg); AmazonSNS sns = new AmazonSNSClient(new BasicAWSCredentials(sub.getAwsAccessKey(), sub.getAwsSecretKey())); if (sub.getSnsEndpoint() != null) { sns.setEndpoint(sub.getSnsEndpoint()); } CreateTopicRequest tRequest = new CreateTopicRequest(); tRequest.setName(msg.getTopicName()); CreateTopicResult result = sns.createTopic(tRequest); PublishRequest pr = new PublishRequest(result.getTopicArn(), msg.getTxtMessage()).withSubject(msg.getSubject()); try { PublishResult pubresult = sns.publish(pr); logger.info("Published msg with id - " + pubresult.getMessageId()); } catch (AmazonClientException ace) { logger.error(ace.getMessage()); return false; } return true; }
Example #19
Source File: AbstractSNSProcessor.java From localization_nifi with Apache License 2.0 | 5 votes |
/** * Create client using aws credentials provider. This is the preferred way for creating clients */ @Override protected AmazonSNSClient createClient(final ProcessContext context, final AWSCredentialsProvider credentialsProvider, final ClientConfiguration config) { getLogger().info("Creating client using aws credentials provider"); return new AmazonSNSClient(credentialsProvider, config); }
Example #20
Source File: AbstractSNSProcessor.java From localization_nifi with Apache License 2.0 | 5 votes |
/** * Create client using AWSCredentials * * @deprecated use {@link #createClient(ProcessContext, AWSCredentialsProvider, ClientConfiguration)} instead */ @Override protected AmazonSNSClient createClient(final ProcessContext context, final AWSCredentials credentials, final ClientConfiguration config) { getLogger().info("Creating client using aws credentials"); return new AmazonSNSClient(credentials, config); }
Example #21
Source File: PutSNS.java From localization_nifi with Apache License 2.0 | 4 votes |
@Override public void onTrigger(final ProcessContext context, final ProcessSession session) { FlowFile flowFile = session.get(); if (flowFile == null) { return; } if (flowFile.getSize() > MAX_SIZE) { getLogger().error("Cannot publish {} to SNS because its size exceeds Amazon SNS's limit of 256KB; routing to failure", new Object[]{flowFile}); session.transfer(flowFile, REL_FAILURE); return; } final Charset charset = Charset.forName(context.getProperty(CHARACTER_ENCODING).evaluateAttributeExpressions(flowFile).getValue()); final ByteArrayOutputStream baos = new ByteArrayOutputStream(); session.exportTo(flowFile, baos); final String message = new String(baos.toByteArray(), charset); final AmazonSNSClient client = getClient(); final PublishRequest request = new PublishRequest(); request.setMessage(message); if (context.getProperty(USE_JSON_STRUCTURE).asBoolean()) { request.setMessageStructure("json"); } final String arn = context.getProperty(ARN).evaluateAttributeExpressions(flowFile).getValue(); final String arnType = context.getProperty(ARN_TYPE).getValue(); if (arnType.equalsIgnoreCase(ARN_TYPE_TOPIC.getValue())) { request.setTopicArn(arn); } else { request.setTargetArn(arn); } final String subject = context.getProperty(SUBJECT).evaluateAttributeExpressions(flowFile).getValue(); if (subject != null) { request.setSubject(subject); } for (final Map.Entry<PropertyDescriptor, String> entry : context.getProperties().entrySet()) { if (entry.getKey().isDynamic() && !StringUtils.isEmpty(entry.getValue())) { final MessageAttributeValue value = new MessageAttributeValue(); value.setStringValue(context.getProperty(entry.getKey()).evaluateAttributeExpressions(flowFile).getValue()); value.setDataType("String"); request.addMessageAttributesEntry(entry.getKey().getName(), value); } } try { client.publish(request); session.transfer(flowFile, REL_SUCCESS); session.getProvenanceReporter().send(flowFile, arn); getLogger().info("Successfully published notification for {}", new Object[]{flowFile}); } catch (final Exception e) { getLogger().error("Failed to publish Amazon SNS message for {} due to {}", new Object[]{flowFile, e}); flowFile = session.penalize(flowFile); session.transfer(flowFile, REL_FAILURE); } }
Example #22
Source File: AmazonSNSClientFactory.java From bender with Apache License 2.0 | 4 votes |
public AmazonSNSClient newInstance() { return new AmazonSNSClient(); }
Example #23
Source File: AmazonNotificationUtils.java From usergrid with Apache License 2.0 | 4 votes |
public static String getTopicArn( final AmazonSNSClient sns, final String queueName, final boolean createOnMissing ) throws Exception { if ( logger.isTraceEnabled() ) { logger.trace( "Looking up Topic ARN: {}", queueName ); } ListTopicsResult listTopicsResult = sns.listTopics(); String topicArn = null; for ( Topic topic : listTopicsResult.getTopics() ) { String arn = topic.getTopicArn(); if ( queueName.equals( arn.substring( arn.lastIndexOf( ':' ) ) ) ) { topicArn = arn; if (logger.isTraceEnabled()) { logger.trace( "Found existing topic arn=[{}] for queue=[{}]", topicArn, queueName ); } } } if ( topicArn == null && createOnMissing ) { if (logger.isTraceEnabled()) { logger.trace("Creating topic for queue=[{}]...", queueName); } CreateTopicResult createTopicResult = sns.createTopic( queueName ); topicArn = createTopicResult.getTopicArn(); if (logger.isTraceEnabled()) { logger.trace("Successfully created topic with name {} and arn {}", queueName, topicArn); } } else { logger.error( "Error looking up topic ARN for queue=[{}] and createOnMissing=[{}]", queueName, createOnMissing ); } if ( logger.isTraceEnabled() ) { logger.trace( "Returning Topic ARN=[{}] for Queue=[{}]", topicArn, queueName ); } return topicArn; }
Example #24
Source File: UserMessageController.java From spring-integration-aws with MIT License | 4 votes |
@Override public void afterPropertiesSet() throws Exception { sns = new AmazonSNSClient(awsCredentialsProvider); }
Example #25
Source File: SNSClientProducer.java From wildfly-camel with Apache License 2.0 | 4 votes |
@Produces @Singleton public SNSClientProvider getClientProvider() throws Exception { AmazonSNSClient client = SNSUtils.createNotificationClient(); return new SNSClientProvider(client); }
Example #26
Source File: SNSClientProducer.java From wildfly-camel with Apache License 2.0 | 4 votes |
public AmazonSNSClient getClient() { return client; }
Example #27
Source File: SNSClientProducer.java From wildfly-camel with Apache License 2.0 | 4 votes |
SNSClientProvider(AmazonSNSClient client) { this.client = client; }
Example #28
Source File: SnsWebhookManager.java From Singularity with Apache License 2.0 | 4 votes |
@Inject public SnsWebhookManager( @Singularity ObjectMapper objectMapper, SingularityConfiguration configuration, SingularityManagedThreadPoolFactory threadPoolFactory, WebhookManager webhookManager ) { this.objectMapper = objectMapper; this.webhookConf = configuration.getWebhookQueueConfiguration(); if ( webhookConf.getAwsAccessKey().isPresent() && webhookConf.getAwsSecretKey().isPresent() ) { this.snsClient = AmazonSNSClient .builder() .withCredentials( new AWSStaticCredentialsProvider( new BasicAWSCredentials( webhookConf.getAwsAccessKey().get(), webhookConf.getAwsSecretKey().get() ) ) ) .withClientConfiguration( new ClientConfiguration() .withMaxConnections(configuration.getMaxConcurrentWebhooks()) .withConnectionTimeout(webhookConf.getSnsConnectTimeout()) .withRequestTimeout(webhookConf.getSnsRequestTimeout()) .withSocketTimeout(webhookConf.getSnsSocketTimeout()) .withClientExecutionTimeout(webhookConf.getSnsTotalTimeout()) ) .withRegion(webhookConf.getAwsRegion().orElse("us-east-1")) .build(); } else { this.snsClient = AmazonSNSClientBuilder.defaultClient(); } this.webhookManager = webhookManager; this.publishExecutor = threadPoolFactory.get("webhook-publish", configuration.getMaxConcurrentWebhooks()); this.typeToArn = new ConcurrentHashMap<>(); }
Example #29
Source File: Handler.java From Building-Serverless-Architectures with MIT License | 4 votes |
@Inject public Handler setAmazonSNSClient(AmazonSNSClient amazonSNSClient) { this.amazonSNSClient = amazonSNSClient; return this; }
Example #30
Source File: Handler.java From Building-Serverless-Architectures with MIT License | 4 votes |
@Inject public Handler setAmazonSNSClient(AmazonSNSClient amazonSNSClient) { this.amazonSNSClient = amazonSNSClient; return this; }