@Override public void run() { onFailure(new TimeoutException("Timeout while calling " + baseUri)); } };
private static String nextCallbackId() { return INCREMENTAL_ID_PREFIX + getAndIncrementCallbackCounter(); }
/** * Create a new JSONP request. * * @param callback The callback instance to notify when the response comes * back * @param timeout Time in ms after which a {@link TimeoutException} will be * thrown * @param expectInteger Should be true if T is {@link Integer}, false * otherwise * @param callbackParam Name of the url param of the callback function name * @param failureCallbackParam Name of the url param containing the * failure callback function name, or null for no failure callback */ JsonpRequest(AsyncCallback<T> callback, int timeout, boolean expectInteger, String callbackParam, String failureCallbackParam) { callbackId = nextCallbackId(); this.callback = callback; this.timeout = timeout; this.expectInteger = expectInteger; this.callbackParam = callbackParam; this.failureCallbackParam = failureCallbackParam; this.canHaveMultipleRequestsForSameId = false; }
/** * Possibly fire off an HTTPRequest for the text resource. */ @SuppressIsTrustedResourceUriCastCheck public void getText(ResourceCallback<TextResource> callback) throws ResourceException { // If we've already parsed the JSON bundle, short-circuit. if (cache[index] != null) { callback.onSuccess(cache[index]); return; } if (md5Hash != null) { // If we have an md5Hash, we should be using JSONP JsonpRequestBuilder rb = new JsonpRequestBuilder(); rb.setPredeterminedId(md5Hash); rb.requestObject(url.asString(), new ETRCallback(callback)); } else { RequestBuilder rb = new RequestBuilder(RequestBuilder.GET, url.asString()); try { rb.sendRequest("", new ETRCallback(callback)); } catch (RequestException e) { throw new ResourceException(this, "Unable to initiate request for external resource", e); } } } }
/** * Sends a JSONP request and expects a JavaScript object as a result. The caller can either use * {@link com.google.gwt.json.client.JSONObject} to parse it, or use a JavaScript overlay class. */ public <T extends JavaScriptObject> JsonpRequest<T> requestObject( @IsTrustedResourceUri String url, AsyncCallback<T> callback) { return send(url, callback, false); }
private <T> JsonpRequest<T> send( @IsTrustedResourceUri String url, AsyncCallback<T> callback, boolean expectInteger) { JsonpRequest<T> request; if (predeterminedId != null) { request = new JsonpRequest<T>(callback, timeout, expectInteger, callbackParam, failureCallbackParam, predeterminedId); } else { request = new JsonpRequest<T>(callback, timeout, expectInteger, callbackParam, failureCallbackParam); } request.send(url); return request; } }
registerCallbacks(CALLBACKS, canHaveMultipleRequestsForSameId); StringBuilder uri = new StringBuilder(baseUri); uri.append(baseUri.contains("?") ? "&" : "?"); getHeadElement().appendChild(script);
/** * Cancels a pending request. Note that if you are using preset ID's, this * will not work, since there is no way of knowing if there are other * requests pending (or have already returned) for the same data. */ public void cancel() { timer.cancel(); unload(); }
/** * Create a new JSONP request with a hardcoded id. This could be used to * manually control which resources are considered duplicates (by giving them * identical ids). Could also be used if the callback name needs to be * completely user controlled (since the id is part of the callback name). * * @param callback The callback instance to notify when the response comes * back * @param timeout Time in ms after which a {@link TimeoutException} will be * thrown * @param expectInteger Should be true if T is {@link Integer}, false * otherwise * @param callbackParam Name of the url param of the callback function name * @param failureCallbackParam Name of the url param containing the * failure callback function name, or null for no failure callback * @param id unique id for the resource that is being fetched */ JsonpRequest(AsyncCallback<T> callback, int timeout, boolean expectInteger, String callbackParam, String failureCallbackParam, String id) { callbackId = getPredeterminedId(id); this.callback = callback; this.timeout = timeout; this.expectInteger = expectInteger; this.callbackParam = callbackParam; this.failureCallbackParam = failureCallbackParam; this.canHaveMultipleRequestsForSameId = true; }
private void onFailure(String message) { onFailure(new Exception(message)); }
@Override public Method timeout(int timeout) { jsonpBuilder.setTimeout(timeout); return this; }
/** * Sends a JSONP request and does not expect any results. */ public void send(@IsTrustedResourceUri String url) { send(url, null, false); }
private void onFailure(Throwable ex) { timer.cancel(); try { if (callback != null) { callback.onFailure(ex); } } finally { unload(); } }
@Override public Method timeout(int timeout) { jsonpBuilder.setTimeout(timeout); return this; }
/** * Sends a JSONP request, does not expect any result, but still allows to be notified when the * request has been executed on the server. */ public JsonpRequest<Void> send(@IsTrustedResourceUri String url, AsyncCallback<Void> callback) { return send(url, callback, false); }
private void onSuccess(T data) { timer.cancel(); try { if (callback != null) { callback.onSuccess(data); } } finally { unload(); } }
public JsonpRequest<Boolean> requestBoolean( @IsTrustedResourceUri String url, AsyncCallback<Boolean> callback) { return send(url, callback, false); }
public JsonpRequest<Double> requestDouble( @IsTrustedResourceUri String url, AsyncCallback<Double> callback) { return send(url, callback, false); }
public JsonpRequest<Integer> requestInteger( @IsTrustedResourceUri String url, AsyncCallback<Integer> callback) { return send(url, callback, true); }
public JsonpRequest<String> requestString( @IsTrustedResourceUri String url, AsyncCallback<String> callback) { return send(url, callback, false); }