001: /*
002: * Copyright 2006 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.user.client.impl;
017:
018: import com.google.gwt.core.client.JavaScriptObject;
019: import com.google.gwt.user.client.ResponseTextHandler;
020:
021: /**
022: * Native implementation associated with
023: * {@link com.google.gwt.user.client.HTTPRequest}.
024: */
025: public class HTTPRequestImpl {
026:
027: static JavaScriptObject nullFunc;
028:
029: public HTTPRequestImpl() {
030: nullFunc = JavaScriptObject.createFunction();
031: }
032:
033: public boolean asyncGet(String url, ResponseTextHandler handler) {
034: return asyncGet(null, null, url, handler);
035: }
036:
037: public boolean asyncGet(String user, String pwd, String url,
038: ResponseTextHandler handler) {
039: return asyncGetImpl(user, pwd, url, handler);
040: }
041:
042: public boolean asyncPost(String url, String postData,
043: ResponseTextHandler handler) {
044: return asyncPost(null, null, url, postData, handler);
045: }
046:
047: public boolean asyncPost(String user, String pwd, String url,
048: String postData, ResponseTextHandler handler) {
049: return asyncPostImpl(user, pwd, url, postData, handler);
050: }
051:
052: public JavaScriptObject createXmlHTTPRequest() {
053: return doCreateXmlHTTPRequest();
054: }
055:
056: /**
057: * All the supported browsers except for IE instantiate it as shown.
058: */
059: protected native JavaScriptObject doCreateXmlHTTPRequest() /*-{
060: return new XMLHttpRequest();
061: }-*/;
062:
063: private native boolean asyncGetImpl(String user, String pwd,
064: String url, ResponseTextHandler handler) /*-{
065: var xmlHttp = this.@com.google.gwt.user.client.impl.HTTPRequestImpl::doCreateXmlHTTPRequest()();
066: try {
067: xmlHttp.open("GET", url, true);
068: xmlHttp.setRequestHeader("Content-Type", "text/plain; charset=utf-8");
069: xmlHttp.onreadystatechange = function() {
070: if (xmlHttp.readyState == 4) {
071: xmlHttp.onreadystatechange = @com.google.gwt.user.client.impl.HTTPRequestImpl::nullFunc;
072: handler.@com.google.gwt.user.client.ResponseTextHandler::onCompletion(Ljava/lang/String;)(xmlHttp.responseText || "");
073: }
074: };
075: xmlHttp.send('');
076: return true;
077: } catch (e) {
078: xmlHttp.onreadystatechange = @com.google.gwt.user.client.impl.HTTPRequestImpl::nullFunc;
079: return false;
080: }
081: }-*/;
082:
083: private native boolean asyncPostImpl(String user, String pwd,
084: String url, String postData, ResponseTextHandler handler) /*-{
085: var xmlHttp = this.@com.google.gwt.user.client.impl.HTTPRequestImpl::doCreateXmlHTTPRequest()();
086: try {
087: xmlHttp.open("POST", url, true);
088: xmlHttp.setRequestHeader("Content-Type", "text/plain; charset=utf-8");
089: xmlHttp.onreadystatechange = function() {
090: if (xmlHttp.readyState == 4) {
091: xmlHttp.onreadystatechange = @com.google.gwt.user.client.impl.HTTPRequestImpl::nullFunc;
092: handler.@com.google.gwt.user.client.ResponseTextHandler::onCompletion(Ljava/lang/String;)(xmlHttp.responseText || "");
093: }
094: };
095: xmlHttp.send(postData);
096: return true;
097: }
098: catch (e) {
099: xmlHttp.onreadystatechange = @com.google.gwt.user.client.impl.HTTPRequestImpl::nullFunc;
100: return false;
101: }
102: }-*/;
103: }
|