<%

/*
 * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * WSO2 Inc. 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.
 */

var payloadFac = require("payloadFactory.jag");

var log = new Log();

function call(webService, serverUrl, options, payload) {
    var cookie = session.get("auth-cookie");
    var userName = session.get("user");
    var pwd = session.get("pwd");

    log.debug(cookie);
    if (cookie != null) {

        options["HTTPHeaders"] = [{name: "cookie", value: cookie}];

        log.debug(options["HTTPHeaders"]);
        webService.open(options, serverUrl, false);
    } else if (userName != null && pwd != null) {
        webService.open(options, serverUrl, false, userName, pwd);
    } else {
        webService.open(options, serverUrl, false);
    }

    log.debug(payload);

    webService.send(payload);

}

function callWSInOnly(webService, serverUrl, action, payload) {
    var options = new Array();
    options.useSOAP = 1.1;
    options.action = action;
    options.mep = 'in-only';

    try {
        call(webService, serverUrl, options, payload);
    }
    catch (e) {
        log.error(e);
        throw e;
    }

}

function callWSInOnlyWithParams(webService, serverUrl, action, params, req) {
    var payload = req;
    if (req == null) {
        if (params != null) {
            payload = payloadFac.getPayload(action, params);
        } else {
            payload = payloadFac.getPayload(options.action);
        }
    }

    try {
        callWSInOnly(webService, serverUrl, action, payload);
    }
    catch (e) {
        log.error(e);
        throw e;
    }

}

function callWSReturnXML(webService, serverUrl, action, params, req) {

    var options = new Array();
    options.useSOAP = 1.1;
    options.action = action;

    var payload = req;
    if (req == null) {
        if (params != null) {
            payload = payloadFac.getPayload(options.action, params);
        } else {
            payload = payloadFac.getPayload(options.action);
        }
    }
    var result;

    try {

        log.debug(payload);

        call(webService, serverUrl, options, payload);
        if (webService.readyState == 4) {
            result = webService.responseE4X;
            return result;
        }

    }
    catch (e) {
        log.error(e);
        throw e;
    }

}

function callWSReturnJSON(webService, serverUrl, action, params, req) {
    var result = callWSReturnXML(webService, serverUrl, action, params, req);
    log.debug(result);
    return E4XtoJSON(result);

}

function E4XtoJSON(xml, ignored) {
    var r, children = xml. *, attributes = xml.@*,
    length = children.length();
    if (length == 0) {
        r = xml.toString();
    } else if (length == 1) {
        var text = xml.text().toString();
        if (text) {
            r = text;
        }
    }
    if (r == undefined) {
        r = {};
        for each(var child
    in
        children
    )
        {
            var name = child.localName();
            var json = E4XtoJSON(child, ignored);
            var value = r[name];
            if (value) {
                if (value.length && value.push) {
                    value.push(json);
                } else {
                    r[name] = [value, json]
                }
            } else {
                r[name] = json;
            }
        }
    }
    if (attributes.length()) {
        var a = {}, c = 0;
        for each(var attribute
    in
        attributes
    )
        {
            var name = attribute.localName();
            if (ignored && ignored.indexOf(name) == -1) {
                a["_" + name] = attribute.toString();
                c++;
            }
        }
        if (c) {
            if (r) a._ = r;
            return a;
        }
    }
    return r;
}

%>
