Search in sources :

Example 1 with XMLHttpRequest

use of elemental2.dom.XMLHttpRequest in project gwtproject by treblereel.

the class ResponseTest method testGetHeadersOffline.

@Test
public void testGetHeadersOffline() {
    ResponseImpl resp = new ResponseImpl(new XMLHttpRequest()) {

        @Override
        protected boolean isResponseReady() {
            return true;
        }
    };
    Header[] headers = resp.getHeaders();
    assertNotNull(headers);
    assertEquals(0, headers.length);
}
Also used : XMLHttpRequest(elemental2.dom.XMLHttpRequest) Test(org.junit.Test)

Example 2 with XMLHttpRequest

use of elemental2.dom.XMLHttpRequest in project gwtproject by treblereel.

the class Request method fireOnResponseReceived.

/*
   * Method called when the JavaScript XmlHttpRequest object's readyState
   * reaches 4 (LOADED).
   */
void fireOnResponseReceived(RequestCallback callback) {
    if (xmlHttpRequest == null) {
        // the request has timed out at this point
        return;
    }
    cancelTimer();
    /*
     * We cannot use cancel here because it would clear the contents of the
     * JavaScript XmlHttpRequest object so we manually null out our reference to
     * the JavaScriptObject
     */
    final XMLHttpRequest xhr = xmlHttpRequest;
    xmlHttpRequest = null;
    Response response = createResponse(xhr);
    callback.onResponseReceived(this, response);
}
Also used : XMLHttpRequest(elemental2.dom.XMLHttpRequest)

Example 3 with XMLHttpRequest

use of elemental2.dom.XMLHttpRequest in project gwtproject by treblereel.

the class RequestBuilder method doSend.

/**
 * Sends an HTTP request based on the current builder configuration. If no request headers have
 * been set, the header "Content-Type" will be used with a value of "text/plain; charset=utf-8".
 *
 * @return a {@link Request} object that can be used to track the request
 * @throws RequestException if the call fails to initiate
 * @throws NullPointerException if request data has not been set
 * @throws NullPointerException if a request callback has not been set
 */
private Request doSend(String requestData, final RequestCallback callback) throws RequestException {
    XMLHttpRequest xmlHttpRequest = new XMLHttpRequest();
    if (user != null && password != null) {
        xmlHttpRequest.open(httpMethod, url, true, user, password);
    } else if (user != null) {
        xmlHttpRequest.open(httpMethod, url, true, user);
    } else {
        xmlHttpRequest.open(httpMethod, url, true);
    }
    setHeaders(xmlHttpRequest);
    if (includeCredentials) {
        xmlHttpRequest.withCredentials = true;
    }
    final Request request = new Request(xmlHttpRequest, timeoutMillis, callback);
    // Must set the onreadystatechange handler before calling send().
    xmlHttpRequest.onreadystatechange = evt -> {
        if (xmlHttpRequest.readyState == XMLHttpRequest.DONE) {
            // XXX: this clearOnReadyStateChange() was in
            // com.google.gwt.http.client.Request, do we really need it (and equivalent) here?
            // com.google.gwt.xhr.client.XMLHttpRequest has this note:
            /*
             * NOTE: Testing discovered that for some bizarre reason, on Mozilla, the
             * JavaScript <code>XmlHttpRequest.onreadystatechange</code> handler
             * function maybe still be called after it is deleted. The theory is that the
             * callback is cached somewhere. Setting it to null or an empty function does
             * seem to work properly, though.
             *
             * On IE, setting onreadystatechange to null (as opposed to an empty function)
             * sometimes throws an exception.
             *
             * End result: *always* set onreadystatechange to an empty function (never to
             * null).
             */
            // xhr.clearOnReadyStateChange();
            request.fireOnResponseReceived(callback);
        }
        return Js.undefined();
    };
    try {
        xmlHttpRequest.send(requestData);
    } catch (Throwable e) {
        throw new RequestException(e.getMessage());
    }
    return request;
}
Also used : XMLHttpRequest(elemental2.dom.XMLHttpRequest) XMLHttpRequest(elemental2.dom.XMLHttpRequest)

Example 4 with XMLHttpRequest

use of elemental2.dom.XMLHttpRequest in project console by hal.

the class Dispatcher method newXhr.

private XMLHttpRequest newXhr(String url, HttpMethod method, Operation operation, OnError error, OnLoad onLoad) {
    XMLHttpRequest xhr = new XMLHttpRequest();
    // The order of the XHR methods is important! Do not rearrange the code unless you know what you're doing!
    xhr.onload = event -> onLoad.onLoad(xhr);
    // NON-NLS
    xhr.addEventListener(// NON-NLS
    "error", event -> handleErrorCodes(url, xhr.status, operation, error), false);
    xhr.open(method.name(), url, true);
    xhr.setRequestHeader(X_MANAGEMENT_CLIENT_NAME.header(), HEADER_MANAGEMENT_CLIENT_VALUE);
    String bearerToken = getBearerToken();
    if (bearerToken != null) {
        xhr.setRequestHeader("Authorization", "Bearer " + bearerToken);
    }
    xhr.withCredentials = true;
    return xhr;
}
Also used : XMLHttpRequest(elemental2.dom.XMLHttpRequest)

Example 5 with XMLHttpRequest

use of elemental2.dom.XMLHttpRequest in project console by hal.

the class ExtensionRegistry method verifyMetadata.

@JsIgnore
public void verifyMetadata(String url, MetadataCallback metadataCallback) {
    SafeUri safeUrl = UriUtils.fromString(url);
    XMLHttpRequest xhr = new XMLHttpRequest();
    xhr.onload = event -> {
        int status = (int) xhr.status;
        if (status >= 200 && status < 400) {
            String responseText = xhr.responseText;
            if (Strings.isNullOrEmpty(responseText)) {
                // 415 - Unsupported Media Type
                metadataCallback.result(415, null);
            } else {
                JsonObject extensionJson = Json.parse(responseText);
                metadataCallback.result(status, extensionJson);
            }
        } else {
            metadataCallback.result(status, null);
        }
    };
    // NON-NLS
    xhr.addEventListener("error", event -> metadataCallback.result(503, null), false);
    xhr.open(GET.name(), safeUrl.asString(), true);
    xhr.send();
}
Also used : SafeUri(com.google.gwt.safehtml.shared.SafeUri) JsonObject(org.jboss.hal.js.JsonObject) XMLHttpRequest(elemental2.dom.XMLHttpRequest) Point(org.jboss.hal.core.extension.Extension.Point) JsIgnore(jsinterop.annotations.JsIgnore)

Aggregations

XMLHttpRequest (elemental2.dom.XMLHttpRequest)14 JsIgnore (jsinterop.annotations.JsIgnore)2 Operation (org.jboss.hal.dmr.Operation)2 JsonObject (org.jboss.hal.js.JsonObject)2 Test (org.junit.Test)2 SafeUri (com.google.gwt.safehtml.shared.SafeUri)1 BootstrapFailed (org.jboss.hal.client.bootstrap.BootstrapFailed)1 Version (org.jboss.hal.config.Version)1 Point (org.jboss.hal.core.extension.Extension.Point)1