Ejemplo n.º 1
0
 /// <summary>
 /// Packages the output. <c>Flush()</c> would be called in this method.
 /// </summary>
 /// <param name="response">The response.</param>
 /// <param name="data">The data.</param>
 /// <param name="baseException">The base exception.</param>
 /// <param name="acceptEncoding">Name of the compress.</param>
 /// <param name="noBody">if set to <c>true</c> [no body].</param>
 /// <param name="settings">The settings.</param>
 /// <returns>System.Object.</returns>
 protected virtual void PackageOutput(HttpResponse response, object data, BaseException baseException = null, string acceptEncoding = null, bool noBody = false, RestApiSettings settings = null)
 {
     PackageResponse(response, data, baseException, acceptEncoding, noBody, settings);
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Packages the response. <c>Flush()</c> would be called in this method.
        /// </summary>
        /// <param name="response">The response.</param>
        /// <param name="data">The data.</param>
        /// <param name="ex">The ex.</param>
        /// <param name="acceptEncoding">The accept encoding.</param>
        /// <param name="noBody">if set to <c>true</c> [no body].</param>
        /// <param name="settings">The settings.</param>
        public static void PackageResponse(HttpResponseBase response, object data, BaseException ex = null, string acceptEncoding = null, bool noBody = false, RestApiSettings settings = null)
        {
            if (response != null)
            {
                if (settings == null)
                {
                    settings = defaultRestApiSettings;
                }

                var objectToReturn = ex != null ? (settings.EnableOutputFullExceptionInfo ? ex.ToExceptionInfo() : new SimpleExceptionInfo
                {
                    Message = ex.Hint != null ? (ex.Hint.Message ?? ex.Hint.Cause) : ex.RootCause.Message,
                    Data = data == null ? null : JObject.FromObject(data),
                    Code = ex.Hint?.Code ?? ex.Code
                } as object) : data;

                response.Headers.Add(HttpConstants.HttpHeader.SERVERNAME, EnvironmentCore.ServerName);
                response.Headers.AddIfNotNull(HttpConstants.HttpHeader.TRACEID, ApiTraceContext.TraceId);

                response.StatusCode = (int)(ex == null ? (noBody ? HttpStatusCode.NoContent : HttpStatusCode.OK) : ex.Code.ToHttpStatusCode());

                if (!noBody)
                {
                    response.ContentType = "application/json";

                    if (settings.EnableContentCompression)
                    {
                        acceptEncoding = acceptEncoding.SafeToString().ToLower();
                        if (acceptEncoding.Contains("gzip"))
                        {
                            response.Filter = new System.IO.Compression.GZipStream(response.Filter,
                                                  System.IO.Compression.CompressionMode.Compress);
                            response.Headers.Remove(HttpConstants.HttpHeader.ContentEncoding);
                            response.AppendHeader(HttpConstants.HttpHeader.ContentEncoding, "gzip");
                        }
                        else if (acceptEncoding.Contains("deflate"))
                        {
                            response.Filter = new System.IO.Compression.DeflateStream(response.Filter,
                                                System.IO.Compression.CompressionMode.Compress);
                            response.Headers.Remove(HttpConstants.HttpHeader.ContentEncoding);
                            response.AppendHeader(HttpConstants.HttpHeader.ContentEncoding, "deflate");
                        }
                    }

                    response.Write(objectToReturn.ToJson(true, JsonConverters));
                }

                response.Headers.Add(HttpConstants.HttpHeader.SERVEREXITTIME, DateTime.UtcNow.ToFullDateTimeTzString());
                response.Flush();
            }
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Packages the response. <c>Flush()</c> would be called in this method.
 /// </summary>
 /// <param name="response">The response.</param>
 /// <param name="data">The data.</param>
 /// <param name="ex">The ex.</param>
 /// <param name="acceptEncoding">Name of the compression.</param>
 /// <param name="noBody">if set to <c>true</c> [no body].</param>
 /// <param name="settings">The settings.</param>
 /// <returns>System.Object.</returns>
 public static void PackageResponse(HttpResponse response, object data, BaseException ex = null, string acceptEncoding = null, bool noBody = false, RestApiSettings settings = null)
 {
     if (response != null)
     {
         PackageResponse(new HttpResponseWrapper(response), data, ex, acceptEncoding, noBody, settings);
     }
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Packages the response. <c>Flush()</c> would be called in this method.
        /// </summary>
        /// <param name="response">The response.</param>
        /// <param name="data">The data.</param>
        /// <param name="ex">The ex.</param>
        /// <param name="acceptEncoding">The accept encoding.</param>
        /// <param name="noBody">if set to <c>true</c> [no body].</param>
        /// <param name="contentType">Type of the content.</param>
        /// <param name="settings">The settings.</param>
        public static void PackageResponse(HttpResponseBase response, object data, BaseException ex = null, string acceptEncoding = null, bool noBody = false, string contentType = null, RestApiSettings settings = null)
        {
            if (response != null)
            {
                if (settings == null)
                {
                    settings = defaultRestApiSettings;
                }

                var objectToReturn = ex != null ? (settings.OmitExceptionDetail ? ex.ToSimpleExceptionInfo() : ex.ToExceptionInfo()) : data;

                response.Headers.Add(HttpConstants.HttpHeader.SERVERNAME, EnvironmentCore.ServerName);
                response.Headers.AddIfNotNullOrEmpty(HttpConstants.HttpHeader.TRACEID, ApiTraceContext.TraceId);

                response.StatusCode = (int)(ex == null ? (noBody ? HttpStatusCode.NoContent : HttpStatusCode.OK) : ex.Code.ToHttpStatusCode());

                if (!noBody)
                {
                    response.ContentType = contentType.SafeToString(HttpConstants.ContentType.Json);

                    if (settings.EnableContentCompression)
                    {
                        acceptEncoding = acceptEncoding.SafeToString().ToLower();
                        if (acceptEncoding.Contains(HttpConstants.HttpValues.GZip))
                        {
                            response.Filter = new System.IO.Compression.GZipStream(response.Filter,
                                                  System.IO.Compression.CompressionMode.Compress);
                            response.Headers.Remove(HttpConstants.HttpHeader.ContentEncoding);
                            response.AppendHeader(HttpConstants.HttpHeader.ContentEncoding, HttpConstants.HttpValues.GZip);
                        }
                        else if (acceptEncoding.Contains("deflate"))
                        {
                            response.Filter = new System.IO.Compression.DeflateStream(response.Filter,
                                                System.IO.Compression.CompressionMode.Compress);
                            response.Headers.Remove(HttpConstants.HttpHeader.ContentEncoding);
                            response.AppendHeader(HttpConstants.HttpHeader.ContentEncoding, "deflate");
                        }
                    }

                    response.Write(objectToReturn.ToJson(true, JsonConverters));
                }

                response.Headers.Add(HttpConstants.HttpHeader.SERVEREXITTIME, DateTime.UtcNow.ToFullDateTimeTzString());
            }
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Fills the exit information.
 /// </summary>
 /// <param name="exception">The exception.</param>
 /// <param name="exitStamp">The exit stamp.</param>
 /// <param name="appIdentifier">The application identifier.</param>
 /// <param name="serverIdentifier">The server identifier.</param>
 /// <param name="level">The level.</param>
 /// <param name="operatorIdentifier">The operator identifier.</param>
 internal void FillExitInfo(BaseException exception = null, DateTime? exitStamp = null, string appIdentifier = null, string serverIdentifier = null, ExceptionInfo.ExceptionCriticality level = ExceptionInfo.ExceptionCriticality.Error, string operatorIdentifier = null)
 {
     this.ExitStamp = exitStamp ?? DateTime.UtcNow;
     this.Exception = exception.ToExceptionInfo(appIdentifier, serverIdentifier, level, operatorIdentifier);
 }
 /// <summary>
 /// Handles the unauthorized action.
 /// </summary>
 /// <param name="filterContext">The filter context.</param>
 /// <param name="baseException">The base exception.</param>
 protected virtual void HandleUnauthorizedAction(ActionExecutingContext filterContext, BaseException baseException)
 {
     if (filterContext.HttpContext.Request.IsAjaxRequest())
     {
         ApiHandlerBase.PackageResponse(filterContext.HttpContext.Response, null, baseException, settings: settings);
         filterContext.Result = null;
     }
     else
     {
         filterContext.Result = new RedirectToRouteResult(routeValues: new RouteValueDictionary
         {
             {"controller", "Error"},
             {"action", "Index"},
             {"code", 401},
             {"minor", baseException.Code.Minor},
             {"message", baseException.Message}
         });
     }
 }