/// <summary> /// 处理任何命令 /// </summary> /// <param name="request">请求</param> /// <returns></returns> public Message Any(Message request) { try { var context = new HecpContext(acDomain, HecpRequest.Create(acDomain, request)); acDomain.NodeHost.HecpHandler.Process(context); return context.Response.ToMessage(); } catch (Exception ex) { acDomain.LoggingService.Error(ex); var r = new Message { MessageType = MessageType.Event.ToName(), MessageId = string.Empty }; r.Body.Event.Description = "服务器内部逻辑异常"; r.Body.Event.Status = 500; r.Body.Event.ReasonPhrase = Status.InternalServerError.ToString();// 不使用ToName扩展方法以避免造成新的异常 return r; } }
private Message GetRequest(DistributeContext context) { var command = context.Command; var ticks = DateTime.UtcNow.Ticks; var request = new Message { Verb = command.Verb.Code, Ontology = command.Ontology, MessageType = command.MessageType.ToName(), Body = new BodyData(command.DataTuple.IdItems.Items.ToDto(), command.DataTuple.ValueItems.Items.ToDto()) { Event = new EventData { SourceType = command.EventSourceType, Subject = command.EventSubjectCode, ReasonPhrase = command.ReasonPhrase, Status = command.Status } }, TimeStamp = command.TimeStamp.ToUniversalTime().Ticks, MessageId = command.MessageId, Version = command.Version, IsDumb = command.IsDumb, Credential = new CredentialData { ClientType = ClientType.Node.ToName(), UserType = UserType.None.ToName(), CredentialType = CredentialType.Signature.ToName(), SignatureMethod = SignatureMethod.HMAC_SHA1.ToName(), ClientId = context.ClientAgent.PublicKey, UserName = command.UserName,// UserName Ticks = ticks } }; // 签名 request.Credential.Password = Signature.Sign(request.ToOrignalString(request.Credential), context.ClientAgent.SecretKey); return request; }
/// <summary> /// /// </summary> /// <param name="command"></param> /// <param name="publicKey"></param> /// <param name="ticks"></param> /// <returns></returns> public static void Signature(Message command, string publicKey, Int64 ticks) { var credential = new CredentialData { ClientType = ClientType.Node.ToName(), CredentialType = CredentialType.Signature.ToName(), SignatureMethod = SignatureMethod.HMAC_SHA1.ToName(), ClientId = publicKey, Ticks = ticks, UserName = "******" }; command.Credential = credential; }
public ActionResult AuditApproved(string ontologyCode, string id) { var response = new ResponseData { id = id, success = true }; OntologyDescriptor ontology; if (!AcDomain.NodeHost.Ontologies.TryGetOntology(ontologyCode, out ontology)) { throw new ValidationException("非法的本体码"); } string[] ids = id.Split(','); var localEventIDs = new Guid[ids.Length]; for (int i = 0; i < ids.Length; i++) { Guid tmp; if (Guid.TryParse(ids[i], out tmp)) { localEventIDs[i] = tmp; } else { throw new ValidationException("意外的本地事件标识" + ids[i]); } } foreach (var localEventId in localEventIDs) { MessageEntity evnt = ontology.MessageProvider.GetCommand(MessageTypeKind.LocalEvent, ontology, localEventId); if (evnt != null) { if (evnt.Status == (int)Status.ToAudit && evnt.EventSourceType.Equals("Command", StringComparison.OrdinalIgnoreCase)) { var node = AcDomain.NodeHost.Nodes.ThisNode; var ticks = DateTime.UtcNow.Ticks; var cmd = new Message() { Version = ApiVersion.V1.ToName(), IsDumb = false, MessageType = MessageType.Event.ToName(), Credential = new CredentialData { ClientType = ClientType.Node.ToName(), UserType = UserType.None.ToName(), CredentialType = CredentialType.Token.ToName(), ClientId = node.Node.PublicKey.ToString(), UserName = evnt.UserName,// UserName Password = TokenObject.Token(node.Node.Id.ToString(), ticks, node.Node.SecretKey), Ticks = ticks }, TimeStamp = DateTime.UtcNow.Ticks, MessageId = evnt.Id.ToString(), Verb = evnt.Verb.Code, Ontology = evnt.Ontology, Body = new BodyData(new KeyValue[] { new KeyValue("Id", evnt.LocalEntityId) }, evnt.DataTuple.ValueItems.Items.ToDto()) { Event = new EventData { Status = (int)Status.AuditApproved, ReasonPhrase = Status.AuditApproved.ToName(), SourceType = evnt.EventSourceType, Subject = evnt.EventSubjectCode } } }; var result = AnyMessage.Create(HecpRequest.Create(AcDomain, cmd), AcDomain.NodeHost.Nodes.ThisNode).Response(); if (result.Body.Event.Status == (int)Status.NotExist) { ontology.MessageProvider.DeleteCommand(MessageTypeKind.LocalEvent, ontology, evnt.Id, evnt.IsDumb); } else { if (result.Body.Event.Status < 200 || result.Body.Event.Status >= 400) { response.success = false; response.msg = result.Body.Event.Description; response.Warning(); } } } } else { response.success = false; response.msg = "给定标识的本地事件不存在"; response.Warning(); } } return this.JsonResult(response); }
/// <summary> /// /// </summary> /// <param name="publicKey"></param> /// <returns></returns> public static void Signature(Message data, string publicKey) { Signature(data, publicKey, SystemTime.UtcNow().Ticks); }