/// <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."); }
/// <summary> /// Constructor. /// </summary> public GXServer(string[] prefixes, GXDbConnection connection, object host) { RestMap = new Hashtable(); Parser = new GXJsonParser(); Parser.OnCreateObject += new CreateObjectEventhandler(ParserOnCreateObject); Connection = connection; Host = host; if (MessageMap.Count == 0) { GXGeneral.UpdateRestMessageTypes(MessageMap); if (MessageMap.Count == 0) { throw new Exception("No REST services available."); } } Listener = new HttpListener(); foreach (string it in prefixes) { Listener.Prefixes.Add(it); } Listener.Start(); Thread thread = new Thread(new ParameterizedThreadStart(ListenThread)); thread.Start(this); }
/// <summary> /// Constructor. /// </summary> /// <param name="connection">used database connection.</param> public GXWebServiceModule(GXDbConnection connection, object host) { Connection = connection; Host = host; if (MessageMap == null || MessageMap.Count == 0) { if (MessageMap == null) { MessageMap = new Hashtable(); } GXGeneral.UpdateRestMessageTypes(MessageMap); } }
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)); }
/// <summary> /// Show REST services. /// </summary> /// <param name="parameter"></param> static private void ShowServices(object parameter) { object[] tmp = parameter as object[]; GXServer server = tmp[0] as GXServer; HttpListenerContext context = tmp[1] as HttpListenerContext; StringBuilder writer = new StringBuilder(); writer.AppendLine("<!DOCTYPE html >"); writer.AppendLine("<html>"); writer.AppendLine("<style>"); writer.AppendLine(".tooltip {"); writer.AppendLine("position: relative;"); writer.AppendLine("display: inline-block;"); writer.AppendLine("border-bottom: 1px dotted black;"); writer.AppendLine("}"); writer.AppendLine(".tooltip .tooltiptext {"); writer.AppendLine("visibility: hidden;"); writer.AppendLine("width: 600px;"); writer.AppendLine("background-color: Gray;"); writer.AppendLine("color: #fff;"); writer.AppendLine("text-align: left;"); writer.AppendLine("border-radius: 6px;"); writer.AppendLine("padding: 5px 0;"); /* Position the tooltip */ writer.AppendLine("position: absolute;"); writer.AppendLine("z-index: 1;"); writer.AppendLine("}"); writer.AppendLine(".tooltip:hover .tooltiptext {"); writer.AppendLine("visibility: visible;"); writer.AppendLine("}"); writer.AppendLine("</style>"); writer.AppendLine("<body>"); string info = Convert.ToString(server.Host); if (info != "") { writer.AppendLine("<h1>Server information:</h1>"); writer.AppendLine(info.Replace("\r\n", "<br/>")); writer.AppendLine("<hr>"); } writer.Append("<h1>Available REST operations:"); writer.AppendLine("</h1>"); if (server.MessageMap.Count == 0) { GXGeneral.UpdateRestMessageTypes(server.MessageMap); if (server.MessageMap.Count == 0) { writer.AppendLine("No REST operations available."); } } DescriptionAttribute[] att; foreach (GXRestMethodInfo it in server.MessageMap.Values) { writer.Append("<div class=\"tooltip\">" + it.RequestType.Name); writer.Append("<span class=\"tooltiptext\">"); writer.Append("Method: "); if (it.Get != null) { writer.Append("Get"); } else if (it.Post != null) { writer.Append("Post"); } else if (it.Put != null) { writer.Append("Put"); } else if (it.Delete != null) { writer.Append("Delete"); } writer.Append("<p></p>"); att = (DescriptionAttribute[])it.RequestType.GetCustomAttributes(typeof(DescriptionAttribute), true); if (att.Length != 0) { writer.AppendLine(att[0].Description); writer.AppendLine("<br/>"); } writer.AppendLine("<b>Request:</b><br/>{<br/>"); foreach (PropertyInfo p in it.RequestType.GetProperties()) { ShowProperties(p, writer); } writer.Append("}<p></p>"); att = (DescriptionAttribute[])it.ResponseType.GetCustomAttributes(typeof(DescriptionAttribute), true); if (att.Length != 0) { writer.AppendLine(att[0].Description); writer.AppendLine("<br/>"); } writer.AppendLine("<b>Response:</b><br/>{<br/>"); foreach (PropertyInfo p in it.ResponseType.GetProperties()) { ShowProperties(p, writer); } writer.Append("}<br/>"); writer.Append("</span></div><br/>"); att = (DescriptionAttribute[])it.RequestType.GetCustomAttributes(typeof(DescriptionAttribute), true); if (att.Length != 0) { writer.AppendLine(att[0].Description); } writer.AppendLine("<p></p>"); } writer.AppendLine("</body>"); writer.AppendLine("</html>"); using (BufferedStream bs = new BufferedStream(context.Response.OutputStream)) { StreamWriter sw = new StreamWriter(bs); sw.Write(writer.ToString()); sw.Flush(); } }