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.core.client.GWT;
020 import com.google.gwt.user.client.ResponseTextHandler;
021
022 /**
023 * Native implementation associated with
024 * {@link com.google.gwt.user.client.HTTPRequest}.
025 */
026 public class HTTPRequestImpl {
027
028 public boolean asyncPost(String url, String postData,
029 ResponseTextHandler handler) {
030 return asyncPost(null, null, url, postData, handler);
031 }
032
033 public boolean asyncPost(String user, String pwd, String url,
034 String postData, ResponseTextHandler handler) {
035 String moduleName = GWT.getModuleName();
036 return asyncPostImpl(null, null, url, postData, handler, moduleName);
037 }
038
039 public boolean asyncGet(String url, ResponseTextHandler handler) {
040 return asyncGet(null, null, url, handler);
041 }
042
043 public boolean asyncGet(String user, String pwd, String url,
044 ResponseTextHandler handler) {
045 String moduleName = GWT.getModuleName();
046 return asyncGetImpl(null, null, url, handler, moduleName);
047 }
048
049 /**
050 * All the supported browsers except for IE instantiate it as shown.
051 */
052 protected native JavaScriptObject doCreateXmlHTTPRequest() /*-{
053 return new XMLHttpRequest();
054 }-*/;
055
056 private native boolean asyncPostImpl(String user, String pwd, String url, String postData, ResponseTextHandler handler, String moduleName) /*-{
057 var xmlHttp = this.@com.google.gwt.user.client.impl.HTTPRequestImpl::doCreateXmlHTTPRequest()();
058 try {
059 xmlHttp.open("POST", url, true);
060 xmlHttp.setRequestHeader("Content-Type", "text/plain; charset=utf-8");
061 if (moduleName) {
062 xmlHttp.setRequestHeader("X-GWTCallingModule", moduleName);
063 }
064 xmlHttp.onreadystatechange = function() {
065 if (xmlHttp.readyState == 4) {
066 handler.@com.google.gwt.user.client.ResponseTextHandler::onCompletion(Ljava/lang/String;)(xmlHttp.responseText);
067 xmlHttp = null;
068 }
069 };
070 xmlHttp.send(postData);
071 return true;
072 }
073 catch (e) {
074 return false;
075 }
076 }-*/;
077
078 private native boolean asyncGetImpl(String user, String pwd, String url, ResponseTextHandler handler, String moduleName) /*-{
079 var xmlHttp = this.@com.google.gwt.user.client.impl.HTTPRequestImpl::doCreateXmlHTTPRequest()();
080 try {
081 xmlHttp.open("GET", url, true);
082 xmlHttp.setRequestHeader("Content-Type", "text/plain; charset=utf-8");
083 if (moduleName) {
084 xmlHttp.setRequestHeader("X-GWTCallingModule", moduleName);
085 }
086 xmlHttp.onreadystatechange = function() {
087 if (xmlHttp.readyState == 4) {
088 handler.@com.google.gwt.user.client.ResponseTextHandler::onCompletion(Ljava/lang/String;)(xmlHttp.responseText);
089 xmlHttp = null;
090 }
091 };
092 xmlHttp.send('');
093 return true;
094 }
095 catch (e) {
096 return false;
097 }
098 }-*/;
099 }