/// <summary> /// Is authentication needed. /// </summary> /// <param name="request"></param> /// <returns></returns> private static bool NeedAuthentication(Hashtable messageMap, string httpMethod, string contentType, string path) { if (contentType == null || !contentType.Contains("json")) { return(false); } string method; GXRestMethodInfo mi = GXGeneral.GetTypes(messageMap, path.ToLower(), out method); if (mi == null) { return(false); } if (httpMethod == "POST") { return(mi.PostAuthentication != null); } else if (httpMethod == "GET") { return(mi.GetAuthentication != null); } else if (httpMethod == "PUT") { return(mi.PutAuthentication != null); } else if (httpMethod == "DELETE") { return(mi.DeleteAuthentication != null); } throw new Exception("Invalid http method."); }
public void OnPostMapRequestHandler(object sender, EventArgs e) { string command; HttpContext context = ((HttpApplication)sender).Context; GXWebService s = context.Handler as GXWebService; if (s != null && context.Request.ContentType.Contains("json")) { lock (MessageMap) { s.RestMethodInfo = GXGeneral.GetTypes(MessageMap, context.Request.Path, out command); s.Connection = Connection; s.Host = Host; } } else { context.Response.ContentType = "text/html"; StringBuilder sb = new StringBuilder(); sb.Append("<http><body>"); sb.Append("<h1>Gurux.Service</h1>"); sb.Append("The following operations are supported:<p/>"); sb.Append("<h2>Operations:</h2><p/>"); if (MessageMap == null || MessageMap.Count == 0) { if (MessageMap == null) { MessageMap = new Hashtable(); } GXGeneral.UpdateRestMessageTypes(MessageMap); } foreach (DictionaryEntry it in MessageMap) { sb.Append(it.Key); sb.Append("<p/>"); } sb.Append("</body></http>"); context.Response.Write(sb.ToString()); } }
/// <summary> /// Parse query DTO and return response DTO as string. /// </summary> /// <param name="method">Http method.</param> /// <param name="path">Command to execute.</param> /// <param name="data">Command data.</param> /// <returns>DTO result as string.</returns> static string GetReply(Hashtable messageMap, IPrincipal user, GXServer server, string hostAddress, string method, string path, string data) { InvokeHandler handler; string command; GXRestMethodInfo mi; lock (messageMap) { mi = GXGeneral.GetTypes(messageMap, path, out command); } if (mi == null) { throw new HttpException(405, string.Format("Rest method '{0}' not implemented.", command)); } switch (method.ToUpper()) { case "GET": handler = mi.Get; break; case "POST": handler = mi.Post; break; case "PUT": handler = mi.Put; break; case "DELETE": handler = mi.Delete; break; default: handler = null; break; } if (handler == null) { throw new HttpException(405, string.Format("Method '{0}' not allowed for {1}", method, command)); } object req = server.Parser.Deserialize("{" + data + "}", mi.RequestType); //Get Rest class from cache. GXRestService target = server.RestMap[mi.RestClassType] as GXRestService; if (target == null) { target = GXJsonParser.CreateInstance(mi.RestClassType) as GXRestService; server.RestMap[mi.RestClassType] = target; } target.Host = server.Host; target.User = user; target.Db = server.Connection; target.UserHostAddress = hostAddress; object tmp = handler(target, req); if (tmp == null) { throw new HttpException(405, string.Format("Command '{0}' returned null.", command)); } return(server.Parser.SerializeToHttp(tmp)); }