List of usage examples for com.amazonaws.services.sns.model PublishRequest PublishRequest
public PublishRequest()
From source file:gov.gtas.aws.SmsServiceImpl.java
License:Open Source License
@Override public boolean sendMessage(String s) { String arn = null;//from ww w . j av a 2s . c o m try { arn = lookupRepo.getAppConfigOption(AppConfigurationRepository.SMS_TOPIC_ARN); PublishRequest r = new PublishRequest().withTopicArn(arn).withMessage(s); sns.publish(r); } catch (Exception e) { logger.error("could not publish to topic: " + arn, e); return false; } return true; }
From source file:io.macgyver.plugin.cloud.aws.event.SNSMacGyverEventWriter.java
License:Apache License
public void subscribe(EventSystem eventSystem) { Consumer consumer = new Consumer() { public void accept(Object event) { try { if (isEnabled()) { PublishRequest request = new PublishRequest(); request.setTopicArn(getTopicArn().get()); request.setMessage(MacGyverMessage.class.cast(event).getEnvelope().toString()); getSNSClient().get().publishAsync(request, new ResponseHandler()); }//from ww w .ja va2 s .c o m } catch (Exception e) { logger.error("problem sending message to SNS: {}", e.toString()); } } }; ConcurrentSubscribers.createConcurrentSubscriber(eventSystem.createObservable(MacGyverMessage.class)) .withNewExecutor(b -> { b.withThreadPoolSize(2).withThreadNameFormat("SNSMacGyverEventWriter-%d"); }).subscribe(consumer); }
From source file:io.starter.messaging.tools.AmazonSNSClientWrapper.java
License:Open Source License
private PublishResult publish(String endpointArn, Platform platform, Map<Platform, Map<String, MessageAttributeValue>> attributesMap, String message) { PublishRequest publishRequest = new PublishRequest(); Map<String, MessageAttributeValue> notificationAttributes = getValidNotificationAttributes( attributesMap.get(platform)); if (notificationAttributes != null && !notificationAttributes.isEmpty()) { // TODO: link to updated SNS lib // publishRequest.setMessageAttributes(notificationAttributes); }/* ww w. jav a 2 s . c o m*/ publishRequest.setMessageStructure("json"); // If the message attributes are not set in the requisite method, // notification is sent with default attributes if (message == null) message = getPlatformStarterMessage(platform); Map<String, String> messageMap = new HashMap<String, String>(); messageMap.put(platform.name(), message); message = NotificationMessageGenerator.jsonify(messageMap); // For direct publish to mobile end points, topicArn is not relevant. publishRequest.setTargetArn(endpointArn); // Display the message that will be sent to the endpoint/ System.out.println("{Message Body: " + message + "}"); StringBuilder builder = new StringBuilder(); builder.append("{Message Attributes: "); for (Map.Entry<String, MessageAttributeValue> entry : notificationAttributes.entrySet()) { builder.append("(\"" + entry.getKey() + "\": \"" + entry.getValue().getStringValue() + "\"),"); } builder.deleteCharAt(builder.length() - 1); builder.append("}"); System.out.println(builder.toString()); publishRequest.setMessage(message); return snsClient.publish(publishRequest); }
From source file:jp.classmethod.aws.gradle.sns.AmazonSNSPublishMessageTask.java
License:Apache License
@TaskAction public void publishMessage() { String topicArn = getTopicArn(); String message = getMessage(); String subject = getSubject(); String messageStructure = getMessageStructure(); if (topicArn == null) { throw new GradleException("Must specify SNS topicArn"); }/*from w ww . jav a 2 s . co m*/ if (message == null) { throw new GradleException("Must provide message to send to SNS"); } AmazonSNSPluginExtension ext = getProject().getExtensions().getByType(AmazonSNSPluginExtension.class); AmazonSNS sns = ext.getClient(); PublishRequest request = new PublishRequest().withTopicArn(topicArn).withMessage(message); if (subject != null) { request.setSubject(subject); } if (messageStructure != null) { request.setMessageStructure(messageStructure); } sns.publish(request); }
From source file:org.apache.camel.component.aws.sns.SnsProducer.java
License:Apache License
public void process(Exchange exchange) throws Exception { PublishRequest request = new PublishRequest(); request.setTopicArn(getConfiguration().getTopicArn()); request.setMessage(exchange.getIn().getBody(String.class)); request.setSubject(determineSubject(exchange)); LOG.trace("Sending request [{}] from exchange [{}]...", request, exchange); PublishResult result = getEndpoint().getSNSClient().publish(request); LOG.trace("Received result [{}]", result); Message message = getMessageForResponse(exchange); message.setHeader(SnsConstants.MESSAGE_ID, result.getMessageId()); }
From source file:org.apache.nifi.processors.aws.sns.PutSNS.java
License:Apache License
@Override public void onTrigger(final ProcessContext context, final ProcessSession session) { FlowFile flowFile = session.get();//ww w . ja va2s .c o m 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); } }
From source file:org.finra.herd.dao.impl.SnsOperationsImpl.java
License:Apache License
@Override public PublishResult publish(String topicArn, String messageText, Map<String, MessageAttributeValue> messageAttributes, AmazonSNS amazonSNS) { return amazonSNS.publish(new PublishRequest().withTopicArn(topicArn).withMessage(messageText) .withMessageAttributes(messageAttributes)); }
From source file:org.springframework.integration.aws.outbound.SnsMessageHandler.java
License:Apache License
@Override protected Object handleRequestMessage(Message<?> requestMessage) { Object payload = requestMessage.getPayload(); PublishRequest publishRequest = null; if (payload instanceof PublishRequest) { publishRequest = (PublishRequest) payload; } else {//from www. j a v a2s. co m Assert.state(this.topicArnExpression != null, "'topicArn' or 'topicArnExpression' must be specified."); publishRequest = new PublishRequest(); String topicArn = this.topicArnExpression.getValue(this.evaluationContext, requestMessage, String.class); if (this.resourceIdResolver != null) { topicArn = this.resourceIdResolver.resolveToPhysicalResourceId(topicArn); } publishRequest.setTopicArn(topicArn); if (this.subjectExpression != null) { String subject = this.subjectExpression.getValue(this.evaluationContext, requestMessage, String.class); publishRequest.setSubject(subject); } Object snsMessage = requestMessage.getPayload(); if (this.bodyExpression != null) { snsMessage = this.bodyExpression.getValue(this.evaluationContext, requestMessage); } if (snsMessage instanceof SnsBodyBuilder) { publishRequest.withMessageStructure("json").setMessage(((SnsBodyBuilder) snsMessage).build()); } else { publishRequest.setMessage(getConversionService().convert(snsMessage, String.class)); } } PublishResult publishResult = this.amazonSns.publish(publishRequest); if (this.produceReply) { return getMessageBuilderFactory().withPayload(publishRequest) .setHeader(AwsHeaders.TOPIC, publishRequest.getTopicArn()) .setHeader(AwsHeaders.SNS_PUBLISHED_MESSAGE_ID, publishResult.getMessageId()); } else { return null; } }
From source file:org.thingsboard.rule.engine.aws.sns.TbSnsNode.java
License:Apache License
private TbMsg publishMessage(TbContext ctx, TbMsg msg) { String topicArn = TbNodeUtils.processPattern(this.config.getTopicArnPattern(), msg.getMetaData()); PublishRequest publishRequest = new PublishRequest().withTopicArn(topicArn).withMessage(msg.getData()); PublishResult result = this.snsClient.publish(publishRequest); return processPublishResult(ctx, msg, result); }
From source file:org.thingsboard.server.extensions.sns.plugin.SnsMessageHandler.java
License:Apache License
@Override public void process(PluginContext ctx, TenantId tenantId, RuleId ruleId, RuleToPluginMsg<?> msg) throws RuleException { if (msg instanceof SnsTopicActionMsg) { SnsTopicActionPayload payload = ((SnsTopicActionMsg) msg).getPayload(); PublishRequest publishRequest = new PublishRequest().withTopicArn(payload.getTopicArn()) .withMessage(payload.getMsgBody()); sns.publish(publishRequest);// ww w.j av a2s.c om if (payload.isSync()) { ctx.reply(new ResponsePluginToRuleMsg(msg.getUid(), tenantId, ruleId, BasicStatusCodeResponse.onSuccess(payload.getMsgType(), payload.getRequestId()))); } return; } throw new RuleException("Unsupported message type " + msg.getClass().getName() + "!"); }