All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.gwt.jsonp.client.JsonpRequestBuilder Maven / Gradle / Ivy

There is a newer version: 2.10.0
Show newest version
/*
 * Copyright 2009 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.google.gwt.jsonp.client;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * Class to send cross domain requests to an http server. The server will receive a request
 * including a callback url parameter, which should be used to return the response as following:
 *
 * 
<callback>(<json>);
* * where <callback> is the url parameter (see {@link #setCallbackParam(String)}), and * <json> is the response to the request in json format. * * This will result on the client to call the corresponding {@link AsyncCallback#onSuccess(Object)} * method. * *

* If needed, errors can be handled by a separate callback: * *

<failureCallback>(<error>);
* * where <error> is a string containing an error message. This will result on the client to * call the corresponding {@link AsyncCallback#onFailure(Throwable)} method. See * {@link #setFailureCallbackParam(String)}. * *

* Example using JSON Google Calendar * GData API: * *

 * String url = "http://www.google.com/calendar/feeds/[email protected]/public/full" +
 *     "?alt=json-in-script";
 * JsonpRequestBuilder jsonp = new JsonpRequestBuilder();
 * jsonp.requestObject(url,
 *     new AsyncCallback<Feed>() {
 *       public void onFailure(Throwable throwable) {
 *         Log.severe("Error: " + throwable);
 *       }
 *
 *       public void onSuccess(Feed feed) {
 *         JsArray<Entry> entries = feed.getEntries();
 *         for (int i = 0; i < entries.length(); i++) {
 *           Entry entry = entries.get(i);
 *           Log.info(entry.getTitle() +
 *                    " (" + entry.getWhere() + "): " +
 *                    entry.getStartTime() + " -> " +
 *                    entry.getEndTime());
 *         }
 *       }
 *     });
 * 
* * This example uses these overlay types: * *
 * class Entry extends JavaScriptObject {
 *   protected Entry() {}
 *
 *   public final native String getTitle() /*-{
 *     return this.title.$t;
 *   }-*/;
 *
 *   public final native String getWhere() /*-{
 *     return this.gd$where[0].valueString;
 *   }-*/;
 *
 *   public final native String getStartTime() /*-{
 *     return this.gd$when ? this.gd$when[0].startTime : null;
 *   }-*/;
 *
 *   public final native String getEndTime() /*-{
 *     return this.gd$when ? this.gd$when[0].endTime : null;
 *   }-*/;
 * }
 *
 * class Feed extends JavaScriptObject {
 *   protected Feed() {}
 *
 *   public final native JsArray<Entry> getEntries() /*-{
 *     return this.feed.entry;
 *   }-*/;
 * }
 * 
* *

*/ public class JsonpRequestBuilder { private int timeout = 10000; private String callbackParam = "callback"; private String failureCallbackParam = null; private String predeterminedId = null; /** * Returns the name of the callback url parameter to send to the server. The * default value is "callback". */ public String getCallbackParam() { return callbackParam; } /** * Returns the name of the failure callback url parameter to send to the * server. The default is null. */ public String getFailureCallbackParam() { return failureCallbackParam; } /** * Returns the expected timeout (ms) for this request. */ public int getTimeout() { return timeout; } public JsonpRequest requestBoolean(String url, AsyncCallback callback) { return send(url, callback, false); } public JsonpRequest requestDouble(String url, AsyncCallback callback) { return send(url, callback, false); } public JsonpRequest requestInteger(String url, AsyncCallback callback) { return send(url, callback, true); } /** * 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 JsonpRequest requestObject(String url, AsyncCallback callback) { return send(url, callback, false); } public JsonpRequest requestString(String url, AsyncCallback callback) { return send(url, callback, false); } /** * Sends a JSONP request and does not expect any results. */ public void send(String url) { send(url, null, false); } /** * 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 send(String url, AsyncCallback callback) { return send(url, callback, false); } /** * @param callbackParam The name of the callback url parameter to send to the server. The default * value is "callback". */ public void setCallbackParam(String callbackParam) { this.callbackParam = callbackParam; } /** * @param failureCallbackParam The name of the failure callback url parameter to send to the * server. The default is null. */ public void setFailureCallbackParam(String failureCallbackParam) { this.failureCallbackParam = failureCallbackParam; } public void setPredeterminedId(String id) { this.predeterminedId = id; } /** * @param timeout The expected timeout (ms) for this request. The default is 10s. */ public void setTimeout(int timeout) { this.timeout = timeout; } private JsonpRequest send(String url, AsyncCallback callback, boolean expectInteger) { JsonpRequest request; if (predeterminedId != null) { request = new JsonpRequest(callback, timeout, expectInteger, callbackParam, failureCallbackParam, predeterminedId); } else { request = new JsonpRequest(callback, timeout, expectInteger, callbackParam, failureCallbackParam); } request.send(url); return request; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy