2015-10-26 2 views
0

Мне нужно запустить HTTP-адаптер для доступа к службе SOAP WSDL. Он имеет 2 поля userid и password.Как передать параметры HTTP-адаптеру MobileFirst

У меня есть автоматический сгенерированный адаптер, открывая бэкэнд-услуги. Может ли кто-нибудь указать мне, как сначала передать значения из адаптера для доступа к службе?

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

Где эти две переменные должны пройти? Вы имеете в виду Basic Auth? Можете ли вы поделиться WSDL? Я предполагаю, что вы хотите вызывать 'invokeWebService', правильно? –

+0

Ваш код адаптера выглядит неполным. должно быть определение функции до 'soapEnvNS'; –

+0

У меня есть wsdl, который является логином сотрудника, у которого есть идентификатор пользователя и пароль. Если я ввожу правильные данные, он отображает детали сотрудника. Это очень новое для этого, поэтому я нажимаю правой кнопкой мыши на сервисы и обнаруживаю бэкэнд-услуги и предоставляю URL-адрес wsdl, чтобы он автоматически генерировал содержимое адаптера . Теперь моя задача - это где и как передать параметр адаптеру, чтобы он отображал детали сотрудника? – 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 
}); 
Смежные вопросы