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:ca.upei.ic.timetable.client.Remote.java

License:Apache License

/**
 * Calls a remote method using HTTP GET//ww  w . jav a  2  s  . c om
 * 
 * @param method
 * @param params
 * @param callback
 */
public Request get(String method, Map<String, String> params, RequestCallback callback) {
    // build the query
    StringBuffer q = new StringBuffer();
    q.append("?method=" + method);

    if (null != params) {
        for (String key : params.keySet()) {
            q.append("&" + key + "=" + params.get(key));
        }
    }

    // build the request builder
    RequestBuilder req = new RequestBuilder(RequestBuilder.GET, url_ + q);

    req.setRequestData("");
    req.setCallback(callback);
    Request request = null;
    try {
        request = req.send();
    } catch (RequestException re) {
        callback.onError(request, re);
    }
    return request;
}

From source file:ca.upei.ic.timetable.client.Remote.java

License:Apache License

/**
 * Calls a remote method using HTTP POST
 * /*ww w  .ja  v a 2 s. com*/
 * @param method
 * @param contentType
 * @param data
 * @param callback
 */
public Request post(String method, String contentType, String data, RequestCallback callback) {
    StringBuffer q = new StringBuffer();
    q.append("?method=" + method);

    RequestBuilder req = new RequestBuilder(RequestBuilder.POST, url_ + q);

    req.setHeader("Content-type", contentType);
    req.setRequestData(data);
    req.setCallback(callback);
    Request request = null;
    try {
        request = req.send();
    } catch (RequestException re) {
        callback.onError(request, re);
    }
    return request;
}

From source file:com.agnie.gwt.common.client.rpc.LoaderRequestTransport.java

License:Open Source License

protected RequestCallback createRequestCallback(final TransportReceiver receiver) {
    final RequestCallback callback = super.createRequestCallback(receiver);
    RequestCallback newCallback = new RequestCallback() {

        @Override//  www .  j a v a2 s  . c om
        public void onResponseReceived(Request request, Response response) {
            if (Response.SC_UNAUTHORIZED == response.getStatusCode()) {
                GWT.log("User session timed out or user logged out");
                Window.Location.assign(urlGenerator.getClientSideLoginURL(urlInfo, appDomain,
                        urlInfo.getParameter(QueryString.GWT_DEV_MODE.getKey())));
            } else {
                callback.onResponseReceived(request, response);
            }
        }

        @Override
        public void onError(Request request, Throwable exception) {
            callback.onError(request, exception);

        }
    };
    return newCallback;
}

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/* w ww  . 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.gaerequest.client.GaeAuthRequestTransport.java

License:Apache License

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

    return new RequestCallback() {
        @Override/*from   ww w .j  av a2s .  c o m*/
        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.showcase.client.ContentWidget.java

License:Apache License

/**
 * Send a request for source code.//from w  w w .ja  v  a2 s.  c  o  m
 * 
 * @param callback the {@link RequestCallback} to send
 * @param url the URL to target
 */
private void sendSourceRequest(RequestCallback callback, String url) {
    RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, GWT.getModuleBaseURL() + url);
    builder.setCallback(callback);
    try {
        builder.send();
    } catch (RequestException e) {
        callback.onError(null, e);
    }
}

From source file:com.nanosim.client.ContentWidget.java

License:Apache License

/**
 * Load the contents of a remote file into the specified widget.
 * //w  w w . j  a va2s . 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\">";
    }
    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);
    }
}

From source file:com.totsp.gwittir.rest.client.transports.HTTPTransport.java

License:Open Source License

/** Executes a request and handles the pre and post hooks
 *
 * @param builder the initial builder//from  ww  w .  ja  v  a2 s  .  c  om
 * @param callback the request callback to pass to the final builder
 * @return a RequestControl implementation to return back out.
 */
protected XHRRequestControl doRequest(RequestBuilder builder, final RequestCallback callback) {
    doPreHook(builder);

    RequestCallback innerCallback = new RequestCallback() {
        public void onResponseReceived(Request request, Response response) {
            response = doPostHook(response);
            callback.onResponseReceived(request, response);
        }

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

    builder.setCallback(innerCallback);

    try {
        return new XHRRequestControl(builder.send());
    } catch (RequestException e) {
        callback.onError(null, e);

        return new XHRRequestControl(null);
    }
}

From source file:com.vaadin.client.communication.Heartbeat.java

License:Apache License

/**
 * Sends a heartbeat to the server/*w ww  .j  a v  a  2s. co m*/
 */
public void send() {
    timer.cancel();

    final RequestBuilder rb = new RequestBuilder(RequestBuilder.POST, uri);

    final RequestCallback callback = new RequestCallback() {

        @Override
        public void onResponseReceived(Request request, Response response) {
            int status = response.getStatusCode();

            if (status == Response.SC_OK) {
                connection.getConnectionStateHandler().heartbeatOk();
            } else {
                // Handler should stop the application if heartbeat should
                // no longer be sent
                connection.getConnectionStateHandler().heartbeatInvalidStatusCode(request, response);
            }

            schedule();
        }

        @Override
        public void onError(Request request, Throwable exception) {
            // Handler should stop the application if heartbeat should no
            // longer be sent
            connection.getConnectionStateHandler().heartbeatException(request, exception);
            schedule();
        }
    };

    rb.setCallback(callback);

    try {
        getLogger().fine("Sending heartbeat request...");
        rb.send();
    } catch (RequestException re) {
        callback.onError(null, re);
    }

}

From source file:ecc.gwt.warning.client.JsonRpc.java

License:Apache License

/**
 * Executes a json-rpc request.//  w w w . j a v  a  2 s.  c  om
 * 
 * @param url
 *            The location of the service
 * @param username
 *            The username for basic authentification
 * @param password
 *            The password for basic authentification
 * @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, Map paramMap, boolean isStream,
        final AsyncCallback callback) {

    HashMap request = new HashMap();
    request.putAll(paramMap);
    //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);

    String body = "";
    if (isStream) {
        builder.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
        body = new String(encode(request));
    } else {
        builder.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
        StringBuffer sb = new StringBuffer();
        for (Iterator iterator = request.entrySet().iterator(); iterator.hasNext();) {
            Map.Entry<String, Object> entry = (Map.Entry<String, Object>) iterator.next();
            sb.append(entry.getKey()).append("=").append(String.valueOf(entry.getValue())).append("&");

        }
        body = sb.toString();
        if (body.endsWith("&"))
            body = body.substring(0, body.length() - 1);
    }
    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);
    }
}