Example usage for com.google.gwt.http.client Request fireOnResponseReceived

List of usage examples for com.google.gwt.http.client Request fireOnResponseReceived

Introduction

In this page you can find the example usage for com.google.gwt.http.client Request fireOnResponseReceived.

Prototype

void fireOnResponseReceived(RequestCallback callback) 

Source Link

Usage

From source file:io.reinert.requestor.RequestDispatcherImpl.java

License:Apache License

@Override
protected <D> void send(RequestOrder request, final Deferred<D> deferred, Class<D> resolveType,
        @Nullable Class<?> parametrizedType) {
    final HttpMethod httpMethod = request.getMethod();
    final String url = request.getUrl();
    final Headers headers = request.getHeaders();
    final Payload payload = request.getPayload();
    final ResponseType responseType = request.getResponseType();

    // Create XMLHttpRequest
    final XMLHttpRequest xmlHttpRequest = (XMLHttpRequest) XMLHttpRequest.create();

    // Open XMLHttpRequest
    try {//w w w . j a v a 2s .  c o  m
        xmlHttpRequest.open(httpMethod.getValue(), url);
    } catch (JavaScriptException e) {
        RequestPermissionException requestPermissionException = new RequestPermissionException(url);
        requestPermissionException.initCause(new RequestException(e.getMessage()));
        deferred.reject(requestPermissionException);
        return;
    }

    // Set withCredentials
    xmlHttpRequest.setWithCredentials(request.isWithCredentials());

    // Fulfill headers
    try {
        setHeaders(headers, xmlHttpRequest);
    } catch (JavaScriptException e) {
        deferred.reject(new RequestException("Could not manipulate the XHR headers: " + e.getMessage()));
        return;
    }

    // Set responseType
    xmlHttpRequest.setResponseType(responseType.getValue());

    // Create RequestCallback
    final RequestCallback callback = getRequestCallback(request, xmlHttpRequest, deferred, resolveType,
            parametrizedType);

    // Create the underlying request from gwt.http module
    final com.google.gwt.http.client.Request gwtRequest = new com.google.gwt.http.client.Request(xmlHttpRequest,
            request.getTimeout(), callback);

    // Properly configure XMLHttpRequest's onreadystatechange
    xmlHttpRequest.setOnReadyStateChange(new ReadyStateChangeHandler() {
        public void onReadyStateChange(com.google.gwt.xhr.client.XMLHttpRequest xhr) {
            if (xhr.getReadyState() == XMLHttpRequest.DONE) {
                xhr.clearOnReadyStateChange();
                ((XMLHttpRequest) xhr).clearOnProgress();
                gwtRequest.fireOnResponseReceived(callback);
            }
        }
    });

    // Set XMLHttpRequest's onprogress if available binding to promise's progress
    xmlHttpRequest.setOnProgress(new ProgressHandler() {
        @Override
        public void onProgress(ProgressEvent progress) {
            deferred.notifyDownload(new RequestProgressImpl(progress));
        }
    });

    // Set XMLHttpRequest's upload onprogress if available binding to promise's progress
    xmlHttpRequest.setUploadOnProgress(new ProgressHandler() {
        @Override
        public void onProgress(ProgressEvent progress) {
            deferred.notifyUpload(new RequestProgressImpl(progress));
        }
    });

    // Pass the connection to the deferred to enable it to cancel the request if necessary (RECOMMENDED)
    deferred.setHttpConnection(getConnection(gwtRequest));

    // Send the request
    try {
        if (payload != null) {
            if (payload.isString() != null) {
                xmlHttpRequest.send(payload.isString());
            } else {
                xmlHttpRequest.send(payload.isJavaScriptObject());
            }
        } else {
            xmlHttpRequest.send();
        }
    } catch (JavaScriptException e) {
        deferred.reject(new RequestDispatchException("Could not send the XHR: " + e.getMessage()));
    }
}