Exemplo n.º 1
0
        public static ContentRequest ParseJson(string json)
        {
            var jobject     = JObject.Parse(json);
            var id          = (string)jobject.SelectToken("id");
            var title       = (string)jobject.SelectToken("title");
            var contentType = (string)jobject.SelectToken("content_type");

            DivvyLogManager.LogRequest("JSON parsed successfully.");

            var contentTypePrefix = "Content Type: ";

            if (contentType != null && contentType.StartsWith(contentTypePrefix))
            {
                contentType = new String(contentType.Skip(contentTypePrefix.Length).ToArray());
            }

            var contentRequest = new ContentRequest()
            {
                Id          = int.Parse(id),
                Title       = title,
                ContentType = contentType,
                Description = (string)jobject.SelectToken("description"),
                Calendar    = (string)jobject.SelectToken("calendar"),
                Owner       = (string)jobject.SelectToken("owner_member")
            };

            DivvyLogManager.LogRequest("Parsed Values", contentRequest);

            return(contentRequest);
        }
Exemplo n.º 2
0
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            DivvyManager.Init();  // This will immediately return if Init has already occured

            // If Divvy is disabled, no one is getting in
            if (DivvyManager.Settings.Mode == DivvyMode.Disabled)
            {
                DivvyLogManager.Log("Divvy Integration Not Enabled. Aborting.");
                filterContext.Result = new HttpStatusCodeResult(HttpStatusCode.Forbidden, "Divvy disabled. Divvy integration is installed but not enabled in this Episerver instance.");
                return; // Reject
            }

            // If they're logged into the debug role, they're good...
            if (DivvyManager.Settings.DebugRole != null && filterContext.HttpContext.User.IsInRole(DivvyManager.Settings.DebugRole))
            {
                return; // Allow
            }

            // If their auth token validated, they're good
            var result = DivvyAccessToken.Validate();

            if (result.Authorized)
            {
                return; // Allow
            }

            DivvyLogManager.Log(result.Message);
            filterContext.Result = new HttpUnauthorizedResult(result.Message);
            return; // Default reject everything that gets here
        }
Exemplo n.º 3
0
        private string GetRequestBody()
        {
            // We retrieve and parse this manually so we can run events on it...
            var bodyStream = new StreamReader(Request.InputStream);

            bodyStream.BaseStream.Seek(0, SeekOrigin.Begin);
            var bodyText = bodyStream.ReadToEnd();

            DivvyLogManager.LogRequest($"Request Body Retrieved ({bodyText.Length} byte(s). {bodyText})");
            return(bodyText);
        }
Exemplo n.º 4
0
        private void Execute(DivvyWebhook webhook)
        {
            DivvyLogManager.Log($"Executing Webhook", webhook);

            var sw         = Stopwatch.StartNew();
            var httpClient = new HttpClient();

            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Token", DivvyManager.Settings.AccessToken.ToString());
            var result = httpClient.PostAsJsonAsync <object>(webhook.Uri.AbsoluteUri, webhook.Data).Result;

            DivvyLogManager.Log($"Webhook Executed in {sw.ElapsedMilliseconds}ms", new { result.StatusCode });
        }
Exemplo n.º 5
0
 public DivvyWebhookManager()
 {
     timer = new System.Timers.Timer
     {
         Interval = DivvyManager.Settings.WebhookTimerInterval
     };
     timer.Elapsed += (s, e) =>
     {
         if (!working)
         {
             Process();
         }
     };
     timer.Start();
     DivvyLogManager.Log($"Webhook Manager Initialized", new { timer.Interval });
 }
Exemplo n.º 6
0
        public JsonResult Gateway()
        {
            if (DivvyManager.Settings.LogRequestDebugData)
            {
                DivvyLogManager.Log("Gateway Request Started", new { RequestKey = DivvyLogManager.GetRequestLogKey() });
            }

            var requestBody  = GetRequestBody();
            var responseBody = DivvyManager.ProcessDivvyInput(requestBody);

            if (DivvyManager.Settings.LogRequestDebugData)
            {
                DivvyLogManager.Log("Gateway Request Ended");
            }

            return(Json(responseBody));
        }
Exemplo n.º 7
0
        private void Process()
        {
            if (!this.Any())
            {
                return;
            }

            DivvyLogManager.Log($"Processing Webhook Queue. {this.Count()} item(s)");
            working = true;

            while (this.Any())
            {
                var webhook = Dequeue();
                Execute(webhook);
                Thread.Sleep(DivvyManager.Settings.WebhookTimerInterval);
            }
            ;

            working = false;
            DivvyLogManager.Log($"Webhook Queue Processing Complete");
        }
Exemplo n.º 8
0
 public void Add(DivvyWebhook webhook)
 {
     DivvyLogManager.Log($"Enqueing Webhook", webhook);
     Enqueue(webhook);
 }
Exemplo n.º 9
0
 public JsonResult TurnLoggingOff()
 {
     DivvyManager.Settings.LogRequestDebugData = false;
     DivvyLogManager.Clear();
     return(Json("Logging is off. Log entries deleted.", JsonRequestBehavior.AllowGet));
 }
Exemplo n.º 10
0
 public JsonResult RequestLog(Guid key)
 {
     return(Json(DivvyLogManager.GetLogEntries(key), JsonRequestBehavior.AllowGet));
 }