def POST(self, request, **kwargs): try: aws_msg = json.loads(request.body) self._log.info("%s on %s" % (aws_msg['Type'], aws_msg['TopicArn'])) if aws_msg['TopicArn'] == self._topicArn: aws = SNS(aws_msg) if settings.EVENT_VALIDATE_SNS_SIGNATURE: aws.validate() if aws_msg['Type'] == 'Notification': enrollment = Enrollment(aws.extract()) if settings.EVENT_VALIDATE_ENROLLMENT_SIGNATURE: enrollment.validate() enrollment.process() elif aws_msg['Type'] == 'SubscriptionConfirmation': self._log.info('SubscribeURL: %s' % ( aws_msg['SubscribeURL'])) aws.subscribe() else: self._log.error('Unrecognized TopicARN : %s' % ( aws_msg['TopicArn'])) return self.error_response(400, "Invalid TopicARN") except ValueError as err: self._log.error('JSON : %s' % err) return self.error_response(400, "Invalid JSON") except EventException, err: self._log.error("ENROLLMENT: %s" % (err)) return self.error_response(500, "Internal Server Error")
def gather_events(self): to_fetch = self._settings.get('MESSAGE_GATHER_SIZE') while to_fetch > 0: n = min([to_fetch, 10]) msgs = self._queue.get_messages( num_messages=n, visibility_timeout=self._settings.get('VISIBILITY_TIMEOUT')) for msg in msgs: try: sqs_msg = json.loads(msg.get_body()) if sqs_msg['TopicArn'] == self._topicArn: raw_message = SNS(sqs_msg) if self._settings.get('VALIDATE_SNS_SIGNATURE', True): raw_message.validate() if sqs_msg['Type'] == 'Notification': settings = self._settings.get( 'PAYLOAD_SETTINGS', {}) message = raw_message.extract() self._processor(settings, message).process() elif sqs_msg['Type'] == 'SubscriptionConfirmation': self._log.debug( 'SubscribeURL: ' + sqs_msg['SubscribeURL']) else: self._log.warning( 'Unrecognized TopicARN : ' + sqs_msg['TopicArn']) except ValueError as err: raise GatherException('JSON : %s' % err) except self._exception, err: raise GatherException("MESSAGE: %s" % err) except SNSException, err: raise GatherException("SNS: %s" % err) except Exception, err: self._log.exception("Gather Error") raise GatherException("ERROR: %s" % err)