Example usage for com.google.gwt.xhr.client XMLHttpRequest open

List of usage examples for com.google.gwt.xhr.client XMLHttpRequest open

Introduction

In this page you can find the example usage for com.google.gwt.xhr.client XMLHttpRequest open.

Prototype

public abstract void open(final String httpMethod, String url, String user, String password);

Source Link

Document

Opens an asynchronous connection.

Usage

From source file:com.cgxlib.xq.client.plugins.deferred.PromiseReqBuilder.java

License:Apache License

/**
 * Using this constructor we access to some things in the xmlHttpRequest
 * which are not available in GWT, like adding progress handles or sending
 * javascript data (like forms in modern html5 file api).
 *///ww  w .j  a  v a2s.  c o  m
public PromiseReqBuilder(Settings settings) {
    String httpMethod = settings.getType();
    String url = settings.getUrl();
    IsProperties data = settings.getData();
    String ctype = settings.getContentType();
    Boolean isFormData = data != null && data.getDataImpl() instanceof JavaScriptObject
            && JsUtils.isFormData(data.<JavaScriptObject>getDataImpl());

    XMLHttpRequest xmlHttpRequest = XMLHttpRequest.create();
    try {
        if (settings.getUsername() != null && settings.getPassword() != null) {
            xmlHttpRequest.open(httpMethod, url, settings.getUsername(), settings.getPassword());
        } else if (settings.getUsername() != null) {
            xmlHttpRequest.open(httpMethod, url, settings.getUsername());
        } else {
            xmlHttpRequest.open(httpMethod, url);
        }
    } catch (JavaScriptException e) {
        RequestPermissionException requestPermissionException = new RequestPermissionException(url);
        requestPermissionException.initCause(new RequestException(e.getMessage()));
        onError(null, e);
        return;
    }

    JsUtils.prop(xmlHttpRequest, "onprogress", JsUtils.wrapFunction(new Function() {
        public void f() {
            JsCache p = arguments(0);
            double total = p.getDouble("total");
            double loaded = p.getDouble("loaded");
            double percent = loaded == 0 ? 0 : total == 0 ? 100 : (100 * loaded / total);
            dfd.notify(total, loaded, percent, "download");
        }
    }));

    JavaScriptObject upload = JsUtils.prop(xmlHttpRequest, "upload");
    JsUtils.prop(upload, "onprogress", JsUtils.wrapFunction(new Function() {
        public void f() {
            JsCache p = arguments(0);
            double total = p.getDouble("total");
            double loaded = p.getDouble("loaded");
            double percent = 100 * loaded / total;
            dfd.notify(total, loaded, percent, "upload");
        }
    }));

    IsProperties headers = settings.getHeaders();
    if (headers != null) {
        for (String headerKey : headers.getFieldNames()) {
            xmlHttpRequest.setRequestHeader(headerKey, String.valueOf(headers.get(headerKey)));
        }
    }

    if (data != null && !isFormData && !"GET".equalsIgnoreCase(httpMethod)) {
        xmlHttpRequest.setRequestHeader("Content-Type", ctype);
    }

    // Using xq to set credentials since this method was added in 2.5.1
    // xmlHttpRequest.setWithCredentials(true);
    JsUtils.prop(xmlHttpRequest, "withCredentials", settings.getWithCredentials());

    final Request request = createRequestVltr(xmlHttpRequest, settings.getTimeout(), this);

    xmlHttpRequest.setOnReadyStateChange(new ReadyStateChangeHandler() {
        public void onReadyStateChange(XMLHttpRequest xhr) {
            if (xhr.getReadyState() == XMLHttpRequest.DONE) {
                xhr.clearOnReadyStateChange();
                fireOnResponseReceivedVltr(request, PromiseReqBuilder.this);
            }
        }
    });

    try {
        JsUtils.runJavascriptFunction(xmlHttpRequest, "send",
                isFormData ? data.getDataImpl() : settings.getDataString());
    } catch (JavaScriptException e) {
        onError(null, e);
    }
}