Skip to content

Add OAIPMH support #1656

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion IM/REST.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
from radl.radl import RADL, Features, Feature
from IM.tosca.Tosca import Tosca
from IM.openid.JWT import JWT
from IM.oaipmh.oai import OAI
from IM.oaipmh.utils import Repository

logger = logging.getLogger('InfrastructureManager')

Expand Down Expand Up @@ -471,7 +473,7 @@ def RESTCreateInfrastructure():
if "application/json" in content_type:
radl_data = parse_radl_json(radl_data)
elif "text/yaml" in content_type or "text/x-yaml" in content_type or "application/yaml" in content_type:
tosca_data = Tosca(radl_data)
tosca_data = Tosca(radl_data, tosca_repo=Config.OAIPMH_REPO_BASE_IDENTIFIER_URL)
_, radl_data = tosca_data.to_radl()
elif "text/plain" in content_type or "*/*" in content_type or "text/*" in content_type:
content_type = "text/plain"
Expand Down Expand Up @@ -1121,6 +1123,42 @@ def RESTGetStats():
return return_error(400, "Error getting stats: %s" % get_ex_error(ex))


@app.route('/oai', methods=['GET', 'POST'])
def oaipmh():
if not (Config.OAIPMH_REPO_BASE_IDENTIFIER_URL and
Config.OAIPMH_REPO_NAME and Config.OAIPMH_REPO_DESCRIPTION):
return return_error(400, "OAI-PMH not enabled.")

oai = OAI(Config.OAIPMH_REPO_NAME, flask.request.base_url, Config.OAIPMH_REPO_DESCRIPTION,
Config.OAIPMH_REPO_BASE_IDENTIFIER_URL, repo_admin_email=Config.OAIPMH_REPO_ADMIN_EMAIL)

# Get list of TOSCA templates from Config.OAIPMH_REPO_BASE_IDENTIFIER_URL
metadata_dict = {}
try:
repo = Repository.create(Config.OAIPMH_REPO_BASE_IDENTIFIER_URL)
for name, elem in repo.list().items():
tosca = yaml.safe_load(repo.get(elem))
metadata = tosca["metadata"]
metadata["identifier"] = Config.OAIPMH_REPO_BASE_IDENTIFIER_URL + name
metadata["resource_type"] = "software"
metadata["rights"] = "openaccess"
metadata_dict[name] = metadata
except Exception as ex:
logger.exception("Error getting metadata from TOSCA templates")
return return_error(400, "Error getting metadata from TOSCA templates: %s" % get_ex_error(ex))

response_xml = oai.processRequest(flask.request, metadata_dict)
return flask.make_response(response_xml, 200, {'Content-Type': 'text/xml'})


@app.route('/static/<filename>', methods=['GET'])
def static_files(filename):
if Config.STATIC_FILES_DIR:
return flask.send_from_directory(Config.STATIC_FILES_DIR, filename)
else:
return return_error(404, "Static files not enabled.")


@app.errorhandler(403)
def error_mesage_403(error):
return return_error(403, error.description)
Expand Down
5 changes: 5 additions & 0 deletions IM/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ class Config:
VM_TAG_IM_URL = None
VM_TAG_IM = None
ADMIN_USER = {}
STATIC_FILES_DIR = None
OAIPMH_REPO_ADMIN_EMAIL = None
OAIPMH_REPO_NAME = None
OAIPMH_REPO_DESCRIPTION = None
OAIPMH_REPO_BASE_IDENTIFIER_URL = None


config = ConfigParser()
Expand Down
Empty file added IM/oaipmh/__init__.py
Empty file.
100 changes: 100 additions & 0 deletions IM/oaipmh/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#
# IM - Infrastructure Manager
# Copyright (C) 2024 - GRyCAP - Universitat Politecnica de Valencia
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
from lxml.etree import Element # nosec


class Errors():

@staticmethod
def badVerb():
# Create the root element with the specified verb
error = Element('error')
error.set('code', 'badVerb')
error.text = 'Value of the verb argument is not a legal OAI-PMH verb, \
the verb argument is missing, or the verb argument is repeated.'

return error

@staticmethod
def badArgument():
# Create the root element with the specified verb
error = Element('error')
error.set('code', 'badArgument')
error.text = 'The request includes illegal arguments, is missing required \
arguments, includes a repeated argument, or values for arguments have an illegal syntax.'

return error

@staticmethod
def cannotDisseminateFormat():
# Create the root element with the specified verb
error = Element('error')
error.set('code', 'cannotDisseminateFormat')
error.text = 'The metadata format identified by the value given for the metadataPrefix \
argument is not supported by the item or by the repository.'

return error

@staticmethod
def idDoesNotExist():
# Create the root element with the specified verb
error = Element('error')
error.set('code', 'idDoesNotExist')
error.text = 'The value of the identifier argument is unknown or illegal in this repository.'

return error

@staticmethod
def badResumptionToken():
# Create the root element with the specified verb
error = Element('error')
error.set('code', 'badResumptionToken')
error.text = 'The value of the resumptionToken argument is invalid or expired.'

return error

@staticmethod
def noRecordsMatch():
# Create the root element with the specified verb
error = Element('error')
error.set('code', 'noRecordsMatch')
error.text = 'The combination of the values of the from, until, set, and metadataPrefix \
arguments results in an empty list.'

return error

@staticmethod
def noMetadataFormats():
# Create the root element with the specified verb
error = Element('error')
error.set('code', 'noMetadataFormats')
error.text = 'There are no metadata formats available for the specified item.'

return error

@staticmethod
def noSetHierarchy():
# Create the root element with the specified verb
error = Element('error')
error.set('code', 'noSetHierarchy')
error.text = 'The repository does not support sets'

return error
Loading