<commit_before>/*
 * Copyright 2014 Telefonica Investigación y Desarrollo, S.A.U
 *
 * This file is part of fiware-orion-pep
 *
 * fiware-orion-pep is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 *
 * fiware-orion-pep is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public
 * License along with fiware-orion-pep.
 * If not, seehttp://www.gnu.org/licenses/.
 *
 * For those usages not covered by the GNU Affero General Public License
 * please contact with::[<EMAIL>]
 */

'use strict';

var http = require('http'),
    https = require('https'),
    fs = require('fs'),
    express = require('express'),
    errors = require('./errors'),
    config = require('../config'),
    adminMiddleware = require('./middleware/administration'),
    validation = require('./services/validation'),
    proxyMiddleware = require('./middleware/proxy'),
    authorization,
    async = require('async'),
    logger = require('logops'),
    domainMiddleware = require('./middleware/domain').requestDomain();

/**
 * Generic error handler for every request that raises an error in the validation process. TODO: change the error code
 * depending on the error type.
 *
 * @param {Object} error         Error that triggered the execution of the handler.
 * @param {Object} req           Incoming request.
 * @param {Object} res           Outgoing response.
 * @param {Function} next        Call to the next error handler in the chain.
 */
function handleError(error, req, res, next) {
    var code;

    if (error.code) {
        code = error.code;
    } else {
        code = 403;
    }

    res.json(code, {
        name: error.name,
        message: error.message
    });
}

/**
 * Error handler intended to transform errors triggered in the Express framework by middlewares we don't
 * control, wrapping the foreign errors in an error of our own. This middleware is very specific for the different
 * types of external errors.
 *
 * @param {Object} error         Error that triggered the execution of the handler.
 * @param {Object} req           Incoming request.
 * @param {Object} res           Outgoing response.
 * @param {Function} next        Call to the next error handler in the chain.
 */
function transformSystemErrors(error, req, res, next) {
    if (error.name === 'SyntaxError') {
        var newError = new errors.WrongJsonPayload();
        newError.code = 400;

        next(newError);
    } else {
        next(error);
    }
}

function setAuthenticationModule(moduleName) {
    authorization = require('./services/' + config.authentication.module + 'Auth');
}

/**
 * Middleware that makes Express read the incoming body if the content-type is text/xml or application/xml (the default
 * behavior is to read the body if it can be parsed and leave it unread in any other case).
 *
 * @param {Object} req           Incoming request.
 * @param {Object} res           Outgoing response.
 * @param {Function} next        Invokes the next middleware in the chain.
 */
function xmlRawBody(req, res, next) {<commit_msg>FIX Remove code overwritting in transform system errors<commit_after>/*
 * Copyright 2014 Telefonica Investigación y Desarrollo, S.A.U
 *
 * This file is part of fiware-orion-pep
 *
 * fiware-orion-pep is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 *
 * fiware-orion-pep is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public
 * License along with fiware-orion-pep.
 * If not, seehttp://www.gnu.org/licenses/.
 *
 * For those usages not covered by the GNU Affero General Public License
 * please contact with::[<EMAIL>]
 */

'use strict';

var http = require('http'),
    https = require('https'),
    fs = require('fs'),
    express = require('express'),
    errors = require('./errors'),
    config = require('../config'),
    adminMiddleware = require('./middleware/administration'),
    validation = require('./services/validation'),
    proxyMiddleware = require('./middleware/proxy'),
    authorization,
    async = require('async'),
    logger = require('logops'),
    domainMiddleware = require('./middleware/domain').requestDomain();

/**
 * Generic error handler for every request that raises an error in the validation process. TODO: change the error code
 * depending on the error type.
 *
 * @param {Object} error         Error that triggered the execution of the handler.
 * @param {Object} req           Incoming request.
 * @param {Object} res           Outgoing response.
 * @param {Function} next        Call to the next error handler in the chain.
 */
function handleError(error, req, res, next) {
    var code;

    if (error.code) {
        code = error.code;
    } else {
        code = 403;
    }

    res.json(code, {
        name: error.name,
        message: error.message
    });
}

/**
 * Error handler intended to transform errors triggered in the Express framework by middlewares we don't
 * control, wrapping the foreign errors in an error of our own. This middleware is very specific for the different
 * types of external errors.
 *
 * @param {Object} error         Error that triggered the execution of the handler.
 * @param {Object} req           Incoming request.
 * @param {Object} res           Outgoing response.
 * @param {Function} next        Call to the next error handler in the chain.
 */
function transformSystemErrors(error, req, res, next) {
    if (error.name === 'SyntaxError') {
        next(new errors.WrongJsonPayload());
    } else {
        next(error);
    }
}

function setAuthenticationModule(moduleName) {
    authorization = require('./services/' + config.authentication.module + 'Auth');
}

/**
 * Middleware that makes Express read the incoming body if the content-type is text/xml or application/xml (the default
 * behavior is to read the body if it can be parsed and leave it unread in any other case).
 *
 * @param {Object} req           Incoming request.
 * @param {Object} res           Outgoing response.
 * @param {Function} next        Invokes the next middleware in the chain.
 */
function xmlRawBody(req, res, next) {
{'fraction_non_alphanumeric': 0.09868135052890885, 'fraction_numerical': 0.0027532241704100856, 'mean_word_length': 3.091286307053942, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2063562', 'n_tokens_mistral': 1824, 'n_tokens_neox': 1797, 'n_words': 772}