// ------------------------------------------------------- // 保存 删除 // ------------------------------------------------------- #region 函数:Save(XmlDocument doc) /// <summary>保存记录</summary> /// <param name="doc">Xml 文档对象</param> /// <returns>返回操作结果</returns> public string Save(XmlDocument doc) { ConnectCallInfo param = new ConnectCallInfo(); param = (ConnectCallInfo)AjaxUtil.Deserialize(param, doc); this.service.Save(param); return(MessageObject.Stringify("0", I18n.Strings["msg_save_success"])); }
// ------------------------------------------------------- // 保存 删除 // ------------------------------------------------------- #region 函数:Save(ConnectCallInfo param) /// <summary>保存记录</summary> /// <param name="param">AccountInfo 实例详细信息</param> /// <param name="message">数据库操作返回的相关信息</param> /// <returns>AccountInfo 实例详细信息</returns> public ConnectCallInfo Save(ConnectCallInfo param) { // 如果消息队列可用,则将数据发送到队列。 if (ConnectConfigurationView.Instance.MessageQueueMode == "ON" && queue.Enabled) { queue.Send(param); return(param); } else { return(this.provider.Save(param)); } }
// ------------------------------------------------------- // 查询 // ------------------------------------------------------- #region 函数:FindOne(XmlDocument doc) /// <summary>获取详细信息</summary> /// <param name="doc">Xml 文档对象</param> /// <returns>返回操作结果</returns> public string FindOne(XmlDocument doc) { StringBuilder outString = new StringBuilder(); string id = XmlHelper.Fetch("id", doc); ConnectCallInfo param = this.service.FindOne(id); outString.Append("{\"data\":" + AjaxUtil.Parse <ConnectCallInfo>(param) + ","); outString.Append(MessageObject.Stringify("0", I18n.Strings["msg_query_success"], true) + "}"); return(outString.ToString()); }
public void TestSave() { Assert.IsNotNull(KernelContext.Current.AuthenticationManagement); Assert.IsNotNull(KernelContext.Current.User); ConnectCallInfo param = new ConnectCallInfo(); param.Id = "test-" + DateHelper.GetTimestamp(); param.AppKey = Guid.NewGuid().ToString(); param = ConnectContext.Instance.ConnectCallService.Save(param); Assert.IsNotNull(param); }
/// <summary>处理请求</summary> public static string ProcessRequest(HttpContextBase context, string methodName, string rawInput, ILog logger, APIInvokeDelegate methodInvoke) { // 请求响应的内容 string responseText = string.Empty; // clientId string clientId = RequestHelper.Fetch("clientId", new string[] { "client_id", "app_key", "appKey" }); // accessToken string accessToken = RequestHelper.Fetch("accessToken", "access_token"); // 默认支持 form-data 方式 string xml = (context.Request.Form["xhr-xml"] == null) ? string.Empty : context.Request.Form["xhr-xml"]; // 支持 application/xml 请求方式 if (context.Request.ContentType.IndexOf("application/xml") > -1 && string.IsNullOrEmpty(xml) && !string.IsNullOrEmpty(rawInput)) { xml = rawInput; } // 支持 application/json 请求方式 if (context.Request.ContentType.IndexOf("application/json") > -1 && string.IsNullOrEmpty(xml) && !string.IsNullOrEmpty(rawInput)) { XmlDocument xmlDoc = JsonHelper.ToXmlDocument(rawInput); xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n<request>" + xmlDoc.DocumentElement.InnerXml + "</request>"; } if (!string.IsNullOrEmpty(xml) || context.Request.QueryString.Count > 0 || (context.Request.HttpMethod == "POST" && context.Request.Form.Count > 0)) { XmlDocument doc = new XmlDocument(); if (string.IsNullOrEmpty(xml)) { doc.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n<request></request>"); } else { doc.LoadXml(xml); } // 将 QueryString 中,除 xhr-name 外的所有参数转为统一的Xml文档的数据 if (context.Request.QueryString.Count > 0) { for (int i = 0; i < context.Request.QueryString.Count; i++) { if (string.IsNullOrEmpty(context.Request.QueryString.Keys[i])) { continue; } if (context.Request.QueryString.Keys[i] != "xhr-name" && context.Request.QueryString.Keys[i].IndexOf("[") == -1) { XmlElement element = doc.CreateElement(context.Request.QueryString.Keys[i]); element.InnerText = context.Request.QueryString[i]; doc.DocumentElement.AppendChild(element); } } doc = AnalyzePagingXml(doc, context.Request.QueryString); doc = AnalyzeQueryXml(doc, context.Request.QueryString); } // 将表单中,除 xhr-name 和 xhr-xml 外的所有参数转为统一的Xml文档的数据 if (context.Request.HttpMethod == "POST" && context.Request.Form.Count > 0) { for (int i = 0; i < context.Request.Form.Count; i++) { if (string.IsNullOrEmpty(context.Request.Form.Keys[i])) { continue; } if (context.Request.Form.Keys[i] != "xhr-name" && context.Request.Form.Keys[i] != "xhr-xml" && context.Request.Form.Keys[i].IndexOf("[") == -1) { XmlElement element = doc.CreateElement(context.Request.Form.Keys[i]); element.InnerText = context.Request.Form[i]; doc.DocumentElement.AppendChild(element); } } doc = AnalyzeQueryXml(doc, context.Request.QueryString); } string clientTargetObject = XmlHelper.Fetch("clientTargetObject", doc); try { // 记录 if (ConnectConfigurationView.Instance.EnableCallLog == "ON") { ConnectCallInfo call = new ConnectCallInfo(clientId, context.Request.RawUrl, string.IsNullOrEmpty(rawInput) ? doc.InnerXml : rawInput); call.AccessToken = accessToken; try { call.Start(); var responseObject = methodInvoke(methodName, doc, logger); responseText = call.ResponseData = (responseObject == null) ? string.Empty : responseObject.ToString(); if (call.RequestData.Length > 2048) { call.RequestData = "[Long String] " + call.RequestData.Length; } if (call.ResponseData.Length > 2048) { call.ResponseData = "[Long String] " + call.ResponseData.Length; } call.ReturnCode = 0; } catch { call.ReturnCode = 1; throw; } finally { call.Finish(); call.IP = IPQueryContext.GetClientIP(); ConnectContext.Instance.ConnectCallService.Save(call); } } else { var responseObject = methodInvoke(methodName, doc, logger); responseText = (responseObject == null) ? string.Empty : responseObject.ToString(); } if (responseText.IndexOf("\"message\":") > -1 && !string.IsNullOrEmpty(clientTargetObject)) { responseText = responseText.Insert(responseText.IndexOf("\"message\":"), "\"clientTargetObject\":\"" + clientTargetObject + "\","); } } catch (GenericException genericException) { responseText = genericException.ToString(); } catch (ThreadAbortException threadAbortException) { GenericException exception = new GenericException("9999", threadAbortException); responseText = exception.ToString(); } catch (Exception ex) { GenericException exception = null; if (ex.InnerException is GenericException) { exception = (GenericException)ex.InnerException; } else { exception = new GenericException("-1", ex); } responseText = exception.ToString(); } } // JSONP string callback = context.Request["callback"]; return(string.IsNullOrEmpty(callback) ? responseText : callback + "(" + responseText + ")"); }
/// <summary></summary> public override void ProcessRequest(HttpContext context) { string responseText = string.Empty; // 示例: /api/application.method.hi.aspx // 获取客户端签名 clientId 和 clientSecret 或 clientId, clientSignature, timestamp, nonce string clientId = this.TryFetchRequstValue(context, "clientId", "client_id"); string clientSecret = this.TryFetchRequstValue(context, "clientSecret", "client_secret"); string clientSignature = this.TryFetchRequstValue(context, "clientSignature", "client_signature"); string timestamp = context.Request["timestamp"] == null ? string.Empty : context.Request["timestamp"]; string nonce = context.Request["nonce"] == null ? string.Empty : context.Request["nonce"]; string accessToken = this.TryFetchRequstValue(context, "accessToken", "access_token"); string name = context.Request.QueryString["name"]; // 验证权限 bool allowAccess = false; if (!string.IsNullOrEmpty(accessToken) && ConnectContext.Instance.ConnectAccessTokenService.IsExist(accessToken)) { // 验证会话 allowAccess = true; } else if (!string.IsNullOrEmpty(clientId)) { // 2.第三方应用连接 ConnectInfo connect = ConnectContext.Instance.ConnectService[clientId]; if (connect == null) { allowAccess = false; } else { if (!string.IsNullOrEmpty(clientSignature) && !string.IsNullOrEmpty(timestamp) && !string.IsNullOrEmpty(nonce)) { // 加密方式签名 var signature = Encrypter.EncryptSHA1(Encrypter.SortAndConcat(connect.AppSecret, timestamp, nonce)); if (clientSignature == signature) { allowAccess = true; } } else if (!string.IsNullOrEmpty(clientSecret) && connect.AppSecret == clientSecret) { // 明文客户端密钥 allowAccess = true; } else if (name == "connect.auth.authorize" || name == "connect.auth.token" || name == "connect.auth.callback" || name == "connect.oauth2.authorize" || name == "connect.oauth2.token" || name == "connect.oauth2.callback" || name == "session.me") { // 3.如果以上场景都不是,确认是否是用户登录验证的方法 allowAccess = true; } else { allowAccess = false; } } } else if (name == "membership.member.login" || name == "session.me") { // 3.如果以上场景都不是,确认是否是用户登录验证的方法 allowAccess = true; } if (!allowAccess) { ApplicationError.Write(401); } string xml = (context.Request.Form["xhr-xml"] == null) ? string.Empty : context.Request.Form["xhr-xml"]; if (!string.IsNullOrEmpty(name) && (!string.IsNullOrEmpty(xml) || context.Request.QueryString.Count > 1)) { XmlDocument doc = new XmlDocument(); if (string.IsNullOrEmpty(xml)) { doc.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n<root></root>"); } else { doc.LoadXml(xml); } // 将 QueryString 中,除 xhr-name 外的所有参数转为统一的Xml文档的数据 if (context.Request.QueryString.Count > 1) { for (int i = 0; i < context.Request.QueryString.Count; i++) { if (context.Request.QueryString.Keys[i] == null) { continue; } if (context.Request.QueryString.Keys[i] != "xhr-name") { XmlElement element = CreateXmlElement(doc, context.Request.QueryString.Keys[i]); element.InnerText = context.Request.QueryString[i]; doc.DocumentElement.AppendChild(element); } } } // 将表单中,除 xhr-name 和 xhr-xml 外的所有参数转为统一的Xml文档的数据 if (context.Request.HttpMethod == "POST" && context.Request.Form.Count > 1) { for (int i = 0; i < context.Request.Form.Count; i++) { if (context.Request.Form.Keys[i] == null) { continue; } if (context.Request.Form.Keys[i] != "xhr-name" && context.Request.Form.Keys[i] != "xhr-xml") { XmlElement element = CreateXmlElement(doc, context.Request.Form.Keys[i]); element.InnerText = context.Request.Form[i]; doc.DocumentElement.AppendChild(element); } } } string clientTargetObject = XmlHelper.Fetch("clientTargetObject", doc); string resultType = (context.Request.Form["resultType"] == null) ? "json" : context.Request.Form["resultType"]; // 设置输出的内容类型,默认为 json 格式。 HttpContentTypeHelper.SetValue(resultType); try { // 记录 if (ConnectConfigurationView.Instance.EnableCallLog == "ON") { ConnectCallInfo call = new ConnectCallInfo(clientId, context.Request.RawUrl, doc.InnerXml); try { call.Start(); responseText = X3Platform.Web.APIs.Methods.MethodInvoker.Invoke(name, doc, logger); call.ReturnCode = 0; } catch { call.ReturnCode = 1; throw; } finally { call.Finish(); call.IP = IPQueryContext.GetClientIP(); ConnectContext.Instance.ConnectCallService.Save(call); } } else { responseText = X3Platform.Web.APIs.Methods.MethodInvoker.Invoke(name, doc, logger); } if (resultType == "json" && responseText.IndexOf("\"message\":") > -1 && !string.IsNullOrEmpty(clientTargetObject)) { responseText = responseText.Insert(responseText.IndexOf("\"message\":"), "\"clientTargetObject\":\"" + clientTargetObject + "\","); } } catch (ThreadAbortException threadAbortException) { responseText = "{\"message\":{" + "\"returnCode\":\"2\"," + "\"category\":\"exception\"," + "\"value\":\"" + StringHelper.ToSafeJson(threadAbortException.Message) + "\"," + "\"description\":\"" + StringHelper.ToSafeJson(threadAbortException.ToString()) + "\"" + "}}"; } catch (Exception generalException) { responseText = "{\"message\":{" + "\"returnCode\":\"1\"," + "\"category\":\"exception\"," + "\"value\":\"" + StringHelper.ToSafeJson(generalException.Message) + "\"," + "\"description\":\"" + StringHelper.ToSafeJson(generalException.ToString()) + "\"" + "}}"; } HttpContext.Current.Response.ContentType = HttpContentTypeHelper.GetValue(true); HttpContext.Current.Response.Write(responseText); HttpContext.Current.Response.End(); } }
/// <summary> /// Called by the <see cref="IScheduler" /> when a /// <see cref="ITrigger" /> fires that is associated with the <see cref="IJob" />. /// </summary> public virtual void Execute(IJobExecutionContext context) { logger.Info("Fetching..."); ConnectionFactory factory = new ConnectionFactory(); /* * IMessageQueueObject queue = new RabbitQueueObject<ConnectCallInfo>(ConnectConfigurationView.Instance.MessageQueueHostName, * ConnectConfigurationView.Instance.MessageQueuePort, * ConnectConfigurationView.Instance.MessageQueueUsername, * ConnectConfigurationView.Instance.MessageQueuePassword, * ConnectConfigurationView.Instance.MessageQueueName); */ factory.HostName = ConnectConfigurationView.Instance.MessageQueueHostName; factory.Port = ConnectConfigurationView.Instance.MessageQueuePort; factory.UserName = ConnectConfigurationView.Instance.MessageQueueUsername; factory.Password = ConnectConfigurationView.Instance.MessageQueuePassword; using (IConnection connection = factory.CreateConnection()) { using (IModel channel = connection.CreateModel()) { ConnectCallInfo data = new ConnectCallInfo(); while (true) { BasicGetResult result = channel.BasicGet(ConnectConfigurationView.Instance.MessageQueueName, false); if (result == null) { break; } else { bool t = result.Redelivered; byte[] bytes = result.Body; XmlDocument doc = new XmlDocument(); doc.LoadXml(Encoding.UTF8.GetString(bytes)); data.Deserialize(doc.DocumentElement); // 回复确认 channel.BasicAck(result.DeliveryTag, false); // return data; if (data != null) { ConnectContext.Instance.ConnectCallService.Save(data); logger.Info("id:" + data.Id + " insert success."); } } } } } /* * queue.Open(); * * ConnectCallInfo param = (ConnectCallInfo)queue.Receive(); * * if (param != null) * { * ConnectContext.Instance.ConnectCallService.Save(param); * * logger.Info("id:" + param.Id + " insert success."); * } * * queue.Close(); */ }
// ------------------------------------------------------- // 保存 添加 修改 删除 // ------------------------------------------------------- #region 函数:Save(ConnectCallInfo param) /// <summary>保存记录</summary> /// <param name="param">实例<see cref="ConnectCallInfo"/>详细信息</param> /// <returns>实例<see cref="ConnectCallInfo"/>详细信息</returns> public ConnectCallInfo Save(ConnectCallInfo param) { this.ibatisMapper.Insert(StringHelper.ToProcedurePrefix(string.Format("{0}_Save", this.tableName)), param); return(this.FindOne(param.Id)); }