Exemple #1
0
    def render_POST(self, request):
        response = {}

        logger.info('Received a new request from the client')
        try :
            # process the message encoding
            encoding = request.getHeader('Content-Type')
            data = request.content.read()
            if encoding == 'application/json' :

                try:
                    input_json_str = json.loads(data.decode('utf-8'))
                    input_json = json.loads(input_json_str)
                    jrpc_id = input_json["id"]
                    response = self._process_request(input_json_str)

                except AttributeError:
                    logger.error("Error while loading input json")
                    response = utility.create_error_response(
                        WorkorderError.UNKNOWN_ERROR,
                        jrpc_id,
                        "UNKNOWN_ERROR: Error while loading the input JSON file")
                    return response

            else :
                # JRPC response with 0 as id is returned because id can't be fecthed
                # from a request with unknown encoding
                response = utility.create_error_response(
                        WorkorderError.UNKNOWN_ERROR,
                        0,
                        "UNKNOWN_ERROR: unknown message encoding")
                return response

        except :
            logger.exception('exception while decoding http request %s', request.path)
            # JRPC response with 0 as id is returned because id can't be
            # fetched from improper request
            response = utility.create_error_response(
                    WorkorderError.UNKNOWN_ERROR,
                    0,
                    "UNKNOWN_ERROR: unable to decode incoming request")
            return response

        # send back the results
        try :
            if encoding == 'application/json' :
                response = json.dumps(response)
            logger.info('response[%s]: %s', encoding, response)
            request.setHeader('content-type', encoding)
            request.setResponseCode(http.OK)
            return response.encode('utf8')

        except :
            logger.exception('unknown exception while processing request %s', request.path)
            response = utility.create_error_response(
                    WorkorderError.UNKNOWN_ERROR,
                    jrpc_id,
                    "UNKNOWN_ERROR: unknown exception processing http \
                    request {0}".format(request.path))
            return response
Exemple #2
0
    def render_POST(self, request):
        response = ""

        logger.info('Received a new request from the client')

        try:
            # Process the message encoding
            encoding = request.getHeader('Content-Type')
            data = request.content.read().decode('utf-8')

            if encoding == 'text/plain; charset=utf-8':
                response = self._process_request(data)
            else:
                response = 'UNKNOWN_ERROR: unknown message encoding'
                return response

        except:
            logger.exception('exception while decoding http request %s',
                             request.path)
            response = 'UNKNOWN_ERROR: unable to decode incoming request '
            return response

        # Send back the results
        try:
            logger.info('response[%s]: %s', encoding, response.encode('utf-8'))
            request.setHeader('content-type', encoding)
            request.setResponseCode(http.OK)
            return response.encode('utf-8')

        except:
            logger.exception('unknown exception while processing request %s',
                             request.path)
            response = 'UNKNOWN_ERROR: unknown exception processing ' + \
                'http request {0}'.format(request.path)
            return response
Exemple #3
0
    def render_POST(self, request):
        response = {}
        response['error'] = {}
        response['error']['code'] = WorkorderError.UNKNOWN_ERROR

        logger.info('Received a new request from the client')

        try:
            # process the message encoding
            encoding = request.getHeader('Content-Type')
            data = request.content.read()

            if encoding == 'application/json':

                try:
                    input_json = json.loads(data.decode('utf-8'))
                    response = self._process_request(input_json)

                except AttributeError:
                    logger.error("Error while loading input json")
                    response['error'][
                        'message'] = 'UNKNOWN_ERROR: Error while loading the input JSON file'
                    return response

            else:
                response['error'][
                    'message'] = 'UNKNOWN_ERROR: unknown message encoding'
                return response

        except:
            logger.exception('exception while decoding http request %s',
                             request.path)
            response['error'][
                'message'] = 'UNKNOWN_ERROR: unable to decode incoming request '
            return response

        # send back the results
        try:
            if encoding == 'application/json':
                response = json.dumps(response)

            logger.info('response[%s]: %s', encoding, response)
            request.setHeader('content-type', encoding)
            request.setResponseCode(http.OK)
            return response.encode('utf8')

        except:
            logger.exception('unknown exception while processing request %s',
                             request.path)
            response['error'][
                'message'] = 'UNKNOWN_ERROR: unknown exception processing http request {0}'.format(
                    request.path)
            return response