001: /*
002: * GWT-Ext Widget Library
003: * Copyright(c) 2007-2008, GWT-Ext.
004: * licensing@gwt-ext.com
005: *
006: * http://www.gwt-ext.com/license
007: */
008:
009: package com.gwtext.client.core;
010:
011: import com.google.gwt.core.client.JavaScriptObject;
012: import com.gwtext.client.core.event.ConnectionListener;
013:
014: /**
015: * The class encapsulates a connection to the page's originating domain, allowing requests to be made either to a configured URL, or to a URL specified at request time.
016: * Requests made by this class are asynchronous, and will return immediately. No data from the server will be available to the statement immediately following the request call. To process returned data, use a callback in the request options object, or an event listener.
017: * Note: If you are doing a file upload, you will not get a normal response object sent back to your callback or event handler. Since the upload is handled via in IFRAME, there is no XMLHttpRequest. The response object is created using the innerHTML of the IFRAME's document as the responseText property and, if present, the IFRAME's XML document as the responseXML property.
018: * This means that a valid XML or HTML document must be returned. If JSON data is required, it is suggested that it be placed either inside a <textarea> in an HTML document and retrieved from the responseText using a regex, or inside a CDATA section in an XML document and retrieved from the responseXML using standard DOM methods.
019: */
020: public class Connection extends JsObject {
021:
022: /**
023: * HTTP request method constants.
024: */
025: public static final class Method {
026: private final String name;
027:
028: private Method(String name) {
029: this .name = name;
030: }
031:
032: public String getMethod() {
033: return name;
034: }
035:
036: public String toString() {
037: return name;
038: }
039: }
040:
041: /**
042: * Specifies that the HTTP GET method should be used.
043: */
044: public static final Method GET = new Method("GET");
045:
046: /**
047: * Specifies that the HTTP POST method should be used.
048: */
049: public static final Method POST = new Method("POST");
050:
051: protected Connection() {
052: }
053:
054: /**
055: * Constructs a Connection using a native connection object.
056: *
057: * @param jsObj native connection object
058: */
059: public Connection(JavaScriptObject jsObj) {
060: super (jsObj);
061: }
062:
063: /**
064: * Constructs a Connection using the configuration parameters passed.
065: *
066: * @param config the connection config
067: */
068: public Connection(ConnectionConfig config) {
069: jsObj = create(config.getJsObj());
070: }
071:
072: private native JavaScriptObject create(JavaScriptObject config) /*-{
073: return new $wnd.Ext.data.Connection(config);
074: }-*/;
075:
076: /**
077: * Aborts the last outstanding request.
078: */
079: public native void abort() /*-{
080: var conn = this.@com.gwtext.client.core.JsObject::getJsObj()();
081: conn.abort();
082: }-*/;
083:
084: /**
085: * Aborts any outstanding request.
086: *
087: * @param transactionId the transaction to abort
088: */
089: public native void abort(long transactionId) /*-{
090: var conn = this.@com.gwtext.client.core.JsObject::getJsObj()();
091: conn.abort(transactionId);
092: }-*/;
093:
094: /**
095: * Determine whether this object has a request outstanding.
096: *
097: * @return true if loading
098: */
099: public native boolean isLoading() /*-{
100: var conn = this.@com.gwtext.client.core.JsObject::getJsObj()();
101: return conn.isLoading();
102: }-*/;
103:
104: /**
105: * Determine whether the specified transaction has a request outstanding.
106: *
107: * @param transactionId the transaction id
108: * @return true if loading
109: */
110: public native boolean isLoading(long transactionId) /*-{
111: var conn = this.@com.gwtext.client.core.JsObject::getJsObj()();
112: return conn.isLoading(transactionId);
113: }-*/;
114:
115: /**
116: * Sends an HTTP request to a remote server.
117: *
118: * @return the transaction id
119: */
120: public native long request() /*-{
121: var conn = this.@com.gwtext.client.core.JsObject::getJsObj()();
122: var transId = conn.request();
123: return transId == null || transId === undefined ? -1 : transId;
124: }-*/;
125:
126: /**
127: * Sends an HTTP request to a remote server.
128: *
129: * @param param the request params
130: * @return the transaction id
131: */
132: public native long request(RequestParam param) /*-{
133: var conn = this.@com.gwtext.client.core.JsObject::getJsObj()();
134: var paramJS = param.@com.gwtext.client.core.JsObject::getJsObj()();
135: var transId = conn.request(paramJS);
136: return transId == null || transId === undefined ? -1 : transId;
137: }-*/;
138:
139: /**
140: * Adds a connection listener to this connection object.
141: *
142: * @param listener the connection listener
143: */
144: public native void addListener(ConnectionListener listener)/*-{
145: var conn = this.@com.gwtext.client.core.JsObject::getJsObj()();
146: var connJ = this;
147:
148: conn.addListener('beforerequest',
149: function(conn, options) {
150: return listener.@com.gwtext.client.core.event.ConnectionListener::doBeforeRequest(Lcom/gwtext/client/core/Connection;)(connJ);
151: }
152: );
153:
154: conn.addListener('requestcomplete',
155: function(conn, response, options) {
156: return listener.@com.gwtext.client.core.event.ConnectionListener::onRequestComplete(Lcom/gwtext/client/core/Connection;Ljava/lang/String;)(connJ, response.responseText);
157: }
158: );
159:
160: conn.addListener('requestexception',
161: function(conn, response, options) {
162: return listener.@com.gwtext.client.core.event.ConnectionListener::onRequestException(Lcom/gwtext/client/core/Connection;ILjava/lang/String;)(connJ, response.status, response.responseText);
163: }
164: );
165: }-*/;
166: }
|