Ejemplo n.º 1
0
        protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            Stopwatch watch = new Stopwatch();
            watch.Start();
            HttpMessage message = new HttpMessage(request);
            string requestData = request.Content.ReadAsStringAsync().Result;
            StringBuilder requestContent = new StringBuilder();

            #region log

            try
            {
                if (Log.IsInfoEnabled)
                {

                    string api = message.GetRequestUriTemplate();

                    requestContent.Append("\r\n\tRequest ClientIP : ").Append(message.GetClientIP()).Append("\r\n");
                    requestContent.Append("\tRequest URL : ").Append(message.GetHttpMethod()).Append(" ").Append(message.GetRequestUri()).Append("\r\n");
                    requestContent.Append("\tRequest Hearder : ");
                    requestContent.Append(message.GetHttpHeader());

                    if (!String.IsNullOrEmpty(requestData))
                    {
                        requestContent.Append("\r\n\tRequest Body : ").Append(requestData);
                    }
                }
            }
            catch (Exception exception)
            {
                Log.Error(exception.ToString());
            }

            #endregion

            SetContext(request);

            // Token validation
            if (Context.RequestUriTemplate == "apis/v1/token")
            {
                return base.SendAsync(request, cancellationToken);
            }

            if (String.IsNullOrEmpty(Context.Token))
            {
                return HttpResponseHelper.SendAsync(UnAuthenticationResponse());
            }
            else
            {
                TokenRepository tr = new TokenRepository();
                var tokenModel = tr.GetTokenModel(Context.Token);
                if (tokenModel == null)
                {
                    return HttpResponseHelper.SendAsync(UnAuthenticationResponse());
                }
                else
                {
                    Context.LoginName = tokenModel.UserName;
                    var response = base.SendAsync(request, cancellationToken);

                    #region Response Log

                    if (Log.IsInfoEnabled)
                    {
                        try
                        {
                            Task<string> responseMessage = null;

                            if (response.Result != null && response.Result.Content != null)
                            {
                                responseMessage = response.Result.Content.ReadAsStringAsync();
                            }

                            watch.Stop();
                            int requestExcuteTime = Convert.ToInt32(watch.ElapsedMilliseconds);
                            watch = null;
                            requestContent.Append("\r\n\tResponse : ");
                            if (responseMessage != null)
                            {
                                requestContent.Append(responseMessage.Result);
                            }
                            requestContent.Append("\r\n\tCost(ms) : ").Append(requestExcuteTime);

                            Log.Info(requestContent.ToString());
                        }
                        catch (Exception exception)
                        {
                            Log.Error(exception.ToString());
                        }
                    }
                    #endregion

                    return response;
                }
            }
        }
Ejemplo n.º 2
0
 private void SetContext(HttpRequestMessage request)
 {
     // Get neccessary data from http request message
     HttpMessage message = new HttpMessage(request);
     Context.ClientIP = message.GetClientIP();
     //Context.IsAuthed = false;
     Context.Token = message.GetToken();
     Context.RequestUri = message.GetRequestUri();
     Context.RequestUriTemplate = message.GetRequestUriTemplate();
     Context.HttpMethod = message.GetHttpMethod();
     Context.HttpHeader = message.GetHttpHeader();
     Context.ContentType = message.GetContentType();
     Context.ContentLength = message.GetContentLength();
 }