KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > http > client > XMLHTTPRequest


1 /*
2  * Copyright 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.http.client;
17
18 import com.google.gwt.core.client.JavaScriptObject;
19
20 /**
21  * Utility class that serves as the one place where we interact with the
22  * JavaScript <code>XmlHttpRequest</code> object.
23  */

24 final class XMLHTTPRequest {
25
26   public static final int UNITIALIZED = 0;
27   public static final int OPEN = 1;
28   public static final int SENT = 2;
29   public static final int RECEIVING = 3;
30   public static final int LOADED = 4;
31
32   /*
33    * NOTE: Testing discovered that for some bizarre reason, on Mozilla, the
34    * JavaScript <code>XmlHttpRequest.onreadystatechange</code> handler function
35    * maybe still be called after it is deleted. The theory is that the callback
36    * is cached somewhere. Setting the handler to null has the desired effect on
37    * Mozilla but it causes IE to crash during the assignment.
38    */

39   static native void abort(JavaScriptObject xmlHttpRequest) /*-{
40    delete xmlHttpRequest.onreadystatechange;
41    
42    xmlHttpRequest.abort();
43    }-*/
;
44
45   static native String JavaDoc getAllResponseHeaders(JavaScriptObject xmlHttpRequest) /*-{
46    return xmlHttpRequest.getAllResponseHeaders();
47    }-*/
;
48
49   /**
50    * Tests if the JavaScript <code>XmlHttpRequest.status</code> property is
51    * readable. This can return failure in two different known scenarios:
52    *
53    * <ol>
54    * <li>On Mozilla, after a network error, attempting to read the status
55    * code results in an exception being thrown. See
56    * <a HREF="https://bugzilla.mozilla.org/show_bug.cgi?id=238559">https://bugzilla.mozilla.org/show_bug.cgi?id=238559</a>.
57    * </li>
58    * <li>On Safari, if the HTTP response does not include any response text. See
59    * <a HREF="http://bugs.webkit.org/show_bug.cgi?id=3810">http://bugs.webkit.org/show_bug.cgi?id=3810</a>.
60    * </li>
61    * </ol>
62    *
63    * @param xmlHttpRequest the JavaScript <code>XmlHttpRequest</code> object
64    * to test
65    * @return a String message containing an error message if the
66    * <code>XmlHttpRequest.status</code> code is unreadable or null if the
67    * status code could be successfully read.
68    */

69   static native String JavaDoc getBrowserSpecificFailure(
70       JavaScriptObject xmlHttpRequest) /*-{
71     try {
72       if (xmlHttpRequest.status === undefined) {
73         return "XmlHttpRequest.status == undefined, please see Safari bug " +
74                "http://bugs.webkit.org/show_bug.cgi?id=3810 for more details";
75       }
76       return null;
77     }
78     catch ( e ) {
79       return "Unable to read XmlHttpRequest.status; likely causes are a " +
80              "networking error or bad cross-domain request. Please see " +
81              "https://bugzilla.mozilla.org/show_bug.cgi?id=238559 for more " +
82              "details";
83     }
84   }-*/
;
85
86   /**
87    * Returns an array of headers built by parsing the string of headers returned
88    * by the JavaScript <code>XmlHttpRequest</code> object.
89    *
90    * @param xmlHttpRequest
91    * @return array of Header items
92    */

93   static Header[] getHeaders(JavaScriptObject xmlHttpRequest) {
94     String JavaDoc allHeaders = getAllResponseHeaders(xmlHttpRequest);
95     String JavaDoc[] unparsedHeaders = allHeaders.split("\n");
96     Header[] parsedHeaders = new Header[unparsedHeaders.length];
97
98     for (int i = 0, n = unparsedHeaders.length; i < n; ++i) {
99       String JavaDoc unparsedHeader = unparsedHeaders[i];
100
101       if (unparsedHeader.length() == 0) {
102         continue;
103       }
104
105       int endOfNameIdx = unparsedHeader.indexOf(':');
106       if (endOfNameIdx < 0) {
107         continue;
108       }
109
110       final String JavaDoc name = unparsedHeader.substring(0, endOfNameIdx).trim();
111       final String JavaDoc value = unparsedHeader.substring(endOfNameIdx + 1).trim();
112       Header header = new Header() {
113         public String JavaDoc getName() {
114           return name;
115         }
116
117         public String JavaDoc getValue() {
118           return value;
119         }
120
121         public String JavaDoc toString() {
122           return name + " : " + value;
123         }
124       };
125
126       parsedHeaders[i] = header;
127     }
128
129     return parsedHeaders;
130   }
131
132   static native int getReadyState(JavaScriptObject xmlHttpRequest) /*-{
133    return xmlHttpRequest.readyState;
134    }-*/
;
135
136   static native String JavaDoc getResponseHeader(JavaScriptObject xmlHttpRequest,
137       String JavaDoc header) /*-{
138    try {
139    return xmlHttpRequest.getResponseHeader(header);
140    } catch (ex) {
141    // purposely ignored
142    }
143    
144    return null;
145    }-*/
;
146
147   static native String JavaDoc getResponseText(JavaScriptObject xmlHttpRequest) /*-{
148    return xmlHttpRequest.responseText;
149    }-*/
;
150
151   static native int getStatusCode(JavaScriptObject xmlHttpRequest) /*-{
152    return xmlHttpRequest.status;
153    }-*/
;
154
155   static native String JavaDoc getStatusText(JavaScriptObject xmlHttpRequest) /*-{
156    return xmlHttpRequest.statusText;
157    }-*/
;
158
159   static boolean isResponseReady(JavaScriptObject xmlHttpRequest) {
160     return getReadyState(xmlHttpRequest) == LOADED;
161   }
162
163   /**
164    * Opens the request and catches any exceptions thrown. If an exception is
165    * caught, its string representation will be returned. This is the only signal
166    * that an error has occurred.
167    *
168    * @param xmlHttpRequest JavaScript <code>XmlHttpRequest</code> object
169    * @param httpMethod the method to use for open call
170    * @param url the URL to use for the open call
171    * @param async true if we should do an asynchronous open
172    * @param user user to use in the URL
173    * @param password password to use in the URL
174    * @return error message if an exception is thrown or null if there is none
175    */

176   static native String JavaDoc open(JavaScriptObject xmlHttpRequest, String JavaDoc httpMethod,
177       String JavaDoc url, boolean async, String JavaDoc user, String JavaDoc password) /*-{
178    try {
179    xmlHttpRequest.open(httpMethod, url, async, user, password);
180    } catch (e) {
181    return e.toString();
182    }
183    
184    return null;
185    }-*/
;
186
187   /*
188    * Creates a closure that calls the HTTPRequest::fireOnResponseRecieved method
189    * when the server's response is received and sends the data.
190    */

191   static native String JavaDoc send(JavaScriptObject xmlHttpRequest,
192       Request httpRequest, String JavaDoc requestData, RequestCallback callback) /*-{
193    var xmlHttp = xmlHttpRequest;
194
195    xmlHttp.onreadystatechange = function() {
196    if (xmlHttp.readyState == @com.google.gwt.http.client.XMLHTTPRequest::LOADED) {
197    delete xmlHttp.onreadystatechange;
198    
199    httpRequest.@com.google.gwt.http.client.Request::fireOnResponseReceived(Lcom/google/gwt/http/client/RequestCallback;)(callback);
200    }
201    };
202    
203    try {
204    xmlHttp.send(requestData);
205    } catch (e) {
206    return e.toString();
207    }
208    
209    return null;
210    }-*/
;
211
212   static native String JavaDoc setRequestHeader(JavaScriptObject xmlHttpRequest,
213       String JavaDoc header, String JavaDoc value) /*-{
214    try {
215    xmlHttpRequest.setRequestHeader(header, value);
216    } catch (e) {
217    return e.toString();
218    }
219    
220    return null;
221    }-*/
;
222
223   private XMLHTTPRequest() {
224   }
225 }
226
Popular Tags