/// <summary> /// Declares that a method is to be exposed to the web. When no permission is declared, the method is callable by anyone /// </summary> /// <param name="webCallingConvention">The kind of calling convention supported. This instructs ObjectCloud on how to map the HTTP request to the C# method's parameters</param> /// <param name="webReturnConvention">The kind of return convention supported. This instructs the JavaScript code generator on how handle the transaction object</param> public WebCallableAttribute( WebCallingConvention webCallingConvention, WebReturnConvention webReturnConvention) { _WebCallingConvention = webCallingConvention; _WebReturnConvention = webReturnConvention; _MinimumPermissionForWeb = null; _MinimumPermissionForTrusted = null; }
/// <summary> /// Declares that a method is to be exposed to the web /// </summary> /// <param name="webCallingConvention">The kind of calling convention supported. This instructs ObjectCloud on how to map the HTTP request to the C# method's parameters</param> /// <param name="webReturnConvention">The kind of return convention supported. This instructs the JavaScript code generator on how handle the transaction object</param> /// <param name="minimumPermission">The minimum permission needed to call this method from both the web and other trusted sources</param> public WebCallableAttribute( WebCallingConvention webCallingConvention, WebReturnConvention webReturnConvention, FilePermissionEnum minimumPermission) { _WebCallingConvention = webCallingConvention; _WebReturnConvention = webReturnConvention; _MinimumPermissionForWeb = minimumPermission; _MinimumPermissionForTrusted = minimumPermission; }
/// <summary> /// Generates the success handler /// </summary> /// <returns></returns> public static string GenerateSyncronousReturnHandler(WebReturnConvention? webReturnConvention) { switch (webReturnConvention) { case WebReturnConvention.JSON: return @" if ((httpRequest.status >= 200) && (httpRequest.status < 300)) return JSON.parse(httpRequest.responseText); else throw httpRequest; "; case WebReturnConvention.JavaScriptObject: return @" if ((httpRequest.status >= 200) && (httpRequest.status < 300)) return eval('(' + httpRequest.responseText + ')'); else throw httpRequest; "; default: return @" if ((httpRequest.status >= 200) && (httpRequest.status < 300)) return httpRequest.responseText; else throw httpRequest; "; } }
/// <summary> /// Generates a POST wrapper for urlencoded queries /// </summary> /// <param name="methodAndWCA"></param> /// <returns></returns> public static string GeneratePOST_Sync(string methodName, WebReturnConvention webReturnConvention, string sendString) { // Create the funciton declaration StringBuilder toReturn = new StringBuilder(string.Format("\"{0}_Sync\"", methodName)); toReturn.Append(SyncronousFunctionDeclaration); // Create the AJAX request toReturn.Append(CreateSyncronousAJAXRequest); // Open the AJAX request toReturn.Append(" httpRequest.open('POST', '{0}?Method=" + methodName + "' + urlPostfix, false);\n"); toReturn.Append(GenerateSend(sendString)); toReturn.Append(GenerateSyncronousReturnHandler(webReturnConvention)); toReturn.Append('}'); return toReturn.ToString(); }
/// <summary> /// Generates a POST wrapper for urlencoded queries /// </summary> /// <param name="methodAndWCA"></param> /// <returns></returns> public static string GeneratePOST_urlencoded_Sync(string methodName, WebReturnConvention webReturnConvention) { // Create the funciton declaration StringBuilder toReturn = new StringBuilder(string.Format("\"{0}_Sync\"", methodName)); toReturn.Append(SyncronousFunctionDeclaration); // Create a urlEncoded string for all of the parameters toReturn.Append(CreateEncodedParameters); // Create the AJAX request toReturn.Append(CreateSyncronousAJAXRequest); // Open the AJAX request toReturn.Append(" httpRequest.open('POST', '{0}?Method=" + methodName + "' + urlPostfix, false);\n"); toReturn.Append(" httpRequest.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');\n"); toReturn.Append(GenerateSend(" httpRequest.send(encodedParameters);\n")); toReturn.Append(GenerateSyncronousReturnHandler(webReturnConvention)); toReturn.Append('}'); return toReturn.ToString(); }
/// <summary> /// Generates a POST wrapper for urlencoded queries /// </summary> /// <param name="methodAndWCA"></param> /// <returns></returns> public static string GeneratePOST_Sync(string methodName, WebReturnConvention webReturnConvention) { return GeneratePOST_Sync( methodName, webReturnConvention, " httpRequest.send(parameters);\n"); }
/// <summary> /// Generates a POST wrapper for urlencoded queries /// </summary> /// <param name="methodAndWCA"></param> /// <returns></returns> public static string GeneratePOST_JSON(string methodName, WebReturnConvention webReturnConvention) { return GeneratePOST( methodName, webReturnConvention, " httpRequest.send(JSON.stringify(parameters));\n"); }
/// <summary> /// Generates the success handler /// </summary> /// <returns></returns> public static string GenerateOnSuccessHandler(WebReturnConvention? webReturnConvention) { switch (webReturnConvention) { case WebReturnConvention.JSON: return @" if (onSuccess) { var oldOnSuccess = onSuccess; onSuccess = function(responseText, transport) { oldOnSuccess(JSON.parse(responseText), transport); }; } else onSuccess = function(responseText, transport) {}; "; case WebReturnConvention.Primitive: return @" if (!onSuccess) onSuccess = function(responseText, transport) { alert(responseText); }; "; case WebReturnConvention.JavaScriptObject: return @" if (onSuccess) { var oldOnSuccess = onSuccess; onSuccess = function(responseText, transport) { var js; try { js = eval('(' + responseText + ')'); } catch (error) { alert('Error evaluating response: ' + error); } oldOnSuccess(js, transport); }; } else onSuccess = function(responseText, transport) { eval('(' + responseText + ')'); }; "; default: return @" if (!onSuccess) onSuccess = function(responseText, transport) { alert(responseText); }; "; } }
/// <summary> /// Generates a POST wrapper for urlencoded queries /// </summary> /// <param name="methodAndWCA"></param> /// <returns></returns> public static string GenerateGET_urlencoded_Sync(string methodName, WebReturnConvention webReturnConvention) { // Create the funciton declaration StringBuilder toReturn = new StringBuilder(string.Format("\"{0}_Sync\"", methodName)); toReturn.Append(SyncronousFunctionDeclaration); // Create a urlEncoded string for all of the parameters toReturn.AppendFormat(" if (parameters == undefined)\n"); toReturn.Append(" parameters = {};\n"); toReturn.AppendFormat(" parameters.Method='{0}';\n", methodName); toReturn.Append(CreateEncodedParameters); // Create the AJAX request toReturn.Append(CreateSyncronousAJAXRequest); // Open the AJAX request toReturn.Append(" httpRequest.open('GET', '{0}?' + encodedParameters + urlPostfix, false);\n"); toReturn.Append(GenerateSend(" httpRequest.send(null);\n")); toReturn.Append(GenerateSyncronousReturnHandler(webReturnConvention)); toReturn.Append('}'); return toReturn.ToString(); }
/// <summary> /// Generates a POST wrapper for urlencoded queries /// </summary> /// <param name="methodAndWCA"></param> /// <returns></returns> public static string GenerateGET_urlencoded(string methodName, WebReturnConvention webReturnConvention) { // Create the funciton declaration StringBuilder toReturn = new StringBuilder(string.Format("\"{0}\"", methodName)); toReturn.Append(FunctionDeclaration); toReturn.Append(GenerateOnSuccessHandler(webReturnConvention)); toReturn.Append(FunctionBegin); // Create a urlEncoded string for all of the parameters toReturn.AppendFormat(" parameters.Method='{0}';\n", methodName); toReturn.Append(CreateEncodedParameters); // Create the AJAX request toReturn.Append(CreateAJAXRequest); // Open the AJAX request toReturn.Append(" httpRequest.open('GET', '{0}?' + encodedParameters + urlPostfix, true);\n"); toReturn.Append(GenerateSend(" httpRequest.send(null);\n")); toReturn.Append('}'); return toReturn.ToString(); }