<commit_before>var util = require('util'),
    events = require('events'),
    express = require('express'),
    bodyParser = require('body-parser'),
    Promise = require('promise'),
    xml2js = require('xml2js'),
    logger = require('./logger'),
    agentUtils = require('./utils');

function NotificationHandler(config) {
    var self = this;

    this.sessionNotification = function (notificationSet) {
        notificationSet.Notification.forEach(function (notification) {
            agentUtils.parseXml(notification)
                .then(function (parsed) {
                    self.emit('session', parsed.SessionNotification.Session[0].$);
                })
        });
    };

    this.router = express.Router();
    this.logger = config && config.logger ? config.logger : logger();

    this.router.use('/agent/notification', bodyParser.text({type: 'text/xml'}), function (req, res) {
        res.send('OK');
        agentUtils.parseXml(req.body)
            .then(function (parsed) {
                switch (parsed.NotificationSet.$.svcid) {
                    case 'session':
                        self.sessionNotification(parsed.NotificationSet);
                        break;
                    default:
                        self.logger.error('unknown notification type %s', parsed.$.svcid)
                }
            })
            .catch(function (err) {
                self.logger.error(err);
            });
    });
}

util.inherits(NotificationHandler, events.EventEmitter);
<commit_msg>jsdoc<commit_after>var util = require('util'),
    events = require('events'),
    express = require('express'),
    bodyParser = require('body-parser'),
    Promise = require('promise'),
    xml2js = require('xml2js'),
    logger = require('./logger'),
    agentUtils = require('./utils');

/**
 * Handles notifications
 * @param {object} [options] Options
 * @param {winston~Logger|Logger} [options.logger=new Logger()] Logger
 *
 * @constructor
 */
function NotificationHandler(options) {
    var self = this;

    /**
     * Parses notifications in a notification set and emits a 'session' event for each. CookieShield instances listen
     * on this event to delete any destroyed cookies from the agent's session cache.
     *
     * @param {object} notificationSet OpenAM notification set (parsed XML document)
     * @fires session
     *
     * @example
     * app.use(agent.notifications.router);
     *   agent.notifications.on('session', function (session) {
     *   console.log('server - session changed: %s', JSON.stringify(session));
     * });
     */
    this.sessionNotification = function (notificationSet) {
        notificationSet.Notification.forEach(function (notification) {
            agentUtils.parseXml(notification)
                .then(function (parsed) {
                    self.emit('session', parsed.SessionNotification.Session[0].$);
                });
        });
    };

    /**
     * A express router for the notification receiver endpoint. It can be used as a middleware for your express
     * application. It adds a single route: /agent/notifications which can be used to receive notifications from OpenAM.
     * When a notification is received, its contents will be parsed and handled by one of the handler functions.
     *
     * @type {express~Router}
     *
     * @example
     * var app = require('express')(),
     *     agent = new (require('openam-agent').PolicyAgent)({...});
     *
     * app.use(agent.notifications)
     */
    this.router = express.Router();

    /**
     * Logger
     *
     * @type {winston~Logger|Logger}
     */
    this.logger = options && options.logger ? options.logger : logger();

    this.router.use('/agent/notification', bodyParser.text({type: 'text/xml'}), function (req, res) {
        res.send('OK');
        agentUtils.parseXml(req.body)
            .then(function (parsed) {
                switch (parsed.NotificationSet.$.svcid) {
                    case 'session':
                        self.sessionNotification(parsed.NotificationSet);
                        break;
                    default:
                        self.logger.error('unknown notification type %s', parsed.$.svcid)
                }
            })
            .catch(function (err) {
                self.logger.error(err);
            });
    });
}

util.inherits(NotificationHandler, events.EventEmitter);

{'fraction_non_alphanumeric': 0.13036565977742448, 'fraction_numerical': 0.0013627072450601862, 'mean_word_length': 2.1913043478260867, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4186002', 'n_tokens_mistral': 1147, 'n_tokens_neox': 1124, 'n_words': 259}