2015-10-26 11 views
0

我需要运行HTTP适配器来访问SOAP WSDL服务。它有2个字段useridpassword如何将参数传递给MobileFirst HTTP适配器

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

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;'); 
} 
+0

在哪里这两个变量应该去?你指的是基本身份验证?你可以分享WSDL吗?我假设你想调用'invokeWebService',是吗? –

+0

您的适配器代码看起来不完整。 'soapEnvNS'之前应该有一个函数定义; –

+0

我有一个wsdl,它是具有userid和password的员工登录名,如果我输入正确的详细信息,它会显示员工details.im,对此很新,所以我右键单击服务并发现后端服务,并为wsdl url自动生成适配器内容。现在我的任务是在哪里以及如何将参数传递给适配器,以便显示员工详细信息? – david

回答

0

要调用适配器,并通过你需要调用WL.Client.invokeProcedure你的具体情况的参数,你可以使用

var invocationData = { 
    adapter: 'YOUR_ADAPTER_NAME', 
    procedure: 'userlogin_ep_process', 
    parameters: { 
     process: { 
      username: 'YOUR_USERNAME', 
      userpwd: 'YOUR_PASSWORD' 
     } 
    } 
}; 
WL.Client.invokeProcedure(invocationData, { 
    onSuccess: yourSuccessFunction, 
    onFailure: yourFailureFunction 
});