예제 #1
0
 /// <summary>
 /// Zaloguje spravu
 /// </summary>
 /// <param name="exception">Chyba ktoru chceme zalogovat</param>
 public static void Trace(Exception exception)
 {
     if (exception != null)
     {
         WebUtility.Trace(TraceTypes.Error, exception.ToString());
     }
 }
예제 #2
0
        /// <summary>
        /// GetProfile
        /// </summary>
        public override OAuthUserProfile GetProfile(String key, String secret, String code, String url)
        {
            //get token
            url = String.Format("https://graph.facebook.com/oauth/access_token?client_id={0}&redirect_uri={1}&client_secret={2}&code={3}", key, url, secret, code);
            WebUtility.Trace("Url: {0}", url);
            String response = this.InternalHttpRequest(url);

            WebUtility.Trace("Response: {0}", (String.IsNullOrWhiteSpace(response) ? "NULL" : response));
            //validate token
            if (String.IsNullOrWhiteSpace(response))
            {
                return(null);
            }
            //get profile
            url = String.Format("https://graph.facebook.com/me?{0}&fields=email,first_name,last_name,gender,hometown,locale,name,id,timezone,picture", response);
            WebUtility.Trace("Url: {0}", url);
            response = this.InternalHttpRequest(url);
            WebUtility.Trace("Response: {0}", (String.IsNullOrWhiteSpace(response) ? "NULL" : response));
            //validate profile
            if (String.IsNullOrWhiteSpace(response))
            {
                return(null);
            }
            //parse model
            return(this.InternalParseUserProfile(response));
        }
 /// <summary>
 /// Vytvori request Id do hlavicky
 /// </summary>
 /// <param name="request">Request do ktoreho chceme pridat id</param>
 public static void TraceRequestHeaders(this HttpRequest request)
 {
     if (request != null)
     {
         StringBuilder builder = new StringBuilder();
         builder.Append("Request Header:");
         foreach (String key in request.Headers)
         {
             builder.Append(Environment.NewLine);
             builder.AppendFormat("{0} -> {1}", key, request.Headers[key]);
         }
         WebUtility.Trace(builder.ToString());
     }
 }
예제 #4
0
 /// <summary>
 /// InternalHttpRequest
 /// </summary>
 protected String InternalHttpRequest(String url)
 {
     try
     {
         HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
         using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
         {
             if (response.StatusCode != HttpStatusCode.OK)
             {
                 throw new Exception(String.Format("Server error (HTTP {0}: {1}).", response.StatusCode, response.StatusDescription));
             }
             using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
             {
                 return(streamReader.ReadToEnd());
             }
         }
     }
     catch (Exception ex)
     {
         WebUtility.Trace(ex);
         return(null);
     }
 }
예제 #5
0
        /// <summary>
        /// InternalSendEmail
        /// </summary>
        private Boolean InternalSendEmail(String viewName, String masterName, Object model, String subject, String copyAddress, String senderAddress, String replyAddress, params String[] address)
        {
            try
            {
                // find view
                ViewEngineResult viewEngineResult = ViewEngines.Engines.FindView(this.ControllerContext, viewName, masterName);

                if (viewEngineResult == null)
                {
                    throw new FileNotFoundException("View cannot be found.");
                }

                // get view
                var view = viewEngineResult.View;
                this.ControllerContext.Controller.ViewData.Model = model;

                // render
                using (StringWriter stringWriter = new StringWriter())
                {
                    var ctx = new ViewContext(this.ControllerContext,
                                              view,
                                              this.ControllerContext.Controller.ViewData,
                                              this.ControllerContext.Controller.TempData,
                                              stringWriter);
                    view.Render(ctx, stringWriter);

                    //send email
                    return(this.InternalSendEmail(stringWriter.ToString(), subject, copyAddress, senderAddress, replyAddress, address));
                }
            }
            catch (Exception ex)
            {
                WebUtility.Trace(ex);
                return(false);
            }
        }
        /// <summary>
        /// Vypise chyby aktualneho formu
        /// </summary>
        /// <param name="controller">Controller this</param>
        public static void PrintModelStateError(this ControllerBase controller)
        {
            WebUtility.Trace("---------- Errors ----------");
            foreach (ModelState modelState in controller.ViewData.ModelState.Values)
            {
                foreach (ModelError error in modelState.Errors)
                {
                    WebUtility.Trace(error.ErrorMessage);
                }
            }
            //zalogujeme request
            HttpContext context = HttpContext.Current;

            if (context != null)
            {
                HttpRequest request = context.Request;
                if (request != null)
                {
                    WebUtility.Trace("---------- Request ----------");
                    request.TraceRequest();
                }
            }
            WebUtility.Trace("----------------------------");
        }
        /// <summary>
        /// Rozparsuje vstupne data z ktorych vyberie guid, pripadne vysku a sirku obrazku
        /// </summary>
        /// <param name="value">Hodnota ktoru chceme parsovat</param>
        /// <param name="key">Jedinecny identifikator obrazku alebo null</param>
        /// <param name="width">sirka obrazku alebo null</param>
        /// <param name="height">Vyska obrazku alebo null;</param>
        /// <returns>True = parsovanie bolo uspesne, inak false</returns>
        private Boolean internalParseImageUrl(ref String value, out Nullable <Guid> key, out Nullable <int> width, out Nullable <int> height)
        {
            //inicialize out
            key    = null;
            width  = null;
            height = null;
            //vyparsujeme data
            Match match = WebImageHandler.m_regex.Match(value);

            if (match.Success)
            {
                try
                {
                    key = match.Groups["guid"].Value.ToGuidWithoutDash();
                    int int_width  = 0;
                    int int_height = 0;
                    if (int.TryParse(match.Groups["width"].Value, out int_width))
                    {
                        width = int_width;
                    }

                    if (int.TryParse(match.Groups["height"].Value, out int_height))
                    {
                        height = int_height;
                    }
                    //ak nie je zadana presna velkost
                    if ((!width.HasValue || width == 0) && (!height.HasValue || height == 0))
                    {
                        String subType = match.Groups["subType"].Value;
                        if (!String.IsNullOrWhiteSpace(subType))
                        {
                            //odstranime subtype
                            value = value.Replace(String.Format("{0}/", subType), String.Empty);
                        }
                        //ak existuje konfiguracia
                        if (WebConfiguration.ImageConfiguration.ContainsKey(subType))
                        {
                            Size size = WebConfiguration.ImageConfiguration[subType];
                            if (!size.IsEmpty)
                            {
                                width  = size.Width;
                                height = size.Height;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    WebUtility.Trace(ex);
                    return(false);
                }
                //ak ide o poziadavku na velky obrazok
                if (width == null || width > 1000)
                {
                    width = null;
                }
                if (height == null || height > 1000)
                {
                    height = null;
                }
                if (key != null && !key.Value.IsEmpty())
                {
                    return(true);
                }
            }
            return(false);
        }
예제 #8
0
 /// <summary>
 /// Zaloguje spravu
 /// </summary>
 /// <param name="message">Sprava ktoru chceme zalogovat</param>
 /// <param name="args">Argumenty pre String.Format</param>
 public static void Trace(String message, params Object[] args)
 {
     WebUtility.Trace(TraceTypes.Verbose, message, args);
 }
예제 #9
0
 /// <summary>
 /// Zaloguje vzniknutu chybu do suboru
 /// </summary>
 /// <param name="exception">Chyba ktora vznikla</param>
 public static void OnException(Exception exception)
 {
     WebUtility.Trace(exception);
     WebUtility.TraceCurrentRequest();
 }
예제 #10
0
 /// <summary>
 /// Zaloguje pozadovanu spravu do suboru
 /// </summary>
 /// <param name="type">Typ logovania</param>
 /// <param name="message">Sprava ktoru chceme zalogovat</param>
 /// <param name="args">Argumenty pre String.Format(message, args)</param>
 public static void OnTrace(TraceTypes type, String message, params Object[] args)
 {
     WebUtility.Trace(type, message, args);
 }
        /// <summary>
        /// Vypise vsetky podstate informacie z requestu
        /// </summary>
        /// <param name="request">Request z ktoreho chceme vypisat informacie</param>
        public static void TraceRequest(this HttpRequest request)
        {
            if (request != null)
            {
                StringBuilder builder = new StringBuilder();
                builder.Append("Request:");
                if (request.AcceptTypes != null && request.AcceptTypes.Length > 0)
                {
                    builder.Append(Environment.NewLine);
                    builder.AppendFormat("AcceptTypes: {0}", String.Join(",", request.AcceptTypes));
                }
                if (request.Form != null && request.Form.Count > 0)
                {
                    builder.Append(Environment.NewLine);
                    builder.AppendFormat("Form: {0}", request.Form.ToStringAllValues());
                }
                if (!String.IsNullOrWhiteSpace(request.UserAgent))
                {
                    builder.Append(Environment.NewLine);
                    builder.AppendFormat("UserAgent: {0}", request.UserAgent);
                }
                if (!String.IsNullOrWhiteSpace(request.HttpMethod))
                {
                    builder.Append(Environment.NewLine);
                    builder.AppendFormat("HttpMethod: {0}", request.HttpMethod);
                }
                if (!String.IsNullOrWhiteSpace(request.UserHostAddress))
                {
                    builder.Append(Environment.NewLine);
                    builder.AppendFormat("UserHostAddress: {0}", request.UserHostAddress);
                }
                if (request.UserLanguages != null && request.UserLanguages.Length > 0)
                {
                    builder.Append(Environment.NewLine);
                    builder.AppendFormat("AcceptTypes: {0}", String.Join(",", request.UserLanguages));
                }
                if (request.UrlReferrer != null)
                {
                    builder.Append(Environment.NewLine);
                    builder.AppendFormat("UrlReferrer: {0}", request.UrlReferrer);
                }
                if (!String.IsNullOrWhiteSpace(request.RawUrl))
                {
                    builder.Append(Environment.NewLine);
                    builder.AppendFormat("RawUrl: {0}", request.RawUrl);
                }

                Stream inputStream = request.InputStream;
                if (inputStream != null)
                {
                    inputStream.Seek(0, System.IO.SeekOrigin.Begin);
                    using (StreamReader reader = new StreamReader(inputStream))
                    {
                        String body = reader.ReadToEnd();
                        if (!String.IsNullOrWhiteSpace(body))
                        {
                            builder.AppendFormat("Body: {0}", body);
                        }
                    }
                }


                WebUtility.Trace(builder.ToString());
            }
        }