List of usage examples for com.google.gwt.xhr.client XMLHttpRequest clearOnReadyStateChange
public abstract void clearOnReadyStateChange();
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 w w w . ja va2 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.codenvy.plugin.contribution.client.steps.GenerateReviewFactoryStep.java
License:Open Source License
private void saveFactory(final FormData formData, final AsyncCallback<Factory> callback) { final String requestUrl = apiTemplate.saveFactory(); final XMLHttpRequest xhr = XMLHttpRequest.create(); xhr.open(HTTPMethod.POST, requestUrl); xhr.setRequestHeader(ACCEPT, APPLICATION_JSON); xhr.setOnReadyStateChange(new ReadyStateChangeHandler() { @Override//w w w . ja va 2s.c o m public void onReadyStateChange(final XMLHttpRequest request) { if (request.getReadyState() == XMLHttpRequest.DONE) { if (request.getStatus() == Response.SC_OK) { request.clearOnReadyStateChange(); final String payLoad = request.getResponseText(); final Factory createdFactory = dtoFactory.createDtoFromJson(payLoad, Factory.class); if (createdFactory.getId() == null || createdFactory.getId().isEmpty()) { final ServiceError error = dtoFactory.createDtoFromJson(payLoad, ServiceError.class); callback.onFailure(new Exception(error.getMessage())); } else { callback.onSuccess(createdFactory); } } else { final Response response = new ResponseImpl(request); callback.onFailure(new ServerException(response)); } } } }); if (!sendFormData(xhr, formData)) { callback.onFailure(new Exception("Could not call service")); } }
From source file:com.google.speedtracer.client.util.Xhr.java
License:Apache License
private static void request(XMLHttpRequest xhr, String method, String url, String requestData, String contentType, XhrCallback callback) { try {/* w w w . j a v a2 s . c o m*/ xhr.setOnReadyStateChange(new Handler(callback)); xhr.open(method, url); xhr.setRequestHeader("Content-type", contentType); xhr.send(requestData); } catch (Exception e) { // Just fail. callback.onFail(xhr); xhr.clearOnReadyStateChange(); } }
From source file:com.google.speedtracer.client.util.Xhr.java
License:Apache License
private static void request(XMLHttpRequest xhr, String method, String url, final XhrCallback callback) { try {//from w w w . j a v a 2 s. c o m xhr.setOnReadyStateChange(new Handler(callback)); xhr.open(method, url); xhr.send(); } catch (Exception e) { // Just fail. callback.onFail(xhr); xhr.clearOnReadyStateChange(); } }
From source file:com.gwtpro.html5.fileapi.client.upload.UploadRequest.java
License:Apache License
/** * Cancels a pending request. If the request has already been canceled or if * it has timed out no action is taken.// ww w.j a va 2 s. com */ public void cancel() { if (this.xmlHttpRequest != null) { XMLHttpRequest xmlHttp = this.xmlHttpRequest; this.xmlHttpRequest = null; xmlHttp.clearOnReadyStateChange(); xmlHttp.abort(); if (this.timer != null) { this.timer.cancel(); } } }
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.//www . j av a2 s . co m * * @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; }
From source file:elemental.js.util.Xhr.java
License:Apache License
private static void request(XMLHttpRequest xhr, String method, String url, String requestData, String contentType, Callback callback) { try {//from w w w .j av a 2 s .c o m xhr.setOnReadyStateChange(new Handler(callback)); xhr.open(method, url); xhr.setRequestHeader("Content-type", contentType); xhr.send(requestData); } catch (JavaScriptException e) { // Just fail. callback.onFail(xhr); xhr.clearOnReadyStateChange(); } }
From source file:elemental.js.util.Xhr.java
License:Apache License
private static void request(XMLHttpRequest xhr, String method, String url, final Callback callback) { try {/*from ww w .j a v a2 s. co m*/ xhr.setOnReadyStateChange(new Handler(callback)); xhr.open(method, url); xhr.send(); } catch (JavaScriptException e) { // Just fail. callback.onFail(xhr); xhr.clearOnReadyStateChange(); } }
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 {//from w w w .j a v a 2 s . 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())); } }
From source file:org.atmosphere.gwt.client.impl.HTTPRequestCometTransport.java
License:Apache License
@Override public void connect(int connectionCount) { init();/*from w w w . jav a2 s . c om*/ xmlHttpRequest = XMLHttpRequest.create(); try { xmlHttpRequest.open("GET", getUrl(connectionCount)); xmlHttpRequest.setRequestHeader("Accept", "application/comet"); xmlHttpRequest.setOnReadyStateChange(new ReadyStateChangeHandler() { @Override public void onReadyStateChange(XMLHttpRequest request) { if (!aborted) { switch (request.getReadyState()) { case XMLHttpRequest.LOADING: onReceiving(request.getStatus(), request.getResponseText()); if (needPolling()) { pollingTimer.scheduleRepeating(POLLING_INTERVAL); } break; case XMLHttpRequest.DONE: onLoaded(request.getStatus(), request.getResponseText()); pollingTimer.cancel(); break; } } else { request.clearOnReadyStateChange(); if (request.getReadyState() != XMLHttpRequest.DONE) { request.abort(); } } } }); xmlHttpRequest.send(); } catch (JavaScriptException e) { if (xmlHttpRequest != null) { xmlHttpRequest.abort(); xmlHttpRequest = null; } listener.onError(new RequestException(e.getMessage()), false); } }