<commit_before>/*
Kettle Server.

Copyright 2012-2013 OCAD University

Licensed under the New BSD license. You may not use this file except in
compliance with this License.

You may obtain a copy of the License at
https://github.com/gpii/kettle/LICENSE.txt
*/

/*global require, __dirname */

(function () {

    "use strict";

    var fluid = require("infusion"),
        path = require("path"),
        express = require("express"),
        $ = fluid.registerNamespace("jQuery"),
        kettle = fluid.registerNamespace("kettle");

    fluid.defaults("kettle.server", {
        gradeNames: [
            "fluid.eventedComponent",
            "autoInit",
            "kettle.use.bodyParser"
        ],
        components: {
            requests: {
                type: "kettle.requests",
                priority: "0"
            },
            requestProxy: {
                type: "kettle.requestProxy",
                priority: "1"
            },
            createRequest: {
                type: "kettle.server.config.createRequest",
                priority: "2"
            },
            callbackWrapper: {
                type: "kettle.requestContextCallbackWrapper"
            }
        },
        invokers: {
            listen: {
                funcName: "kettle.server.listen",
                args: "{that}"
            },
            setLogging: {
                funcName: "fluid.setLogging",
                args: "{that}.options.logging"
            },
            amalgamateHandlers: {
                funcName: "kettle.server.amalgamateHandlers",
                args: ["{that}.options.handlers", "{arguments}.0"]
            },
            registerHandlers: {
                funcName: "kettle.server.registerHandlers",
                args: "{that}"
            },
            stop: {
                funcName: "kettle.server.stop",
                args: "{that}"
            },
            trackConnections: {
                funcName: "kettle.server.trackConnections",
                args: ["{that}.sockets", "{that}.server"]
            },
            closeConnections: {
                funcName: "kettle.server.closeConnections",
                args: "{that}.sockets"
            },
            checkHandlerValue: {
                funcName: "kettle.server.checkHandlerValue",
                args: "{arguments}.0"
            },
            registerHandler: {
                funcName: "kettle.server.registerHandler",
                args: ["{that}", "{arguments}.0", "{arguments}.1"]
            }
        },
        members: {
            server: {
                expander: {
                    func: "kettle.server.express"
                }
            },
            sockets: []
        },
        root: path.join(__dirname, "../../.."),
        events: {
            onRegisterHandler: "preventable",
            onListen: null,
            beforeStop: null,
            onStopped: null
        },
        listeners: {
            onRegisterHandler: [{
                listener: "{that}.checkHandlerValue",
                priority: "first"
            }, {
                listener: "{that}.registerHandler",
                priority: "last"
            }],
            onCreate: [
                "{that}.setLogging",
                "{that}.registerHandlers",
                "{that}.listen"
            ],
            onListen: "{that}.trackConnections",
            onDestroy: "{that}.stop",
            beforeStop: "{that}.closeConnections"
        },
        registerHandlerEventMap: {
            get: "{that}.events.onRegisterHandler",
            post: "{that}.events.onRegisterHandler",
            "delete": "{that}.events.onRegisterHandler"
        },
        handlers: {},
        port: 8080,
        logging: true
    });

    kettle.server.amalgamateHandlers = function (target, source) {
        $.extend(true, target, source);
    };

    kettle.server.registerHandler = function (that, handler, context) {
        that.server[handler.type](handler.route, function (req) {
            that.requests.events.onHandleRequest.fire(req.fluidRequest,
                context);
        });
    };

    /**
     * Check if the handler is a value.
     * @param  {JSON} handler Handler spec
     * @return {bool|undefined} return false to stop handler registration.
     */
    kettle.server.checkHandlerValue = function (handler) {
        if (!handler) {
            return false;
        }
    };

    kettle.server.registerHandlers = function (that) {
        fluid.each(that.options.handlers, function (handler, context) {
            that.options.registerHandlerEventMap[handler.type].fire(handler,
                context);
        });
    };

    kettle.server.stop = function (that) {<commit_msg>GPII-245: fixed up handler null handling.<commit_after>/*
Kettle Server.

Copyright 2012-2013 OCAD University

Licensed under the New BSD license. You may not use this file except in
compliance with this License.

You may obtain a copy of the License at
https://github.com/gpii/kettle/LICENSE.txt
*/

/*global require, __dirname */

(function () {

    "use strict";

    var fluid = require("infusion"),
        path = require("path"),
        express = require("express"),
        $ = fluid.registerNamespace("jQuery"),
        kettle = fluid.registerNamespace("kettle");

    fluid.defaults("kettle.server", {
        gradeNames: [
            "fluid.eventedComponent",
            "autoInit",
            "kettle.use.bodyParser"
        ],
        components: {
            requests: {
                type: "kettle.requests",
                priority: "0"
            },
            requestProxy: {
                type: "kettle.requestProxy",
                priority: "1"
            },
            createRequest: {
                type: "kettle.server.config.createRequest",
                priority: "2"
            },
            callbackWrapper: {
                type: "kettle.requestContextCallbackWrapper"
            }
        },
        invokers: {
            listen: {
                funcName: "kettle.server.listen",
                args: "{that}"
            },
            setLogging: {
                funcName: "fluid.setLogging",
                args: "{that}.options.logging"
            },
            amalgamateHandlers: {
                funcName: "kettle.server.amalgamateHandlers",
                args: ["{that}.options.handlers", "{arguments}.0"]
            },
            registerHandlers: {
                funcName: "kettle.server.registerHandlers",
                args: "{that}"
            },
            stop: {
                funcName: "kettle.server.stop",
                args: "{that}"
            },
            trackConnections: {
                funcName: "kettle.server.trackConnections",
                args: ["{that}.sockets", "{that}.server"]
            },
            closeConnections: {
                funcName: "kettle.server.closeConnections",
                args: "{that}.sockets"
            },
            checkHandlerValue: {
                funcName: "kettle.server.checkHandlerValue",
                args: "{arguments}.0"
            },
            registerHandler: {
                funcName: "kettle.server.registerHandler",
                args: ["{that}", "{arguments}.0", "{arguments}.1"]
            }
        },
        members: {
            server: {
                expander: {
                    func: "kettle.server.express"
                }
            },
            sockets: []
        },
        root: path.join(__dirname, "../../.."),
        events: {
            onRegisterHandler: "preventable",
            onListen: null,
            beforeStop: null,
            onStopped: null
        },
        listeners: {
            onRegisterHandler: [{
                listener: "{that}.checkHandlerValue",
                priority: "first"
            }, {
                listener: "{that}.registerHandler",
                priority: "last"
            }],
            onCreate: [
                "{that}.setLogging",
                "{that}.registerHandlers",
                "{that}.listen"
            ],
            onListen: "{that}.trackConnections",
            onDestroy: "{that}.stop",
            beforeStop: "{that}.closeConnections"
        },
        registerHandlerEventMap: {
            get: "{that}.events.onRegisterHandler",
            post: "{that}.events.onRegisterHandler",
            "delete": "{that}.events.onRegisterHandler"
        },
        handlers: {},
        port: 8080,
        logging: true
    });

    kettle.server.amalgamateHandlers = function (target, source) {
        $.extend(true, target, source);
    };

    kettle.server.registerHandler = function (that, handler, context) {
        that.server[handler.type](handler.route, function (req) {
            that.requests.events.onHandleRequest.fire(req.fluidRequest,
                context);
        });
    };

    /**
     * Check if the handler is a value.
     * @param  {JSON} handler Handler spec
     * @return {bool|undefined} return false to stop handler registration.
     */
    kettle.server.checkHandlerValue = function (handler) {
        if (!handler) {
            return false;
        }
    };

    kettle.server.registerHandlers = function (that) {
        fluid.each(that.options.handlers, function (handler, context) {
            if (!handler) {
                return;
            }
            that.options.registerHandlerEventMap[handler.type].fire(handler,
                context);
        });
    };

    kettle.server.stop = function (that) {
{'fraction_non_alphanumeric': 0.12265917602996254, 'fraction_numerical': 0.004265501456512693, 'mean_word_length': 1.43428716130666, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '4281904', 'n_tokens_mistral': 2587, 'n_tokens_neox': 2411, 'n_words': 468}