private void RequestHandler(IHttpContextProxy httpProxy) { string requestResource = httpProxy.GetURIAbsolutePath(); _logger.Debug(string.Format("Installer.RequestHandler path:{0}", requestResource)); if (!HandleAPI(httpProxy, requestResource)) { HandleResource(requestResource, httpProxy); } }
public void Error(string message, Exception ex) { if (LogLevels.Contains("Error")) { JObject logData = null; if (_httpContextProxy != null) { logData = new JObject() { ["RequestBody"] = _httpContextProxy.GetRequestBody(), ["RequestUrl"] = _httpContextProxy.GetURIAbsolutePath(), ["RequestQueryString"] = _httpContextProxy.GetQueryString() }; logData["RequestHeader"] = string.Join(";", _httpContextProxy.GetHeaders().Select(f => string.Format("{0}:{1}", f.Key, f.Value))); } Error(message, ex, logData); } }
private void WriteStartTransaction(ILogger loggerController, IHttpContextProxy httpProxy, RoutingModel route) { JObject objTxnStartData = new JObject(); objTxnStartData[CommonConst.CommonField.URL] = httpProxy.GetURIAbsolutePath(); objTxnStartData[CommonConst.CommonField.ROUTE] = JObject.Parse(route.GetJson()); string strPayload = httpProxy.GetRequestBody();; JObject payload = null; if (JObjectHelper.TryParseJson(strPayload, ref payload)) { objTxnStartData[CommonConst.CommonField.PAYLOAD] = payload; } else { objTxnStartData[CommonConst.CommonField.PAYLOAD] = strPayload; } //TODO // objTxnStartData[CommonConst.CommonField.USER] = httpProxy.GetRequestBody(); // loggerController.Transaction(objTxnStartData, TransactionState.Start); }
public virtual async Task Invoke(HttpContext context, IAuthorizationService authorizationService) { var route = _routing.GetRoute(_httpContextProxy.GetHttpMethod(), _httpContextProxy.GetURIAbsolutePath()); if (route != null && !string.IsNullOrEmpty(route.ExecultAssembly) && !string.IsNullOrEmpty(route.ExecuteType)) { if (AuthorizedRoute(context, route, authorizationService)) { var type = _assemblyLoader.GetType(route.ExecultAssembly, route.ExecuteType); if (type != null) { context.Response.Headers[CommonConst.CommonField.MODULE_NAME] = route.module_name; context.Response.Headers[CommonConst.CommonField.EXECUTE_TYPE] = route.ExecuteType; context.Response.Headers[CommonConst.CommonField.ROUTE] = $"{route.Method}:{route.Route}"; _logger.Debug(string.Format("Executing route:{0}", route.ToString())); var controller = _serviceResolver.Resolve(type); var method = controller.GetType().GetMethods().FirstOrDefault(f => f.Name == route.ExecuteMethod); if (method != null) { context.Response.ContentType = route.ContentType; object response = null; if (method.ReturnType.BaseType == typeof(Task)) { response = await(dynamic) method.Invoke(controller, null); } else { response = method.Invoke(controller, null); } RemoveHeaders(context); if (response != null) { if (method.ReturnType == typeof(string)) { await context.Response.WriteAsync((response as string)); } else if (method.ReturnType == typeof(byte[])) { var byteResponse = response as byte[]; await context.Response.Body.WriteAsync(byteResponse, 0, byteResponse.Length); } else { await context.Response.WriteAsync(JsonConvert.SerializeObject(response)); } } else { await context.Response.Body.WriteAsync(new byte[] { }); } } else { RemoveHeaders(context); _logger.Error($"Method not found for route : {route.ToString()}"); context.Response.StatusCode = (int)HttpStatusCode.NotFound; await context.Response.WriteAsync(_responseBuilder.NotFound().ToString()); } } else { _logger.Error($"Type not found for route : {route.ToString()}"); context.Response.StatusCode = (int)HttpStatusCode.NotFound; await context.Response.WriteAsync(_responseBuilder.NotFound().ToString()); } } else { context.Response.StatusCode = (int)HttpStatusCode.Unauthorized; await context.Response.WriteAsync(_responseBuilder.Unauthorized().ToString()); } } else { try { route = await _apiGatewayService.GetRouteAsync(_httpContextProxy.GetHttpMethod(), _httpContextProxy.GetURIAbsolutePath()); if (route != null) { try { context.Response.ContentType = route.ContentType; JObject response = await CallRemoteRouteCalls(context, route); if (response != null) { RemoveHeaders(context); if (response["content_type"] != null && response["data"] != null) { await context.Response.WriteAsync(response["data"].ToString()); } else { await context.Response.WriteAsync(response.ToString()); } } else { await _next(context); } } catch (UnauthorizedAccessException ex) { context.Response.StatusCode = (int)HttpStatusCode.Unauthorized; _logger.Error($"UnauthorizedAccessException remote route. {route.ToString() } . {ex.Message}", ex); } catch (Exception ex) { _logger.Error($"Error Executing remote route. {route.ToString() } . {ex.Message}", ex); context.Response.StatusCode = (int)HttpStatusCode.InternalServerError; } } else { await _next(context); } } catch (Exception ex) { _logger.Error($"Error Executing remote route. {route.ToString() } . {ex.Message}", ex); await _next(context); } } }