1 package autotest.common; 2 3 import autotest.common.ui.NotifyManager; 4 5 import com.google.gwt.json.client.JSONArray; 6 import com.google.gwt.json.client.JSONNull; 7 import com.google.gwt.json.client.JSONNumber; 8 import com.google.gwt.json.client.JSONObject; 9 import com.google.gwt.json.client.JSONString; 10 import com.google.gwt.json.client.JSONValue; 11 12 import java.util.HashMap; 13 import java.util.Map; 14 import java.util.Set; 15 16 public abstract class JsonRpcProxy { 17 public static final String AFE_BASE_URL = "/afe/server/"; 18 public static final String TKO_BASE_URL = "/new_tko/server/"; 19 private static final String RPC_URL_SUFFIX = "rpc/"; 20 private static final String JSON_RPC_URL_SUFFIX = "jsonp_rpc/"; 21 22 private static String defaultBaseUrl; 23 private static final Map<String, JsonRpcProxy> instanceMap = 24 new HashMap<String, JsonRpcProxy>(); 25 protected static final NotifyManager notify = NotifyManager.getInstance(); 26 27 public static void setDefaultBaseUrl(String baseUrl) { 28 defaultBaseUrl = baseUrl; 29 } 30 31 public static JsonRpcProxy createProxy(String baseUrl, boolean isPaddedJson) { 32 if (isPaddedJson) { 33 return new PaddedJsonRpcProxy(baseUrl + JSON_RPC_URL_SUFFIX); 34 } 35 return new XhrJsonRpcProxy(baseUrl + RPC_URL_SUFFIX); 36 } 37 38 public static JsonRpcProxy getProxy(String baseUrl) { 39 if (!instanceMap.containsKey(baseUrl)) { 40 instanceMap.put(baseUrl, createProxy(baseUrl, false)); 41 } 42 return instanceMap.get(baseUrl); 43 } 44 45 public static JsonRpcProxy getProxy() { 46 assert defaultBaseUrl != null; 47 return getProxy(defaultBaseUrl); 48 } 49 50 public static void setProxy(String baseUrl, JsonRpcProxy proxy) { 51 instanceMap.put(baseUrl, proxy); 52 } 53 54 /** 55 * Make an RPC call. 56 * @param method name of the method to call 57 * @param params dictionary of parameters to pass 58 * @param callback callback to be notified of RPC call results 59 */ 60 public void rpcCall(String method, JSONObject params, final JsonRpcCallback callback) { 61 JSONObject request = buildRequestObject(method, params); 62 sendRequest(request, callback); 63 } 64 65 protected abstract void sendRequest(JSONObject request, final JsonRpcCallback callback); 66 67 public static JSONObject buildRequestObject(String method, JSONObject params) { 68 JSONObject request = new JSONObject(); 69 request.put("method", new JSONString(method)); 70 request.put("params", processParams(params)); 71 request.put("id", new JSONNumber(0)); 72 return request; 73 } 74 75 private static JSONArray processParams(JSONObject params) { 76 JSONArray result = new JSONArray(); 77 JSONObject newParams = new JSONObject(); 78 if (params != null) { 79 Set<String> keys = params.keySet(); 80 for (String key : keys) { 81 if (params.get(key) != JSONNull.getInstance()) 82 newParams.put(key, params.get(key)); 83 } 84 } 85 result.set(0, newParams); 86 return result; 87 } 88 89 protected static void handleResponseObject(JSONObject responseObject, 90 JsonRpcCallback callback) { 91 JSONValue error = responseObject.get("error"); 92 if (error == null) { 93 notify.showError("Bad JSON response", responseObject.toString()); 94 callback.onError(null); 95 return; 96 } 97 else if (error.isObject() != null) { 98 callback.onError(error.isObject()); 99 return; 100 } 101 102 JSONValue result = responseObject.get("result"); 103 callback.onSuccess(result); 104 } 105 } 106