root / trunk / web / dojo / dojox / rpc / Service.js
History | View | Annotate | Download (5.64 KB)
1 |
/*
|
---|---|
2 |
Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
|
3 |
Available via Academic Free License >= 2.1 OR the modified BSD license.
|
4 |
see: http://dojotoolkit.org/license for details
|
5 |
*/
|
6 |
|
7 |
|
8 |
if(!dojo._hasResource["dojox.rpc.Service"]){ |
9 |
dojo._hasResource["dojox.rpc.Service"]=true; |
10 |
dojo.provide("dojox.rpc.Service");
|
11 |
dojo.require("dojo.AdapterRegistry");
|
12 |
dojo.declare("dojox.rpc.Service",null,{constructor:function(_1,_2){ |
13 |
var _3;
|
14 |
var _4=this; |
15 |
function _5(_6){ |
16 |
_6._baseUrl=new dojo._Url((dojo.isBrowser?location.href:dojo.config.baseUrl),_3||".")+""; |
17 |
_4._smd=_6; |
18 |
for(var _7 in _4._smd.services){ |
19 |
var _8=_7.split("."); |
20 |
var _9=_4;
|
21 |
for(var i=0;i<_8.length-1;i++){ |
22 |
_9=_9[_8[i]]||(_9[_8[i]]={}); |
23 |
} |
24 |
_9[_8[_8.length-1]]=_4._generateService(_7,_4._smd.services[_7]);
|
25 |
} |
26 |
}; |
27 |
if(_1){
|
28 |
if((dojo.isString(_1))||(_1 instanceof dojo._Url)){ |
29 |
if(_1 instanceof dojo._Url){ |
30 |
_3=_1+"";
|
31 |
}else{
|
32 |
_3=_1; |
33 |
} |
34 |
var _a=dojo._getText(_3);
|
35 |
if(!_a){
|
36 |
throw new Error("Unable to load SMD from "+_1); |
37 |
}else{
|
38 |
_5(dojo.fromJson(_a)); |
39 |
} |
40 |
}else{
|
41 |
_5(_1); |
42 |
} |
43 |
} |
44 |
this._options=(_2?_2:{});
|
45 |
this._requestId=0; |
46 |
},_generateService:function(_b,_c){ |
47 |
if(this[_c]){ |
48 |
throw new Error("WARNING: "+_b+" already exists for service. Unable to generate function"); |
49 |
} |
50 |
_c.name=_b; |
51 |
var _d=dojo.hitch(this,"_executeMethod",_c); |
52 |
var _e=dojox.rpc.transportRegistry.match(_c.transport||this._smd.transport); |
53 |
if(_e.getExecutor){
|
54 |
_d=_e.getExecutor(_d,_c,this);
|
55 |
} |
56 |
var _f=_c.returns||(_c._schema={});
|
57 |
var _10="/"+_b+"/"; |
58 |
_f._service=_d; |
59 |
_d.servicePath=_10; |
60 |
_d._schema=_f; |
61 |
_d.id=dojox.rpc.Service._nextId++; |
62 |
return _d;
|
63 |
},_getRequest:function(_11,_12){ |
64 |
var smd=this._smd; |
65 |
var _13=dojox.rpc.envelopeRegistry.match(_11.envelope||smd.envelope||"NONE"); |
66 |
var _14=(_11.parameters||[]).concat(smd.parameters||[]);
|
67 |
if(_13.namedParams){
|
68 |
if((_12.length==1)&&dojo.isObject(_12[0])){ |
69 |
_12=_12[0];
|
70 |
}else{
|
71 |
var _15={};
|
72 |
for(var i=0;i<_11.parameters.length;i++){ |
73 |
if(typeof _12[i]!="undefined"||!_11.parameters[i].optional){ |
74 |
_15[_11.parameters[i].name]=_12[i]; |
75 |
} |
76 |
} |
77 |
_12=_15; |
78 |
} |
79 |
if(_11.strictParameters||smd.strictParameters){
|
80 |
for(i in _12){ |
81 |
var _16=false; |
82 |
for(var j=0;j<_14.length;j++){ |
83 |
if(_14[i].name==i){
|
84 |
_16=true;
|
85 |
} |
86 |
} |
87 |
if(!_16){
|
88 |
delete _12[i];
|
89 |
} |
90 |
} |
91 |
} |
92 |
for(i=0;i<_14.length;i++){ |
93 |
var _17=_14[i];
|
94 |
if(!_17.optional&&_17.name&&!_12[_17.name]){
|
95 |
if(_17["default"]){ |
96 |
_12[_17.name]=_17["default"];
|
97 |
}else{
|
98 |
if(!(_17.name in _12)){ |
99 |
throw new Error("Required parameter "+_17.name+" was omitted"); |
100 |
} |
101 |
} |
102 |
} |
103 |
} |
104 |
}else{
|
105 |
if(_14&&_14[0]&&_14[0].name&&(_12.length==1)&&dojo.isObject(_12[0])){ |
106 |
if(_13.namedParams===false){ |
107 |
_12=dojox.rpc.toOrdered(_14,_12); |
108 |
}else{
|
109 |
_12=_12[0];
|
110 |
} |
111 |
} |
112 |
} |
113 |
if(dojo.isObject(this._options)){ |
114 |
_12=dojo.mixin(_12,this._options);
|
115 |
} |
116 |
var _18=_11._schema||_11.returns;
|
117 |
var _19=_13.serialize.apply(this,[smd,_11,_12]); |
118 |
_19._envDef=_13; |
119 |
var _1a=(_11.contentType||smd.contentType||_19.contentType);
|
120 |
return dojo.mixin(_19,{sync:dojox.rpc._sync,contentType:_1a,headers:{},target:_19.target||dojox.rpc.getTarget(smd,_11),transport:_11.transport||smd.transport||_19.transport,envelope:_11.envelope||smd.envelope||_19.envelope,timeout:_11.timeout||smd.timeout,callbackParamName:_11.callbackParamName||smd.callbackParamName,schema:_18,handleAs:_19.handleAs||"auto",preventCache:_11.preventCache||smd.preventCache,frameDoc:this._options.frameDoc||undefined}); |
121 |
},_executeMethod:function(_1b){ |
122 |
var _1c=[];
|
123 |
var i;
|
124 |
for(i=1;i<arguments.length;i++){ |
125 |
_1c.push(arguments[i]);
|
126 |
} |
127 |
var _1d=this._getRequest(_1b,_1c); |
128 |
var _1e=dojox.rpc.transportRegistry.match(_1d.transport).fire(_1d);
|
129 |
_1e.addBoth(function(_1f){
|
130 |
return _1d._envDef.deserialize.call(this,_1f); |
131 |
}); |
132 |
return _1e;
|
133 |
}}); |
134 |
dojox.rpc.getTarget=function(smd,_20){ |
135 |
var _21=smd._baseUrl;
|
136 |
if(smd.target){
|
137 |
_21=new dojo._Url(_21,smd.target)+""; |
138 |
} |
139 |
if(_20.target){
|
140 |
_21=new dojo._Url(_21,_20.target)+""; |
141 |
} |
142 |
return _21;
|
143 |
}; |
144 |
dojox.rpc.toOrdered=function(_22,_23){ |
145 |
if(dojo.isArray(_23)){
|
146 |
return _23;
|
147 |
} |
148 |
var _24=[];
|
149 |
for(var i=0;i<_22.length;i++){ |
150 |
_24.push(_23[_22[i].name]); |
151 |
} |
152 |
return _24;
|
153 |
}; |
154 |
dojox.rpc.transportRegistry=new dojo.AdapterRegistry(true); |
155 |
dojox.rpc.envelopeRegistry=new dojo.AdapterRegistry(true); |
156 |
dojox.rpc.envelopeRegistry.register("URL",function(str){ |
157 |
return str=="URL"; |
158 |
},{serialize:function(smd,_25,_26){ |
159 |
var d=dojo.objectToQuery(_26);
|
160 |
return {data:d,transport:"POST"}; |
161 |
},deserialize:function(_27){ |
162 |
return _27;
|
163 |
},namedParams:true}); |
164 |
dojox.rpc.envelopeRegistry.register("JSON",function(str){ |
165 |
return str=="JSON"; |
166 |
},{serialize:function(smd,_28,_29){ |
167 |
var d=dojo.toJson(_29);
|
168 |
return {data:d,handleAs:"json",contentType:"application/json"}; |
169 |
},deserialize:function(_2a){ |
170 |
return _2a;
|
171 |
}}); |
172 |
dojox.rpc.envelopeRegistry.register("PATH",function(str){ |
173 |
return str=="PATH"; |
174 |
},{serialize:function(smd,_2b,_2c){ |
175 |
var i;
|
176 |
var _2d=dojox.rpc.getTarget(smd,_2b);
|
177 |
if(dojo.isArray(_2c)){
|
178 |
for(i=0;i<_2c.length;i++){ |
179 |
_2d+="/"+_2c[i];
|
180 |
} |
181 |
}else{
|
182 |
for(i in _2c){ |
183 |
_2d+="/"+i+"/"+_2c[i]; |
184 |
} |
185 |
} |
186 |
return {data:"",target:_2d}; |
187 |
},deserialize:function(_2e){ |
188 |
return _2e;
|
189 |
}}); |
190 |
dojox.rpc.transportRegistry.register("POST",function(str){ |
191 |
return str=="POST"; |
192 |
},{fire:function(r){ |
193 |
r.url=r.target; |
194 |
r.postData=r.data; |
195 |
return dojo.rawXhrPost(r);
|
196 |
}}); |
197 |
dojox.rpc.transportRegistry.register("GET",function(str){ |
198 |
return str=="GET"; |
199 |
},{fire:function(r){ |
200 |
r.url=r.target+(r.data?"?"+r.data:""); |
201 |
return dojo.xhrGet(r);
|
202 |
}}); |
203 |
dojox.rpc.transportRegistry.register("JSONP",function(str){ |
204 |
return str=="JSONP"; |
205 |
},{fire:function(r){ |
206 |
r.url=r.target+((r.target.indexOf("?")==-1)?"?":"&")+r.data; |
207 |
r.callbackParamName=r.callbackParamName||"callback";
|
208 |
return dojo.io.script.get(r);
|
209 |
}}); |
210 |
dojox.rpc.Service._nextId=1;
|
211 |
dojo._contentHandlers.auto=function(xhr){ |
212 |
var _2f=dojo._contentHandlers;
|
213 |
var _30=xhr.getResponseHeader("Content-Type"); |
214 |
var _31=!_30?_2f.text(xhr):_30.match(/\/.*json/)?_2f.json(xhr):_30.match(/\/javascript/)?_2f.javascript(xhr):_30.match(/\/xml/)?_2f.xml(xhr):_2f.text(xhr); |
215 |
return _31;
|
216 |
}; |
217 |
} |