Beispiel #1
0
    def process(self, chain, request, response, responseCnt, **keyargs):
        """
        Create the render for the response object.
        """
        assert isinstance(chain, Chain), "Invalid processors chain %s" % chain
        assert isinstance(request, Request), "Invalid request %s" % request
        assert isinstance(response, Response), "Invalid response %s" % response
        assert isinstance(responseCnt, ResponseContent), "Invalid response content %s" % responseCnt

        chain.proceed()

        # Resolving the character set
        if ResponseContent.charSet in responseCnt:
            try:
                codecs.lookup(responseCnt.charSet)
            except LookupError:
                responseCnt.charSet = None
        else:
            responseCnt.charSet = None

        if responseCnt.charSet is None:
            for charSet in request.accCharSets or ():
                try:
                    codecs.lookup(charSet)
                except LookupError:
                    continue
                responseCnt.charSet = charSet
                break
            else:
                responseCnt.charSet = self.charSetDefault

        resolved = False
        if ResponseContent.type in responseCnt:
            renderChain = Chain(self.renderingProcessing)
            renderChain.process(request=request, response=response, responseCnt=responseCnt, **keyargs)
            if renderChain.doAll().isConsumed():
                if Response.code not in response or response.code.isSuccess:
                    response.code = UNKNOWN_ENCODING
                    response.text = "Content type '%s' not supported for rendering" % responseCnt.type
            else:
                resolved = True

        if not resolved:
            # Adding None in case some encoder is configured as default.
            for contentType in itertools.chain(request.accTypes or (), self.contentTypeDefaults):
                responseCnt.type = contentType
                renderChain = Chain(self.renderingProcessing)
                renderChain.process(request=request, response=response, responseCnt=responseCnt, **keyargs)
                if not renderChain.doAll().isConsumed():
                    break
            else:
                raise DevelError(
                    "There is no renderer available, this is more likely a setup issues since the "
                    "default content types should have resolved the renderer"
                )
Beispiel #2
0
    def processParsing(self, request, requestCnt, response, responseCnt, **keyargs):
        '''
        Process the parsing for the provided contexts.
        
        @return: boolean
            True if the parsing has been successfully done on the request content.
        '''
        assert isinstance(request, Request), 'Invalid request %s' % request
        assert isinstance(requestCnt, RequestContent), 'Invalid request content %s' % requestCnt
        assert isinstance(response, Response), 'Invalid response %s' % response
        assert isinstance(responseCnt, ResponseContent), 'Invalid response content %s' % responseCnt

        # Resolving the character set
        if RequestContent.charSet in requestCnt:
            try: codecs.lookup(requestCnt.charSet)
            except LookupError: requestCnt.charSet = self.charSetDefault
        else: requestCnt.charSet = self.charSetDefault
        if RequestContent.type not in requestCnt: requestCnt.type = responseCnt.type

        chain = Chain(self.parsingProcessing)
        chain.process(request=request, requestCnt=requestCnt, response=response, responseCnt=responseCnt, **keyargs)
        if not chain.doAll().isConsumed(): return True
        if Response.code not in response or response.code.isSuccess:
            response.code = UNKNOWN_ENCODING
            response.text = 'Content type \'%s\' not supported for parsing' % requestCnt.type