def test_verbs(self):
     """oairepository - testing verbs"""
     self.assertNotEqual(None, re.search("Identify", oai_repository_server.oai_identify({'verb': 'Identify'})))
     ret = StringIO()
     oai_repository_server.oai_list_records_or_identifiers(ret, {'verb': 'ListIdentifiers', 'metadataPrefix': 'marcxml'})
     self.assertNotEqual(None, re.search("ListIdentifiers", ret.getvalue()))
     ret = StringIO()
     oai_repository_server.oai_list_records_or_identifiers(ret, {'verb': 'ListRecords', 'metadataPrefix': 'marcxml'})
     self.assertNotEqual(None, re.search("ListRecords", ret.getvalue()))
     self.assertNotEqual(None, re.search("ListMetadataFormats", oai_repository_server.oai_list_metadata_formats({'verb': 'ListMetadataFormats'})))
     self.assertNotEqual(None, re.search("ListSets", oai_repository_server.oai_list_sets({'verb': 'ListSets'})))
     self.assertNotEqual(None, re.search("GetRecord", oai_repository_server.oai_get_record({'identifier': 'oai:atlantis.cern.ch:1', 'verb': 'GetRecord'})))
    def __call__(self, req, form):
        """OAI repository interface"""

        # Clean input arguments. The protocol specifies that an error
        # has to be returned if the same argument is specified several
        # times. Eg:
        # oai2d?verb=ListIdentifiers&metadataPrefix=marcxml&metadataPrefix=marcxml
        # So keep the arguments as list for now so that check_argd can
        # return an error if needed (check_argd also transforms these
        # lists into strings)
        argd = wash_urlargd(form, {'verb': (list, []),
                                   'metadataPrefix': (list, []),
                                   'from': (list, []),
                                   'until': (list, []),
                                   'set': (list, []),
                                   'identifier': (list, []),
                                   'resumptionToken': (list, []),
                                   })

        if CFG_VALIDATE_RESPONSES:
            req.track_writings = True

        ## wash_urlargd(..) function cleaned everything, but also added
        ## unwanted parameters. Remove them now
        for param in argd.keys():
            if not param in form and param != 'verb':
                del argd[param]

        ## wash_urlargd(..) function also removed unknown parameters
        ## that we would like to keep in order to send back an error
        ## as required by the protocol. But we do not need that value,
        ## so set it to empty string.
        for param in form.keys():
            if param not in argd.keys():
                argd[param] = ''

        ## But still remove 'ln' parameter that was automatically added.
        if argd.has_key('ln'):
            del argd['ln']

        ## check request for OAI compliancy
        ## also transform all the list arguments into string
        oai_errors = oai_repository_server.check_argd(argd)

        ## check availability (OAI requests for Identify, ListSets and
        ## ListMetadataFormats are served immediately, otherwise we
        ## shall wait for CFG_OAI_SLEEP seconds between requests):
        if os.path.exists("%s/RTdata/RTdata" % CFG_CACHEDIR) and (argd['verb'] not in ["Identify", "ListMetadataFormats", "ListSets"] and not argd.get('resumptionToken')):
            time_gap = int(time.time() - os.path.getmtime("%s/RTdata/RTdata" % CFG_CACHEDIR))
            if(time_gap < CFG_OAI_SLEEP):
                req.headers_out["Status-Code"] = "503"
                req.headers_out["Retry-After"] = "%d" % (CFG_OAI_SLEEP - time_gap)
                req.status = apache.HTTP_SERVICE_UNAVAILABLE
                return "Retry after %d seconds" % (CFG_OAI_SLEEP - time_gap)
        command = "touch %s/RTdata/RTdata" % CFG_CACHEDIR
        os.system(command)


        ## create OAI response
        req.content_type = "text/xml"
        req.send_http_header()

        if not oai_errors:
            ## OAI Identify
            if argd['verb']   == "Identify":
                req.write(oai_repository_server.oai_identify(argd))

            ## OAI ListSets
            elif argd['verb'] == "ListSets":
                req.write(oai_repository_server.oai_list_sets(argd))

            ## OAI ListIdentifiers or OAI ListRecords
            elif argd['verb'] in ("ListIdentifiers", "ListRecords"):
                oai_repository_server.oai_list_records_or_identifiers(req, argd)

            ## OAI GetRecord
            elif argd['verb'] == "GetRecord":
                req.write(oai_repository_server.oai_get_record(argd))

            ## OAI ListMetadataFormats
            elif argd['verb'] == "ListMetadataFormats":
                req.write(oai_repository_server.oai_list_metadata_formats(argd))

            ## Unknown verb

        ## OAI error
        else:
            req.write(oai_repository_server.oai_error(argd, oai_errors))

        if CFG_VALIDATE_RESPONSES:
            req.track_writings = False
            try:
                OAI_PMH_VALIDATOR.assertValid(etree.parse(cStringIO.StringIO(req.what_was_written)))
            except etree.DocumentInvalid:
                register_exception(req=req, alert_admin=True)
                raise
        return "\n"
    def __call__(self, req, form):
        """OAI repository interface"""

        # Clean input arguments. The protocol specifies that an error
        # has to be returned if the same argument is specified several
        # times. Eg:
        # oai2d?verb=ListIdentifiers&metadataPrefix=marcxml&metadataPrefix=marcxml
        # So keep the arguments as list for now so that check_argd can
        # return an error if needed (check_argd also transforms these
        # lists into strings)
        argd = wash_urlargd(form, {'verb': (list, []),
                                   'metadataPrefix': (list, []),
                                   'from': (list, []),
                                   'until': (list, []),
                                   'set': (list, []),
                                   'identifier': (list, []),
                                   'resumptionToken': (list, []),
                                   })

        if CFG_VALIDATE_RESPONSES:
            req.track_writings = True

        ## wash_urlargd(..) function cleaned everything, but also added
        ## unwanted parameters. Remove them now
        for param in argd.keys():
            if not param in form and param != 'verb':
                del argd[param]

        ## wash_urlargd(..) function also removed unknown parameters
        ## that we would like to keep in order to send back an error
        ## as required by the protocol. But we do not need that value,
        ## so set it to empty string.
        for param in form.keys():
            if param not in argd.keys():
                argd[param] = ''

        ## But still remove 'ln' parameter that was automatically added.
        if argd.has_key('ln'):
            del argd['ln']

        ## check request for OAI compliancy
        ## also transform all the list arguments into string
        oai_errors = oai_repository_server.check_argd(argd)

        ## check availability (OAI requests for Identify, ListSets and
        ## ListMetadataFormats are served immediately, otherwise we
        ## shall wait for CFG_OAI_SLEEP seconds between requests):
        if os.path.exists("%s/RTdata/RTdata" % CFG_CACHEDIR) and (argd['verb'] not in ["Identify", "ListMetadataFormats", "ListSets"] and not argd.get('resumptionToken')):
            time_gap = int(time.time() - os.path.getmtime("%s/RTdata/RTdata" % CFG_CACHEDIR))
            if(time_gap < CFG_OAI_SLEEP):
                req.headers_out["Status-Code"] = "503"
                req.headers_out["Retry-After"] = "%d" % (CFG_OAI_SLEEP - time_gap)
                req.status = apache.HTTP_SERVICE_UNAVAILABLE
                return "Retry after %d seconds" % (CFG_OAI_SLEEP - time_gap)
        command = "touch %s/RTdata/RTdata" % CFG_CACHEDIR
        os.system(command)


        ## create OAI response
        req.content_type = "text/xml"
        req.send_http_header()

        if not oai_errors:
            ## OAI Identify
            if argd['verb']   == "Identify":
                req.write(oai_repository_server.oai_identify(argd))

            ## OAI ListSets
            elif argd['verb'] == "ListSets":
                req.write(oai_repository_server.oai_list_sets(argd))

            ## OAI ListIdentifiers or OAI ListRecords
            elif argd['verb'] in ("ListIdentifiers", "ListRecords"):
                oai_repository_server.oai_list_records_or_identifiers(req, argd)

            ## OAI GetRecord
            elif argd['verb'] == "GetRecord":
                req.write(oai_repository_server.oai_get_record(argd))

            ## OAI ListMetadataFormats
            elif argd['verb'] == "ListMetadataFormats":
                req.write(oai_repository_server.oai_list_metadata_formats(argd))

            ## Unknown verb

        ## OAI error
        else:
            req.write(oai_repository_server.oai_error(argd, oai_errors))

        if CFG_VALIDATE_RESPONSES:
            req.track_writings = False
            try:
                OAI_PMH_VALIDATOR.assertValid(etree.parse(cStringIO.StringIO(req.what_was_written)))
            except etree.DocumentInvalid:
                register_exception(req=req, alert_admin=True)
                raise
        return "\n"