RestTestServices.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. *
  27. */
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Reflection;
  31. using OpenSim.Framework.Servers;
  32. using OpenSim.Framework.Servers.HttpServer;
  33. namespace OpenSim.ApplicationPlugins.Rest.Inventory
  34. {
  35. public class RestTestServices : IRest
  36. {
  37. private bool enabled = false;
  38. private string qPrefix = "test";
  39. // A simple constructor is used to handle any once-only
  40. // initialization of working classes.
  41. public RestTestServices()
  42. {
  43. Rest.Log.InfoFormat("{0} Test services initializing", MsgId);
  44. Rest.Log.InfoFormat("{0} Using REST Implementation Version {1}", MsgId, Rest.Version);
  45. // If a relative path was specified, make it absolute by adding
  46. // the standard prefix, e.g. /admin
  47. if (!qPrefix.StartsWith(Rest.UrlPathSeparator))
  48. {
  49. Rest.Log.InfoFormat("{0} Domain is relative, adding absolute prefix", MsgId);
  50. qPrefix = String.Format("{0}{1}{2}", Rest.Prefix, Rest.UrlPathSeparator, qPrefix);
  51. Rest.Log.InfoFormat("{0} Domain is now <{1}>", MsgId, qPrefix);
  52. }
  53. // Load test cases
  54. loadTests();
  55. foreach (ITest test in tests)
  56. {
  57. test.Initialize();
  58. }
  59. // Register interface
  60. Rest.Plugin.AddPathHandler(DoTests,qPrefix,Allocate);
  61. // Activate
  62. enabled = true;
  63. Rest.Log.InfoFormat("{0} Test services initialization complete", MsgId);
  64. }
  65. // Post-construction, pre-enabled initialization opportunity
  66. // Not currently exploited.
  67. public void Initialize()
  68. {
  69. }
  70. // Called by the plug-in to halt REST processing. Local processing is
  71. // disabled, and control blocks until all current processing has
  72. // completed. No new processing will be started
  73. public void Close()
  74. {
  75. enabled = false;
  76. foreach (ITest test in tests)
  77. {
  78. test.Close();
  79. }
  80. Rest.Log.InfoFormat("{0} Test services closing down", MsgId);
  81. }
  82. // Properties
  83. internal string MsgId
  84. {
  85. get { return Rest.MsgId; }
  86. }
  87. #region Interface
  88. private RequestData Allocate(OSHttpRequest request, OSHttpResponse response, string prefix)
  89. {
  90. return new RequestData(request, response, prefix);
  91. }
  92. // Inventory Handler
  93. private void DoTests(RequestData rdata)
  94. {
  95. if (!enabled)
  96. return;
  97. // Now that we know this is a serious attempt to
  98. // access inventory data, we should find out who
  99. // is asking, and make sure they are authorized
  100. // to do so. We need to validate the caller's
  101. // identity before revealing anything about the
  102. // status quo. Authenticate throws an exception
  103. // via Fail if no identity information is present.
  104. //
  105. // With the present HTTP server we can't use the
  106. // builtin authentication mechanisms because they
  107. // would be enforced for all in-bound requests.
  108. // Instead we look at the headers ourselves and
  109. // handle authentication directly.
  110. try
  111. {
  112. if (!rdata.IsAuthenticated)
  113. {
  114. rdata.Fail(Rest.HttpStatusCodeNotAuthorized,
  115. String.Format("user \"{0}\" could not be authenticated", rdata.userName));
  116. }
  117. }
  118. catch (RestException e)
  119. {
  120. if (e.statusCode == Rest.HttpStatusCodeNotAuthorized)
  121. {
  122. Rest.Log.WarnFormat("{0} User not authenticated", MsgId);
  123. Rest.Log.DebugFormat("{0} Authorization header: {1}", MsgId, rdata.request.Headers.Get("Authorization"));
  124. }
  125. else
  126. {
  127. Rest.Log.ErrorFormat("{0} User authentication failed", MsgId);
  128. Rest.Log.DebugFormat("{0} Authorization header: {1}", MsgId, rdata.request.Headers.Get("Authorization"));
  129. }
  130. throw (e);
  131. }
  132. // Check that a test was specified
  133. if (rdata.Parameters.Length < 1)
  134. {
  135. Rest.Log.DebugFormat("{0} Insufficient parameters", MsgId);
  136. rdata.Fail(Rest.HttpStatusCodeBadRequest, "not enough parameters");
  137. }
  138. // Select the test
  139. foreach (ITest test in tests)
  140. {
  141. if (!rdata.handled)
  142. test.Execute(rdata);
  143. }
  144. }
  145. #endregion Interface
  146. private static bool testsLoaded = false;
  147. private static List<Type> classes = new List<Type>();
  148. private static List<ITest> tests = new List<ITest>();
  149. private static Type[] parms = new Type[0];
  150. private static Object[] args = new Object[0];
  151. static RestTestServices()
  152. {
  153. Module[] mods = Assembly.GetExecutingAssembly().GetModules();
  154. foreach (Module m in mods)
  155. {
  156. Type[] types = m.GetTypes();
  157. foreach (Type t in types)
  158. {
  159. try
  160. {
  161. if (t.GetInterface("ITest") != null)
  162. {
  163. classes.Add(t);
  164. }
  165. }
  166. catch (Exception e)
  167. {
  168. Rest.Log.WarnFormat("[STATIC-TEST] Unable to include test {0} : {1}", t, e.Message);
  169. }
  170. }
  171. }
  172. }
  173. /// <summary>
  174. /// This routine loads all of the handlers discovered during
  175. /// instance initialization. Each handler is responsible for
  176. /// registering itself with this handler.
  177. /// I was not able to make this code work in a constructor.
  178. /// </summary>
  179. private void loadTests()
  180. {
  181. lock (tests)
  182. {
  183. if (!testsLoaded)
  184. {
  185. ConstructorInfo ci;
  186. Object ht;
  187. foreach (Type t in classes)
  188. {
  189. try
  190. {
  191. if (t.GetInterface("ITest") != null)
  192. {
  193. ci = t.GetConstructor(parms);
  194. ht = ci.Invoke(args);
  195. tests.Add((ITest)ht);
  196. Rest.Log.InfoFormat("{0} Test {1} added", MsgId, t);
  197. }
  198. }
  199. catch (Exception e)
  200. {
  201. Rest.Log.WarnFormat("{0} Unable to load test {1} : {2}", MsgId, t, e.Message);
  202. }
  203. }
  204. testsLoaded = true;
  205. }
  206. }
  207. }
  208. }
  209. }