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  * @class
 19  * @name _Transports
 20  * @memberOf myfaces._impl.xhrCore
 21  * @description
 22  *
 23  * The xhr core adapter
 24  * which provides the transport mechanisms to the calling
 25  * objects, and controls the queue behavior, the error handling
 26  * and partial page submit functionality among other things
 27  * <p />
 28  * The idea behind this is to make the ajax request object as barebones
 29  * as possible and shift the extra functionality like queuing
 30  * parameter handling etc... to this class so that our transports become more easily
 31  * pluggable. This should keep the path open to iframe submits and other transport layers
 32  * <p />
 33  * the call to the corresponding transport just should be a
 34  * transport.xhrQueuedPost <br />
 35  * or transport.xhrPost,transport.xhrGet  etc... in the future
 36  * <p />
 37  * Note we have taken a pattern lesson or two from the dojo toolkit and its excellent handling
 38  * of transports by our patterns here (which is mainly a centralized transport singleton which routes
 39  * to different transport implementations and the auto passing of parameters into their
 40  * corresponding protected attributes on class level in the transports themselves)
 41  */
 42 _MF_SINGLTN(_PFX_XHR + "_Transports", _MF_OBJECT,
 43         /** @lends myfaces._impl.xhrCore._Transports.prototype */ {
 44 
 45     _PAR_ERRORLEVEL:"errorlevel",
 46     _PAR_QUEUESIZE:"queuesize",
 47     _PAR_PPS:"pps",
 48     _PAR_TIMEOUT:"timeout",
 49     _PAR_DELAY:"delay",
 50 
 51 
 52     /**
 53      * a singleton queue
 54      * note the structure of our inheritance
 55      * is that that _queue is attached to prototype
 56      * and hence the pointer to the request qeue
 57      * is shared over all instances
 58      *
 59      * if you need to have it per instance for complex objects
 60      * you have to initialize in the constructor
 61      *
 62      * (This is the same limitation dojo class inheritance
 63      * where our inheritance pattern is derived from has)
 64      */
 65     _q: new myfaces._impl.xhrCore._AjaxRequestQueue(),
 66 
 67     /**
 68      * xhr post with enqueuing as defined by the jsf 2.0 specification
 69      *
 70      * mapped options already have the exec and view properly in place
 71      * myfaces specifics can be found under mappedOptions.myFaces
 72      * @param {Node} source the source of this call
 73      * @param {Node} sourceForm the html form which is the source of this call
 74      * @param {Object} context (Map) the internal pass through context
 75      * @param {Object} passThrgh (Map) values to be passed through
 76      **/
 77     xhrQueuedPost : function(source, sourceForm, context, passThrgh) {
 78         this._q.enqueue(
 79                 new (this._getAjaxReqClass(context))(this._getArguments(source, sourceForm, context, passThrgh)));
 80     },
 81 
 82     /**
 83      * creates the arguments map and
 84      * fetches the config params in a proper way in to
 85      * deal with them in a flat way (from the nested context way)
 86      *
 87      * @param source the source of the request
 88      * @param sourceForm the sourceform
 89      * @param context   the context holding all values
 90      * @param passThrgh the passThrough values to be blended into the response
 91      */
 92     _getArguments: function(source, sourceForm, context, passThrgh) {
 93         var _RT = myfaces._impl.core._Runtime,
 94         /** @ignore */
 95              _Lang = myfaces._impl._util._Lang,
 96              applyCfg = _Lang.hitch(this, this._applyConfig),
 97             //RT does not have this references, hence no hitch needed
 98              getCfg = _RT.getLocalOrGlobalConfig,
 99 
100 
101             ret = {
102                 "source": source,
103                 "sourceForm": sourceForm,
104                 "context": context,
105                 "passThrough": passThrgh,
106                 "xhrQueue": this._q
107             };
108 
109         //we now mix in the config settings which might either be set globally
110         //or pushed in under the context myfaces.<contextValue> into the current request
111         applyCfg(ret, context, "alarmThreshold", this._PAR_ERRORLEVEL);
112         applyCfg(ret, context, "queueSize", this._PAR_QUEUESIZE);
113         //TODO timeout probably not needed anymore
114         applyCfg(ret, context, "timeout", this._PAR_TIMEOUT);
115         //applyCfg(ret, context, "delay", this._PAR_DELAY);
116 
117         //now partial page submit needs a different treatment
118         //since pps == execute strings
119         if (getCfg(context, this._PAR_PPS, false)
120                 && _Lang.exists(passThrgh, myfaces._impl.core.Impl.P_EXECUTE)
121                 && passThrgh[myfaces._impl.core.Impl.P_EXECUTE].length > 0) {
122             ret['partialIdsArray'] = passThrgh[myfaces._impl.core.Impl.P_EXECUTE].split(" ");
123         }
124         return ret;
125     },
126 
127     /**
128      * helper method to apply a config setting to our varargs param list
129      *
130      * @param destination the destination map to receive the setting
131      * @param context the current context
132      * @param destParm the destination param of the destination map
133      * @param srcParm the source param which is the key to our config setting
134      */
135     _applyConfig: function(destination, context, destParm, srcParm) {
136         var _RT = myfaces._impl.core._Runtime;
137         /** @ignore */
138         var _getConfig = _RT.getLocalOrGlobalConfig;
139         if (_getConfig(context, srcParm, null) != null) {
140             destination[destParm] = _getConfig(context, srcParm, null);
141         }
142     },
143 
144     /**
145      * centralized transport switching helper
146      * for the multipart submit case
147      *
148      * @param context the context which is passed down
149      */
150     _getMultipartReqClass: function(context) {
151         //if (this._RT.getLocalOrGlobalConfig(context, "transportAutoSelection", false) && this._RT.getXHRLvl() >= 2) {
152         //    return myfaces._impl.xhrCore._AjaxRequestLevel2;
153         //} else {
154         return myfaces._impl.xhrCore._IFrameRequest;
155         //}
156     },
157 
158 
159     _getAjaxReqClass: function(context) {
160         // var _RT = myfaces._impl.core._Runtime;
161         //if(_RT.getXHRLvl() < 2) {
162             return myfaces._impl.xhrCore._AjaxRequest;
163         //} else {
164         //    return myfaces._impl.xhrCore._AjaxRequestLevel2;
165         //}
166     }
167 
168 });
169