Exemplo n.º 1
0
 def consume(self, event, *args, **kwargs):
     try:
         event = event.convert(JSONEvent)
         if self.flatten:
             event.data = event.data[event.data.keys()[0]]
     except Exception as err:
         self.logger.error("Unable to convert to XML: {0}".format(err), event=event)
         raise MalformedEventData(err.message)
     else:
         self.logger.info("Successfully converted XML to Dict", event=event)
         self.send_event(event)
Exemplo n.º 2
0
    def consume(self, event, *args, **kwargs):
        modify_value = self.get_modify_value(event)

        if self.log_change:
            self.logger.info("Changed event.{key} to {value}".format(
                key=self.key, value=modify_value),
                             event=event)

        try:
            event = self.get_key_chain_value(event, modify_value)
            self.send_event(event)
        except Exception as err:
            raise MalformedEventData(err)
Exemplo n.º 3
0
 def __default_error_handler(self, res):
     '''
         Handles Bottle raised exceptions and applies event based error messaging and response formatting
     '''
     event = HttpEvent(
         environment=self._format_bottle_env(request.environ), 
         _error=MalformedEventData(res.body), 
         accept=self._get_accept(), 
         status=res._status_line)
     event = self._process_response_accept(event=event)
     local_response = self._create_response(event=event)
     res.body = local_response.body
     res.headers.update(**local_response.headers)
     return res.body # we want to still use bottle built attributes s.a. status
Exemplo n.º 4
0
 def _process_error(self, message, event):
     self.logger.error(
         "Error applying transform. Error was: {0}".format(message),
         event=event)
     raise MalformedEventData("Malformed Request: Invalid XML")
Exemplo n.º 5
0
    def callback(self, queue=None, *args, **kwargs):
        queue_name = queue or self.name
        queue = self.pool.outbound.get(queue_name, None)
        ctype = request.content_type.split(';')[0]

        accept_header = request.headers.get("Accept", "*/*")
        try:
            accept = mimeparse.best_match(self.CONTENT_TYPES, accept_header)
        except ValueError:
            accept = "*/*"
            self.logger.warning("Invalid mimetype defined in client Accepts header. '{accept}' is not a valid mime type".format(accept=accept_header))

        if request.method in ["GET", "OPTIONS", "HEAD", "DELETE"]:
            for accept_type in accept_header:
                if accept_type in self.CONTENT_TYPE_MAP.keys():
                    pass

        if ctype == '':
            ctype = None

        try:
            event_class = None
            data = None
            environment = self._format_bottle_env(request.environ)

            if not queue:
                self.logger.error("Received {method} request with URL '{url}'. Queue name '{queue_name}' was not found".format(method=request.method,
                                                                                                                           url=request.path,
                                                                                                                           queue_name=queue_name))
                raise ResourceNotFound("Service '{0}' not found".format(queue_name))

            if ctype == self.X_WWW_FORM_URLENCODED:
                if len(request.forms.items()) < 1:
                    raise MalformedEventData("Mismatched content type")
                else:
                    for item in request.forms.items():
                        event_class, data = self.X_WWW_FORM_URLENCODED_KEY_MAP[item[0]], item[1]
                        break
            else:
                event_class = self.CONTENT_TYPE_MAP[ctype]
                try:
                    data = request.body.read()
                except:
                    # A body is not required
                    data = None

            if data == '':
                data = None

            event = event_class(environment=environment, service=queue_name, data=data, accept=accept, **kwargs)

        except (ResourceNotFound, InvalidEventDataModification, MalformedEventData) as err:
            event_class = event_class or JSONHttpEvent
            event = event_class(environment=environment, service=queue_name, accept=accept, **kwargs)
            event.error = err
            queue = self.pool.inbound[self.pool.inbound.keys()[0]]

        response_queue = Queue()
        self.responders.update({event.event_id: (event_class, response_queue)})
        local_response = response_queue
        self.logger.info("Received {0} request for service {1}".format(request.method, queue_name), event=event)
        self.send_event(event, queues=[queue])

        return local_response
Exemplo n.º 6
0
 def process_error(self, message, event):
     self.logger.error("Error validating incoming XML: {0}".format(message), event=event)
     raise MalformedEventData(message)