com.google.gwt.xhr.client.XMLHttpRequest Java Examples
The following examples show how to use
com.google.gwt.xhr.client.XMLHttpRequest.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UrlDownloaderTest.java From gdx-fireapp with Apache License 2.0 | 6 votes |
@Test public void onSuccess() { // Given String downloadUrl = "http://url.with.token.com?token=abc"; FuturePromise futurePromise = Mockito.spy(FuturePromise.class); UrlDownloader urlDownloader = new UrlDownloader(futurePromise); Mockito.when(xmlHttpRequest.getReadyState()).thenReturn(XMLHttpRequest.DONE); Mockito.when(xmlHttpRequest.getStatus()).thenReturn(200); // When urlDownloader.onSuccess(downloadUrl); // Then Mockito.verify(xmlHttpRequest, VerificationModeFactory.times(1)).open(Mockito.eq("GET"), Mockito.eq(downloadUrl)); Mockito.verify(xmlHttpRequest, VerificationModeFactory.times(1)).setRequestHeader(Mockito.eq("Authorization"), Mockito.eq("Bearer abc")); Mockito.verify(xmlHttpRequest, VerificationModeFactory.times(1)).send(); Mockito.verify(futurePromise, VerificationModeFactory.times(1)).doComplete(Mockito.any(byte[].class)); }
Example #2
Source File: UrlDownloaderTest.java From gdx-fireapp with Apache License 2.0 | 6 votes |
@Test public void onSuccess_fail() { // Given String downloadUrl = "http://url.with.token.com?token=abc"; FuturePromise futurePromise = Mockito.spy(FuturePromise.class); futurePromise.silentFail(); UrlDownloader urlDownloader = new UrlDownloader(futurePromise); Mockito.when(xmlHttpRequest.getReadyState()).thenReturn(XMLHttpRequest.DONE); Mockito.when(xmlHttpRequest.getStatus()).thenReturn(501); // When urlDownloader.onSuccess(downloadUrl); // Then Mockito.verify(futurePromise, VerificationModeFactory.times(1)).doFail(Mockito.any(Exception.class)); }
Example #3
Source File: Fluent.java From vertxui with GNU General Public License v3.0 | 6 votes |
/** * Load javascript files asynchronously and evalue/execute them directly too. As * browsers do not allow synchronous calls, you must also give a function that * is executed right afterwards. * * If you do not need js files right away in your startup sequence, add them * through Fluent.script(). * * If you do need your js file right away in your startup sequence, use * EntryPoint::getScripts() or Fluent.scriptSync(). * * * @param then what should happen after loading in the script * @param jss javascript file(s) * @return this */ public Fluent scriptSync(Consumer<Void> then, String... jss) { if (!GWT.isClient()) { return this; } for (String js : jss) { XMLHttpRequestSyc xhr = (XMLHttpRequestSyc) XMLHttpRequestSyc.create(); xhr.setOnReadyStateChange(a -> { if (a.getReadyState() == XMLHttpRequest.DONE && a.getStatus() == 200) { eval(xhr.getResponseText()); // new Fluent("script", this).inner(xhr.getResponseText()); // Element src = document.createElement("script"); // src.setAttribute("type", "text/javascript"); // // src.setAttribute("src", js); // src.setInnerText(xhr.getResponseText()); // element.appendChild(src); then.accept(null); } }); xhr.open("GET", js, true); xhr.send(); } return this; }
Example #4
Source File: Pojofy.java From vertxui with GNU General Public License v3.0 | 6 votes |
public static <I, O> void ajax(String protocol, String url, I model, ObjectMapper<I> inMapper, ObjectMapper<O> outMapper, BiConsumer<Integer, O> handler) { XMLHttpRequest xhr = XMLHttpRequest.create(); xhr.setOnReadyStateChange(a -> { if (handler == null || xhr.getReadyState() != 4) { return; } O result = null; if (xhr.getStatus() == 200) { result = out(xhr.getResponseText(), outMapper); } handler.accept(xhr.getStatus(), result); }); xhr.open(protocol, url); xhr.send(in(model, inMapper)); }
Example #5
Source File: Xhr.java From flow with Apache License 2.0 | 6 votes |
private static XMLHttpRequest request(XMLHttpRequest xhr, String method, String url, String requestData, String contentType, Callback callback) { try { xhr.setOnReadyStateChange(new Handler(callback)); xhr.open(method, url); xhr.setRequestHeader("Content-type", contentType); xhr.setWithCredentials(true); xhr.send(requestData); } catch (JavaScriptException e) { // Just fail. Console.error(e); callback.onFail(xhr, e); xhr.clearOnReadyStateChange(); } return xhr; }
Example #6
Source File: UrlDownloaderTest.java From gdx-fireapp with Apache License 2.0 | 6 votes |
@Before public void setUp() { PowerMockito.mockStatic(XMLHttpRequest.class); PowerMockito.mockStatic(TypedArrays.class); xmlHttpRequest = PowerMockito.mock(XMLHttpRequest.class); Mockito.when(XMLHttpRequest.create()).thenReturn(xmlHttpRequest); Mockito.doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) { ((ReadyStateChangeHandler) invocation.getArgument(0)).onReadyStateChange(xmlHttpRequest); return null; } }).when(xmlHttpRequest).setOnReadyStateChange(Mockito.any(ReadyStateChangeHandler.class)); Uint8Array uint8Array = Mockito.mock(Uint8Array.class); Mockito.when(xmlHttpRequest.getResponseArrayBuffer()).thenReturn(Mockito.mock(ArrayBuffer.class)); Mockito.when(uint8Array.length()).thenReturn(10); Mockito.when(TypedArrays.createUint8Array(Mockito.any(ArrayBuffer.class))).thenReturn(uint8Array); }
Example #7
Source File: DefaultConnectionStateHandler.java From flow with Apache License 2.0 | 6 votes |
@Override public void heartbeatInvalidStatusCode(XMLHttpRequest xhr) { int statusCode = xhr.getStatus(); Console.warn("Heartbeat request returned " + statusCode); if (statusCode == Response.SC_GONE) { // Session expired registry.getSystemErrorHandler().handleSessionExpiredError(null); stopApplication(); } else if (statusCode == Response.SC_NOT_FOUND) { // UI closed, do nothing as the UI will react to this // Should not trigger reconnect dialog as this will prevent user // input } else { handleRecoverableError(Type.HEARTBEAT, null); } }
Example #8
Source File: XhrConnection.java From flow with Apache License 2.0 | 6 votes |
@Override public void onSuccess(XMLHttpRequest xhr) { Console.log("Server visit took " + Profiler.getRelativeTimeString(requestStartTime) + "ms"); // for(;;);["+ realJson +"]" String responseText = xhr.getResponseText(); ValueMap json = MessageHandler.parseWrappedJson(responseText); if (json == null) { // Invalid string (not wrapped as expected or can't parse) registry.getConnectionStateHandler().xhrInvalidContent( new XhrConnectionError(xhr, payload, null)); return; } registry.getConnectionStateHandler().xhrOk(); Console.log("Received xhr message: " + responseText); registry.getMessageHandler().handleMessage(json); }
Example #9
Source File: XhrConnection.java From flow with Apache License 2.0 | 6 votes |
/** * Sends an asynchronous UIDL request to the server using the given URI. * * @param payload * The URI to use for the request. May includes GET parameters */ public void send(JsonObject payload) { XhrResponseHandler responseHandler = createResponseHandler(); responseHandler.setPayload(payload); responseHandler.setRequestStartTime(Profiler.getRelativeTimeMillis()); String payloadJson = WidgetUtil.stringify(payload); XMLHttpRequest xhr = Xhr.post(getUri(), payloadJson, JsonConstants.JSON_CONTENT_TYPE, responseHandler); Console.log("Sending xhr message to server: " + payloadJson); if (webkitMaybeIgnoringRequests && BrowserInfo.get().isWebkit()) { final int retryTimeout = 250; new Timer() { @Override public void run() { // Use native js to access private field in Request if (resendRequest(xhr) && webkitMaybeIgnoringRequests) { // Schedule retry if still needed schedule(retryTimeout); } } }.schedule(retryTimeout); } }
Example #10
Source File: DefaultConnectionStateHandler.java From flow with Apache License 2.0 | 5 votes |
private void handleUnrecoverableCommunicationError(String details, XhrConnectionError xhrConnectionError) { int statusCode = -1; if (xhrConnectionError != null) { XMLHttpRequest xhr = xhrConnectionError.getXhr(); if (xhr != null) { statusCode = xhr.getStatus(); } } handleCommunicationError(details, statusCode); stopApplication(); }
Example #11
Source File: ImageCanvasTest.java From TGAReader with MIT License | 5 votes |
private void addTGACanvas(String url) { XMLHttpRequest request = XMLHttpRequest.create(); request.open("GET", url); request.setResponseType(ResponseType.ArrayBuffer); request.setOnReadyStateChange(new ReadyStateChangeHandler() { @Override public void onReadyStateChange(XMLHttpRequest xhr) { if(xhr.getReadyState() == XMLHttpRequest.DONE) { if(xhr.getStatus() >= 400) { // error System.out.println("Error"); } else { try { ArrayBuffer arrayBuffer = xhr.getResponseArrayBuffer(); Uint8ArrayNative u8array = Uint8ArrayNative.create(arrayBuffer); byte [] buffer = new byte[u8array.length()]; for(int i=0; i<buffer.length; i++) { buffer[i] = (byte)u8array.get(i); } int pixels [] = TGAReader.read(buffer, TGAReader.ABGR); int width = TGAReader.getWidth(buffer); int height = TGAReader.getHeight(buffer); Canvas canvas = createImageCanvas(pixels, width, height); panel.add(canvas); } catch(Exception e) { e.printStackTrace(); } } } } }); request.send(); }
Example #12
Source File: PageDecoder.java From djvu-html5 with GNU General Public License v2.0 | 5 votes |
private void downloadFile(final String url) { XMLHttpRequest request = XMLHttpRequest.create(); request.open("GET", url); request.setResponseType(ResponseType.ArrayBuffer); request.setOnReadyStateChange(new ReadyStateChangeHandler() { @Override public void onReadyStateChange(XMLHttpRequest xhr) { if (xhr.getReadyState() == XMLHttpRequest.DONE) { downloadsInProgress--; if (xhr.getStatus() == 200) { FileItem entry = getCachedFile(url); entry.data = TypedArrays.createUint8Array(xhr.getResponseArrayBuffer()); entry.dataSize = entry.data.byteLength(); filesMemoryUsage += entry.dataSize; checkFilesMemory(); context.startProcessing(); fireReady(url); continueDownload(); } else { GWT.log("Error downloading " + url); GWT.log("response status: " + xhr.getStatus() + " " + xhr.getStatusText()); context.setStatus(ProcessingContext.STATUS_ERROR); fileCache.get(url).downloadStarted = false; } } } }); request.send(); fileCache.get(url).downloadStarted = true; downloadsInProgress++; }
Example #13
Source File: Xhr.java From flow with Apache License 2.0 | 5 votes |
private static XMLHttpRequest request(XMLHttpRequest xhr, String method, String url, final Callback callback) { try { xhr.setOnReadyStateChange(new Handler(callback)); xhr.open(method, url); xhr.send(); } catch (JavaScriptException e) { // Just fail. Console.error(e); callback.onFail(xhr, e); xhr.clearOnReadyStateChange(); } return xhr; }
Example #14
Source File: Xhr.java From flow with Apache License 2.0 | 5 votes |
@Override public void onReadyStateChange(XMLHttpRequest xhr) { if (xhr.getReadyState() == XMLHttpRequest.DONE) { if (xhr.getStatus() == 200) { callback.onSuccess(xhr); xhr.clearOnReadyStateChange(); return; } callback.onFail(xhr, null); xhr.clearOnReadyStateChange(); } }
Example #15
Source File: XhrConnection.java From flow with Apache License 2.0 | 5 votes |
@Override public void onFail(XMLHttpRequest xhr, Exception e) { XhrConnectionError errorEvent = new XhrConnectionError(xhr, payload, e); if (e == null) { // Response other than 200 registry.getConnectionStateHandler() .xhrInvalidStatusCode(errorEvent); return; } else { registry.getConnectionStateHandler().xhrException(errorEvent); } }
Example #16
Source File: XhrConnection.java From flow with Apache License 2.0 | 5 votes |
private static native boolean resendRequest(XMLHttpRequest xhr) /*-{ if (xhr.readyState != 1) { // Progressed to some other readyState -> no longer blocked return false; } try { xhr.send(); return true; } catch (e) { // send throws exception if it is running for real return false; } }-*/;
Example #17
Source File: DragAndDropUploader.java From document-management-system with GNU General Public License v2.0 | 5 votes |
/** * Cancel task. */ public void cancelTask(boolean cancel) { this.cancelled = cancel; if (cancel) { ((XMLHttpRequest) currentElement).abort(); } }
Example #18
Source File: Client.java From vertxui with GNU General Public License v3.0 | 5 votes |
private void clicked(Event e) { button.setAttribute("disabled", ""); thinking.getStyle().setProperty("display", ""); XMLHttpRequest xhr = XMLHttpRequest.create(); xhr.setOnReadyStateChange(a -> { if (xhr.getReadyState() == 4 && xhr.getStatus() == 200) { responsed(xhr.getResponseText()); } }); xhr.open("POST", url); xhr.send(); }
Example #19
Source File: DefaultConnectionStateHandler.java From flow with Apache License 2.0 | 4 votes |
@Override public void heartbeatException(XMLHttpRequest request, Exception exception) { Console.error("Heartbeat exception: " + exception.getMessage()); handleRecoverableError(Type.HEARTBEAT, null); }
Example #20
Source File: TexturedCube.java From TGAReader with MIT License | 4 votes |
void loadTexture(String url, int index) { final int i = index; XMLHttpRequest request = XMLHttpRequest.create(); request.open("GET", url); request.setResponseType(ResponseType.ArrayBuffer); request.setOnReadyStateChange(new ReadyStateChangeHandler() { @Override public void onReadyStateChange(XMLHttpRequest xhr) { if(xhr.getReadyState() == XMLHttpRequest.DONE) { if(xhr.getStatus() >= 400) { // error System.out.println("Error"); } else { try { ArrayBuffer arrayBuffer = xhr.getResponseArrayBuffer(); Uint8ArrayNative u8array = Uint8ArrayNative.create(arrayBuffer); byte [] buffer = new byte[u8array.length()]; for(int i=0; i<buffer.length; i++) { buffer[i] = (byte)u8array.get(i); } int [] pixels = TGAReader.read(buffer, TGAReader.ABGR); int width = TGAReader.getWidth(buffer); int height = TGAReader.getHeight(buffer); Canvas canvas = createImageCanvas(pixels, width, height); WebGLTexture texture = gl.createTexture(); gl.enable(TEXTURE_2D); gl.bindTexture(TEXTURE_2D, texture); gl.texImage2D(TEXTURE_2D, 0, RGBA, RGBA, UNSIGNED_BYTE, canvas.getElement()); gl.texParameteri(TEXTURE_2D, TEXTURE_WRAP_S, CLAMP_TO_EDGE); gl.texParameteri(TEXTURE_2D, TEXTURE_WRAP_T, CLAMP_TO_EDGE); gl.texParameteri(TEXTURE_2D, TEXTURE_MAG_FILTER, LINEAR); gl.texParameteri(TEXTURE_2D, TEXTURE_MIN_FILTER, LINEAR); textures[i] = texture; draw(); } catch(Exception e) { e.printStackTrace(); } } } } }); request.send(); }
Example #21
Source File: Xhr.java From flow with Apache License 2.0 | 4 votes |
private static XMLHttpRequest create() { return create(Browser.getWindow()); }
Example #22
Source File: XhrConnectionError.java From flow with Apache License 2.0 | 3 votes |
/** * Creates a XhrConnectionError for the given request using the given * payload. * * @param xhr * the request which caused the error * @param payload * the payload which was on its way to the server * @param exception * the exception which caused the error or null if the error was * not caused by an exception */ public XhrConnectionError(XMLHttpRequest xhr, JsonObject payload, Exception exception) { this.xhr = xhr; this.payload = payload; this.exception = exception; }
Example #23
Source File: RequestorRequest.java From requestor with Apache License 2.0 | 2 votes |
/** * Constructs an instance of the Request object. * * @param xmlHttpRequest JavaScript XmlHttpRequest object instance * @param timeoutMillis number of milliseconds to wait for a response * @param callback callback interface to use for notification * * @throws IllegalArgumentException if timeoutMillis < 0 * @throws NullPointerException if xmlHttpRequest, or callback are null */ public RequestorRequest(XMLHttpRequest xmlHttpRequest, int timeoutMillis, RequestCallback callback) { super(xmlHttpRequest, timeoutMillis, callback); }
Example #24
Source File: XhrConnectionError.java From flow with Apache License 2.0 | 2 votes |
/** * Returns {@link XMLHttpRequest} which failed to reach the server. * * @return the request which failed * */ public XMLHttpRequest getXhr() { return xhr; }
Example #25
Source File: Xhr.java From flow with Apache License 2.0 | 2 votes |
/** * Replacement for {@link XMLHttpRequest#create()} that allows better * control of which window object is used to access the XMLHttpRequest * constructor. */ private static native XMLHttpRequest create(Window window) /*-{ return new window.XMLHttpRequest(); }-*/;
Example #26
Source File: Xhr.java From flow with Apache License 2.0 | 2 votes |
/** * Send a POST request to the <code>url</code> and dispatch updates to the * <code>callback</code>. * * @param window * the window object used to access the XMLHttpRequest * constructor * @param url * the URL * @param requestData * the data to be passed to XMLHttpRequest.send * @param contentType * a value for the Content-Type HTTP header * @param callback * the callback to notify * @return a reference to the sent XmlHttpRequest */ public static XMLHttpRequest post(Window window, String url, String requestData, String contentType, Callback callback) { return request(create(window), "POST", url, requestData, contentType, callback); }
Example #27
Source File: Xhr.java From flow with Apache License 2.0 | 2 votes |
/** * Send a POST request to the <code>url</code> and dispatch updates to the * <code>callback</code>. * * @param url * the URL * @param requestData * the data to be passed to XMLHttpRequest.send * @param contentType * a value for the Content-Type HTTP header * @param callback * the callback to notify * @return a reference to the sent XmlHttpRequest */ public static XMLHttpRequest post(String url, String requestData, String contentType, Callback callback) { return request(create(), "POST", url, requestData, contentType, callback); }
Example #28
Source File: Xhr.java From flow with Apache License 2.0 | 2 votes |
/** * Send a HEAD request to the <code>url</code> and dispatch updates to the * <code>callback</code>. * * @param window * the window object used to access the XMLHttpRequest * constructor * @param url * the URL * @param callback * the callback to be notified * @return a reference to the sent XmlHttpRequest */ public static XMLHttpRequest head(Window window, String url, Callback callback) { return request(create(window), "HEAD", url, callback); }
Example #29
Source File: Xhr.java From flow with Apache License 2.0 | 2 votes |
/** * Send a HEAD request to the <code>url</code> and dispatch updates to the * <code>callback</code>. * * @param url * the URL * @param callback * the callback to be notified * @return a reference to the sent XmlHttpRequest */ public static XMLHttpRequest head(String url, Callback callback) { return request(create(), "HEAD", url, callback); }
Example #30
Source File: Xhr.java From flow with Apache License 2.0 | 2 votes |
/** * Send a GET request to the <code>url</code> and dispatch updates to the * <code>callback</code>. * * @param window * the window object used to access the XMLHttpRequest * constructor * @param url * the URL * @param callback * the callback to be notified * @return a reference to the sent XmlHttpRequest */ public static XMLHttpRequest get(Window window, String url, Callback callback) { return request(create(window), "GET", url, callback); }