Example usage for com.google.gwt.jsonp.client JsonpRequestBuilder getTimeout

List of usage examples for com.google.gwt.jsonp.client JsonpRequestBuilder getTimeout

Introduction

In this page you can find the example usage for com.google.gwt.jsonp.client JsonpRequestBuilder getTimeout.

Prototype

public int getTimeout() 

Source Link

Document

Returns the expected timeout (ms) for this request.

Usage

From source file:org.thechiselgroup.biomixer.client.workbench.util.url.JsonpUrlFetchService.java

License:Apache License

/**
 * This is primarily used by the {@link RetryAsyncCallbackErrorHandler} that
 * this uses internally.//from ww  w  .j  a v a2  s . com
 * 
 * @param url
 * @param callback
 * @param previousNumberTries
 */
public void fetchURL(final String url, final AsyncCallback<String> callback, int previousNumberTries) {
    JsonpRequestBuilder jsonp = new JsonpRequestBuilder();
    // Could change timeout, but probably better to change retry attempt
    // number...except that exacerbates server load. Maybe longer timeout is
    // ok.
    jsonp.setTimeout(jsonp.getTimeout() * 4);

    jsonp.requestObject(url, new ErrorHandlingAsyncCallback<JavaScriptObject>(
            new RetryAsyncCallbackErrorHandler(callback, url, previousNumberTries, this)) {

        @Override
        protected void runOnSuccess(JavaScriptObject result) throws Exception {
            // Had trouble with injection...explicitly creating
            // instead.
            ErrorCodeJSONParser errorCodeParser = new ErrorCodeJSONParser(new JsJsonParser());

            JSONObject jsonObject = new JSONObject(result);
            // This JSONObect method changes what appear to be
            // array entries to be string-integer indexed property
            // entries.
            // This happens when an array is presented without
            // explicit numeric indices.
            String jsonString = jsonObject.toString();

            // Need to check for understood errors in response, such
            // as 403 forbidden.

            Integer errorCode = errorCodeParser.parse(jsonString);

            if (null != errorCode && 500 == errorCode) {
                // 500 errors don't get here! Caught lower down?
                // We can retry this once, since I have already seen
                // cases of very singular failures here.
                boolean retryAttempted = ((RetryAsyncCallbackErrorHandler) callback).manualRetry();
                if (retryAttempted) {
                    return;
                }
                // else if (403 == errorCode) {
                // // This error code, forbidden, is something I
                // want
                // // to ignore at the moment.
                // return;
                // }
            } else { // if (null == errorCode) {
                callback.onSuccess(jsonString);
                return;
            }

            // This wasn't a success, and we got an error code
            // we don't understand.
            // Treat as an error for the callback.
            callback.onFailure(new Exception("Error code, status: " + errorCode + "."));
            throw new Exception("Status " + errorCode);

        }

    });

}