Example usage for com.google.gwt.http.client RequestCallback onError

List of usage examples for com.google.gwt.http.client RequestCallback onError

Introduction

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

Prototype

void onError(Request request, Throwable exception);

Source Link

Document

Called when a com.google.gwt.http.client.Request does not complete normally.

Usage

From source file:es.deusto.weblab.client.comm.FakeWebLabRequestBuilder.java

License:Open Source License

@Override
public Request sendRequest(String header, RequestCallback callback) throws RequestException {
    if (this.nextAnswer) {
        callback.onResponseReceived(null, this.getResponseToSend());
    } else if (this.nextThrow) {
        throw this.toThrow;
    } else if (this.nextError) {
        callback.onError(null, this.toError);
    }// w w  w . java 2 s  . c o  m
    return null;
}

From source file:fi.jasoft.uidlcompressor.client.ui.UIDLCompressorApplicationConnection.java

License:Apache License

@Override
protected void doAsyncUIDLRequest(String uri, String payload, final RequestCallback requestCallback)
        throws RequestException {

    // Wrap the request callback
    RequestCallback wrappedCallback = new RequestCallback() {
        public void onResponseReceived(Request request, final Response response) {

            Response jsonResponse = new Response() {

                /*/*from   w  ww .ja v  a2  s. c o m*/
                 * Caching the decoded json string in case getText() is
                 * called several times
                 */
                private String decodedJson;

                @Override
                public String getText() {
                    String base64 = response.getText();
                    if (base64.startsWith("for(")) {
                        // Server is sending json, digress
                        return base64;
                    }

                    if (decodedJson == null) {
                        long start = new Date().getTime();
                        decodedJson = decompressBase64Gzip(base64);
                        long end = new Date().getTime();
                        VConsole.log("Decoding JSON took " + (end - start) + "ms");
                    }

                    return decodedJson;
                }

                @Override
                public String getStatusText() {
                    return response.getStatusText();
                }

                @Override
                public int getStatusCode() {
                    return response.getStatusCode();
                }

                @Override
                public String getHeadersAsString() {
                    return response.getHeadersAsString();
                }

                @Override
                public Header[] getHeaders() {
                    return response.getHeaders();
                }

                @Override
                public String getHeader(String header) {
                    return response.getHeader(header);
                }
            };

            requestCallback.onResponseReceived(request, jsonResponse);
        }

        public void onError(Request request, Throwable exception) {
            requestCallback.onError(request, exception);
        }
    };

    // TODO Add payload compression here
    super.doAsyncUIDLRequest(uri, payload, wrappedCallback);
}

From source file:fr.putnami.pwt.doc.client.page.sample.decorator.SampleDecorator.java

License:Open Source License

private void requestFile(final String fileName) {
    this.sourceCode.asWidget().setVisible(false);
    this.sourceCode.setText("");

    RequestCallback callBack = new RequestCallback() {

        @Override//from   w w  w .j  av  a  2 s.com
        public void onResponseReceived(Request request, Response response) {
            if (fileName.endsWith("xml")) {
                SampleDecorator.this.sourceCode.setConfiguration(XmlConfiguration.XML_CONFIGURATION);
            } else if (fileName.endsWith("java")) {
                SampleDecorator.this.sourceCode.setConfiguration(JavaConfiguration.JAVA_CONFIGURATION);
            } else {
                SampleDecorator.this.displayError(new RuntimeException("Unknow file type"));
            }
            SampleDecorator.this.sourceCode.setText(response.getText());
            SampleDecorator.this.sourceCode.asWidget().setVisible(true);
        }

        @Override
        public void onError(Request request, Throwable exception) {
            SampleDecorator.this.displayError(exception);
        }
    };
    RequestBuilder builder = new RequestBuilder(RequestBuilder.GET,
            GWT.getModuleBaseURL() + "sample/" + fileName);
    builder.setCallback(callBack);
    try {
        builder.send();
    } catch (RequestException e) {
        callBack.onError(null, e);
    }
}

From source file:fr.putnami.pwt.doc.client.page.sample.SamplePage.java

License:Open Source License

private void requestFile(final String fileName) {
    this.samplePageLayout.sourceCode.asWidget().setVisible(false);
    this.samplePageLayout.sourceCode.setText("");

    RequestCallback callBack = new RequestCallback() {

        @Override//ww w . j  a  v  a  2  s.c om
        public void onResponseReceived(Request request, Response response) {
            if (fileName.endsWith("xml")) {
                SamplePage.this.samplePageLayout.sourceCode
                        .setConfiguration(XmlConfiguration.XML_CONFIGURATION);
            } else if (fileName.endsWith("java")) {
                SamplePage.this.samplePageLayout.sourceCode
                        .setConfiguration(JavaConfiguration.JAVA_CONFIGURATION);
            } else {
                SamplePage.this.displayError(new RuntimeException("Unknow file type"));
            }
            SamplePage.this.samplePageLayout.sourceCode.setText(response.getText());
            SamplePage.this.samplePageLayout.sourceCode.asWidget().setVisible(true);
        }

        @Override
        public void onError(Request request, Throwable exception) {
            SamplePage.this.displayError(exception);
        }
    };
    RequestBuilder builder = new RequestBuilder(RequestBuilder.GET,
            GWT.getModuleBaseURL() + "sample/" + fileName);
    builder.setCallback(callBack);
    try {
        builder.send();
    } catch (RequestException e) {
        callBack.onError(null, e);
    }
}

From source file:fr.putnami.pwt.doc.client.page.sample.SampleView.java

License:Open Source License

private void requestFile(final String fileName) {
    samplePageLayout.sourceCode.asWidget().setVisible(false);
    samplePageLayout.sourceCode.setText("");

    RequestCallback callBack = new RequestCallback() {

        @Override// w w w.  j  a v a2 s  . c o m
        public void onResponseReceived(Request request, Response response) {
            if (fileName.endsWith("xml")) {
                samplePageLayout.sourceCode.setConfiguration(XmlConfiguration.XML_CONFIGURATION);
            } else if (fileName.endsWith("java")) {
                samplePageLayout.sourceCode.setConfiguration(JavaConfiguration.JAVA_CONFIGURATION);
            } else {
                displayError(new RuntimeException("Unknow file type"));
            }
            samplePageLayout.sourceCode.setText(response.getText());
            samplePageLayout.sourceCode.asWidget().setVisible(true);
        }

        @Override
        public void onError(Request request, Throwable exception) {
            displayError(exception);
        }
    };
    RequestBuilder builder = new RequestBuilder(RequestBuilder.GET,
            GWT.getModuleBaseURL() + "sample/" + fileName);
    builder.setCallback(callBack);
    try {
        builder.send();
    } catch (RequestException e) {
        callBack.onError(null, e);
    }
}

From source file:gwt.dojo.showcase.client.Showcase.java

License:Apache License

private void loadAndSwitchView(final ListItem listItem) {

    final RequestCallback requestCallback = new RequestCallback() {
        @Override// w  w  w  . j  a  v  a 2s.com
        public void onResponseReceived(Request request, Response response) {
            if (200 == response.getStatusCode()) {
                try {
                    // Process the response in response.getText()

                    // fillInDemoSource();
                    DivElement rightPane = Document.get().getElementById("rightPane").cast();
                    DivElement tmpContainer = Document.get().createDivElement();
                    tmpContainer.setInnerHTML(response.getText());
                    rightPane.appendChild(tmpContainer);
                    JsArray ws = MobileParser.parse(tmpContainer);
                    for (int i = 0, n = ws.length(); i < n; i++) {
                        if (ws.getJsObject(i).hasProperty("startup")) {
                            _WidgetBase w = ws.getJsObject(i);
                            w.startup();
                        }
                    }

                    // reparent
                    rightPane.removeChild(tmpContainer);
                    NodeList<Node> children = tmpContainer.getChildNodes();
                    for (int i = 0, n = children.getLength(); i < n; i++) {
                        Element elem = tmpContainer.getChild(i).cast();
                        rightPane.appendChild(elem);
                    }

                    showProgressIndicator(false);
                    Scheduler.get().scheduleDeferred(new ScheduledCommand() {
                        @Override
                        public void execute() {
                            initView(listItem);
                            listItem.transitionTo(listItem.getString("viewId"));
                            // triggreTransition(listItem,
                            // listItem.getString("id"));
                        }
                    });
                } catch (Exception e) {
                    Window.alert("Error: " + e);
                }
            } else {
                // Handle the error. Can get the status text from
                // response.getStatusText()
                onError(request, new RequestException("HTTP Error: " + response.getStatusCode()));
            }
        }

        @Override
        public void onError(Request request, Throwable exception) {
            Window.alert("Failed to load demo.");
            showProgressIndicator(false);
            inTransitionOrLoading = false;
        }
    };

    showProgressIndicator(true);

    String url = listItem.getString("demourl");
    RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, URL.encode(url));

    Request request = null;
    try {
        request = builder.sendRequest(null, requestCallback);
    } catch (RequestException e) {
        requestCallback.onError(request, e);
    }
}

From source file:n3phele.client.CacheManager.java

License:Open Source License

public void test(String username, String password, RequestCallback requestCallback) {

    // Send request to server and catch any errors.
    RequestBuilder builder = AuthenticatedRequestFactory.request(RequestBuilder.GET,
            ServiceAddress + "user/byName?id=" + URL.encodeQueryString(username), username, password);
    GWT.log("Sending authentication test request");
    Request request = null;/*from   www.ja v a 2  s . co  m*/
    try {
        request = builder.sendRequest(null, requestCallback);
    } catch (RequestException e) {
        requestCallback.onError(request, e);
    }

}

From source file:n3phele.client.presenter.helpers.RequestWithProgressDialog.java

License:Open Source License

public Request sendRequest(final String requestData, final RequestCallback callback) throws RequestException {
    request = rb.sendRequest(requestData, new RequestCallback() {
        @Override/*from w w  w.  j a va  2 s .  c  o m*/
        public void onResponseReceived(Request request, Response response) {
            hideLoadingMessage();
            callback.onResponseReceived(request, response);

        }

        @Override
        public void onError(Request request, Throwable exception) {
            GWT.log(exception.toString(), exception);
            if (retries-- <= 0) {
                alert("Failure " + exception.toString());
                callback.onError(request, exception);
            } else {
                try {
                    alert("Failure " + exception.toString() + " retrying..");
                    request = sendRequest(requestData, callback);
                } catch (RequestException e) {
                    this.onError(request, e);
                }
            }
        }
    });
    if (request != null)
        createLoadingMessage();
    return request;
}

From source file:net.ffxml.gwt.json.client.JsonRpc.java

License:Apache License

/**
 * Executes a json-rpc request.//from   w w  w .  j a v a  2  s  .  co m
 * 
 * @param url
 *            The location of the service
 * @param username
 *            The username for basic authentification
 * @param password
 *            The password for basic authentification
 * @param method
 *            The method name
 * @param params
 *            An array of objects containing the parameters
 * @param callback
 *            A callbackhandler like in gwt's rpc.
 */
public void request(final String url, String username, String password, final String method, Object[] params,
        final AsyncCallback callback) {

    HashMap request = new HashMap();
    request.put("method", method);
    if (params == null) {
        params = new Object[] {};
    }
    request.put("params", params);
    request.put("id", new Integer(requestSerial++));

    if (username == null)
        if (requestUser != null)
            username = requestUser;
    if (password == null)
        if (requestPassword != null)
            password = requestPassword;

    RequestCallback handler = new RequestCallback() {
        public void onResponseReceived(Request request, Response response) {
            try {
                String resp = response.getText();
                if (resp.equals(""))
                    throw new RuntimeException("empty");
                HashMap reply = (HashMap) decode(resp);

                if (reply == null) {
                    RuntimeException runtimeException = new RuntimeException("parse: " + response.getText());
                    fireFailure(runtimeException);
                    callback.onFailure(runtimeException);
                }

                if (isErrorResponse(reply)) {
                    RuntimeException runtimeException = new RuntimeException("error: " + reply.get("error"));
                    fireFailure(runtimeException);
                    callback.onFailure(runtimeException);
                } else if (isSuccessfulResponse(reply)) {
                    callback.onSuccess(reply.get("result"));
                } else {
                    RuntimeException runtimeException = new RuntimeException("syntax: " + response.getText());
                    fireFailure(runtimeException);
                    callback.onFailure(runtimeException);
                }
            } catch (RuntimeException e) {
                fireFailure(e);
                callback.onFailure(e);
            } finally {
                decreaseRequestCounter();
            }
        }

        public void onError(Request request, Throwable exception) {
            try {
                if (exception instanceof RequestTimeoutException) {
                    RuntimeException runtimeException = new RuntimeException("timeout");
                    fireFailure(runtimeException);
                    callback.onFailure(runtimeException);
                } else {
                    RuntimeException runtimeException = new RuntimeException("other");
                    fireFailure(runtimeException);
                    callback.onFailure(runtimeException);
                }
            } catch (RuntimeException e) {
                fireFailure(e);
                callback.onFailure(e);
            } finally {
                decreaseRequestCounter();
            }
        }

        private boolean isErrorResponse(HashMap response) {
            return response.get("error") != null && response.get("result") == null;
        }

        private boolean isSuccessfulResponse(HashMap response) {
            return response.get("error") == null && response.containsKey("result");
        }
    };

    increaseRequestCounter();

    RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, url);
    if (requestTimeout > 0)
        builder.setTimeoutMillis(requestTimeout);
    builder.setHeader("Content-Type", "application/json; charset=utf-8");
    String body = new String(encode(request));
    builder.setHeader("Content-Length", Integer.toString(body.length()));
    if (requestCookie != null)
        if (Cookies.getCookie(requestCookie) != null)
            builder.setHeader("X-Cookie", Cookies.getCookie(requestCookie));
    if (username != null)
        builder.setUser(username);
    if (password != null)
        builder.setPassword(password);
    try {
        builder.sendRequest(body, handler);
    } catch (RequestException exception) {
        handler.onError(null, exception);
    }
}

From source file:net.s17fabu.vip.gwt.showcase.client.ContentWidget.java

License:Apache License

/**
 * Load the contents of a remote file into the specified widget.
 * //from   w w  w.  j  av  a  2 s .c  o m
 * @param url a partial path relative to the module base URL
 * @param target the target Widget to place the contents
 * @param callback the callback when the call completes
 */
protected void requestSourceContents(String url, final HTML target, final RequestCallback callback) {
    // Show the loading image
    if (loadingImage == null) {
        loadingImage = "<img src=\"" + GWT.getModuleBaseURL() + "images/loading.gif\">";
    }
    target.setDirection(HasDirection.Direction.LTR);
    DOM.setStyleAttribute(target.getElement(), "textAlign", "left");
    target.setHTML("&nbsp;&nbsp;" + loadingImage);

    // Request the contents of the file
    RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, GWT.getModuleBaseURL() + url);
    RequestCallback realCallback = new RequestCallback() {
        public void onError(Request request, Throwable exception) {
            target.setHTML("Cannot find resource");
            if (callback != null) {
                callback.onError(request, exception);
            }
        }

        public void onResponseReceived(Request request, Response response) {
            target.setHTML(response.getText());
            if (callback != null) {
                callback.onResponseReceived(request, response);
            }
        }
    };
    builder.setCallback(realCallback);

    // Send the request
    Request request = null;
    try {
        request = builder.send();
    } catch (RequestException e) {
        realCallback.onError(request, e);
    }
}