정적 WSDL 스크립트 포함 예시
이 예제에서는 정적 WSDL 동작의 대부분을 구현하는 FakeStockValue 스크립트 포함을 보여 줍니다.
var FakeStockValue = Class.create();
FakeStockValue.prototype = {
initialize : function(requestXML) {
//Use some backend XML utilities...you could use string tools if you wish
this.xmlutil = Packages.com.glide.util.XMLUtil;
//converting the string to an XML Document
this.fSoapDoc = new XMLDocument(requestXML);
},
process : function() {
var soapBody = this.fSoapDoc.getNode("/Envelope/Body");
//Our WSDL was formatted to have the only first child element be the function
var funcNode = this.xmlutil.getFirstChildElement(soapBody);
var nodeName = this.xmlutil.getNodeNameNS(funcNode);
//If the function for this SOAP request is TradePriceRequest, perform the necessary actions
if (nodeName == "TradePriceRequest") {
return this.fakeOutTradePriceRequest(funcNode);
}
//Couldn't find any supported functions in this SOAP request
return this.generateSoapFault("un-supported API call: " + nodeName);
},
fakeOutTradePriceRequest : function (funcNode) {
//Create the beginnings of our XML response
var r = new XMLDocument("<GetLastTradePriceOutput xmlns='https://www.service-now.com/vws/FakeStockValue'/>");
//Do the necessary actions here...we're going to get the USER ID of the user
//used to make this SOAP call. Then we will return the
//stock symbol they were asking about
var usersysid = gs.getUserID();
var now_GR = new GlideRecord("sys_user");
gr.get(usersysid);
var username = gr.user_name;
var quoteSymbol = this.xmlutil.getText(funcNode);
//Create a "message" element to store our response message
r.createElement("message", username + ", You were looking for a quote on "+quoteSymbol);
return r.getDocumentElement();
},
generateSoapFault : function (str) {
var f = "<SOAP-ENV:Fault>" +
"<faultcode xsi:type='xsd:string'>SOAP-ENV:FakeStockValue</faultcode>" +
"<faultstring xsi:type='xsd:string'>" + str +
"</faultstring>" +
"</SOAP-ENV:Fault>"
var s = new XMLDocument(f);
return s.getDocumentElement();
}
}
함수 초기화
initialize 함수는 XML 요청 문자열을 사용하여 라이브러리를 사용하여 탐색하고 조작할 수 있는 XML 문서 개체로 변환합니다. 또는 XML 요청을 문자열로 유지하고 정규 표현식을 사용하여 탐색할 수 있습니다.
프로세스 기능
프로세스 함수는 스크립트된 웹 서비스에 의해 호출됩니다. 이 함수는 XML에서 body 요소 뒤의 첫 번째 자식 요소를 가져옵니다. WSDL은 이 자식 요소를 사용하여 사용할 함수를 결정합니다. 이 WSDL에는 하나의 함수만 사용할 수 있지만 대부분의 WSDL은 많은 기능을 제공합니다. 더 많은 함수를 사용할 수 있다면, 다양한 함수 이름에 대한 첫 번째 자식 요소를 테스트하는 "if" 문이 더 많아질 것입니다.
fakeOutTradePriceRequest 함수
fakeOutTradePriceRequest 함수는 WSDL에서 유일하게 사용할 수 있는 함수를 구현한 것입니다. 이 함수는 SOAP 요청이 인증된 사용자를 조회하고 user_name 검색한 다음 SOAP 클라이언트에 반환합니다. fakeOutTradePriceRequest 함수를 확장하여 주식 기호를 조회하고 마지막으로 거래된 가격을 반환하는 등의 유용한 작업을 수행할 수 있습니다.
generateSoapFault 함수
generateSoapFault 함수는 문제가 있는 경우 호출할 수 있는 SOAP 오류를 반환합니다.