1 /* Licensed to the Apache Software Foundation (ASF) under one or more 2 * contributor license agreements. See the NOTICE file distributed with 3 * this work for additional information regarding copyright ownership. 4 * The ASF licenses this file to you under the Apache License, Version 2.0 5 * (the "License"); you may not use this file except in compliance with 6 * the License. You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /** 18 * An implementation of an xhr request object 19 * with partial page submit functionality, and jsf 20 * ppr request and timeout handling capabilities 21 * 22 * Author: Werner Punz (latest modification by $Author: ganeshpuri $) 23 * Version: $Revision: 1.4 $ $Date: 2009/05/31 09:16:44 $ 24 */ 25 26 /** 27 * @class 28 * @name _AjaxRequest 29 * @memberOf myfaces._impl.xhrCore 30 * @extends myfaces._impl.core.Object 31 */ 32 _MF_CLS(_PFX_XHR + "_AjaxRequest", _MF_OBJECT, /** @lends myfaces._impl.xhrCore._AjaxRequest.prototype */ { 33 34 _contentType: "application/x-www-form-urlencoded", 35 /** source element issuing the request */ 36 _source: null, 37 /** context passed down from the caller */ 38 _context:null, 39 /** source form issuing the request */ 40 _sourceForm: null, 41 /** passthrough parameters */ 42 _passThrough: null, 43 44 /** queue control */ 45 _timeout: null, 46 /** enqueuing delay */ 47 //_delay:null, 48 /** queue size */ 49 _queueSize:-1, 50 51 /** 52 back reference to the xhr queue, 53 only set if the object really is queued 54 */ 55 _xhrQueue: null, 56 57 /** pps an array of identifiers which should be part of the submit, the form is ignored */ 58 _partialIdsArray : null, 59 60 /** xhr object, internal param */ 61 _xhr: null, 62 63 /** predefined method */ 64 _ajaxType:"POST", 65 66 //CONSTANTS 67 ENCODED_URL:"javax.faces.encodedURL", 68 /* 69 * constants used internally 70 */ 71 _CONTENT_TYPE:"Content-Type", 72 _HEAD_FACES_REQ:"Faces-Request", 73 _VAL_AJAX: "partial/ajax", 74 _XHR_CONST: myfaces._impl.xhrCore.engine.XhrConst, 75 76 // _exception: null, 77 // _requestParameters: null, 78 /** 79 * Constructor 80 * <p /> 81 * note there is a load of common properties 82 * inherited by the base class which define the corner 83 * parameters and the general internal behavior 84 * like _onError etc... 85 * @param {Object} args an arguments map which an override any of the given protected 86 * instance variables, by a simple name value pair combination 87 */ 88 constructor_: function(args) { 89 90 try { 91 this._callSuper("constructor_", args); 92 93 this._initDefaultFinalizableFields(); 94 delete this._resettableContent["_xhrQueue"]; 95 96 this.applyArgs(args); 97 98 /*namespace remapping for readability*/ 99 //we fetch in the standard arguments 100 //and apply them to our protected attributes 101 //we do not gc the entry hence it is not defined on top 102 var xhrCore = myfaces._impl.xhrCore; 103 this._AJAXUTIL = xhrCore._AjaxUtils; 104 105 } catch (e) { 106 //_onError 107 this._stdErrorHandler(this._xhr, this._context, e); 108 } 109 }, 110 111 /** 112 * Sends an Ajax request 113 */ 114 send : function() { 115 116 var _Lang = this._Lang; 117 var _RT = this._RT; 118 119 try { 120 121 var scopeThis = _Lang.hitch(this, function(functionName) { 122 return _Lang.hitch(this, this[functionName]); 123 }); 124 this._xhr = _Lang.mixMaps(this._getTransport(), { 125 onprogress: scopeThis("onprogress"), 126 ontimeout: scopeThis("ontimeout"), 127 //remove for xhr level2 support (chrome has problems with it) 128 onloadend: scopeThis("ondone"), 129 onload: scopeThis("onsuccess"), 130 onerror: scopeThis("onerror") 131 132 }, true); 133 var xhr = this._xhr, 134 sourceForm = this._sourceForm, 135 targetURL = (typeof sourceForm.elements[this.ENCODED_URL] == 'undefined') ? 136 sourceForm.action : 137 sourceForm.elements[this.ENCODED_URL].value, 138 formData = this.getFormData(); 139 140 for (var key in this._passThrough) { 141 if(!this._passThrough.hasOwnProperty(key)) continue; 142 formData.append(key, this._passThrough[key]); 143 } 144 145 xhr.open(this._ajaxType, targetURL + 146 ((this._ajaxType == "GET") ? "?" + this._formDataToURI(formData) : "") 147 , true); 148 149 xhr.timeout = this._timeout || 0; 150 151 var contentType = this._contentType+"; charset=utf-8"; 152 153 xhr.setRequestHeader(this._CONTENT_TYPE, contentType); 154 xhr.setRequestHeader(this._HEAD_FACES_REQ, this._VAL_AJAX); 155 156 //some webkit based mobile browsers do not follow the w3c spec of 157 // setting the accept headers automatically 158 if(this._RT.browser.isWebKit) { 159 xhr.setRequestHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); 160 } 161 this._sendEvent("BEGIN"); 162 //Check if it is a custom form data object 163 //if yes we use makefinal for the final handling 164 if (formData && formData.makeFinal) { 165 formData = formData.makeFinal() 166 } 167 xhr.send((this._ajaxType != "GET") ? formData : null); 168 169 } catch (e) { 170 //_onError//_onError 171 e = (e._mfInternal)? e: this._Lang.makeException(new Error(), "sendError","sendError", this._nameSpace, "send", e.message); 172 this._stdErrorHandler(this._xhr, this._context, e); 173 } 174 }, 175 176 177 ondone: function() { 178 this._requestDone(); 179 }, 180 181 182 onsuccess: function(/*evt*/) { 183 184 var context = this._context; 185 var xhr = this._xhr; 186 try { 187 this._sendEvent("COMPLETE"); 188 //now we have to reroute into our official api 189 //because users might want to decorate it, we will split it apart afterwards 190 191 context._mfInternal = context._mfInternal || {}; 192 jsf.ajax.response((xhr.getXHRObject) ? xhr.getXHRObject() : xhr, context); 193 194 195 196 } catch (e) { 197 this._stdErrorHandler(this._xhr, this._context, e); 198 } 199 //add for xhr level2 support 200 //} finally { 201 //W3C spec onloadend must be called no matter if success or not 202 // this.ondone(); 203 //} 204 }, 205 206 onerror: function(/*evt*/) { 207 //TODO improve the error code detection here regarding server errors etc... 208 //and push it into our general error handling subframework 209 var context = this._context; 210 var xhr = this._xhr; 211 var _Lang = this._Lang; 212 213 var errorText = ""; 214 this._sendEvent("COMPLETE"); 215 try { 216 var UNKNOWN = _Lang.getMessage("UNKNOWN"); 217 //status can be 0 and statusText can be "" 218 var status = ('undefined' != xhr.status && null != xhr.status)? xhr.status : UNKNOWN; 219 var statusText = ('undefined' != xhr.statusText && null != xhr.statusText)? xhr.statusText : UNKNOWN; 220 errorText = _Lang.getMessage("ERR_REQU_FAILED", null,status,statusText); 221 222 } catch (e) { 223 errorText = _Lang.getMessage("ERR_REQ_FAILED_UNKNOWN", null); 224 } finally { 225 var _Impl = this.attr("impl"); 226 _Impl.sendError(xhr, context, _Impl.HTTPERROR, 227 _Impl.HTTPERROR, errorText,"","myfaces._impl.xhrCore._AjaxRequest","onerror"); 228 //add for xhr level2 support 229 //since chrome does not call properly the onloadend we have to do it manually 230 //to eliminate xhr level1 for the compile profile modern 231 //W3C spec onloadend must be called no matter if success or not 232 //this.ondone(); 233 } 234 //_onError 235 }, 236 237 onprogress: function(/*evt*/) { 238 //do nothing for now 239 }, 240 241 ontimeout: function(/*evt*/) { 242 try { 243 //we issue an event not an error here before killing the xhr process 244 this._sendEvent("TIMEOUT_EVENT"); 245 //timeout done we process the next in the queue 246 } finally { 247 this._requestDone(); 248 } 249 }, 250 251 _formDataToURI: function(formData) { 252 if (formData && formData.makeFinal) { 253 formData = formData.makeFinal() 254 } 255 return formData; 256 }, 257 258 _getTransport: function() { 259 260 var xhr = this._RT.getXHRObject(); 261 //the current xhr level2 timeout w3c spec is not implemented by the browsers yet 262 //we have to do a fallback to our custom routines 263 264 //add for xhr level2 support 265 //Chrome fails in the current builds, on our loadend, we disable the xhr 266 //level2 optimisations for now 267 //if (/*('undefined' == typeof this._timeout || null == this._timeout) &&*/ this._RT.getXHRLvl() >= 2) { 268 //no timeout we can skip the emulation layer 269 // return xhr; 270 //} 271 return new myfaces._impl.xhrCore.engine.Xhr1({xhrObject: xhr}); 272 }, 273 274 275 276 //----------------- backported from the base request -------------------------------- 277 //non abstract ones 278 /** 279 * Spec. 13.3.1 280 * Collect and encode input elements. 281 * Additionally the hidden element javax.faces.ViewState 282 * 283 * 284 * @return an element of formDataWrapper 285 * which keeps the final Send Representation of the 286 */ 287 getFormData : function() { 288 var _AJAXUTIL = this._AJAXUTIL, myfacesOptions = this._context.myfaces; 289 return this._Lang.createFormDataDecorator(jsf.getViewState(this._sourceForm)); 290 }, 291 292 /** 293 * Client error handlers which also in the long run route into our error queue 294 * but also are able to deliver more meaningful messages 295 * note, in case of an error all subsequent xhr requests are dropped 296 * to get a clean state on things 297 * 298 * @param request the xhr request object 299 * @param context the context holding all values for further processing 300 * @param exception the embedded exception 301 */ 302 _stdErrorHandler: function(request, context, exception) { 303 var xhrQueue = this._xhrQueue; 304 try { 305 this.attr("impl").stdErrorHandler(request, context, exception); 306 } finally { 307 if (xhrQueue) { 308 xhrQueue.cleanup(); 309 } 310 } 311 }, 312 313 _sendEvent: function(evtType) { 314 var _Impl = this.attr("impl"); 315 _Impl.sendEvent(this._xhr, this._context, _Impl[evtType]); 316 }, 317 318 _requestDone: function() { 319 var queue = this._xhrQueue; 320 if (queue) { 321 queue.processQueue(); 322 } 323 //ie6 helper cleanup 324 delete this._context.source; 325 this._finalize(); 326 }, 327 328 //cleanup 329 _finalize: function() { 330 if (this._xhr.readyState == this._XHR_CONST.READY_STATE_DONE) { 331 this._callSuper("_finalize"); 332 } 333 } 334 }); 335 336