Example #1
0
        public static long ToUnixTimeSeconds(this string _value,
                                             string fromCustomFormat = Constants.DateFormat.ServerDate)
        {
            var dateTime = SafeTypeHelper.SafeTime(_value.ToDateTime(fromCustomFormat));

            TimeSpan epochTicks = new TimeSpan(new DateTime(1970, 1, 1).Ticks);
            TimeSpan unixTicks  = new TimeSpan(dateTime.Ticks) - epochTicks;

            return((long)unixTicks.TotalSeconds);
        }
Example #2
0
        public static bool WriteLog(string logDirectory, string customLogMessage)
        {
            LogDirectory = Path.GetFullPath(logDirectory);
            if (!SafeTypeHelper.SafeString(LogDirectory[LogDirectory.Length - 1]).Equals("/"))
            {
                LogDirectory = LogDirectory + "/";
            }

            return(Write(customLogMessage, null));
        }
Example #3
0
        public static bool WriteLog(string logDirectory, string exceptionSourceName, Exception ex)
        {
            LogDirectory = Path.GetFullPath(logDirectory);
            if (!SafeTypeHelper.SafeString(LogDirectory[LogDirectory.Length - 1]).Equals("/"))
            {
                LogDirectory = LogDirectory + "/";
            }

            return(Write(exceptionSourceName, ex));
        }
Example #4
0
        public static async Task <bool> WriteLogAsync(string logDirectory, string customLogMessage)
        {
            LogDirectory = Path.GetFullPath(logDirectory);
            if (!SafeTypeHelper.SafeString(LogDirectory[LogDirectory.Length - 1]).Equals("/"))
            {
                LogDirectory = LogDirectory + "/";
            }

            return(await WriteAsync(customLogMessage, null));
        }
Example #5
0
 public static object SqlNullify(this string _value)
 {
     if (string.IsNullOrEmpty(SafeTypeHelper.SafeString(_value)))
     {
         return(DBNull.Value);
     }
     else
     {
         return(_value);
     }
 }
Example #6
0
        public static string RemoveLastChar(this string _value)
        {
            string newVal = SafeTypeHelper.SafeString(_value);

            if (!string.IsNullOrEmpty(newVal))
            {
                newVal = newVal.Remove(newVal.Length - 1);
            }

            return(newVal);
        }
Example #7
0
 public static string Nullify(this string _value)
 {
     if (string.IsNullOrEmpty(SafeTypeHelper.SafeString(_value)))
     {
         return(null);
     }
     else
     {
         return(_value);
     }
 }
Example #8
0
 public static bool IsNullOrEmpty(this string _value)
 {
     if (string.IsNullOrEmpty(SafeTypeHelper.SafeString(_value)))
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
Example #9
0
        private T FetchRESTResponse <T>(ServiceType serviceType, RestMethod methodType,
                                        string clientUrl, string resourceUrl, Dictionary <string, string> headerList, bool addDirectToBody,
                                        Dictionary <string, string> parametersList, Object parametersObj)
        {
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

            string jStr = string.Empty;

            try
            {
                string postData = string.Empty;

                if (parametersList != null)
                {
                    StringBuilder updatedParameterList = new StringBuilder();
                    foreach (var key in parametersList.Keys)
                    {
                        if (!string.IsNullOrEmpty(updatedParameterList.ToString()))
                        {
                            updatedParameterList.Append("&");
                        }

                        updatedParameterList.Append(string.Format("{0}={1}", key, parametersList[key]));
                    }

                    if (!string.IsNullOrEmpty(updatedParameterList.ToString()))
                    {
                        postData = updatedParameterList.ToString();
                    }

                    postData = (!string.IsNullOrEmpty(postData) ? string.Format("{0}{1}", "?", postData) : string.Empty);
                }
                else if (parametersObj != null)
                {
                    if (serviceType != ServiceType.XML)
                    {
                        postData = Newtonsoft.Json.JsonConvert.SerializeObject(parametersObj);
                    }
                    else
                    {
                        postData = SafeTypeHelper.SafeString(parametersObj);
                    }
                }

                if (serviceType != ServiceType.DEFAULT_OAUTH)
                {
                    if (serviceType == ServiceType.SOA || methodType == RestMethod.GET || (methodType == RestMethod.POST && parametersList != null) ||
                        (methodType == RestMethod.GET && parametersObj != null))
                    {
                        if (methodType == RestMethod.GET && parametersObj != null)
                        {
                            HttpWebRequest = (HttpWebRequest)WebRequest.CreateDefault(new Uri(string.Format("{0}{1}/{2}/", clientUrl, resourceUrl, SafeTypeHelper.SafeString(parametersObj))));
                        }
                        else
                        {
                            HttpWebRequest = (HttpWebRequest)WebRequest.CreateDefault(new Uri(string.Format("{0}{1}{2}", clientUrl, resourceUrl, postData)));
                        }
                    }
                    else
                    {
                        HttpWebRequest = (HttpWebRequest)WebRequest.CreateDefault(new Uri(string.Format("{0}{1}", clientUrl, resourceUrl)));
                    }
                }
                else
                {
                    HttpWebRequest = (HttpWebRequest)WebRequest.CreateDefault(new Uri(string.Format("{0}{1}", clientUrl, resourceUrl)));
                }

                HttpWebRequest.ContentType = "application/json";
                if (serviceType == ServiceType.DEFAULT_OAUTH)
                {
                    HttpWebRequest.ContentType = "application/x-www-form-urlencoded";
                }
                if (serviceType == ServiceType.XML)
                {
                    HttpWebRequest.ContentType = "application/xml";
                    HttpWebRequest.Accept      = "application/xml";
                }

                if (RequestTimeout > 0)
                {
                    HttpWebRequest.Timeout = RequestTimeout;
                }
                if (headerList != null)
                {
                    foreach (var key in headerList.Keys)
                    {
                        HttpWebRequest.Headers.Add(key, headerList[key]);
                    }
                }

                if (serviceType == ServiceType.SOA)
                {
                    HttpWebRequest.Method = RestMethod.POST.ToString();
                }
                else
                {
                    HttpWebRequest.Method = methodType.ToString();
                    if (methodType != RestMethod.GET)
                    {
                        if (parametersList == null && !string.IsNullOrEmpty(postData) && serviceType != ServiceType.XML)
                        {
                            byte[] postArray = System.Text.Encoding.UTF8.GetBytes(postData);
                            HttpWebRequest.ContentLength = postArray.Length;
                            using (Stream reqStream = HttpWebRequest.GetRequestStream())
                            {
                                reqStream.Write(postArray, 0, postArray.Length);
                                reqStream.Close();
                            }
                        }
                        else
                        {
                            if ((serviceType == ServiceType.DEFAULT_OAUTH ||
                                 serviceType == ServiceType.XML) && !string.IsNullOrEmpty(postData))
                            {
                                postData = postData.TrimStart('?');

                                byte[] postArray = System.Text.Encoding.UTF8.GetBytes(postData);
                                if (serviceType == ServiceType.XML)
                                {
                                    postArray = System.Text.Encoding.ASCII.GetBytes(postData);
                                }

                                HttpWebRequest.ContentLength = postArray.Length;
                                using (Stream reqStream = HttpWebRequest.GetRequestStream())
                                {
                                    reqStream.Write(postArray, 0, postArray.Length);
                                    reqStream.Close();
                                }
                            }
                            else
                            {
                                HttpWebRequest.ContentLength = 0;
                            }
                        }
                    }
                }

                //get response
                HttpWebResponse = (HttpWebResponse)HttpWebRequest.GetResponse();
                using (StreamReader sr = new StreamReader(GetStreamForResponse(HttpWebResponse)))
                    jStr = sr.ReadToEnd();

                Type typeParameterType = typeof(T);
                if (!typeParameterType.Equals(typeof(string)))
                {
                    return(Newtonsoft.Json.JsonConvert.DeserializeObject <T>(jStr));
                }
                else
                {
                    return((T)(object)jStr);
                }
            }
            catch (WebException ex)
            {
                string errorMessage = string.Empty;
                if (ex.Response != null)
                {
                    HttpWebResponse = (HttpWebResponse)ex.Response;
                    using (var stream = GetStreamForResponse(ex.Response))
                    {
                        using (var reader = new StreamReader(stream))
                        {
                            errorMessage = reader.ReadToEnd();
                        }
                    }
                }

                ClientError restException = new ClientError()
                {
                    StatusCode   = HttpWebResponse != null ? HttpWebResponse.StatusCode : HttpStatusCode.BadRequest,
                    InnerMessage = ex.Message,
                    Content      = errorMessage
                };
                throw new Exception(Newtonsoft.Json.JsonConvert.SerializeObject(restException));
            }
            catch (Exception ex)
            {
                ClientError restException = new ClientError()
                {
                    StatusCode   = HttpWebResponse != null ? HttpWebResponse.StatusCode : HttpStatusCode.BadRequest,
                    InnerMessage = ex.Message,
                    Content      = jStr
                };
                throw new Exception(Newtonsoft.Json.JsonConvert.SerializeObject(restException));
            }
        }
Example #10
0
 public static string ToSafeString(this string _value)
 {
     return(SafeTypeHelper.SafeString(_value));
 }
Example #11
0
        private static bool Write(string logContent, Exception ex, bool splitBySize = false, long splitSize = 5000000)
        {
            bool Status = false;

            DateTime CurrentDateTime       = DateTime.Now;
            string   CurrentDateTimeString = CurrentDateTime.ToString();

            CheckCreateLogDirectory(LogDirectory);

            StringBuilder logMessage = new StringBuilder();

            if (ex != null)
            {
                logMessage.Append(GetExceptionString(ex, logContent));
            }
            else
            {
                logMessage.Append(logContent);
            }

            if (!string.IsNullOrEmpty(SafeTypeHelper.SafeString(logMessage)))
            {
                string logLine = BuildLogLine(CurrentDateTime, SafeTypeHelper.SafeString(logMessage));

                string fileName = string.Empty;
                if (!splitBySize)
                {
                    fileName = DateTime.Now.ToString("ddMMyyyy") + ".txt";
                }
                else
                {
                    fileName = "LOGS.txt";
                }

                if (File.Exists(LogDirectory + fileName))
                {
                    FileInfo fileInfo = new FileInfo(LogDirectory + fileName);
                    if (splitBySize && fileInfo.Length > splitSize)
                    {
                        System.IO.File.Move(LogDirectory + fileName, LogDirectory + "Logs_" + DateTime.Now.ToString("ddMMyyyy-HHmmss") + ".txt");
                    }
                }

                lock (synchronizerLogObj)
                {
                    StreamWriter oStreamWriter = null;
                    try
                    {
                        oStreamWriter = new StreamWriter(LogDirectory + fileName, true);
                        oStreamWriter.WriteLine(logLine);
                        Status = true;
                    }
                    catch
                    { }
                    finally
                    {
                        if (oStreamWriter != null)
                        {
                            oStreamWriter.Close();
                        }
                    }
                }
            }

            //Delete 2 days before logs
            //DirectoryInfo di = new DirectoryInfo(LOG_DIRECTORY);
            //if (di.GetFiles().Length > 0)
            //    foreach (FileInfo fileInfo in di.GetFiles())
            //    {
            //        if (fileInfo.CreationTime <= DateTime.Now.AddDays(-2))
            //            fileInfo.Delete();
            //    }

            return(Status);
        }