Esempio n. 1
0
        public static void SendBoxResponse(HttpContext context, string templateName)
        {
            HttpResponse response = context.Response;

            string clientKey = context.Request.QueryString["ck"];

            if (string.IsNullOrEmpty(clientKey))
            {
                throw new JsonInvalidOperationException("No client key was specified");
            }

            BoxResponses boxResponses = SingletonHelper.GetApplicationProvider <BoxResponses>();

            if (IsScriptRequest(context.Request))
            {
                response.Clear();
                response.Write(boxResponses[clientKey].Script);
                boxResponses.Remove(clientKey);
                response.Flush();
                response.SuppressContent = true;
                context.ApplicationInstance.CompleteRequest();
                return;
            }

            string virtualBoxPath = GetVirtualBoxPath(context, templateName);

            try
            {
                if (string.IsNullOrEmpty(virtualBoxPath))
                {
                    throw new JsonInvalidOperationException("The box template file could not be found: [" + templateName + "]");
                }

                // this is the dom id of the requesting "box" div element
                string requesterId = context.Request.QueryString["domid"]; // TODO: enumify magic strings

                BoxResponse boxResponse = GetBoxResponse(context, virtualBoxPath, null, true);

                boxResponses.Add(clientKey, boxResponse);

                response.Clear();
                response.Write(boxResponse.Html);
                response.Flush();
                response.SuppressContent = true;
                context.ApplicationInstance.CompleteRequest();
            }
            catch (Exception ex)
            {
                SendBoxedError(context, ex);
            }

            return;
        }
Esempio n. 2
0
        public string Template <T, PropType>(string propertyName, string templateName) where T : class
        {
            PropType propVal = Data <T>().DataProp <T, PropType>(propertyName);

            if (propVal == null)
            {
                return(string.Empty);
            }
            string      path = BoxServer.GetVirtualDataBoxPath(HttpContext.Current, typeof(PropType), templateName);
            BoxResponse resp = BoxServer.GetBoxResponse(HttpContext.Current, path, propVal);

            return(resp.Html);
        }
Esempio n. 3
0
        public static BoxResponse GetBoxResponse(HttpContext context, string virtualBoxPath, object injectionObject, bool postWindowLoad)
        {
            try
            {
                BoxResponse retVal = new BoxResponse();
                Control control = LoadControl(virtualBoxPath, false, string.Empty, injectionObject, postWindowLoad);
                //Control scriptVersion = LoadControl(virtualBoxPath, true);
                byte[] htmlBytes = GetBytes(control);
                byte[] scriptBytes = GetBytes(control, true);
                string html = Encoding.UTF8.GetString(htmlBytes); //SecreteHtml(htmlVersion);//
                string script = Encoding.ASCII.GetString(scriptBytes); //SecreteJavascript(scriptVersion);//
                retVal.Html = html;
                retVal.Script = script;
                return retVal;
            }
            catch (Exception ex)
            {
                SendBoxedError(context, ex);
            }

            return null;
        }
Esempio n. 4
0
        public static BoxResponse GetBoxResponse(HttpContext context, string virtualBoxPath, object injectionObject, bool postWindowLoad)
        {
            try
            {
                BoxResponse retVal  = new BoxResponse();
                Control     control = LoadControl(virtualBoxPath, false, string.Empty, injectionObject, postWindowLoad);
                //Control scriptVersion = LoadControl(virtualBoxPath, true);
                byte[] htmlBytes   = GetBytes(control);
                byte[] scriptBytes = GetBytes(control, true);
                string html        = Encoding.UTF8.GetString(htmlBytes);    //SecreteHtml(htmlVersion);//
                string script      = Encoding.ASCII.GetString(scriptBytes); //SecreteJavascript(scriptVersion);//
                retVal.Html   = html;
                retVal.Script = script;
                return(retVal);
            }
            catch (Exception ex)
            {
                SendBoxedError(context, ex);
            }

            return(null);
        }
Esempio n. 5
0
 public void Add(string clientKey, BoxResponse dataBoxResponse)
 {
     responses.Add(clientKey, dataBoxResponse);
 }
Esempio n. 6
0
 public void Add(string clientKey, BoxResponse dataBoxResponse)
 {
     responses.Add(clientKey, dataBoxResponse);
 }
Esempio n. 7
0
        /// <summary>
        /// This method will send the "ConglomerateScript" for the specified templateName.
        /// </summary>
        /// <remarks>A ConglomerateScript represents all the scripts required by the JsonControls
        /// in an ascx file.   The BoxUserControl class prepares the resulting ascx control for
        /// Json/Box rendering.</remarks>
        /// <param name="context">The HttpContext significant to the current request.</param>
        /// <param name="templateName">The name of an ascx file contained in the
        /// BoxServer.BoxTemplateDirectory or the virutal path to an ascx file.</param>
        /// <returns></returns>
        //public static void SendBoxScriptResponse(HttpContext context, string templateName)
        //{
        //    string virtualBoxPath = GetVirtualBoxPath(context, templateName);
        //    try
        //    {
        //        if (string.IsNullOrEmpty(virtualBoxPath))
        //            throw new InvalidOperationException("The box template could not be found: [" + templateName + "]");

        //        string requesterId = context.Request.QueryString["domid"];

        //        HttpResponse response = context.Response;
        //        //string responseString = BoxServer.GetScriptString(virtualBoxPath, requesterId);
        //        //GetDataBoxResponse(context, virtualBoxPath, null);
        //        response.Clear();
        //        response.Write(responseString);
        //        response.Flush();
        //        response.SuppressContent = true;
        //        context.ApplicationInstance.CompleteRequest();
        //    }
        //    catch (Exception ex)
        //    {
        //        SendBoxedError(context, ex);
        //    }
        //}

        /// <summary>
        /// Applies a template to the Data property of the specified JsonResult object
        /// and sends the result to the client as an html section or "Box".
        /// </summary>
        /// <param name="context">The HttpContext</param>
        /// <param name="jsonResult">The result to be Boxed</param>
        public static void SendDataBoxResponse(HttpContext context, JsonResult jsonResult)
        {
            #region check JsonResult
            HttpResponse response = context.Response;

            // Check the status of the jsonResult
            // if status is error send default error box
            if (jsonResult.Status == JsonResultStatus.Error)
            {
                SendBoxedError(response, jsonResult);
                return;
            }

            if (jsonResult.Data == null)
            {
                SendBoxedError(response, jsonResult);
                return;
            }
            #endregion


            HttpRequest request = context.Request;

            string boxTemplateName = request.QueryString["dbx"];// TODO: create enums to represent all the magic strings used by the different script components
            string clientKey       = request.QueryString["ck"];
            if (string.IsNullOrEmpty(boxTemplateName))
            {
                SendBoxedError(response, jsonResult);
                return;
            }

            object[] objectsToTemplate = new object[] { jsonResult.Data };
            if (jsonResult.Data is Array)
            {
                objectsToTemplate = (object[])jsonResult.Data;
            }

            BoxResponses responses = SingletonHelper.GetApplicationProvider <BoxResponses>();
            string       allHtml   = string.Empty;
            string       allScript = string.Empty;
            try
            {
                foreach (object objectToTemplate in objectsToTemplate)
                {
                    // getting the property variables for each object will allow objects of different types to
                    // be templated in one pass.  This incurs some processing overhead but the flexibility is
                    // worth it.
                    string[] propertyVariables = GetPropertyVariables(objectToTemplate.GetType());
                    string   virtualBoxPath    = GetVirtualDataBoxPath(context, objectToTemplate.GetType(), boxTemplateName);
                    if (string.IsNullOrEmpty(virtualBoxPath))
                    {
                        SendBoxedError(context, ExceptionHelper.CreateException <JsonInvalidOperationException>("Unable to find specified data box template ['{0}.{1}']", objectToTemplate.GetType().Name, boxTemplateName));
                    }

                    BoxResponse dataBoxResponse = GetBoxResponse(context, virtualBoxPath, objectToTemplate, true);
                    string      html            = dataBoxResponse.Html;
                    string      script          = dataBoxResponse.Script;

                    allHtml   += GetVariableReplaceText(html, propertyVariables, objectToTemplate);
                    allScript += GetVariableReplaceText(script, propertyVariables, objectToTemplate);
                }
            }
            catch (Exception ex)
            {
                SendBoxedError(context, ex);
            }

            responses.Add(clientKey, new BoxResponse(allHtml, allScript));

            response.Clear();
            response.Write(allHtml);
            response.Flush();
            response.SuppressContent = true;

            HttpContext.Current.ApplicationInstance.CompleteRequest();
            //response.End();
        }