Example #1
0
    def setUp(self):

        schema_file = file("lr/public/schemas/OAI/2.0/OAI-PMH-LR.xsd", "r")
        schema_doc = etree.parse(schema_file)
        self.oailrschema = etree.XMLSchema(schema_doc)
        
        global test_data_delete, nsdl_data, dc_data
        self.o = oaipmh()
        self.server = self.o.server
        self.db = self.o.db
        
        view_data = self.db.view('oai-pmh/test-data')
        if (len(view_data) == 0):
            nsdl_data = json.load(file("lr/tests/data/nsdl_dc/data-0.json"))
            for doc in nsdl_data["documents"]:
                doc["doc_ID"] = "NSDL-TEST-DATA-"+str(uuid.uuid1())
            
            self.app.post('/publish', params=json.dumps(nsdl_data), headers=json_headers)
            
            dc_data = json.load(file("lr/tests/data/oai_dc/data-0.json"))
            for doc in dc_data["documents"]:
                doc["doc_ID"] = "OAI-DC-TEST-DATA-"+str(uuid.uuid1())
                
            self.app.post('/publish', params=json.dumps(dc_data), headers=json_headers)
            view_data = self.db.view('oai-pmh/test-data')
        
        nsdl_data = { "documents" : [] }
        dc_data = { "documents" : [] }
        for row in view_data:
            if re.search("^NSDL-TEST-DATA-", row.key) != None:
                nsdl_data["documents"].append(row.value)
            if re.search("^OAI-DC-TEST-DATA-", row.key) != None:
                dc_data["documents"].append(row.value)
    def setUpClass(self):

        schema_file = file("lr/public/schemas/OAI/2.0/OAI-PMH-LR.xsd", "r")
        schema_doc = etree.parse(schema_file)
        self.oailrschema = etree.XMLSchema(schema_doc)
        
        global test_data_delete, nsdl_data, dc_data
        self.o = oaipmh()
        self.server = self.o.server
        self.db = self.o.db
        
        view_data = self.db.view('oai-pmh-test-data/docs')
        if (len(view_data) == 0):
            
            if hasattr(self, "attr"):
                app = self.app
            else:
                controller =  TestOaiPmhController(methodName="test_empty")
                app = controller.app
            
            nsdl_data = json.load(file("lr/tests/data/nsdl_dc/data-000000000.json"))
            for doc in nsdl_data["documents"]:
                doc["doc_ID"] = "NSDL-TEST-DATA-"+str(uuid.uuid1())
            
            app.post('/publish', params=json.dumps(nsdl_data), headers=json_headers)
            
            dc_data = json.load(file("lr/tests/data/oai_dc/data-000000000.json"))
            for doc in dc_data["documents"]:
                doc["doc_ID"] = "OAI-DC-TEST-DATA-"+str(uuid.uuid1())
                
            app.post('/publish', params=json.dumps(dc_data), headers=json_headers)
            view_data = self.db.view('oai-pmh/test-data')
        
        nsdl_data = { "documents" : [] }
        dc_data = { "documents" : [] }
        for row in view_data:
            if re.search("^NSDL-TEST-DATA-", row.key) != None and re.search("-distributable$", row.key) == None:
                nsdl_data["documents"].append(row.value)
            if re.search("^OAI-DC-TEST-DATA-", row.key) != None and re.search("-distributable$", row.key) == None:
                dc_data["documents"].append(row.value)
        opts = {
                "startkey":"_design/",
                "endkey": "_design0",
                "include_docs": True
        }
        
        # Force indexing in oai views 
        design_docs = self.db.view('_all_docs', **opts)
        for row in design_docs:
            if re.match("^_design/oai-pmh-", row.key) != None and "views" in row.doc and len(row.doc["views"].keys()) > 0:
                view_name = "{0}/_view/{1}".format( row.key, row.doc["views"].keys()[0])
                log.error("Indexing: {0}".format( view_name))
                self.db.view(view_name, limit=1, descending=True)
            else:
                log.error("Not Indexing: {0}".format( row.key))
Example #3
0
    def setUp(self):

        schema_file = file("lr/public/schemas/OAI/2.0/OAI-PMH-LR.xsd", "r")
        schema_doc = etree.parse(schema_file)
        self.oailrschema = etree.XMLSchema(schema_doc)

        global test_data_delete, nsdl_data, dc_data
        self.o = oaipmh()
        self.server = self.o.server
        self.db = self.o.db

        view_data = self.db.view('oai-pmh/test-data')
        if (len(view_data) == 0):
            nsdl_data = json.load(file("lr/tests/data/nsdl_dc/data-0.json"))
            for doc in nsdl_data["documents"]:
                doc["doc_ID"] = "NSDL-TEST-DATA-" + str(uuid.uuid1())

            self.app.post('/publish',
                          params=json.dumps(nsdl_data),
                          headers=json_headers)

            dc_data = json.load(file("lr/tests/data/oai_dc/data-0.json"))
            for doc in dc_data["documents"]:
                doc["doc_ID"] = "OAI-DC-TEST-DATA-" + str(uuid.uuid1())

            self.app.post('/publish',
                          params=json.dumps(dc_data),
                          headers=json_headers)
            view_data = self.db.view('oai-pmh/test-data')

        nsdl_data = {"documents": []}
        dc_data = {"documents": []}
        for row in view_data:
            if re.search("^NSDL-TEST-DATA-", row.key) != None:
                nsdl_data["documents"].append(row.value)
            if re.search("^OAI-DC-TEST-DATA-", row.key) != None:
                dc_data["documents"].append(row.value)
Example #4
0
    def _handleOAIRequest(self, format='html'):
        t_req = request._current_obj()
        t_res = response._current_obj()

        enable_flow_control = False
        fc_id_limit = None
        fc_doc_limit = None
        service_id = None
        serviceDoc = h.getServiceDocument(appConfig['lr.oaipmh.docid'])
        if serviceDoc != None:
            if 'service_id' in serviceDoc:
                service_id = serviceDoc['service_id']

            if 'service_data' in serviceDoc:
                serviceData = serviceDoc['service_data']
                if 'flow_control' in serviceData:
                    enable_flow_control = serviceData['flow_control']

                if enable_flow_control and 'id_limit' in serviceData:
                    fc_id_limit = serviceData['id_limit']
                elif enable_flow_control:
                    fc_id_limit = 100

                if enable_flow_control and 'doc_limit' in serviceData:
                    fc_doc_limit = serviceData['doc_limit']
                elif enable_flow_control:
                    fc_doc_limit = 100

        o = oaipmh()

        def GetRecord(params):
            try:
                from lr.mustache.oaipmh import GetRecord as must_GetRecord
                identifier = params["identifier"]
                if params["by_doc_ID"] == True:
                    resolver = OAIPMHDocumentResolver()
                    single_doc = o.get_record(params["identifier"])
                    if single_doc is not None:
                        docList = [resolver.process({"doc": single_doc})]
                    else:
                        docList = []
                else:
                    docList = o.get_records_by_resource(params["identifier"])

                doc_idx = 0
                valid_docs = 0
                mustache = must_GetRecord()
                for doc in docList:
                    if doc is not None:
                        doc_idx += 1

                        if "payload_schema" in doc and params[
                                "metadataPrefix"] in map(
                                    lambda x: o_mod.getMetadataPrefix(x),
                                    doc["payload_schema"]
                                ) and OAIPMHDocumentResolver.PAYLOAD_ERROR not in doc:
                            valid_docs += 1

                            if valid_docs == 1:
                                part = mustache.prefix(**self._initMustache(
                                    args=params, req=t_req))
                                yield h.fixUtf8(
                                    self._returnResponse(part, res=t_res))

                            part = mustache.doc(doc)
                            yield h.fixUtf8(
                                self._returnResponse(part, res=t_res))

                if doc_idx == 0:
                    raise IdDoesNotExistError(params['verb'], req=t_req)
                elif valid_docs == 0:
                    raise CannotDisseminateFormatError(params['verb'],
                                                       req=t_req)
                else:
                    yield h.fixUtf8(
                        self._returnResponse(mustache.suffix(), res=t_res))

            except oaipmherrors.Error as e:
                from lr.mustache.oaipmh import Error as err_stache
                err = err_stache()
                yield h.fixUtf8(self._returnResponse(err.xml(e), res=t_res))

        def ListGeneric(params, showDocs=False, record_limit=None):
            if not showDocs:
                from lr.mustache.oaipmh import ListIdentifiers as must_ListID
                mustache = must_ListID()
            else:
                from lr.mustache.oaipmh import ListRecords as must_ListRec
                mustache = must_ListRec()

            try:

                doc_index = 0
                err_count = 0
                metadataPrefix = params["metadataPrefix"]
                from_date = params["from"]
                until_date = params["until"]
                doc_err = None
                rendered_init = False
                resumptionToken = None if "resumptionToken" not in params else params[
                    'resumptionToken']
                records = o.list_identifiers_or_records(metadataPrefix,
                                                        from_date=from_date,
                                                        until_date=until_date,
                                                        rt=resumptionToken,
                                                        fc_limit=record_limit,
                                                        include_docs=showDocs)
                for ident in records:
                    doc_index += 1
                    doc_err = False

                    if OAIPMHDocumentResolver.PAYLOAD_ERROR in ident:
                        err_count += 1
                        doc_err = True
                        log.debug(
                            "Payload Error detected, doc_index: {0}, err_count: {1}"
                            .format(doc_index, err_count))

                    if doc_index - err_count == 1:
                        rendered_init = True
                        part = mustache.prefix(
                            **self._initMustache(args=params, req=t_req))
                        yield h.fixUtf8(self._returnResponse(part, res=t_res))

                    if doc_err is False and (record_limit is None
                                             or doc_index <= record_limit):
                        part = mustache.doc(ident)
                        yield h.fixUtf8(part)
                    elif enable_flow_control:
                        from lr.lib import resumption_token
                        if doc_index - err_count > 0 and doc_index > record_limit:
                            opts = o.list_opts(
                                metadataPrefix,
                                h.convertToISO8601UTC(ident["node_timestamp"]),
                                until_date)
                            opts["startkey_docid"] = ident["doc_ID"]
                            token = resumption_token.get_token(
                                serviceid=service_id,
                                from_date=from_date,
                                until_date=until_date,
                                **opts)
                            part = mustache.resumptionToken(token)
                            yield h.fixUtf8(part)
                            break
                        elif doc_index - err_count == 0 and doc_index > record_limit:
                            opts = o.list_opts(
                                metadataPrefix,
                                h.convertToISO8601UTC(ident["node_timestamp"]),
                                until_date)
                            opts["startkey_docid"] = ident["doc_ID"]
                            payload = resumption_token.get_payload(
                                from_date=from_date,
                                until_date=until_date,
                                **opts)
                            records = o.list_identifiers_or_records(
                                metadataPrefix,
                                from_date=from_date,
                                until_date=until_date,
                                rt=payload,
                                fc_limit=record_limit,
                                include_docs=showDocs)
                            doc_index = 0
                            err_count = 0

                if doc_index == 0 and err_count == 0:
                    raise NoRecordsMatchError(params['verb'], req=t_req)
                elif (doc_index - err_count) == 0:
                    raise CannotDisseminateFormatError(params['verb'],
                                                       req=t_req)
                else:
                    if enable_flow_control and doc_index <= record_limit:
                        yield h.fixUtf8(mustache.resumptionToken())
                    yield h.fixUtf8(mustache.suffix())

            except oaipmherrors.Error as e:
                if not rendered_init:
                    from lr.mustache.oaipmh import Error as err_stache
                    err = err_stache()
                    yield h.fixUtf8(self._returnResponse(err.xml(e),
                                                         res=t_res))
                else:
                    from lr.mustache.oaipmh import ErrorOnly as err_stache
                    err = err_stache()
                    yield h.fixUtf8(
                        self._returnResponse(err.xml(e) + mustache.suffix(),
                                             res=t_res))
            except:
                log.exception("Unknown Error Occurred")

        def ListIdentifiers(params):
            return ListGeneric(params, False, fc_id_limit)

        def ListRecords(params):
            return ListGeneric(params, True, fc_doc_limit)
#        def ListRecords(params):
#            try:
#                from lr.mustache.oaipmh import ListRecords as must_ListRec
#
#                doc_index = 0
#                mustache = must_ListRec()
#                for record in o.list_records(params["metadataPrefix"],from_date=params["from"], until_date=params["until"] ):
#                    doc_index += 1
#                    log.debug(json.dumps(record))
#                    if doc_index == 1:
#                        part = mustache.prefix(**self._initMustache(args=params, req=t_req))
#                        yield self._returnResponse(part, res=t_res)
#
#                    part = mustache.doc(record)
#                    yield self._returnResponse(part, res=t_res)
#
#
#                if doc_index == 0:
#                    raise NoRecordsMatchError(params['verb'], req=t_req)
#                else:
#                    yield mustache.suffix()
#
#            except oaipmherrors.Error as e:
#                from lr.mustache.oaipmh import Error as err_stache
#                err = err_stache()
#                yield self._returnResponse(err.xml(e), res=t_res)
#            except:
#                log.exception("Unable to render template")

        def Identify(params=None):
            body = ""
            try:
                self._initRender(params, ctx=c, req=t_req)
                c.identify = o.identify()
                body = render("/oaipmh-Identify.mako")
            except Exception as e:
                raise BadVerbError()
            return self._returnResponse(body, res=t_res)

        def ListMetadataFormats(params):
            body = ""
            try:
                self._initRender(params, ctx=c, req=t_req)

                fmts = o.list_metadata_formats(identity=params["identifier"],
                                               by_doc_ID=params["by_doc_ID"])
                if len(fmts) == 0:
                    raise NoMetadataFormats(params["verb"])
                c.formats = fmts
                body = render("/oaipmh-ListMetadataFormats.mako")
                return self._returnResponse(body, res=t_res)
            except Error as e:
                raise e

        def ListSets(params=None):
            raise NoSetHierarchyError(verb)

        def NotYetSupported(params=None):
            raise BadVerbError()

        switch = {
            'GetRecord': GetRecord,
            'ListRecords': ListRecords,
            'ListIdentifiers': ListIdentifiers,
            'Identify': Identify,
            'ListMetadataFormats': ListMetadataFormats,
            'ListSets': ListSets
        }
        try:
            params = self._parseParams(flow_control=enable_flow_control,
                                       serviceid=service_id)

            # If this is a special case where we are actually using OAI interface to serve basic harvest
            if params.has_key("metadataPrefix") and params[
                    "metadataPrefix"] == "LR_JSON_0.10.0":
                if params.has_key("identifier") == True:
                    params[self.REQUESTID] = params["identifier"]
                if params.has_key("from") and isinstance(
                        params["from"], datetime):
                    params["from"] = h.convertToISO8601Zformat(params["from"])
                if params.has_key("until") and isinstance(
                        params["until"], datetime):
                    params["until"] = h.convertToISO8601Zformat(
                        params["until"])

                return HarvestController.harvest(self, params, request.body,
                                                 params['verb'].lower())

            verb = params['verb']
            response.headers['Content-Type'] = "text/xml; charset=utf-8"

            return switch[verb](params)
        except Error as e:
            from lr.mustache.oaipmh import Error as err_stache
            err = err_stache()
            return self._returnResponse(err.xml(e), res=t_res)
Example #5
0
    def _handleOAIRequest(self, format='html'):
        o = oaipmh()
        
        def GetRecord(params):
            try:
                c.identifier = params["identifier"]
                if params["by_doc_ID"]  == True:
                    c.docList = [o.get_record(params["identifier"])]
                else:
                    c.docList = o.get_records_by_resource(params["identifier"])
                    if len(c.docList) == 0:
                        raise IdDoesNotExistError(params['verb'])
            except ResourceNotFound:
                raise IdDoesNotExistError(params['verb'])
            
            self._initRender(params)
            
            if c.docList != None:
                for doc in c.docList:
                    if c.metadataPrefix not in doc["payload_schema"]:
                        raise CannotDisseminateFormatError(params['verb'])

            body = "<error/>"
            try:
                body = render("/oaipmh-GetRecord.mako")
            except:
                log.exception("Unable to render template")
            return self._returnResponse(body)
        
        
        def ListIdentifiers(params):
            body = ""
            try:
                c.identifiers = o.list_identifiers(params["metadataPrefix"],from_date=params["from"], until_date=params["until"] )
                
                if len(c.identifiers) == 0:
                    raise NoRecordsMatchError(params['verb'])
                
                self._initRender(params)
                body = render("/oaipmh-ListIdentifiers.mako")
            except NoRecordsMatchError as e:
                raise e
            except:
                log.exception("Unable to render template")
            return self._returnResponse(body)
        
        def ListRecords(params):
            body = ""
            try:
                c.records = o.list_records(params["metadataPrefix"],from_date=params["from"], until_date=params["until"] )
                
                if len(c.records) == 0:
                    raise NoRecordsMatchError(params['verb'])
                
                self._initRender(params)
                body = render("/oaipmh-ListRecords.mako")
            except NoRecordsMatchError as e:
                raise e
            except:
                log.exception("Unable to render template")
            return self._returnResponse(body)
        
        def Identify(params=None):
            body = ""
            try:
                self._initRender(params)
                c.identify = o.identify()
                body = render("/oaipmh-Identify.mako")
            except Exception as e:
                raise BadVerbError()
            return self._returnResponse(body)
        
        def ListMetadataFormats(params):
            body = ""
            try:
                self._initRender(params)
                
                fmts = o.list_metadata_formats(identity=params["identifier"], by_doc_ID=params["by_doc_ID"])
                if len(fmts) == 0:
                    raise NoMetadataFormats(params["verb"])
                c.formats = fmts
                body = render("/oaipmh-ListMetadataFormats.mako")
                return self._returnResponse(body)
            except Error as e:
                raise e
#            except Exception as e:
#                raise NoMetadataFormats(params["verb"])
                
        
        def ListSets(params=None):
            raise NoSetHierarchyError(verb)
            
        def NotYetSupported(params=None):
            raise BadVerbError()
        
            
        switch = {
                  'GetRecord': GetRecord,
                  'ListRecords': ListRecords,
                  'ListIdentifiers': ListIdentifiers,
                  'Identify': Identify,
                  'ListMetadataFormats': ListMetadataFormats,
                  'ListSets': ListSets
                  }
        try:
            params = self._parseParams()
            
            # If this is a special case where we are actually using OAI interface to serve basic harvest
            if params.has_key("metadataPrefix") and params["metadataPrefix"] == "LR_JSON_0.10.0":
                if params.has_key("identifier") == True:
                    params["request_id"] = params["identifier"]
                
                return HarvestController.harvest(self, params, request.body, params['verb'].lower())
        
            verb = params['verb']
            
            return switch[verb](params)
        except Error as e:
            c.error = e
            return self._returnResponse(render('oaipmh-Error.mako'))
    def _handleOAIRequest(self, format='html'):
        t_req = request._current_obj()
        t_res = response._current_obj()
        
        enable_flow_control = False
        fc_id_limit = None
        fc_doc_limit = None
        service_id = None
        serviceDoc = h.getServiceDocument(appConfig['lr.oaipmh.docid'])
        if serviceDoc != None:
            if 'service_id' in serviceDoc:
                service_id = serviceDoc['service_id']
                
            if 'service_data' in serviceDoc:
                serviceData = serviceDoc['service_data']
                if 'flow_control' in serviceData:
                    enable_flow_control = serviceData['flow_control']
                
                if enable_flow_control and 'id_limit' in serviceData:
                    fc_id_limit = serviceData['id_limit']
                elif enable_flow_control:
                    fc_id_limit = 100
                
                if enable_flow_control and 'doc_limit' in serviceData:
                    fc_doc_limit = serviceData['doc_limit']
                elif enable_flow_control:
                    fc_doc_limit = 100
        
        o = oaipmh()
        
        def GetRecord(params):
            try:
                from lr.mustache.oaipmh import GetRecord as must_GetRecord
                identifier = params["identifier"]
                if params["by_doc_ID"] == True:
                    resolver = OAIPMHDocumentResolver()
                    single_doc = o.get_record(params["identifier"])
                    if single_doc is not None: 
                        docList = [resolver.process({ "doc": single_doc })]
                    else:
                        docList = []
                else:
                    docList = o.get_records_by_resource(params["identifier"])

                doc_idx = 0
                valid_docs = 0
                mustache = must_GetRecord()
                for doc in docList:
                    if doc is not None:
                        doc_idx += 1
                        
                        if "payload_schema" in doc and params["metadataPrefix"] in map(lambda x: o_mod.getMetadataPrefix(x), doc["payload_schema"]) and OAIPMHDocumentResolver.PAYLOAD_ERROR not in doc:
                            valid_docs += 1
                        
                            if valid_docs == 1:
                                part = mustache.prefix(**self._initMustache(args=params, req=t_req))
                                yield h.fixUtf8(self._returnResponse(part, res=t_res))
                                
                            part = mustache.doc(doc)
                            yield h.fixUtf8(self._returnResponse(part, res=t_res))
                        
                if doc_idx == 0:
                    raise IdDoesNotExistError(params['verb'], req=t_req)
                elif valid_docs == 0:
                    raise CannotDisseminateFormatError(params['verb'], req=t_req)
                else:
                    yield h.fixUtf8(self._returnResponse(mustache.suffix(), res=t_res))
                
            except oaipmherrors.Error as e:
                from lr.mustache.oaipmh import Error as err_stache
                err = err_stache()
                yield h.fixUtf8(self._returnResponse(err.xml(e), res=t_res))

        def ListGeneric(params, showDocs=False, record_limit=None):
            if not showDocs:
                from lr.mustache.oaipmh import ListIdentifiers as must_ListID
                mustache = must_ListID()
            else:
                from lr.mustache.oaipmh import ListRecords as must_ListRec
                mustache = must_ListRec()
                
            try:
                
                doc_index = 0
                err_count = 0
                metadataPrefix=params["metadataPrefix"]
                from_date=params["from"]
                until_date=params["until"]
                doc_err = None
                rendered_init = False
                resumptionToken = None if "resumptionToken" not in params else params['resumptionToken']
                records = o.list_identifiers_or_records(metadataPrefix,
                                                from_date=from_date, 
                                                until_date=until_date, 
                                                rt=resumptionToken, 
                                                fc_limit=record_limit, 
                                                include_docs=showDocs )
                for ident in records:
                    doc_index += 1
                    doc_err = False
                    
                    if OAIPMHDocumentResolver.PAYLOAD_ERROR in ident:
                        err_count += 1
                        doc_err = True
                        log.debug("Payload Error detected, doc_index: {0}, err_count: {1}".format(doc_index, err_count))
                    
                    if doc_index - err_count == 1:
                        rendered_init = True
                        part = mustache.prefix(**self._initMustache(args=params, req=t_req))
                        yield h.fixUtf8(self._returnResponse(part, res=t_res))

                    if doc_err is False and (record_limit is None or doc_index <= record_limit):
                        part = mustache.doc(ident)
                        yield h.fixUtf8(part)
                    elif enable_flow_control:
                        from lr.lib import resumption_token
                        if doc_index - err_count > 0 and doc_index > record_limit:
                            opts = o.list_opts(metadataPrefix, h.convertToISO8601UTC(ident["node_timestamp"]), until_date)
                            opts["startkey_docid"] = ident["doc_ID"]
                            token = resumption_token.get_token(serviceid=service_id, from_date=from_date, until_date=until_date, **opts)
                            part = mustache.resumptionToken(token)
                            yield h.fixUtf8(part)
                            break
                        elif doc_index - err_count == 0 and doc_index > record_limit:
                            opts = o.list_opts(metadataPrefix, h.convertToISO8601UTC(ident["node_timestamp"]), until_date)
                            opts["startkey_docid"] = ident["doc_ID"]
                            payload = resumption_token.get_payload(from_date=from_date, until_date=until_date, **opts)
                            records = o.list_identifiers_or_records(metadataPrefix,
                                                from_date=from_date, 
                                                until_date=until_date, 
                                                rt=payload, 
                                                fc_limit=record_limit, 
                                                include_docs=showDocs )
                            doc_index = 0
                            err_count = 0
                
                if doc_index == 0 and err_count == 0:
                    raise NoRecordsMatchError(params['verb'], req=t_req)
                elif (doc_index - err_count) == 0:
                    raise CannotDisseminateFormatError(params['verb'], req=t_req)
                else:
                    if enable_flow_control and doc_index <= record_limit:
                        yield h.fixUtf8(mustache.resumptionToken())
                    yield h.fixUtf8(mustache.suffix())
                    
            except oaipmherrors.Error as e:
                if not rendered_init:
                    from lr.mustache.oaipmh import Error as err_stache
                    err = err_stache()
                    yield h.fixUtf8(self._returnResponse(err.xml(e), res=t_res))
                else:
                    from lr.mustache.oaipmh import ErrorOnly as err_stache
                    err = err_stache()
                    yield h.fixUtf8(self._returnResponse(err.xml(e)+mustache.suffix(), res=t_res))
            except:
                log.exception("Unknown Error Occurred")

        def ListIdentifiers(params):
            return ListGeneric(params, False, fc_id_limit)
        
        def ListRecords(params):
            return ListGeneric(params, True, fc_doc_limit)
#        def ListRecords(params):
#            try:
#                from lr.mustache.oaipmh import ListRecords as must_ListRec
#                
#                doc_index = 0
#                mustache = must_ListRec()
#                for record in o.list_records(params["metadataPrefix"],from_date=params["from"], until_date=params["until"] ):
#                    doc_index += 1
#                    log.debug(json.dumps(record))
#                    if doc_index == 1:
#                        part = mustache.prefix(**self._initMustache(args=params, req=t_req))
#                        yield self._returnResponse(part, res=t_res)
#                    
#                    part = mustache.doc(record)
#                    yield self._returnResponse(part, res=t_res)
#                    
#                
#                if doc_index == 0:
#                    raise NoRecordsMatchError(params['verb'], req=t_req)
#                else:
#                    yield mustache.suffix()
#                
#            except oaipmherrors.Error as e:
#                from lr.mustache.oaipmh import Error as err_stache
#                err = err_stache()
#                yield self._returnResponse(err.xml(e), res=t_res)
#            except:
#                log.exception("Unable to render template")
        
        def Identify(params=None):
            body = ""
            try:
                self._initRender(params, ctx=c, req=t_req)
                c.identify = o.identify()
                body = render("/oaipmh-Identify.mako")
            except Exception as e:
                raise BadVerbError()
            return self._returnResponse(body, res=t_res)
        
        def ListMetadataFormats(params):
            body = ""
            try:
                self._initRender(params, ctx=c, req=t_req)
                
                fmts = o.list_metadata_formats(identity=params["identifier"], by_doc_ID=params["by_doc_ID"])
                if len(fmts) == 0:
                    raise NoMetadataFormats(params["verb"])
                c.formats = fmts
                body = render("/oaipmh-ListMetadataFormats.mako")
                return self._returnResponse(body, res=t_res)
            except Error as e:
                raise e
        
        def ListSets(params=None):
            raise NoSetHierarchyError(verb)
            
        def NotYetSupported(params=None):
            raise BadVerbError()
        
        
            
        switch = {
                  'GetRecord': GetRecord,
                  'ListRecords': ListRecords,
                  'ListIdentifiers': ListIdentifiers,
                  'Identify': Identify,
                  'ListMetadataFormats': ListMetadataFormats,
                  'ListSets': ListSets
                  }
        try:
            params = self._parseParams(flow_control=enable_flow_control, serviceid=service_id)
            
            # If this is a special case where we are actually using OAI interface to serve basic harvest
            if params.has_key("metadataPrefix") and params["metadataPrefix"] == "LR_JSON_0.10.0":
                if params.has_key("identifier") == True:
                    params[self.REQUESTID] = params["identifier"]
                if params.has_key("from") and isinstance(params["from"], datetime):
                    params["from"] = h.convertToISO8601Zformat(params["from"])
                if params.has_key("until") and isinstance(params["until"], datetime):
                    params["until"] = h.convertToISO8601Zformat(params["until"])
                
                return HarvestController.harvest(self, params, request.body, params['verb'].lower())
        
            verb = params['verb']
            response.headers['Content-Type'] = "text/xml; charset=utf-8"
            
            return switch[verb](params)
        except Error as e:
            from lr.mustache.oaipmh import Error as err_stache
            err = err_stache()
            return self._returnResponse(err.xml(e), res=t_res)
Example #7
0
    def _handleOAIRequest(self, format='html'):
        t_req = request._current_obj()
        t_res = response._current_obj()
        
        o = oaipmh()
        
        def GetRecord(params):
            try:
                from lr.mustache.oaipmh import GetRecord as must_GetRecord
                identifier = params["identifier"]
                if params["by_doc_ID"] == True:
                    docList = [o.get_record(params["identifier"])]
                else:
                    docList = o.get_records_by_resource(params["identifier"])

                doc_idx = 0
                valid_docs = 0
                mustache = must_GetRecord()
                for doc in docList:
                    if doc is not None:
                        doc_idx += 1
                        
                        if "payload_schema" in doc and params["metadataPrefix"] in doc["payload_schema"]:
                            valid_docs += 1
                        
                            if valid_docs == 1:
                                part = mustache.prefix(**self._initMustache(args=params, req=t_req))
                                yield self._returnResponse(part, res=t_res)
                                
                            part = mustache.doc(doc)
                            yield self._returnResponse(part, res=t_res)
                        
                if doc_idx == 0:
                    raise IdDoesNotExistError(params['verb'], req=t_req)
                elif valid_docs == 0:
                    raise CannotDisseminateFormatError(params['verb'], req=t_req)
                else:
                    yield self._returnResponse(mustache.suffix(), res=t_res)
                
            except oaipmherrors.Error as e:
                from lr.mustache.oaipmh import Error as err_stache
                err = err_stache()
                yield self._returnResponse(err.xml(e), res=t_res)
            
        
        
        def ListIdentifiers(params):
            try:
                from lr.mustache.oaipmh import ListIdentifiers as must_ListID
                
                doc_index = 0
                mustache = must_ListID()
                for ident in o.list_identifiers(params["metadataPrefix"],from_date=params["from"], until_date=params["until"] ):
                    doc_index += 1
                    log.debug(json.dumps(ident))
                    if doc_index == 1:
#                        self._initRender(params, c, t_req)
#                        part = t_render("/oaipmh-ListIdentifiers-prefix.mako")
                        part = mustache.prefix(**self._initMustache(args=params, req=t_req))
                        yield self._returnResponse(part, res=t_res)
                    
                    part = mustache.doc(ident)
                    yield part
                
                if doc_index == 0:
                    raise NoRecordsMatchError(params['verb'], req=t_req)
                else:
                    yield mustache.suffix()
                    
            except oaipmherrors.Error as e:
                from lr.mustache.oaipmh import Error as err_stache
                err = err_stache()
                yield self._returnResponse(err.xml(e), res=t_res)
            except:
                log.exception("Unable to render template")

        
        def ListRecords(params):
            try:
                from lr.mustache.oaipmh import ListRecords as must_ListRec
                
                doc_index = 0
                mustache = must_ListRec()
                for record in o.list_records(params["metadataPrefix"],from_date=params["from"], until_date=params["until"] ):
                    doc_index += 1
                    log.debug(json.dumps(record))
                    if doc_index == 1:
                        part = mustache.prefix(**self._initMustache(args=params, req=t_req))
                        yield self._returnResponse(part, res=t_res)
                    
                    part = mustache.doc(record)
                    yield self._returnResponse(part, res=t_res)
                    
                
                if doc_index == 0:
                    raise NoRecordsMatchError(params['verb'], req=t_req)
                else:
                    yield mustache.suffix()
                
            except oaipmherrors.Error as e:
                from lr.mustache.oaipmh import Error as err_stache
                err = err_stache()
                yield self._returnResponse(err.xml(e), res=t_res)
            except:
                log.exception("Unable to render template")
        
        def Identify(params=None):
            body = ""
            try:
                self._initRender(params)
                c.identify = o.identify()
                body = render("/oaipmh-Identify.mako")
            except Exception as e:
                raise BadVerbError()
            return self._returnResponse(body)
        
        def ListMetadataFormats(params):
            body = ""
            try:
                self._initRender(params)
                
                fmts = o.list_metadata_formats(identity=params["identifier"], by_doc_ID=params["by_doc_ID"])
                if len(fmts) == 0:
                    raise NoMetadataFormats(params["verb"])
                c.formats = fmts
                body = render("/oaipmh-ListMetadataFormats.mako")
                return self._returnResponse(body)
            except Error as e:
                raise e
#            except Exception as e:
#                raise NoMetadataFormats(params["verb"])
                
        
        def ListSets(params=None):
            raise NoSetHierarchyError(verb)
            
        def NotYetSupported(params=None):
            raise BadVerbError()
        
        
            
        switch = {
                  'GetRecord': GetRecord,
                  'ListRecords': ListRecords,
                  'ListIdentifiers': ListIdentifiers,
                  'Identify': Identify,
                  'ListMetadataFormats': ListMetadataFormats,
                  'ListSets': ListSets
                  }
        try:
            params = self._parseParams()
            
            # If this is a special case where we are actually using OAI interface to serve basic harvest
            if params.has_key("metadataPrefix") and params["metadataPrefix"] == "LR_JSON_0.10.0":
                if params.has_key("identifier") == True:
                    params["request_id"] = params["identifier"]
                
                return HarvestController.harvest(self, params, request.body, params['verb'].lower())
        
            verb = params['verb']
            
            return switch[verb](params)
        except Error as e:
            from lr.mustache.oaipmh import Error as err_stache
            err = err_stache()
            return self._returnResponse(err.xml(e), res=t_res)
Example #8
0
    def _handleOAIRequest(self, format='html'):
        o = oaipmh()

        def GetRecord(params):
            try:
                c.identifier = params["identifier"]
                if params["by_doc_ID"] == True:
                    c.docList = [o.get_record(params["identifier"])]
                else:
                    c.docList = o.get_records_by_resource(params["identifier"])
                    if len(c.docList) == 0:
                        raise IdDoesNotExistError(params['verb'])
            except ResourceNotFound:
                raise IdDoesNotExistError(params['verb'])

            self._initRender(params)

            if c.docList != None:
                for doc in c.docList:
                    if c.metadataPrefix not in doc["payload_schema"]:
                        raise CannotDisseminateFormatError(params['verb'])

            body = "<error/>"
            try:
                body = render("/oaipmh-GetRecord.mako")
            except:
                log.exception("Unable to render template")
            return self._returnResponse(body)

        def ListIdentifiers(params):
            body = ""
            try:
                c.identifiers = o.list_identifiers(params["metadataPrefix"],
                                                   from_date=params["from"],
                                                   until_date=params["until"])

                if len(c.identifiers) == 0:
                    raise NoRecordsMatchError(params['verb'])

                self._initRender(params)
                body = render("/oaipmh-ListIdentifiers.mako")
            except NoRecordsMatchError as e:
                raise e
            except:
                log.exception("Unable to render template")
            return self._returnResponse(body)

        def ListRecords(params):
            body = ""
            try:
                c.records = o.list_records(params["metadataPrefix"],
                                           from_date=params["from"],
                                           until_date=params["until"])

                if len(c.records) == 0:
                    raise NoRecordsMatchError(params['verb'])

                self._initRender(params)
                body = render("/oaipmh-ListRecords.mako")
            except NoRecordsMatchError as e:
                raise e
            except:
                log.exception("Unable to render template")
            return self._returnResponse(body)

        def Identify(params=None):
            body = ""
            try:
                self._initRender(params)
                c.identify = o.identify()
                body = render("/oaipmh-Identify.mako")
            except Exception as e:
                raise BadVerbError()
            return self._returnResponse(body)

        def ListMetadataFormats(params):
            body = ""
            try:
                self._initRender(params)

                fmts = o.list_metadata_formats(identity=params["identifier"],
                                               by_doc_ID=params["by_doc_ID"])
                if len(fmts) == 0:
                    raise NoMetadataFormats(params["verb"])
                c.formats = fmts
                body = render("/oaipmh-ListMetadataFormats.mako")
                return self._returnResponse(body)
            except Error as e:
                raise e
#            except Exception as e:
#                raise NoMetadataFormats(params["verb"])

        def ListSets(params=None):
            raise NoSetHierarchyError(verb)

        def NotYetSupported(params=None):
            raise BadVerbError()

        switch = {
            'GetRecord': GetRecord,
            'ListRecords': ListRecords,
            'ListIdentifiers': ListIdentifiers,
            'Identify': Identify,
            'ListMetadataFormats': ListMetadataFormats,
            'ListSets': ListSets
        }
        try:
            params = self._parseParams()

            # If this is a special case where we are actually using OAI interface to serve basic harvest
            if params.has_key("metadataPrefix") and params[
                    "metadataPrefix"] == "LR_JSON_0.10.0":
                if params.has_key("identifier") == True:
                    params["request_id"] = params["identifier"]

                return HarvestController.harvest(self, params, request.body,
                                                 params['verb'].lower())

            verb = params['verb']

            return switch[verb](params)
        except Error as e:
            c.error = e
            return self._returnResponse(render('oaipmh-Error.mako'))