|
| 1 | +namespace Rollbar.Net.AspNet.WebApi |
| 2 | +{ |
| 3 | + using System.Net.Http; |
| 4 | + using System.Net.Http.Headers; |
| 5 | + using System.Web.Http.Controllers; |
| 6 | + using System.Collections.Generic; |
| 7 | + using System.Linq; |
| 8 | + using Rollbar.DTOs; |
| 9 | + |
| 10 | + /// <summary> |
| 11 | + /// Class HttpActionContextPackageDecorator. |
| 12 | + /// Implements the <see cref="Rollbar.RollbarPackageDecoratorBase" /> |
| 13 | + /// </summary> |
| 14 | + /// <seealso cref="Rollbar.RollbarPackageDecoratorBase" /> |
| 15 | + public class HttpActionContextPackageDecorator |
| 16 | + : RollbarPackageDecoratorBase |
| 17 | + { |
| 18 | + /// <summary> |
| 19 | + /// The HTTP action context |
| 20 | + /// </summary> |
| 21 | + private readonly HttpActionContext _httpActionContext; |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Initializes a new instance of the <see cref="HttpActionContextPackageDecorator"/> class. |
| 25 | + /// </summary> |
| 26 | + /// <param name="packageToDecorate">The package to decorate.</param> |
| 27 | + /// <param name="httpActionContext">The HTTP action context.</param> |
| 28 | + public HttpActionContextPackageDecorator(IRollbarPackage packageToDecorate, HttpActionContext httpActionContext) |
| 29 | + : this(packageToDecorate, httpActionContext, false) |
| 30 | + { |
| 31 | + } |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Initializes a new instance of the <see cref="HttpActionContextPackageDecorator"/> class. |
| 35 | + /// </summary> |
| 36 | + /// <param name="packageToDecorate">The package to decorate.</param> |
| 37 | + /// <param name="httpActionContext">The HTTP action context.</param> |
| 38 | + /// <param name="mustApplySynchronously">if set to <c>true</c> [must apply synchronously].</param> |
| 39 | + public HttpActionContextPackageDecorator(IRollbarPackage packageToDecorate, HttpActionContext httpActionContext, bool mustApplySynchronously) |
| 40 | + : base(packageToDecorate, mustApplySynchronously) |
| 41 | + { |
| 42 | + this._httpActionContext = httpActionContext; |
| 43 | + } |
| 44 | + |
| 45 | + /// <summary> |
| 46 | + /// Decorates the specified rollbar data. |
| 47 | + /// </summary> |
| 48 | + /// <param name="rollbarData">The rollbar data.</param> |
| 49 | + protected override void Decorate(Data rollbarData) |
| 50 | + { |
| 51 | + if (this._httpActionContext == null) |
| 52 | + { |
| 53 | + return; //nothing to do... |
| 54 | + } |
| 55 | + |
| 56 | + if (rollbarData.Request == null) |
| 57 | + { |
| 58 | + rollbarData.Request = new Request(); |
| 59 | + } |
| 60 | + |
| 61 | + if (rollbarData.Request.Params == null) |
| 62 | + { |
| 63 | + rollbarData.Request.Params = new Dictionary<string, object>(); |
| 64 | + } |
| 65 | + rollbarData.Request.Params["controller"] = |
| 66 | + this._httpActionContext.ControllerContext?.ControllerDescriptor?.ControllerName; |
| 67 | + //rollbarData.Request.Params["controller_properties"] = |
| 68 | + // this._httpActionContext.ControllerContext?.ControllerDescriptor?.Properties; |
| 69 | + rollbarData.Request.Params["action"] = |
| 70 | + this._httpActionContext.ActionDescriptor?.ActionName; |
| 71 | + //rollbarData.Request["action_properties"] = |
| 72 | + // this._httpActionContext.ActionDescriptor?.Properties; |
| 73 | + |
| 74 | + if (this._httpActionContext.Request != null) |
| 75 | + { |
| 76 | + rollbarData.Request.Url = this._httpActionContext.Request.RequestUri?.PathAndQuery; |
| 77 | + rollbarData.Request.Method = this._httpActionContext.Request.Method?.ToString().ToUpper(); |
| 78 | + rollbarData.Response.Headers = Convert(this._httpActionContext.Request.Headers); |
| 79 | + rollbarData.Request.PostBody = ReadInHttpMessageBody(this._httpActionContext.Request.Content); |
| 80 | + rollbarData.Request["request_properties"] = this._httpActionContext.Request.Properties; |
| 81 | + } |
| 82 | + |
| 83 | + if (this._httpActionContext.Response != null) |
| 84 | + { |
| 85 | + if (rollbarData.Response == null) |
| 86 | + { |
| 87 | + rollbarData.Response = new Response(); |
| 88 | + } |
| 89 | + |
| 90 | + var responseStatusCode = this._httpActionContext.Response.StatusCode; |
| 91 | + if (responseStatusCode != null) |
| 92 | + { |
| 93 | + rollbarData.Response.StatusCode = (int)responseStatusCode; |
| 94 | + } |
| 95 | + |
| 96 | + if (this._httpActionContext.Response.Headers != null) |
| 97 | + { |
| 98 | + rollbarData.Response.Headers = Convert(this._httpActionContext.Response.Headers); |
| 99 | + } |
| 100 | + |
| 101 | + rollbarData.Response.Body = ReadInHttpMessageBody(this._httpActionContext.Response.Content); |
| 102 | + rollbarData.Response["reason_phrase"] = this._httpActionContext.Response.ReasonPhrase; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + /// <summary> |
| 107 | + /// Reads the in HTTP message body. |
| 108 | + /// </summary> |
| 109 | + /// <param name="httpContent">Content of the HTTP.</param> |
| 110 | + /// <returns>System.String.</returns> |
| 111 | + private string ReadInHttpMessageBody(HttpContent httpContent) |
| 112 | + { |
| 113 | + if (httpContent == null) |
| 114 | + { |
| 115 | + return null; |
| 116 | + } |
| 117 | + var resultTask = this._httpActionContext.Response?.Content?.ReadAsStringAsync(); |
| 118 | + if (resultTask == null) |
| 119 | + { |
| 120 | + return null; |
| 121 | + } |
| 122 | + resultTask.Wait(); |
| 123 | + return resultTask.Result; |
| 124 | + } |
| 125 | + |
| 126 | + /// <summary> |
| 127 | + /// Converts the specified HTTP headers. |
| 128 | + /// </summary> |
| 129 | + /// <param name="httpHeaders">The HTTP headers.</param> |
| 130 | + /// <returns>IDictionary<System.String, System.String>.</returns> |
| 131 | + private IDictionary<string, string> Convert(HttpHeaders httpHeaders) |
| 132 | + { |
| 133 | + if (httpHeaders == null) |
| 134 | + { |
| 135 | + return null; |
| 136 | + } |
| 137 | + |
| 138 | + Dictionary<string, string> headers = new Dictionary<string, string>(httpHeaders.Count()); |
| 139 | + foreach (var httpHeader in httpHeaders) |
| 140 | + { |
| 141 | + headers[httpHeader.Key] = httpHeader.Value.Aggregate(string.Empty, (combined, next) => combined += (next + ", ")); |
| 142 | + } |
| 143 | + |
| 144 | + return headers; |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments