288
|
1 /*
|
|
2 This program is free software: you can redistribute it and/or modify
|
|
3 it under the terms of the GNU General Public License as published by
|
|
4 the Free Software Foundation, either version 3 of the License, or
|
|
5 (at your option) any later version.
|
|
6
|
|
7 This program is distributed in the hope that it will be useful,
|
|
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
10 GNU General Public License for more details.
|
|
11
|
|
12 You should have received a copy of the GNU General Public License
|
|
13 along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
14 */
|
|
15 //Singleton SOAP Client
|
|
16 var SOAPClient = {
|
|
17 Proxy: "",
|
|
18 SOAPServer: "",
|
|
19 ContentType: "text/xml",
|
|
20 CharSet: "utf-8",
|
|
21 ResponseXML: null,
|
|
22 ResponseText: "",
|
|
23 Status: 0,
|
|
24 ContentLength: 0,
|
|
25 Namespace: function(name, uri) {
|
|
26 return {"name":name, "uri":uri};
|
|
27 },
|
|
28 SendRequest: function(soapReq, callback,arg) {
|
|
29 if(!!SOAPClient.Proxy) {
|
|
30 SOAPClient.ResponseText = "";
|
|
31 SOAPClient.ResponseXML = null;
|
|
32 SOAPClient.Status = 0;
|
|
33
|
|
34 var content = soapReq.toString();
|
|
35 SOAPClient.ContentLength = content.length;
|
|
36
|
|
37 function getResponse(xData) {
|
|
38 if(!!callback) {
|
|
39 SOAPClient.Status = xData.status;
|
|
40 SOAPClient.ResponseText = xData.responseText;
|
|
41 SOAPClient.ResponseXML = xData.responseXML;
|
|
42 var jsOut = $.xmlToJSON(xData.responseXML);
|
|
43 callback(jsOut,arg);
|
|
44 }
|
|
45 }
|
|
46 $.ajax({
|
|
47 type: "POST",
|
|
48 url: SOAPClient.Proxy,
|
|
49 dataType: "xml",
|
|
50 processData: false,
|
|
51 data: content,
|
|
52 complete: getResponse,
|
|
53 contentType: SOAPClient.ContentType + "; charset=\"" + SOAPClient.CharSet + "\"",
|
|
54 beforeSend: function(req) {
|
|
55 req.setRequestHeader("Method", "POST");
|
|
56 req.setRequestHeader("Content-Length", SOAPClient.ContentLength);
|
|
57 req.setRequestHeader("SOAPServer", SOAPClient.SOAPServer);
|
|
58 req.setRequestHeader("SOAPAction", soapReq.Action);
|
|
59 }
|
|
60 });
|
|
61 }
|
|
62 },
|
|
63 ToXML: function(soapObj) {
|
|
64 var out = [];
|
|
65 var isNSObj=false;
|
|
66 try {
|
|
67 if(!!soapObj&&typeof(soapObj)==="object"&&soapObj.typeOf==="SOAPObject") {
|
|
68 //Namespaces
|
|
69 if(!!soapObj.ns) {
|
|
70 if(typeof(soapObj.ns)==="object") {
|
|
71 isNSObj=true;
|
|
72 out.push("<"+soapObj.ns.name+":"+soapObj.name);
|
|
73 out.push(" xmlns:"+soapObj.ns.name+"=\""+soapObj.ns.uri+"\"");
|
|
74 } else {
|
|
75 out.push("<"+soapObj.name);
|
|
76 out.push(" xmlns=\""+soapObj.ns+"\"");
|
|
77 }
|
|
78 } else {
|
|
79 out.push("<"+soapObj.name);
|
|
80 }
|
|
81 //Node Attributes
|
|
82 if(soapObj.attributes.length > 0) {
|
|
83 var cAttr;
|
|
84 var aLen=soapObj.attributes.length-1;
|
|
85 do {
|
|
86 cAttr=soapObj.attributes[aLen];
|
|
87 if(isNSObj) {
|
|
88 out.push(" "+soapObj.ns.name+":"+cAttr.name+"=\""+cAttr.value+"\"");
|
|
89 } else {
|
|
90 out.push(" "+cAttr.name+"=\""+cAttr.value+"\"");
|
|
91 }
|
|
92 } while(aLen--);
|
|
93 }
|
|
94 out.push(">");
|
|
95 //Node children
|
|
96 if(soapObj.hasChildren()) {
|
|
97 var cPos, cObj;
|
|
98 for(cPos in soapObj.children){
|
|
99 cObj = soapObj.children[cPos];
|
|
100 if(typeof(cObj)==="object"){out.push(SOAPClient.ToXML(cObj));}
|
|
101 }
|
|
102 }
|
|
103 //Node Value
|
|
104 if(!!soapObj.value){out.push(soapObj.value);}
|
|
105 //Close Tag
|
|
106 if(isNSObj){out.push("</"+soapObj.ns.name+":"+soapObj.name+">");}
|
|
107 else {out.push("</"+soapObj.name+">");}
|
|
108 return out.join("");
|
|
109 }
|
|
110 } catch(e){alert("Unable to process SOAPObject! Object must be an instance of SOAPObject");}
|
|
111 }
|
|
112 };
|
|
113 //Soap request - this is what being sent using SOAPClient.SendRequest
|
|
114 var SOAPRequest=function(action, soapObj) {
|
|
115 this.Action=action;
|
|
116 var nss=[];
|
|
117 var headers=[];
|
|
118 var bodies=(!!soapObj)?[soapObj]:[];
|
|
119 this.addNamespace=function(ns, uri){nss.push(new SOAPClient.Namespace(ns, uri));};
|
|
120 this.addHeader=function(soapObj){headers.push(soapObj);};
|
|
121 this.addBody=function(soapObj){bodies.push(soapObj);};
|
|
122 this.toString=function() {
|
|
123 var soapEnv = new SOAPObject("soapenv:Envelope");
|
|
124 soapEnv.attr("xmlns:soapenv","http://schemas.xmlsoap.org/soap/envelope/");
|
|
125 //Add Namespace(s)
|
|
126 if(nss.length>0){
|
|
127 var tNs, tNo;
|
|
128 for(tNs in nss){if(!nss.hasOwnProperty || nss.hasOwnProperty(tNs)){tNo=nss[tNs];if(typeof(tNo)==="object"){soapEnv.attr("xmlns:"+tNo.name, tNo.uri);}}}
|
|
129 }
|
|
130 //Add Header(s)
|
|
131 if(headers.length>0) {
|
|
132 var soapHeader = soapEnv.appendChild(new SOAPObject("soapenv:Header"));
|
|
133 var tHdr;
|
|
134 for(tHdr in headers){if(!headers.hasOwnProperty || headers.hasOwnProperty(tHdr)){soapHeader.appendChild(headers[tHdr]);}}
|
|
135 }
|
|
136 //Add Body(s)
|
|
137 if(bodies.length>0) {
|
|
138 var soapBody = soapEnv.appendChild(new SOAPObject("soapenv:Body"));
|
|
139 var tBdy;
|
|
140 for(tBdy in bodies){if(!bodies.hasOwnProperty || bodies.hasOwnProperty(tBdy)){soapBody.appendChild(bodies[tBdy]);}}
|
|
141 }
|
|
142 return soapEnv.toString();
|
|
143 };
|
|
144 };
|
|
145
|
|
146 //Soap Object - Used to build body envelope and other structures
|
|
147 var SOAPObject = function(name) {
|
|
148 this.typeOf="SOAPObject";
|
|
149 this.ns=null;
|
|
150 this.name=name;
|
|
151 this.attributes=[];
|
|
152 this.children=[];
|
|
153 this.value=null;
|
|
154 this.attr=function(name, value){this.attributes.push({"name":name, "value":value});return this;};
|
|
155 this.appendChild=function(obj){this.children.push(obj);return obj;};
|
|
156 this.hasChildren=function(){return (this.children.length > 0)?true:false;};
|
|
157 this.val=function(v){if(!v){return this.value;}else{this.value=v;return this;}};
|
|
158 this.toString=function(){return SOAPClient.ToXML(this);};
|
|
159 };
|