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

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

Introduction

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

Prototype

RequestCallback

Source Link

Usage

From source file:com.google.gwt.examples.http.client.PostExample.java

public static void doPost(String url, String postData) {
    RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, url);

    try {// w  w w  .jav  a 2s .  c om
        builder.setHeader("Content-Type", "application/x-www-form-urlencoded");
        Request response = builder.sendRequest(postData, new RequestCallback() {

            public void onError(Request request, Throwable exception) {
                // code omitted for clarity
            }

            public void onResponseReceived(Request request, Response response) {
                // code omitted for clarity
            }
        });
    } catch (RequestException e) {
        Window.alert("Failed to send the request: " + e.getMessage());
    }
}

From source file:com.google.gwt.examples.http.client.TimeoutExample.java

public static void doGetWithTimeout(String url) {
    RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);

    try {//from  www .j a  va 2 s.c  om
        /*
         * wait 2000 milliseconds for the request to complete
         */
        builder.setTimeoutMillis(2000);

        Request response = builder.sendRequest(null, new RequestCallback() {
            public void onError(Request request, Throwable exception) {
                if (exception instanceof RequestTimeoutException) {
                    // handle a request timeout
                } else {
                    // handle other request errors
                }
            }

            public void onResponseReceived(Request request, Response response) {
                // code omitted for clarity
            }
        });
    } catch (RequestException e) {
        Window.alert("Failed to send the request: " + e.getMessage());
    }
}

From source file:com.google.gwt.sample.authrequest.client.SampleAuthRequestTransport.java

License:Apache License

@Override
protected RequestCallback createRequestCallback(final TransportReceiver receiver) {
    final RequestCallback superCallback = super.createRequestCallback(receiver);

    return new RequestCallback() {
        @Override//from w w  w  .ja v  a  2  s  .  c  om
        public void onError(Request request, Throwable exception) {
            superCallback.onError(request, exception);
        }

        @Override
        public void onResponseReceived(Request request, Response response) {
            /*
             * The GaeAuthFailure filter responds with Response.SC_UNAUTHORIZED and
             * adds a "login" url header if the user is not logged in. When we
             * receive that combo, post an event so that the app can handle things
             * as it sees fit.
             */
            if (Response.SC_UNAUTHORIZED == response.getStatusCode()) {
                String loginUrl = response.getHeader("login");
                if (loginUrl != null) {
                    /*
                     * Hand the receiver a non-fatal callback, so that
                     * com.google.web.bindery.requestfactory.shared.Receiver will not
                     * post a runtime exception.
                     */
                    receiver.onTransportFailure(new ServerFailure("Unauthenticated user", null, null, false));
                    eventBus.fireEvent(new SampleAuthenticationFailureEvent(loginUrl));
                    return;
                }
            }
            superCallback.onResponseReceived(request, response);
        }
    };
}

From source file:com.google.gwt.sample.client.mystockwatcherEntryPoint.java

/**
 * Generate random stock prices.//from   w  w w.  j a  va 2s  .  c o  m
 */
private void refreshWatchList() {
    if (stocks.size() == 0) {
        return;
    }

    String url = JSON_URL;

    // Append watch list stock symbols to query URL.
    Iterator iter = stocks.iterator();
    while (iter.hasNext()) {
        url += iter.next();
        if (iter.hasNext()) {
            url += "+";
        }
    }

    url = URL.encode(url);

    // Send request to server and catch any errors.
    RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);

    try {
        Request request = builder.sendRequest(null, new RequestCallback() {

            @Override
            public void onError(Request request, Throwable exception) {
                displayError("Couldn't retrieve JSON");
            }

            public void onResponseReceived(Request request, Response response) {
                if (200 == response.getStatusCode()) {
                    updateTable(asArrayOfStockData(response.getText()));
                } else {
                    displayError("Couldn't retrieve JSON (" + response.getStatusText() + ")");
                }
            }
        });
    } catch (RequestException e) {
        displayError("Couldn't retrieve JSON");
    }
}

From source file:com.google.gwt.sample.gaerequest.client.GaeAuthRequestTransport.java

License:Apache License

@Override
protected RequestCallback createRequestCallback(final TransportReceiver receiver) {
    final RequestCallback superCallback = super.createRequestCallback(receiver);

    return new RequestCallback() {
        @Override/*w ww.j a v  a 2  s  .  c om*/
        public void onError(Request request, Throwable exception) {
            superCallback.onError(request, exception);
        }

        @Override
        public void onResponseReceived(Request request, Response response) {
            /*
             * The GaeAuthFailure filter responds with Response.SC_UNAUTHORIZED and
             * adds a "login" url header if the user is not logged in. When we
             * receive that combo, post an event so that the app can handle things
             * as it sees fit.
             */
            if (Response.SC_UNAUTHORIZED == response.getStatusCode()) {
                String loginUrl = response.getHeader("login");
                if (loginUrl != null) {
                    /*
                     * Hand the receiver a non-fatal callback, so that
                     * com.google.web.bindery.requestfactory.shared.Receiver will not
                     * post a runtime exception.
                     */
                    receiver.onTransportFailure(new ServerFailure("Unauthenticated user", null, null, false));
                    eventBus.fireEvent(new GaeAuthenticationFailureEvent(loginUrl));
                    return;
                }
            }
            superCallback.onResponseReceived(request, response);
        }
    };
}

From source file:com.google.gwt.sample.healthyeatingapp.client.FacebookGraph.java

private void QueryGraph(String id, Method method, String params,
        final Callback<JSONObject, Throwable> callback) {
    final String requestData = "access_token=" + token + (params != null ? "&" + params : "");
    RequestBuilder builder;//from  www .  j av a  2 s  .  co m
    if (method == RequestBuilder.POST) {
        builder = new RequestBuilder(method, "https://graph.facebook.com/" + id);
        builder.setHeader("Content-Type", "application/x-www-form-urlencoded");
    } else if (method == RequestBuilder.GET) {
        builder = new RequestBuilder(method, "https://graph.facebook.com/" + id + "?" + requestData);
    } else {
        callback.onFailure(new IOException("doGraph only supports GET and POST requests"));
        return;
    }
    try {
        builder.sendRequest(requestData, new RequestCallback() {
            public void onError(Request request, Throwable exception) {
                callback.onFailure(exception);
            }

            public void onResponseReceived(Request request, Response response) {
                if (Response.SC_OK == response.getStatusCode()) {
                    callback.onSuccess(JSONParser.parseStrict(response.getText()).isObject());
                } else if (Response.SC_BAD_REQUEST == response.getStatusCode()) {
                    callback.onFailure(new IOException("Error: " + response.getText()));
                } else {
                    callback.onFailure(
                            new IOException("Couldn't retrieve JSON (" + response.getStatusText() + ")"));
                }
            }

        });
    } catch (RequestException e) {
        callback.onFailure(e);
    }
}

From source file:com.google.gwt.sample.showcase.client.ContentWidget.java

License:Apache License

/**
 * Get the source code for a raw file.//from   ww  w .  j a  v a  2s . c  o m
 * 
 * @param filename the filename to load
 * @param callback the callback to call when loaded
 */
public void getRawSource(final String filename, final Callback<String> callback) {
    if (rawSource.containsKey(filename)) {
        callback.onSuccess(rawSource.get(filename));
    } else {
        RequestCallback rc = new RequestCallback() {
            public void onError(Request request, Throwable exception) {
                callback.onError();
            }

            public void onResponseReceived(Request request, Response response) {
                String text = response.getText();
                rawSource.put(filename, text);
                callback.onSuccess(text);
            }
        };

        String className = this.getClass().getName();
        className = className.substring(className.lastIndexOf(".") + 1);
        sendSourceRequest(rc, ShowcaseConstants.DST_SOURCE_RAW + filename + ".html");
    }
}

From source file:com.google.gwt.sample.showcase.client.ContentWidget.java

License:Apache License

/**
 * Request the styles associated with the widget.
 * //w  w w . j av  a2 s.c o m
 * @param callback the callback used when the styles become available
 */
public void getStyle(final Callback<String> callback) {
    if (styleDefs != null) {
        callback.onSuccess(styleDefs);
    } else {
        RequestCallback rc = new RequestCallback() {
            public void onError(Request request, Throwable exception) {
                callback.onError();
            }

            public void onResponseReceived(Request request, Response response) {
                styleDefs = response.getText();
                callback.onSuccess(styleDefs);
            }
        };

        String srcPath = ShowcaseConstants.DST_SOURCE_STYLE + Showcase.THEME;
        if (LocaleInfo.getCurrentLocale().isRTL()) {
            srcPath += "_rtl";
        }
        String className = this.getClass().getName();
        className = className.substring(className.lastIndexOf(".") + 1);
        sendSourceRequest(rc, srcPath + "/" + className + ".html");
    }
}

From source file:com.google.gwt.sample.showcase.client.ContentWidget.java

License:Apache License

/**
 * Request the source code associated with the widget.
 * /*from ww w . j a v  a 2  s  .c  o m*/
 * @param callback the callback used when the source become available
 */
public void getSource(final Callback<String> callback) {
    if (sourceCode != null) {
        callback.onSuccess(sourceCode);
    } else {
        RequestCallback rc = new RequestCallback() {
            public void onError(Request request, Throwable exception) {
                callback.onError();
            }

            public void onResponseReceived(Request request, Response response) {
                sourceCode = response.getText();
                callback.onSuccess(sourceCode);
            }
        };

        String className = this.getClass().getName();
        className = className.substring(className.lastIndexOf(".") + 1);
        sendSourceRequest(rc, ShowcaseConstants.DST_SOURCE_EXAMPLE + className + ".html");
    }
}

From source file:com.google.gwt.sample.simplexml.client.SimpleXML.java

License:Apache License

public void onModuleLoad() {
    RequestBuilder requestBuilder = new RequestBuilder(RequestBuilder.GET, "customerRecord.xml");

    try {/* w  ww.j a  va2  s . c o  m*/
        requestBuilder.sendRequest(null, new RequestCallback() {
            public void onError(Request request, Throwable exception) {
                requestFailed(exception);
            }

            public void onResponseReceived(Request request, Response response) {
                renderXML(response.getText());
            }
        });
    } catch (RequestException ex) {
        requestFailed(ex);
    }
}