Ejemplo n.º 1
0
 private string GET_JSON(WSClientMeta meta, object response)
 {
     try
     {
         JsonSerializerSettings settings = new JsonSerializerSettings();
         settings.Converters.Add(new WSRecordJsonConverter(meta.Request.OUT_FIELDS, meta.Request, meta.ClientFunctions, meta.Request.DBContext, response.GetType()));
         return(JsonConvert.SerializeObject(response, Newtonsoft.Json.Formatting.Indented, settings));
     }
     catch (Exception) { }
     return(string.Empty);
 }
Ejemplo n.º 2
0
 public WSResponse(WSClientMeta _meta)
 {
     meta = _meta;
     if (meta.Request != null && meta.Request.Security != null)
     {
         if (meta.Request.Security.IsLogged && meta.Request.Security.AuthToken.User.role != iostatus.CODE)
         {
             iostatus = WSStatus.NONE.clone(meta.Request.Security.AuthToken.User.role);
         }
         init();
     }
     else
     {
         iostatus = WSStatus.NONE.clone(WSConstants.ACCESS_LEVEL.READ);
     }
 }
Ejemplo n.º 3
0
 public WSStaticResponse(WSClientMeta _meta) : base(_meta)
 {
 }
Ejemplo n.º 4
0
 public WSSystemResponse(WSClientMeta _meta) : base(_meta)
 {
 }
Ejemplo n.º 5
0
        public void proceed(HttpContext context)
        {
            List <string> statuses = new List <string>();

            try
            {
                object   response = null;
                DateTime now      = DateTime.Now;
                long     millis   = 0;

                Meta = (WSClientMeta)Activator.CreateInstance(typeof(T), new object[] { context });

                init(ref response);

                statuses.Add($"{{URL1:[{Meta.Request.Url.PathAndQuery}],INPUT1:{{{Meta.Request.INPUT.Select(x => x.Key + ":" + x.Value).Aggregate((a, b) => a + "," + b)}}}");

                millis = (DateTime.Now.Ticks - now.Ticks) / 10000; now = DateTime.Now;
                statuses.Add("2. Init : " + millis + " millis");

                if (response != null)
                {
                    if (Meta.Request.FORMAT.Equals(WSConstants.FORMAT.JSON_FORMAT) || response is V1SystemResponseRecord)
                    {
                        #region JSON RESPONSE
                        context.Response.ContentType = "application/json; charset=UTF-8";
                        string responseJson = GET_JSON(Meta, response);
                        context.Response.Write(responseJson);
                        #endregion
                    }
                    else if (Meta.Request.FORMAT.Equals(WSConstants.FORMAT.JSONP_FORMAT))
                    {
                        #region JSONP RESPONSE
                        context.Response.ContentType = "application/javascript; charset=UTF-8";
                        context.Response.Write(Meta.Request.CALLBACK + "(" + GET_JSON(Meta, response) + ")");
                        #endregion
                    }
                    else if (Meta.Request.FORMAT.Equals(WSConstants.FORMAT.XML_FORMAT))
                    {
                        #region XML RESPONSE
                        context.Response.ContentType = "application/xml; charset=UTF-8";
                        using (XmlWriter writer = new XmlTextWriter(context.Response.OutputStream, Encoding.Unicode))
                        {
                            if (response is IXmlSerializable)
                            {
                                ((IXmlSerializable)response).WriteXml(writer);
                            }
                            else
                            {
                                XmlSerializer serializer = new XmlSerializer(response.GetType());
                                serializer.Serialize(writer, response);
                            }
                        }
                        #endregion
                    }
                    else if (Meta.Request.FORMAT.Equals(WSConstants.FORMAT.PDF_FORMAT))
                    {
                        #region PDF RESPONSE
                        BinaryPDFResponse BResponse = (BinaryPDFResponse)response;
                        if (string.IsNullOrEmpty(BResponse.URL))
                        {
                            context.Response.ContentType = "text/plain";
                            context.Response.Write("Error! Could not find file" + (BResponse.ID < 0 ? "" : string.Format(" with ID:{0}", BResponse.ID)) + ".");
                        }
                        else
                        {
                            context.Response.Redirect(BResponse.URL, true);
                        }
                        #endregion
                    }
                    else if (Meta.Request.FORMAT.Equals(WSConstants.FORMAT.IMAGE_FORMAT))
                    {
                        #region IMAGE RESPONSE
                        BinaryImageResponse IResponse = (BinaryImageResponse)response;
                        if (IResponse.image == null)
                        {
                            context.Response.ContentType = "text/plain";
                            context.Response.Write(string.IsNullOrEmpty(IResponse.status) ? ("Error! Could not find image" + (IResponse.ID < 0 ? "" : string.Format(" with ID:{0}", IResponse.ID)) + ".") : IResponse.status);
                        }
                        else
                        {
                            context.Response.ContentType = "image/jpeg";
                            context.Response.AddHeader("Content-disposition", IResponse.load_mode.NAME + "; filename=HA" + IResponse.ID + ".jpg");
                            try { IResponse.image.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); }
                            catch (Exception) { IResponse.image.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png); }
                        }
                        #endregion
                    }
                }
                millis = (DateTime.Now.Ticks - now.Ticks) / 10000; now = DateTime.Now;
                statuses.Add("2. Print (serialize) : " + millis + " millis");
            }
            catch (ArgumentException ex)
            {
                #region THROW EXCEPTION (as short description text)
                context.Response.ContentType = "text/plain";
                context.Response.Write("[ARGUMENT EXCEPTION:[" + ex.Message + (":" + ex.StackTrace) + "]");
                #endregion
            }
            catch (Exception ex)
            {
                #region THROW EXCEPTION (as short description text)
                context.Response.ContentType = "text/plain";
                context.Response.Write($"[GENERAL EXCEPTION:[{ex.Message}:{ex.StackTrace}][{statuses.Aggregate((a, b) => a + "," + b)}]");
                #endregion
            }
            finally
            {
                #region CLOSE RESPONSE
                Meta.CleanUp();

                context.Response.OutputStream.Flush();
                context.Response.End();
                #endregion
            }
        }