Example usage for com.google.gwt.http.client RequestPermissionException RequestPermissionException

List of usage examples for com.google.gwt.http.client RequestPermissionException RequestPermissionException

Introduction

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

Prototype

public RequestPermissionException(String url) 

Source Link

Document

Constructs an instance of this class for the given URL.

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).
 *//*from ww w  .j  av a2  s  .  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);
    }
}

From source file:com.gwtpro.html5.fileapi.client.upload.UploadRequestBuilder.java

License:Apache License

/**
 * Sends a POST request based to upload a file, on the current builder
 * configuration./*from  ww  w . ja v a2  s.c  om*/
 * 
 * @return a {@link UploadRequest} object that can be used to track the
 *         request
 * @throws RequestException
 *             if the call fails to initiate
 * @throws NullPointerException
 *             if a request callback has not been set
 */
public UploadRequest sendFile(File file) throws RequestException {
    if (this.callback == null) {
        throw new NullPointerException("callback has not been set");
    }
    XMLHttpRequest2 xmlHttpRequest = XMLHttpRequest2.create();
    final UploadRequest request = new UploadRequest(xmlHttpRequest, this.timeoutMillis, this.callback);
    // progress handler must be set before open!!!
    xmlHttpRequest.setOnUploadProgressHandler(new UploadProgressHandler() {

        @Override
        public void onProgress(int bytesUploaded) {
            UploadRequestBuilder.this.callback.onUploadProgress(request, bytesUploaded);
        }
    });
    try {
        if (this.user != null && this.password != null) {
            xmlHttpRequest.open("POST", this.url, this.user, this.password);
        } else if (this.user != null) {
            xmlHttpRequest.open("POST", this.url, this.user);
        } else {
            xmlHttpRequest.open("POST", this.url);
        }
    } catch (JavaScriptException e) {
        RequestPermissionException requestPermissionException = new RequestPermissionException(this.url);
        requestPermissionException.initCause(new RequestException(e.getMessage()));
        throw requestPermissionException;
    }
    if (this.headers != null) {
        for (Map.Entry<String, String> header : this.headers.entrySet()) {
            try {
                xmlHttpRequest.setRequestHeader(header.getKey(), header.getValue());
            } catch (JavaScriptException e) {
                throw new RequestException(e.getMessage());
            }
        }
    }
    xmlHttpRequest.setOnReadyStateChange(new ReadyStateChangeHandler() {

        public void onReadyStateChange(XMLHttpRequest xhr) {
            if (xhr.getReadyState() == XMLHttpRequest.DONE) {
                xhr.clearOnReadyStateChange();
                request.fireOnResponseReceived(UploadRequestBuilder.this.callback);
            }
        }
    });
    try {
        xmlHttpRequest.sendFile(file);
    } catch (JavaScriptException e) {
        throw new RequestException(e.getMessage());
    }
    return request;
}