/// <summary> /// Matches the resource ID, rid, with the registered resource handlers, /// and returns the matching IResourceContext, or null if no matching resource /// handler was found. /// </summary> /// <remarks> /// Should only be called from within the resource's group callback. /// Using the returned value from another thread may cause race conditions. /// </remarks> /// <param name="rid">Resource ID.</param> /// <returns>Resource context matching the resource ID, or null if no match is found.</returns> public IResourceContext Resource(string rid) { string rname = rid; string query = ""; int idx = rid.IndexOf('?'); if (idx > -1) { rname = rid.Substring(0, idx); query = rid.Substring(idx + 1); } Router.Match match = GetHandler(rname); return(match == null ? null : new ResourceContext(this, rname, match.Handler, match.EventHandler, match.Params, query, match.Group)); }
private void handleMessage(object sender, MsgHandlerEventArgs e) { Msg msg = e.Message; String subj = msg.Subject; Log.Trace("==> {0}: {1}", subj, msg.Data == null ? "<null>" : Encoding.UTF8.GetString(msg.Data)); // Assert there is a reply subject if (String.IsNullOrEmpty(msg.Reply)) { OnError("Missing reply subject on request: {0}", subj); return; } // Get request type Int32 idx = subj.IndexOf('.'); if (idx < 0) { // Shouldn't be possible unless NATS is really acting up OnError("Invalid request subject: {0}", subj); return; } String method = null; String rtype = subj.Substring(0, idx); String rname = subj.Substring(idx + 1); if (rtype == "call" || rtype == "auth") { Int32 lastIdx = rname.LastIndexOf('.'); if (idx < 0) { // No method? Resgate must be acting up OnError("Invalid request subject: {0}", subj); return; } method = rname.Substring(lastIdx + 1); rname = rname.Substring(0, lastIdx); } Router.Match match = GetHandler(rname); runWith(match == null ? rname : match.Group, () => processRequest(msg, rtype, rname, method, match)); }
private Task processRequest(Msg msg, String rtype, String rname, String method, Router.Match match) { Request req; // Check if there is no matching handler if (match == null) { // [TODO] Allow for a default handler req = new Request(this, msg); req.NotFound(); return(completedTask); } try { byte[] d = msg.Data; RequestDto reqInput = d == null || d.Length == 0 || (d.Length == 2 && d[0] == '{' && d[1] == '}') ? RequestDto.Empty : JsonConvert.DeserializeObject <RequestDto>(Encoding.UTF8.GetString(msg.Data)); req = new Request( this, msg, rtype, rname, method, match.Handler, match.EventHandler, match.Params, match.Group, reqInput.CID, reqInput.RawParams, reqInput.RawToken, reqInput.Header, reqInput.Host, reqInput.RemoteAddr, reqInput.URI, reqInput.Query); } catch (Exception ex) { OnError("Error deserializing incoming request: {0}", msg.Data == null ? "<null>" : Encoding.UTF8.GetString(msg.Data)); req = new Request(this, msg); req.Error(new ResError(ex)); return(completedTask); } return(req.ExecuteHandler()); }