예제 #1
0
        /// <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)
        {
            var request = context.Request;
            var lang    = request.FilePath;

            lang = lang.Replace(".res.axd", "");

            if (lang.IndexOf("/") >= 0)
            {
                lang = lang.Substring(lang.LastIndexOf("/") + 1);
            }

            if (string.IsNullOrEmpty(lang))
            {
                // Use the current Language if the lang query isn't set.
                lang = BlogSettings.Instance.Language;
            }

            lang = lang.ToLowerInvariant();
            var    sb = new StringBuilder();
            string cacheKey;
            string script;

            if (request.FilePath.Contains("admin.res.axd"))
            {
                lang = BlogSettings.Instance.Culture;

                cacheKey = "admin.resource.axd - " + lang;
                script   = (string)Blog.CurrentInstance.Cache[cacheKey];

                if (String.IsNullOrEmpty(script))
                {
                    System.Globalization.CultureInfo culture;
                    try
                    {
                        culture = new System.Globalization.CultureInfo(lang);
                    }
                    catch (Exception)
                    {
                        culture = Utils.GetDefaultCulture();
                    }

                    var jc = new JsonCulture(culture, JsonCulture.ResourceType.Admin);

                    sb.Append("BlogAdmin = { i18n: " + jc.ToJsonString() + "};");
                    script = sb.ToString();
                }
            }
            else
            {
                cacheKey = "resource.axd - " + lang;
                script   = (string)Blog.CurrentInstance.Cache[cacheKey];

                if (String.IsNullOrEmpty(script))
                {
                    System.Globalization.CultureInfo culture;
                    try
                    {
                        culture = new System.Globalization.CultureInfo(lang);
                    }
                    catch (Exception)
                    {
                        culture = Utils.GetDefaultCulture();
                    }

                    var jc = new JsonCulture(culture, JsonCulture.ResourceType.Blog);

                    // Although this handler is intended to output resource strings,
                    // also outputting other non-resource variables.
                    sb.AppendFormat("webRoot: '{0}',", Utils.RelativeWebRoot);
                    sb.AppendFormat("applicationWebRoot: '{0}',", Utils.ApplicationRelativeWebRoot);
                    sb.AppendFormat("blogInstanceId: '{0}',", Blog.CurrentInstance.Id);
                    sb.AppendFormat("fileExtension: '{0}',", BlogConfig.FileExtension);
                    sb.AppendFormat("i18n: {0}", jc.ToJsonString());
                    script = "BlogEngineRes = {" + sb + "};";
                }
            }

            Blog.CurrentInstance.Cache.Insert(cacheKey, script, null, Cache.NoAbsoluteExpiration, new TimeSpan(3, 0, 0, 0));

            SetHeaders(script.GetHashCode(), context);
            context.Response.Write(script);

            if (BlogSettings.Instance.EnableHttpCompression)
            {
                CompressionModule.CompressResponse(context); // Compress(context);
            }
        }
예제 #2
0
        /// <summary>
        /// Enables a JsHttpHandler object to process of requests.
        /// </summary>
        /// <param name="context">The context.</param>
        public override void ProcessRequest(HttpContextBase context)
        {
            var languageId = EngineContext.Current.Resolve<IWorkContext>().WorkingLanguage.RowId;

            if (!languageId.IsEmpty())
            {
                HttpResponseBase response = context.Response;

                // set hte json object
                JsonCulture jsonCulture = new JsonCulture(languageId);

                // set the content type
                response.ContentType = "text/javascript";

                // create the app engine i18n script
                string jsScript = "$.extend(appEngine, {i18n: " + jsonCulture.ToJsonString() + "});";

                var urlHelper   = new UrlHelper(context.RequestContext());
                JsonUrl jsonUrl = new JsonUrl(urlHelper);

                // implement the urls
                jsScript += "$.extend(appEngine, {path: " +  jsonUrl.ToJsonString() + "});";

                if (!string.IsNullOrEmpty(jsScript))
                {
                    _httpResponseCompressor.Compress(context);

                    // Write
                    using (StreamWriter sw = new StreamWriter((response.OutputStream)))
                    {
                        sw.Write(jsScript);
                    }

                    // cache the contents
                    _httpResponseCacher.Cache(context, TimeSpan.FromDays(7));
                }
            }
        }