KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > user > client > impl > HTTPRequestImpl


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.user.client.impl;
17
18 import com.google.gwt.core.client.JavaScriptObject;
19 import com.google.gwt.user.client.ResponseTextHandler;
20
21 /**
22  * Native implementation associated with
23  * {@link com.google.gwt.user.client.HTTPRequest}.
24  */

25 public class HTTPRequestImpl {
26
27   public boolean asyncGet(String JavaDoc url, ResponseTextHandler handler) {
28     return asyncGet(null, null, url, handler);
29   }
30
31   public boolean asyncGet(String JavaDoc user, String JavaDoc pwd, String JavaDoc url,
32       ResponseTextHandler handler) {
33     return asyncGetImpl(user, pwd, url, handler);
34   }
35
36   public boolean asyncPost(String JavaDoc url, String JavaDoc postData,
37       ResponseTextHandler handler) {
38     return asyncPost(null, null, url, postData, handler);
39   }
40
41   public boolean asyncPost(String JavaDoc user, String JavaDoc pwd, String JavaDoc url,
42       String JavaDoc postData, ResponseTextHandler handler) {
43     return asyncPostImpl(user, pwd, url, postData, handler);
44   }
45
46   public JavaScriptObject createXmlHTTPRequest() {
47     return doCreateXmlHTTPRequest();
48   }
49
50   /**
51    * All the supported browsers except for IE instantiate it as shown.
52    */

53   protected native JavaScriptObject doCreateXmlHTTPRequest() /*-{
54     return new XMLHttpRequest();
55   }-*/
;
56
57   private native boolean asyncGetImpl(String JavaDoc user, String JavaDoc pwd, String JavaDoc url,
58       ResponseTextHandler handler) /*-{
59     var xmlHttp = this.@com.google.gwt.user.client.impl.HTTPRequestImpl::doCreateXmlHTTPRequest()();
60     try {
61       xmlHttp.open("GET", url, true);
62       xmlHttp.setRequestHeader("Content-Type", "text/plain; charset=utf-8");
63       xmlHttp.onreadystatechange = function() {
64         if (xmlHttp.readyState == 4) {
65           delete xmlHttp.onreadystatechange;
66           var localHandler = handler;
67           var responseText = xmlHttp.responseText;
68           handler = null;
69           xmlHttp = null;
70           localHandler.@com.google.gwt.user.client.ResponseTextHandler::onCompletion(Ljava/lang/String;)(responseText);
71         }
72       };
73       xmlHttp.send('');
74       return true;
75     }
76     catch (e) {
77       delete xmlHttp.onreadystatechange;
78       handler = null;
79       xmlHttp = null;
80       return false;
81     }
82   }-*/
;
83
84   private native boolean asyncPostImpl(String JavaDoc user, String JavaDoc pwd, String JavaDoc url,
85       String JavaDoc postData, ResponseTextHandler handler) /*-{
86     var xmlHttp = this.@com.google.gwt.user.client.impl.HTTPRequestImpl::doCreateXmlHTTPRequest()();
87     try {
88       xmlHttp.open("POST", url, true);
89       xmlHttp.setRequestHeader("Content-Type", "text/plain; charset=utf-8");
90       xmlHttp.onreadystatechange = function() {
91         if (xmlHttp.readyState == 4) {
92           delete xmlHttp.onreadystatechange;
93           var localHandler = handler;
94           var responseText = xmlHttp.responseText;
95           handler = null;
96           xmlHttp = null;
97           localHandler.@com.google.gwt.user.client.ResponseTextHandler::onCompletion(Ljava/lang/String;)(responseText);
98         }
99       };
100       xmlHttp.send(postData);
101       return true;
102     }
103     catch (e) {
104       delete xmlHttp.onreadystatechange;
105       handler = null;
106       xmlHttp = null;
107       return false;
108     }
109   }-*/
;
110 }
111
Popular Tags