pid-parser-service/pidResolverService.js

116 lines
3.8 KiB
JavaScript

const axios = require('axios');
var express = require("express");
var bodyParser = require("body-parser");
var PropertiesReader = require('properties-reader');
var properties = PropertiesReader('./properties.file');
var app = express();
var http = null;
const ParserUtils = require("./parsers/ParserUtils");
let resolverAPI = properties.get('PIDMR_API_URL')
const url = require('url');
const utils = new ParserUtils();
// Properties
if (properties.get('ssl')) {
http = require("https");
} else {
http = require("http");
}
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(function (req, res, next) {
res.header('Access-Control-Allow-Origin', req.headers.origin);
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, x-xsrf-token');
res.header('Access-Control-Allow-Credentials', "true");
res.header('Access-Control-Allow-Methods', 'GET, OPTIONS, POST, DELETE');
res.header('Access-Control-Max-Age', "1800");
next();
});
app.get('/:pid', function (req, res) {
const pid = req.params.pid;
let requestUrl = resolverAPI + pid;
console.log("PIDMR url : " + requestUrl);
axios.get(requestUrl)
.then(function (response) {
// handle success
// console.log("status:", response.status, response.data.error);
if (response.status != 200 || response.data.error) {
res.status(response.data.error ? 500 : response.status).json({
code: response.data.error ? 500 : response.status,
message: response.data.error ? response.data.error : ("An error occurred while resolving pid " + pid + ". ")
});
return;
}
const finalUrl = response.request.res.responseUrl;
// Parse the URL to extract the host
const parsedUrl = new url.URL(finalUrl);
const finalHost = parsedUrl.host;
/*if(finalHost.indexOf("pidmr.") != -1 ){
res.status(500).json({
status: response.status,
message: "An error occurred while resolving pid " + pid + (response.data ? " - " + JSON.stringify(response.data) :"")
});
return;
}*/
console.log('Final Redirected URL:', finalUrl);
console.log('Final Host:', finalHost);
if (utils.getSupportedParsers().indexOf(finalHost) == -1) {
console.log(finalHost + ": This endpoint is not supported for parsing");
res.status(404).json({
code: 404,
message: "This endpoint is not supported for parsing."
});
return;
}
if (utils.getXMLParsers().indexOf(finalHost) != -1) {
utils.parseXML(pid, finalHost, response.data).then(entity => {
res.status(200).json(entity);
})
} else {
let entity = utils.parseJSON(pid, finalHost, response.data)
if (entity) {
res.status(200).json(entity);
} else {
console.log("An error occurred while parsing from " + finalHost + " pid " + pid);
res.status(500).json({
code: 500,
message: "An error occurred while parsing from " + finalHost + " pid " + pid
});
}
}
})
.catch(function (error) {
console.log(error.response.status, error.response.data.errorCode)
res.status(error.response.data.errorCode ? error.response.data.errorCode : error.response.status).json(
(error.response.data ? error.response.data : {
code: error.response.code,
message: "An error occurred while resolving pid:" + pid
}));
return
})
.finally(function () {
// always executed
});
});
app.get('/supported-endpoints', function (req, res) {
res.status(200).json(utils.getSupportedParsers());
});
const server = app.listen(properties.get('port'), function () {
console.log("Listening on port %s...", server.address().port);
});