1 /* ************************************************************************
3 qooxdoo - the new era of web development
8 2004-2007 1&1 Internet AG, Germany, http://www.1and1.org
12 LGPL: http://www.gnu.org/licenses/lgpl.html
13 EPL: http://www.eclipse.org/org/documents/epl-v10.php
14 See the LICENSE file in the project's top-level directory for details.
17 * Sebastian Werner (wpbasti)
18 * Andreas Ecker (ecker)
19 * Derrell Lipman (derrell)
21 ************************************************************************ */
23 /* ************************************************************************
27 #require(qx.util.Mime)
29 ************************************************************************ */
32 * This class is used to send HTTP requests to the server.
34 * @event created {qx.event.type.Event}
35 * @event configured {qx.event.type.Event}
36 * @event sending {qx.event.type.Event}
37 * @event receiving {qx.event.type.Event}
38 * @event completed {qx.event.type.Event}
39 * @event failed {qx.event.type.Event}
40 * @event aborted {qx.event.type.Event}
41 * @event timeout {qx.event.type.Event}
43 * @param vUrl {String} Target url to issue the request to.
44 * @param vMethod {String} Determines that type of request to issue (GET or POST). Default is GET.
45 * @param vResponseType {String} The mime type of the response. Default is text/plain {@link qx.util.Mime}.
47 qx.OO.defineClass("qx.io.remote.Request", qx.core.Target,
48 function(vUrl, vMethod, vResponseType)
50 qx.core.Target.call(this);
52 this._requestHeaders = {};
53 this._parameters = {};
56 this.setMethod(vMethod || qx.net.Http.METHOD_GET);
57 this.setResponseType(vResponseType || qx.util.Mime.TEXT);
59 this.setProhibitCaching(true);
61 // Prototype-Style Request Headers
62 this.setRequestHeader("X-Requested-With", "qooxdoo");
63 this.setRequestHeader("X-Qooxdoo-Version", qx.core.Version.toString());
65 // Get the next sequence number for this request
66 this._seqNum = ++qx.io.remote.Request._seqNum;
73 ---------------------------------------------------------------------------
75 ---------------------------------------------------------------------------
78 Target url to issue the request to.
80 qx.OO.addProperty({ name : "url", type : "string" });
82 Determines what type of request to issue (GET or POST).
89 qx.net.Http.METHOD_GET, qx.net.Http.METHOD_POST,
90 qx.net.Http.METHOD_PUT, qx.net.Http.METHOD_HEAD,
91 qx.net.Http.METHOD_DELETE
95 Set the request to asynchronous.
97 qx.OO.addProperty({ name : "asynchronous", type : "boolean", defaultValue : true,
98 getAlias: "isAsynchronous" });
100 Set the data to be sent via this request
102 qx.OO.addProperty({ name : "data", type : "string" });
104 Username to use for HTTP authentication. Null if HTTP authentication
107 qx.OO.addProperty({ name : "username", type : "string" });
109 Password to use for HTTP authentication. Null if HTTP authentication
112 qx.OO.addProperty({ name : "password", type : "string" });
118 "configured", "queued",
119 "sending", "receiving",
120 "completed", "aborted",
123 defaultValue : "configured"
126 Response type of request.
128 The response type is a MIME type, default is text/plain. Other
129 supported MIME types are text/javascript, text/html, application/json,
135 name : "responseType",
139 qx.util.Mime.JAVASCRIPT, qx.util.Mime.JSON,
140 qx.util.Mime.XML, qx.util.Mime.HTML
144 Number of millieseconds before the request is being timed out.
146 If this property is null, the timeout for the request comes is the
147 qx.io.remote.RequestQueue's property defaultTimeout.
149 qx.OO.addProperty({ name : "timeout", type : "number" });
152 Prohibit request from being cached.
154 Setting the value to true adds a parameter "nocache" to the request
155 with a value of the current time. Setting the value to false removes
158 qx.OO.addProperty({ name : "prohibitCaching", type : "boolean" });
160 Indicate that the request is cross domain.
162 A request is cross domain if the request's URL points to a host other
163 than the local host. This switches the concrete implementation that
164 is used for sending the request from qx.io.remote.XmlHttpTransport to
165 qx.io.remote.ScriptTransport, because only the latter can handle cross domain
168 qx.OO.addProperty({ name : "crossDomain", type : "boolean", defaultValue : false });
170 Indicate that the request will be used for a file upload.
172 The request will be used for a file upload. This switches the concrete
173 implementation that is used for sending the request from
174 qx.io.remote.XmlHttpTransport to qx.io.remote.IFrameTransport, because only
175 the latter can handle file uploads.
177 qx.OO.addProperty({ name : "fileUpload", type : "boolean", defaultValue : false });
179 The transport instance used for the request.
181 This is necessary to be able to abort an asynchronous request.
183 qx.OO.addProperty({ name : "transport", type : "object", instance : "qx.io.remote.Exchange" });
185 Use Basic HTTP Authentication
187 qx.OO.addProperty({ name : "useBasicHttpAuth", type : "boolean" });
195 ---------------------------------------------------------------------------
197 ---------------------------------------------------------------------------
200 Schedule this request for transport to server.
202 The request is added to the singleton class qx.io.remote.RequestQueue's list of
205 qx.Proto.send = function() {
206 qx.io.remote.RequestQueue.getInstance().add(this);
210 Abort sending this request.
212 The request is removed from the singleton class qx.io.remote.RequestQueue's
213 list of pending events. If the request haven't been scheduled this
216 qx.Proto.abort = function() {
217 qx.io.remote.RequestQueue.getInstance().abort(this);
220 qx.Proto.reset = function()
222 switch(this.getState())
226 this.error("Aborting already sent request!");
242 ---------------------------------------------------------------------------
244 ---------------------------------------------------------------------------
247 qx.Proto.isConfigured = function() {
248 return this.getState() === "configured";
251 qx.Proto.isQueued = function() {
252 return this.getState() === "queued";
255 qx.Proto.isSending = function() {
256 return this.getState() === "sending";
259 qx.Proto.isReceiving = function() {
260 return this.getState() === "receiving";
263 qx.Proto.isCompleted = function() {
264 return this.getState() === "completed";
267 qx.Proto.isAborted = function() {
268 return this.getState() === "aborted";
271 qx.Proto.isTimeout = function() {
272 return this.getState() === "timeout";
276 Return true if the request is in the failed state
279 qx.Proto.isFailed = function() {
280 return this.getState() === "failed";
290 ---------------------------------------------------------------------------
292 ---------------------------------------------------------------------------
295 qx.Proto._onqueued = function(e)
297 // Modify internal state
298 this.setState("queued");
301 this.dispatchEvent(e);
304 qx.Proto._onsending = function(e)
306 // Modify internal state
307 this.setState("sending");
310 this.dispatchEvent(e);
313 qx.Proto._onreceiving = function(e)
315 // Modify internal state
316 this.setState("receiving");
319 this.dispatchEvent(e);
322 qx.Proto._oncompleted = function(e)
324 // Modify internal state
325 this.setState("completed");
328 this.dispatchEvent(e);
330 // Automatically dispose after event completion
334 qx.Proto._onaborted = function(e)
336 // Modify internal state
337 this.setState("aborted");
340 this.dispatchEvent(e);
342 // Automatically dispose after event completion
346 qx.Proto._ontimeout = function(e)
349 // User's handler can block until timeout.
350 switch(this.getState())
352 // If we're no longer running...
357 // then don't bubble up the timeout event
362 // Modify internal state
363 this.setState("timeout");
366 this.dispatchEvent(e);
368 // Automatically dispose after event completion
372 qx.Proto._onfailed = function(e)
374 // Modify internal state
375 this.setState("failed");
378 this.dispatchEvent(e);
380 // Automatically dispose after event completion
392 ---------------------------------------------------------------------------
394 ---------------------------------------------------------------------------
397 qx.Proto._modifyState = function(propValue, propOldValue, propData)
399 if (qx.Settings.getValueOfClass("qx.io.remote.Exchange", "enableDebug")) {
400 this.debug("State: " + propValue);
406 qx.Proto._modifyProhibitCaching = function(propValue, propOldValue, propData)
408 propValue ? this.setParameter("nocache", new Date().valueOf()) : this.removeParameter("nocache");
413 qx.Proto._modifyMethod = function(propValue, propOldValue, propData)
415 if (propValue === qx.net.Http.METHOD_POST) {
416 this.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
422 qx.Proto._modifyResponseType = function(propValue, propOldValue, propData)
424 this.setRequestHeader("X-Qooxdoo-Response-Type", propValue);
435 ---------------------------------------------------------------------------
437 ---------------------------------------------------------------------------
440 Add a request header to the request.
442 Example: request.setRequestHeader("Content-Type", qx.util.Mime.HTML)
444 qx.Proto.setRequestHeader = function(vId, vValue) {
445 this._requestHeaders[vId] = vValue;
448 qx.Proto.removeRequestHeader = function(vId) {
449 delete this._requestHeaders[vId];
452 qx.Proto.getRequestHeader = function(vId) {
453 return this._requestHeaders[vId] || null;
456 qx.Proto.getRequestHeaders = function() {
457 return this._requestHeaders;
469 ---------------------------------------------------------------------------
471 ---------------------------------------------------------------------------
474 Add a parameter to the request.
476 @param vId String identifier of the parameter to add.
477 @param vValue Value of parameter. May be a string (for one parameter) or an
478 array of strings (for setting multiple parameter values with the same
481 qx.Proto.setParameter = function(vId, vValue) {
482 this._parameters[vId] = vValue;
486 Remove a parameter from the request.
488 @param vId String identifier of the parameter to remove.
490 qx.Proto.removeParameter = function(vId) {
491 delete this._parameters[vId];
495 Get a parameter in the request.
497 @param vId String identifier of the parameter to get.
499 qx.Proto.getParameter = function(vId) {
500 return this._parameters[vId] || null;
504 Returns an object containg all parameters for the request.
506 qx.Proto.getParameters = function() {
507 return this._parameters;
518 ---------------------------------------------------------------------------
520 ---------------------------------------------------------------------------
524 * Sequence (id) number of a request, used to associate a response or error
525 * with its initiating request.
527 qx.io.remote.Request._seqNum = 0;
530 * Obtain the sequence (id) number used for this request
532 qx.Proto.getSequenceNumber = function() {
542 ---------------------------------------------------------------------------
544 ---------------------------------------------------------------------------
547 qx.Proto.dispose = function()
549 if (this.getDisposed()) {
553 this._requestHeaders = null;
554 this._parameters = null;
556 this.setTransport(null);
558 return qx.core.Target.prototype.dispose.call(this);