如何将参数传递给MobileFirst HTTP适配器

时间:2015-10-26 12:03:08

标签: soap ibm-mobilefirst mobilefirst-adapters

我需要运行HTTP适配器来访问SOAP WSDL服务。它有2个字段useridpassword

我通过发现后端服务自动生成了适配器。任何人都可以指导我如何从适配器最初传递值来访问服务吗?

function userlogin_ep_process(params, headers){
var soapEnvNS = '';

soapEnvNS = 'http://schemas.xmlsoap.org/soap/envelope/';

    var mappings = {
        roots: {
            'process': { nsPrefix: 'client', type: 'client:process' }               
        },

        types: {
            'client:process': {
                children: [
                    {'username': { nsPrefix: 'client' }},   
                    {'userpwd': { nsPrefix: 'client' }} 
                ]
            }
        }
    };

    var namespaces = 'xmlns:client="http://xmlns.oracle.com/InternetMobile/AbsManagement/BPELProcessUserLogin" xmlns:plnk="http://docs.oasis-open.org/wsbpel/2.0/plnktype" ';
    var request = buildBody(params, namespaces, mappings, soapEnvNS);
    var soapAction = 'process';
    return invokeWebService(request, headers, soapAction);
}



function buildBody(params, namespaces, mappings, soapEnvNS){
    var body =
        '<soap:Envelope xmlns:soap="' + soapEnvNS + '">\n' +
        '<soap:Body>\n';

    var fixedParams = {};
    for (var paramName in params) {
        if (mappings['roots'][paramName]) { //There is mapping for this param
            var root = mappings['roots'][paramName];
            var name = paramName;
            if (root['nsPrefix'])
                name = root['nsPrefix'] + ':' + paramName;
            fixedParams[name] = handleMappings(params[paramName], root['type'], mappings['types']); 
        }
        else {
            fixedParams[paramName] = params[paramName];
        }
    }

    body = jsonToXml(fixedParams, body, namespaces);

    body += 
        '</soap:Body>\n' +
        '</soap:Envelope>\n';
    return body;
}

function handleMappings(jsonObj, type, mappings) {
    var fixedObj = {};
    var typeMap = mappings[type]['children']; //Get the object that defines the mappings for the specific type

    // loop through the types and see if there is an input param defined
    for(var i = 0; i < typeMap.length; i++) {
        var childType = typeMap[i];

        for(var key in childType) {
            if(jsonObj[key] !== null) { // input param exists
                var childName = key;
                if (childType[key]['nsPrefix'])
                    childName = childType[key]['nsPrefix'] + ':' + key;

                if (!childType[key]['type']) //Simple type element
                    fixedObj[childName] = jsonObj[key];
                else if (typeof jsonObj[key] === 'object' && jsonObj[key].length != undefined) { //Array of complex type elements
                    fixedObj[childName] = [];
                    for (var i=0; i<jsonObj[key].length; i++)
                        fixedObj[childName][i] = handleMappings(jsonObj[key][i], childType[key]['type'], mappings);
                }
                else if (typeof jsonObj[key] === 'object') //Complex type element
                    fixedObj[childName] = handleMappings(jsonObj[key], childType[key]['type'], mappings);
                else if (childType[key]['type'] == '@') //Attribute
                    fixedObj['@' + childName] = jsonObj[key];
            }
        }
    }

    return fixedObj;
}

function getAttributes(jsonObj) {
    var attrStr = '';
    for(var attr in jsonObj) {
        if (attr.charAt(0) == '@') {
            var val = jsonObj[attr];
            attrStr += ' ' + attr.substring(1);
            attrStr += '="' + xmlEscape(val) + '"';
        }
    }
    return attrStr;
}

function jsonToXml(jsonObj, xmlStr, namespaces) {
    var toAppend = '';
    for(var attr in jsonObj) {
        if (attr.charAt(0) != '@') {
            var val = jsonObj[attr];
            if (typeof val  === 'object'  &&  val.length != undefined) {
                for(var i=0; i<val.length; i++) {
                    toAppend += "<" + attr + getAttributes(val[i]);
                    if (namespaces != null)
                        toAppend += ' ' + namespaces;
                    toAppend += ">\n";
                    toAppend = jsonToXml(val[i], toAppend);
                    toAppend += "</" + attr + ">\n";
                }
            }
            else {
                toAppend += "<" + attr;
                if (typeof val  === 'object') {
                    toAppend += getAttributes(val);
                    if (namespaces != null)
                        toAppend += ' ' + namespaces;
                    toAppend += ">\n";
                    toAppend = jsonToXml(val, toAppend);
                }
                else {
                    toAppend += ">" + xmlEscape(val);
                }
                toAppend += "</" + attr + ">\n";
            }
        }
    }
    return xmlStr += toAppend;
}


function invokeWebService(body, headers, soapAction){
    var input = {
        method : 'post',
        returnedContentType : 'xml',
        path : '/soa-infra/services/Mobile/AbsManagement/userlogin_ep',
        body: {
            content : body.toString(),
            contentType : 'text/xml; charset=utf-8'
        }
    };

    //Adding custom HTTP headers if they were provided as parameter to the procedure call
    //Always add header for SOAP action 
    headers = headers || {};
    if (soapAction != 'null')
        headers.SOAPAction = soapAction;
    input['headers'] = headers;

    return WL.Server.invokeHttp(input);
}

function xmlEscape(obj) {
    if(typeof obj !== 'string') {
        return obj;
    }
    return obj.replace(/&/g, '&amp;')
           .replace(/"/g, '&quot;')
           .replace(/'/g, '&apos;')
           .replace(/</g, '&lt;')
           .replace(/>/g, '&gt;');
}

1 个答案:

答案 0 :(得分:0)

要调用适配器并传递您需要在特定情况下调用public ActionResult Find() { Hotel model = new Hotel() { Name = "Cromwell Hotel ", Rating = 5, }; return PartialView("Hotel", model); } 的参数,您可以使用

WL.Client.invokeProcedure