public static string ProcessRequest(HttpContext context, Type type) { if (HttpRuntime.Cache.Get(type.FullName) != null) { return(HttpRuntime.Cache.Get(type.FullName).ToString()); } var path = type.FullName + "," + type.Assembly.FullName.Split(',')[0]; if (Settings.UseAssemblyQualifiedName) { path = type.AssemblyQualifiedName; } if (Settings != null && Settings.UrlNamespaceMappings.ContainsValue(path)) { foreach (string key in Settings.UrlNamespaceMappings.Keys) { if (Settings.UrlNamespaceMappings[key].ToString() == path) { path = key; break; } } } var url = context.Request.ApplicationPath + (context.Request.ApplicationPath.EndsWith("/") ? "" : "/") + HandlerPath + "/" + GetSessionUri() + path + HandlerExtension; var mi = type.GetMethods(); var methodList = new ArrayList(); for (var y = 0; y < mi.Length; y++) { var method = mi[y]; if (!method.IsPublic) { continue; } var ma = (AjaxMethodAttribute[])method.GetCustomAttributes(typeof(AjaxMethodAttribute), true); if (ma.Length == 0) { continue; } var ppa = (PrincipalPermissionAttribute[])method.GetCustomAttributes(typeof(PrincipalPermissionAttribute), true); if (ppa.Length > 0) { var permissionDenied = true; for (var p = 0; p < ppa.Length && permissionDenied; p++) { if (!string.IsNullOrEmpty(ppa[p].Role) && context.User != null && context.User.Identity.IsAuthenticated && !context.User.IsInRole(ppa[p].Role)) { continue; } permissionDenied = false; } if (permissionDenied) { continue; } } //methods[mc++] = method; methodList.Add(method); } var methods = (MethodInfo[])methodList.ToArray(typeof(MethodInfo)); // render client-side proxy file var sb = new StringBuilder(); TypeJavaScriptProvider jsp = null; if (Settings.TypeJavaScriptProvider != null) { try { var jspt = Type.GetType(Settings.TypeJavaScriptProvider); if (jspt != null && typeof(TypeJavaScriptProvider).IsAssignableFrom(jspt)) { jsp = (TypeJavaScriptProvider)Activator.CreateInstance(jspt, new object[] { type, url, sb }); } } catch (Exception) { } } if (jsp == null) { jsp = new TypeJavaScriptProvider(type, url, sb); } jsp.RenderNamespace(); jsp.RenderClassBegin(); jsp.RenderMethods(methods); jsp.RenderClassEnd(); HttpRuntime.Cache.Insert(type.FullName, sb.ToString(), null, DateTime.UtcNow.AddDays(1), Cache.NoSlidingExpiration); return(sb.ToString()); }
public static string ProcessRequest(HttpContext context, Type type) { if (HttpRuntime.Cache.Get(type.FullName) != null) return HttpRuntime.Cache.Get(type.FullName).ToString(); var path = type.FullName + "," + type.Assembly.FullName.Split(',')[0]; if (Settings.UseAssemblyQualifiedName) path = type.AssemblyQualifiedName; if (Settings != null && Settings.UrlNamespaceMappings.ContainsValue(path)) { foreach (string key in Settings.UrlNamespaceMappings.Keys) { if (Settings.UrlNamespaceMappings[key].ToString() == path) { path = key; break; } } } var url = context.Request.ApplicationPath + (context.Request.ApplicationPath.EndsWith("/") ? "" : "/") + HandlerPath + "/" + GetSessionUri() + path + HandlerExtension; var mi = type.GetMethods(); var methodList = new ArrayList(); for (var y = 0; y < mi.Length; y++) { var method = mi[y]; if (!method.IsPublic) continue; var ma = (AjaxMethodAttribute[])method.GetCustomAttributes(typeof(AjaxMethodAttribute), true); if (ma.Length == 0) continue; var ppa = (PrincipalPermissionAttribute[])method.GetCustomAttributes(typeof(PrincipalPermissionAttribute), true); if (ppa.Length > 0) { var permissionDenied = true; for (var p = 0; p < ppa.Length && permissionDenied; p++) { if (!string.IsNullOrEmpty(ppa[p].Role) && context.User != null && context.User.Identity.IsAuthenticated && !context.User.IsInRole(ppa[p].Role)) continue; permissionDenied = false; } if (permissionDenied) continue; } //methods[mc++] = method; methodList.Add(method); } var methods = (MethodInfo[])methodList.ToArray(typeof(MethodInfo)); // render client-side proxy file var sb = new StringBuilder(); TypeJavaScriptProvider jsp = null; if (Settings.TypeJavaScriptProvider != null) { try { var jspt = Type.GetType(Settings.TypeJavaScriptProvider); if (jspt != null && typeof(TypeJavaScriptProvider).IsAssignableFrom(jspt)) { jsp = (TypeJavaScriptProvider)Activator.CreateInstance(jspt, new object[] { type, url, sb }); } } catch (Exception) { } } if (jsp == null) { jsp = new TypeJavaScriptProvider(type, url, sb); } jsp.RenderNamespace(); jsp.RenderClassBegin(); jsp.RenderMethods(methods); jsp.RenderClassEnd(); HttpRuntime.Cache.Insert(type.FullName, sb.ToString(), null, DateTime.UtcNow.AddDays(1), Cache.NoSlidingExpiration); return sb.ToString(); }
/// <summary> /// Enables processing of HTTP Web requests by a custom HttpHandler that implements the <see cref="T:System.Web.IHttpHandler"></see> interface. /// </summary> /// <param name="context">An <see cref="T:System.Web.HttpContext"></see> object that provides references to the intrinsic server objects (for example, Request, Response, Session, and Server) used to service HTTP requests.</param> public void ProcessRequest(HttpContext context) { // The request was not a request to invoke a server-side method. // Now, we will render the Javascript that will be used on the // client to run as a proxy or wrapper for the methods marked // with the AjaxMethodAttribute. if (context.Trace.IsEnabled) { context.Trace.Write(Constant.AjaxID, "Render class proxy Javascript"); } // Check wether the javascript is already rendered and cached in the // current context. string etag = context.Request.Headers["If-None-Match"]; string modSince = context.Request.Headers["If-Modified-Since"]; string path = type.FullName + "," + type.Assembly.FullName.Split(',')[0]; if (Utility.Settings.UseAssemblyQualifiedName) { path = type.AssemblyQualifiedName; } if (Utility.Settings != null && Utility.Settings.UrlNamespaceMappings.ContainsValue(path)) { foreach (string key in Utility.Settings.UrlNamespaceMappings.Keys) { if (Utility.Settings.UrlNamespaceMappings[key].ToString() == path) { path = key; break; } } } if (context.Cache[path] != null) { CacheInfo ci = (CacheInfo)context.Cache[path]; if (etag != null) { if (etag == ci.ETag) // TODO: null check { context.Response.StatusCode = 304; context.Response.SuppressContent = true; return; } } if (modSince != null) { if (modSince.IndexOf(";") > 0) { // If-Modified-Since: Tue, 06 Jun 2006 10:13:38 GMT; length=2935 modSince = modSince.Split(';')[0]; } try { DateTime modSinced = Convert.ToDateTime(modSince.ToString()).ToUniversalTime(); if (DateTime.Compare(modSinced, ci.LastModified.ToUniversalTime()) >= 0) { context.Response.StatusCode = 304; context.Response.SuppressContent = true; return; } } catch (FormatException) { if (context.Trace.IsEnabled) { context.Trace.Write(Constant.AjaxID, "The header value for If-Modified-Since = " + modSince + " could not be converted to a System.DateTime."); } } } } etag = type.AssemblyQualifiedName; etag = MD5Helper.GetHash(System.Text.Encoding.Default.GetBytes(etag)); DateTime now = DateTime.Now; DateTime lastMod = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second); // .ToUniversalTime(); context.Response.AddHeader("Content-Type", "application/x-javascript"); context.Response.ContentEncoding = System.Text.Encoding.UTF8; context.Response.Cache.SetCacheability(System.Web.HttpCacheability.Public); context.Response.Cache.SetETag(etag); context.Response.Cache.SetLastModified(lastMod); // Ok, we do not have the javascript rendered, yet. // Build the javascript source and save it to the current // Application context. string url = context.Request.ApplicationPath + (context.Request.ApplicationPath.EndsWith("/") ? "" : "/") + Utility.HandlerPath + "/" + AjaxPro.Utility.GetSessionUri() + path + Utility.HandlerExtension; // find all methods that are able to be used with AjaxPro MethodInfo[] mi = type.GetMethods(); MethodInfo method; #if (NET20) List <MethodInfo> methods = new List <MethodInfo>(); #else MethodInfo[] methods; System.Collections.ArrayList methodList = new System.Collections.ArrayList(); int mc = 0; #endif for (int y = 0; y < mi.Length; y++) { method = mi[y]; if (!method.IsPublic) { continue; } AjaxMethodAttribute[] ma = (AjaxMethodAttribute[])method.GetCustomAttributes(typeof(AjaxMethodAttribute), true); if (ma.Length == 0) { continue; } PrincipalPermissionAttribute[] ppa = (PrincipalPermissionAttribute[])method.GetCustomAttributes(typeof(PrincipalPermissionAttribute), true); if (ppa.Length > 0) { bool permissionDenied = true; for (int p = 0; p < ppa.Length && permissionDenied; p++) { #if (_____NET20) if (Roles.Enabled) { try { if (!String.IsNullOrEmpty(ppa[p].Role) && !Roles.IsUserInRole(ppa[p].Role)) { continue; } } catch (Exception) { // Should we disable this AjaxMethod of there is an exception? continue; } } else #endif if (ppa[p].Role != null && ppa[p].Role.Length > 0 && context.User != null && context.User.Identity.IsAuthenticated && !context.User.IsInRole(ppa[p].Role)) { continue; } permissionDenied = false; } if (permissionDenied) { continue; } } #if (NET20) methods.Add(method); #else //methods[mc++] = method; methodList.Add(method); #endif } #if (!NET20) methods = (MethodInfo[])methodList.ToArray(typeof(MethodInfo)); #endif // render client-side proxy file System.Text.StringBuilder sb = new System.Text.StringBuilder(); TypeJavaScriptProvider jsp = null; if (Utility.Settings.TypeJavaScriptProvider != null) { try { Type jspt = Type.GetType(Utility.Settings.TypeJavaScriptProvider); if (jspt != null && typeof(TypeJavaScriptProvider).IsAssignableFrom(jspt)) { jsp = (TypeJavaScriptProvider)Activator.CreateInstance(jspt, new object[3] { type, url, sb }); } } catch (Exception) { } } if (jsp == null) { jsp = new TypeJavaScriptProvider(type, url, sb); } jsp.RenderNamespace(); jsp.RenderClassBegin(); #if (NET20) jsp.RenderMethods(methods.ToArray()); #else jsp.RenderMethods(methods); #endif jsp.RenderClassEnd(); context.Response.Write(sb.ToString()); context.Response.Write("\r\n"); // save the javascript in current Application context to // speed up the next requests. // TODO: was können wir hier machen?? // System.Web.Caching.CacheDependency fileDepend = new System.Web.Caching.CacheDependency(type.Assembly.Location); context.Cache.Add(path, new CacheInfo(etag, lastMod), null, System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Normal, null); if (context.Trace.IsEnabled) { context.Trace.Write(Constant.AjaxID, "End ProcessRequest"); } }
/// <summary> /// Enables processing of HTTP Web requests by a custom HttpHandler that implements the <see cref="T:System.Web.IHttpHandler"></see> interface. /// </summary> /// <param name="context">An <see cref="T:System.Web.HttpContext"></see> object that provides references to the intrinsic server objects (for example, Request, Response, Session, and Server) used to service HTTP requests.</param> public void ProcessRequest(HttpContext context) { // The request was not a request to invoke a server-side method. // Now, we will render the Javascript that will be used on the // client to run as a proxy or wrapper for the methods marked // with the AjaxMethodAttribute. if(context.Trace.IsEnabled) context.Trace.Write(Constant.AjaxID, "Render class proxy Javascript"); // Check wether the javascript is already rendered and cached in the // current context. string etag = context.Request.Headers["If-None-Match"]; string modSince = context.Request.Headers["If-Modified-Since"]; string path = type.FullName + "," + type.Assembly.FullName.Split(',')[0]; if (Utility.Settings.UseAssemblyQualifiedName) path = type.AssemblyQualifiedName; if(Utility.Settings != null && Utility.Settings.UrlNamespaceMappings.ContainsValue(path)) { foreach(string key in Utility.Settings.UrlNamespaceMappings.Keys) { if(Utility.Settings.UrlNamespaceMappings[key].ToString() == path) { path = key; break; } } } if(context.Cache[path] != null) { CacheInfo ci = (CacheInfo)context.Cache[path]; if(etag != null) { if(etag == ci.ETag) // TODO: null check { context.Response.StatusCode = 304; context.Response.SuppressContent = true; return; } } if(modSince != null) { if (modSince.IndexOf(";") > 0) { // If-Modified-Since: Tue, 06 Jun 2006 10:13:38 GMT; length=2935 modSince = modSince.Split(';')[0]; } try { DateTime modSinced = Convert.ToDateTime(modSince.ToString()).ToUniversalTime(); if(DateTime.Compare(modSinced, ci.LastModified.ToUniversalTime()) >= 0) { context.Response.StatusCode = 304; context.Response.SuppressContent = true; return; } } catch(FormatException) { if(context.Trace.IsEnabled) context.Trace.Write(Constant.AjaxID, "The header value for If-Modified-Since = " + modSince + " could not be converted to a System.DateTime."); } } } etag = type.AssemblyQualifiedName; etag = MD5Helper.GetHash(System.Text.Encoding.Default.GetBytes(etag)); DateTime now = DateTime.Now; DateTime lastMod = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second); // .ToUniversalTime(); context.Response.AddHeader("Content-Type", "application/x-javascript"); context.Response.ContentEncoding = System.Text.Encoding.UTF8; context.Response.Cache.SetCacheability(System.Web.HttpCacheability.Public); context.Response.Cache.SetETag(etag); context.Response.Cache.SetLastModified(lastMod); // Ok, we do not have the javascript rendered, yet. // Build the javascript source and save it to the current // Application context. string url = context.Request.ApplicationPath + (context.Request.ApplicationPath.EndsWith("/") ? "" : "/") + Utility.HandlerPath + "/" + AjaxPro.Utility.GetSessionUri() + path + Utility.HandlerExtension; // find all methods that are able to be used with AjaxPro MethodInfo[] mi = type.GetMethods(); MethodInfo method; #if(NET20) List<MethodInfo> methods = new List<MethodInfo>(); #else MethodInfo[] methods; System.Collections.ArrayList methodList = new System.Collections.ArrayList(); int mc = 0; #endif for (int y = 0; y < mi.Length; y++) { method = mi[y]; if (!method.IsPublic) continue; AjaxMethodAttribute[] ma = (AjaxMethodAttribute[])method.GetCustomAttributes(typeof(AjaxMethodAttribute), true); if (ma.Length == 0) continue; PrincipalPermissionAttribute[] ppa = (PrincipalPermissionAttribute[])method.GetCustomAttributes(typeof(PrincipalPermissionAttribute), true); if (ppa.Length > 0) { bool permissionDenied = true; for (int p = 0; p < ppa.Length && permissionDenied; p++) { #if(_____NET20) if (Roles.Enabled) { try { if (!String.IsNullOrEmpty(ppa[p].Role) && !Roles.IsUserInRole(ppa[p].Role)) continue; } catch (Exception) { // Should we disable this AjaxMethod of there is an exception? continue; } } else #endif if (ppa[p].Role != null && ppa[p].Role.Length > 0 && context.User != null && context.User.Identity.IsAuthenticated && !context.User.IsInRole(ppa[p].Role)) continue; permissionDenied = false; } if (permissionDenied) continue; } #if(NET20) methods.Add(method); #else //methods[mc++] = method; methodList.Add(method); #endif } #if(!NET20) methods = (MethodInfo[])methodList.ToArray(typeof(MethodInfo)); #endif // render client-side proxy file System.Text.StringBuilder sb = new System.Text.StringBuilder(); TypeJavaScriptProvider jsp = null; if (Utility.Settings.TypeJavaScriptProvider != null) { try { Type jspt = Type.GetType(Utility.Settings.TypeJavaScriptProvider); if (jspt != null && typeof(TypeJavaScriptProvider).IsAssignableFrom(jspt)) { jsp = (TypeJavaScriptProvider)Activator.CreateInstance(jspt, new object[3] { type, url, sb }); } } catch (Exception) { } } if (jsp == null) { jsp = new TypeJavaScriptProvider(type, url, sb); } jsp.RenderNamespace(); jsp.RenderClassBegin(); #if(NET20) jsp.RenderMethods(methods.ToArray()); #else jsp.RenderMethods(methods); #endif jsp.RenderClassEnd(); context.Response.Write(sb.ToString()); context.Response.Write("\r\n"); // save the javascript in current Application context to // speed up the next requests. // TODO: was können wir hier machen?? // System.Web.Caching.CacheDependency fileDepend = new System.Web.Caching.CacheDependency(type.Assembly.Location); context.Cache.Add(path, new CacheInfo(etag, lastMod), null, System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Normal, null); if(context.Trace.IsEnabled) context.Trace.Write(Constant.AjaxID, "End ProcessRequest"); }