/// <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> /// 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(); } }