root / trunk / web / dojo / dojox / cometd / RestChannels.js @ 12
History | View | Annotate | Download (7.41 KB)
1 | 9 | andrej.cim | /*
|
---|---|---|---|
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.cometd.RestChannels"]){ |
||
9 | dojo._hasResource["dojox.cometd.RestChannels"]=true; |
||
10 | dojo.provide("dojox.cometd.RestChannels");
|
||
11 | dojo.require("dojox.rpc.Client");
|
||
12 | dojo.requireIf(dojox.data&&!!dojox.data.JsonRestStore,"dojox.data.restListener");
|
||
13 | (function(){
|
||
14 | dojo.declare("dojox.cometd.RestChannels",null,{constructor:function(_1){ |
||
15 | dojo.mixin(this,_1);
|
||
16 | if(dojox.rpc.Rest&&this.autoSubscribeRoot){ |
||
17 | var _2=dojox.rpc.Rest._get;
|
||
18 | var _3=this; |
||
19 | dojox.rpc.Rest._get=function(_4,id){ |
||
20 | var _5=dojo.xhrGet;
|
||
21 | dojo.xhrGet=function(r){ |
||
22 | var _6=_3.autoSubscribeRoot;
|
||
23 | return (_6&&r.url.substring(0,_6.length)==_6)?_3.get(r.url,r):_5(r); |
||
24 | }; |
||
25 | var _7=_2.apply(this,arguments); |
||
26 | dojo.xhrGet=_5; |
||
27 | return _7;
|
||
28 | }; |
||
29 | } |
||
30 | },absoluteUrl:function(_8,_9){ |
||
31 | return new dojo._Url(_8,_9)+""; |
||
32 | },acceptType:"application/rest+json,application/http;q=0.9,*/*;q=0.7",subscriptions:{},subCallbacks:{},autoReconnectTime:3000,reloadDataOnReconnect:true,sendAsJson:false,url:"/channels",autoSubscribeRoot:"/",open:function(){ |
||
33 | this.started=true; |
||
34 | if(!this.connected){ |
||
35 | this.connectionId=dojox.rpc.Client.clientId;
|
||
36 | var _a=this.createdClientId?"Client-Id":"Create-Client-Id"; |
||
37 | this.createdClientId=true; |
||
38 | var _b={Accept:this.acceptType}; |
||
39 | _b[_a]=this.connectionId;
|
||
40 | var _c=dojo.xhrPost({headers:_b,url:this.url,noStatus:true}); |
||
41 | var _d=this; |
||
42 | this.lastIndex=0; |
||
43 | var _e,_f=function(_10){ |
||
44 | if(typeof dojo=="undefined"){ |
||
45 | return null; |
||
46 | } |
||
47 | if(xhr&&xhr.status>400){ |
||
48 | return _e(true); |
||
49 | } |
||
50 | if(typeof _10=="string"){ |
||
51 | _10=_10.substring(_d.lastIndex); |
||
52 | } |
||
53 | var _11=xhr&&(xhr.contentType||xhr.getResponseHeader("Content-Type"))||(typeof _10!="string"&&"already json"); |
||
54 | var _12=_d.onprogress(xhr,_10,_11);
|
||
55 | if(_12){
|
||
56 | if(_e()){
|
||
57 | return new Error(_12); |
||
58 | } |
||
59 | } |
||
60 | if(!xhr||xhr.readyState==4){ |
||
61 | xhr=null;
|
||
62 | if(_d.connected){
|
||
63 | _d.connected=false;
|
||
64 | _d.open(); |
||
65 | } |
||
66 | } |
||
67 | return _10;
|
||
68 | }; |
||
69 | _e=function(_13){ |
||
70 | if(xhr&&xhr.status==409){ |
||
71 | _d.disconnected(); |
||
72 | return null; |
||
73 | } |
||
74 | _d.createdClientId=false;
|
||
75 | _d.disconnected(); |
||
76 | return _13;
|
||
77 | }; |
||
78 | _c.addCallbacks(_f,_e); |
||
79 | var xhr=_c.ioArgs.xhr;
|
||
80 | if(xhr){
|
||
81 | xhr.onreadystatechange=function(){ |
||
82 | var _14;
|
||
83 | try{
|
||
84 | if(xhr.readyState==3){ |
||
85 | _d.readyState=3;
|
||
86 | _14=xhr.responseText; |
||
87 | } |
||
88 | } |
||
89 | catch(e){
|
||
90 | } |
||
91 | if(typeof _14=="string"){ |
||
92 | _f(_14); |
||
93 | } |
||
94 | }; |
||
95 | } |
||
96 | if(window.attachEvent){
|
||
97 | window.attachEvent("onunload",function(){ |
||
98 | _d.connected=false;
|
||
99 | if(xhr){
|
||
100 | xhr.abort(); |
||
101 | } |
||
102 | }); |
||
103 | } |
||
104 | this.connected=true; |
||
105 | } |
||
106 | },_send:function(_15,_16,_17){ |
||
107 | if(this.sendAsJson){ |
||
108 | _16.postData=dojo.toJson({target:_16.url,method:_15,content:_17,params:_16.content,subscribe:_16.headers["Subscribe"]}); |
||
109 | _16.url=this.url;
|
||
110 | _15="POST";
|
||
111 | }else{
|
||
112 | _16.postData=dojo.toJson(_17); |
||
113 | } |
||
114 | return dojo.xhr(_15,_16,_16.postData);
|
||
115 | },subscribe:function(_18,_19){ |
||
116 | _19=_19||{}; |
||
117 | _19.url=this.absoluteUrl(this.url,_18); |
||
118 | if(_19.headers){
|
||
119 | delete _19.headers.Range;
|
||
120 | } |
||
121 | var _1a=this.subscriptions[_18]; |
||
122 | var _1b=_19.method||"HEAD"; |
||
123 | var _1c=_19.since;
|
||
124 | var _1d=_19.callback;
|
||
125 | var _1e=_19.headers||(_19.headers={});
|
||
126 | this.subscriptions[_18]=_1c||_1a||0; |
||
127 | var _1f=this.subCallbacks[_18]; |
||
128 | if(_1d){
|
||
129 | this.subCallbacks[_18]=_1f?function(m){ |
||
130 | _1f(m); |
||
131 | _1d(m); |
||
132 | }:_1d; |
||
133 | } |
||
134 | if(!this.connected){ |
||
135 | this.open();
|
||
136 | } |
||
137 | if(_1a===undefined||_1a!=_1c){ |
||
138 | _1e["Cache-Control"]="max-age=0"; |
||
139 | _1c=typeof _1c=="number"?new Date(_1c).toUTCString():_1c; |
||
140 | if(_1c){
|
||
141 | _1e["Subscribe-Since"]=_1c;
|
||
142 | } |
||
143 | _1e["Subscribe"]=_19.unsubscribe?"none":"*"; |
||
144 | var dfd=this._send(_1b,_19); |
||
145 | var _20=this; |
||
146 | dfd.addBoth(function(_21){
|
||
147 | var xhr=dfd.ioArgs.xhr;
|
||
148 | if(!(_21 instanceof Error)){ |
||
149 | if(_19.confirmation){
|
||
150 | _19.confirmation(); |
||
151 | } |
||
152 | } |
||
153 | if(xhr&&xhr.getResponseHeader("Subscribed")=="OK"){ |
||
154 | var _22=xhr.getResponseHeader("Last-Modified"); |
||
155 | if(xhr.responseText){
|
||
156 | _20.subscriptions[_18]=_22||new Date().toUTCString();
|
||
157 | }else{
|
||
158 | return null; |
||
159 | } |
||
160 | }else{
|
||
161 | if(xhr&&!(_21 instanceof Error)){ |
||
162 | delete _20.subscriptions[_18];
|
||
163 | } |
||
164 | } |
||
165 | if(!(_21 instanceof Error)){ |
||
166 | var _23={responseText:xhr&&xhr.responseText,channel:_18,getResponseHeader:function(_24){ |
||
167 | return xhr.getResponseHeader(_24);
|
||
168 | },getAllResponseHeaders:function(){ |
||
169 | return xhr.getAllResponseHeaders();
|
||
170 | },result:_21};
|
||
171 | if(_20.subCallbacks[_18]){
|
||
172 | _20.subCallbacks[_18](_23); |
||
173 | } |
||
174 | }else{
|
||
175 | if(_20.subCallbacks[_18]){
|
||
176 | _20.subCallbacks[_18](xhr); |
||
177 | } |
||
178 | } |
||
179 | return _21;
|
||
180 | }); |
||
181 | return dfd;
|
||
182 | } |
||
183 | return null; |
||
184 | },publish:function(_25,_26){ |
||
185 | return this._send("POST",{url:_25,contentType:"application/json"},_26); |
||
186 | },_processMessage:function(_27){ |
||
187 | _27.event=_27.event||_27.getResponseHeader("Event");
|
||
188 | if(_27.event=="connection-conflict"){ |
||
189 | return "conflict"; |
||
190 | } |
||
191 | try{
|
||
192 | _27.result=_27.result||dojo.fromJson(_27.responseText); |
||
193 | } |
||
194 | catch(e){
|
||
195 | } |
||
196 | var _28=this; |
||
197 | var loc=_27.channel=new dojo._Url(this.url,_27.source||_27.getResponseHeader("Content-Location"))+""; |
||
198 | if(loc in this.subscriptions&&_27.getResponseHeader){ |
||
199 | this.subscriptions[loc]=_27.getResponseHeader("Last-Modified"); |
||
200 | } |
||
201 | if(this.subCallbacks[loc]){ |
||
202 | setTimeout(function(){
|
||
203 | _28.subCallbacks[loc](_27); |
||
204 | },0);
|
||
205 | } |
||
206 | this.receive(_27);
|
||
207 | return null; |
||
208 | },onprogress:function(xhr,_29,_2a){ |
||
209 | if(!_2a||_2a.match(/application\/rest\+json/)){ |
||
210 | var _2b=_29.length;
|
||
211 | _29=_29.replace(/^\s*[,\[]?/,"[").replace(/[,\]]?\s*$/,"]"); |
||
212 | try{
|
||
213 | var _2c=dojo.fromJson(_29);
|
||
214 | this.lastIndex+=_2b;
|
||
215 | } |
||
216 | catch(e){
|
||
217 | } |
||
218 | }else{
|
||
219 | if(dojox.io&&dojox.io.httpParse&&_2a.match(/application\/http/)){ |
||
220 | var _2d=""; |
||
221 | if(xhr&&xhr.getAllResponseHeaders){
|
||
222 | _2d=xhr.getAllResponseHeaders(); |
||
223 | } |
||
224 | _2c=dojox.io.httpParse(_29,_2d,xhr.readyState!=4);
|
||
225 | }else{
|
||
226 | if(typeof _29=="object"){ |
||
227 | _2c=_29; |
||
228 | } |
||
229 | } |
||
230 | } |
||
231 | if(_2c){
|
||
232 | for(var i=0;i<_2c.length;i++){ |
||
233 | if(this._processMessage(_2c[i])){ |
||
234 | return "conflict"; |
||
235 | } |
||
236 | } |
||
237 | return null; |
||
238 | } |
||
239 | if(!xhr){
|
||
240 | return "error"; |
||
241 | } |
||
242 | if(xhr.readyState!=4){ |
||
243 | return null; |
||
244 | } |
||
245 | if(xhr.__proto__){
|
||
246 | xhr={channel:"channel",__proto__:xhr}; |
||
247 | } |
||
248 | return this._processMessage(xhr); |
||
249 | },get:function(_2e,_2f){ |
||
250 | (_2f=_2f||{}).method="GET";
|
||
251 | return this.subscribe(_2e,_2f); |
||
252 | },receive:function(_30){ |
||
253 | if(dojox.data&&dojox.data.restListener){
|
||
254 | dojox.data.restListener(_30); |
||
255 | } |
||
256 | },disconnected:function(){ |
||
257 | var _31=this; |
||
258 | if(this.connected){ |
||
259 | this.connected=false; |
||
260 | if(this.started){ |
||
261 | setTimeout(function(){
|
||
262 | var _32=_31.subscriptions;
|
||
263 | _31.subscriptions={}; |
||
264 | for(var i in _32){ |
||
265 | if(_31.reloadDataOnReconnect&&dojox.rpc.JsonRest){
|
||
266 | delete dojox.rpc.Rest._index[i];
|
||
267 | dojox.rpc.JsonRest.fetch(i); |
||
268 | }else{
|
||
269 | _31.subscribe(i,{since:_32[i]});
|
||
270 | } |
||
271 | } |
||
272 | _31.open(); |
||
273 | },this.autoReconnectTime);
|
||
274 | } |
||
275 | } |
||
276 | },unsubscribe:function(_33,_34){ |
||
277 | _34=_34||{}; |
||
278 | _34.unsubscribe=true;
|
||
279 | this.subscribe(_33,_34);
|
||
280 | },disconnect:function(){ |
||
281 | this.started=false; |
||
282 | this.xhr.abort();
|
||
283 | }}); |
||
284 | var _35=dojox.cometd.RestChannels.defaultInstance=new dojox.cometd.RestChannels(); |
||
285 | if(dojox.cometd.connectionTypes){
|
||
286 | _35.startup=function(_36){ |
||
287 | _35.open(); |
||
288 | this._cometd._deliver({channel:"/meta/connect",successful:true}); |
||
289 | }; |
||
290 | _35.check=function(_37,_38,_39){ |
||
291 | for(var i=0;i<_37.length;i++){ |
||
292 | if(_37[i]=="rest-channels"){ |
||
293 | return !_39;
|
||
294 | } |
||
295 | } |
||
296 | return false; |
||
297 | }; |
||
298 | _35.deliver=function(_3a){ |
||
299 | }; |
||
300 | dojo.connect(this,"receive",null,function(_3b){ |
||
301 | _3b.data=_3b.result; |
||
302 | this._cometd._deliver(_3b);
|
||
303 | }); |
||
304 | _35.sendMessages=function(_3c){ |
||
305 | for(var i=0;i<_3c.length;i++){ |
||
306 | var _3d=_3c[i];
|
||
307 | var _3e=_3d.channel;
|
||
308 | var _3f=this._cometd; |
||
309 | var _40={confirmation:function(){ |
||
310 | _3f._deliver({channel:_3e,successful:true}); |
||
311 | }}; |
||
312 | if(_3e=="/meta/subscribe"){ |
||
313 | this.subscribe(_3d.subscription,_40);
|
||
314 | }else{
|
||
315 | if(_3e=="/meta/unsubscribe"){ |
||
316 | this.unsubscribe(_3d.subscription,_40);
|
||
317 | }else{
|
||
318 | if(_3e=="/meta/connect"){ |
||
319 | _40.confirmation(); |
||
320 | }else{
|
||
321 | if(_3e=="/meta/disconnect"){ |
||
322 | _35.disconnect(); |
||
323 | _40.confirmation(); |
||
324 | }else{
|
||
325 | if(_3e.substring(0,6)!="/meta/"){ |
||
326 | this.publish(_3e,_3d.data);
|
||
327 | } |
||
328 | } |
||
329 | } |
||
330 | } |
||
331 | } |
||
332 | } |
||
333 | }; |
||
334 | dojox.cometd.connectionTypes.register("rest-channels",_35.check,_35,false,true); |
||
335 | } |
||
336 | })(); |
||
337 | } |