RestTestServices.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 OpenSim 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 OpenMetaverse;
  29. using Nini.Config;
  30. using System;
  31. using System.IO;
  32. using System.Collections.Generic;
  33. using System.Reflection;
  34. using System.Threading;
  35. using OpenSim.Framework;
  36. using OpenSim.Framework.Servers;
  37. using OpenSim.Framework.Communications;
  38. using OpenSim.Framework.Communications.Cache;
  39. namespace OpenSim.ApplicationPlugins.Rest.Inventory
  40. {
  41. public class RestTestServices : IRest
  42. {
  43. private bool enabled = false;
  44. private string qPrefix = "test";
  45. // A simple constructor is used to handle any once-only
  46. // initialization of working classes.
  47. public RestTestServices()
  48. {
  49. Rest.Log.InfoFormat("{0} Test services initializing", MsgId);
  50. Rest.Log.InfoFormat("{0} Using REST Implementation Version {1}", MsgId, Rest.Version);
  51. // If a relative path was specified, make it absolute by adding
  52. // the standard prefix, e.g. /admin
  53. if (!qPrefix.StartsWith(Rest.UrlPathSeparator))
  54. {
  55. Rest.Log.InfoFormat("{0} Domain is relative, adding absolute prefix", MsgId);
  56. qPrefix = String.Format("{0}{1}{2}", Rest.Prefix, Rest.UrlPathSeparator, qPrefix);
  57. Rest.Log.InfoFormat("{0} Domain is now <{1}>", MsgId, qPrefix);
  58. }
  59. // Load test cases
  60. loadTests();
  61. foreach (ITest test in tests)
  62. {
  63. test.Initialize();
  64. }
  65. // Register interface
  66. Rest.Plugin.AddPathHandler(DoTests,qPrefix,Allocate);
  67. // Activate
  68. enabled = true;
  69. Rest.Log.InfoFormat("{0} Test services initialization complete", MsgId);
  70. }
  71. // Post-construction, pre-enabled initialization opportunity
  72. // Not currently exploited.
  73. public void Initialize()
  74. {
  75. }
  76. // Called by the plug-in to halt REST processing. Local processing is
  77. // disabled, and control blocks until all current processing has
  78. // completed. No new processing will be started
  79. public void Close()
  80. {
  81. enabled = false;
  82. foreach (ITest test in tests)
  83. {
  84. test.Close();
  85. }
  86. Rest.Log.InfoFormat("{0} Test services closing down", MsgId);
  87. }
  88. // Properties
  89. internal string MsgId
  90. {
  91. get { return Rest.MsgId; }
  92. }
  93. #region Interface
  94. private RequestData Allocate(OSHttpRequest request, OSHttpResponse response, string prefix)
  95. {
  96. return new RequestData(request, response, prefix);
  97. }
  98. // Inventory Handler
  99. private void DoTests(RequestData rdata)
  100. {
  101. if (!enabled)
  102. return;
  103. // Now that we know this is a serious attempt to
  104. // access inventory data, we should find out who
  105. // is asking, and make sure they are authorized
  106. // to do so. We need to validate the caller's
  107. // identity before revealing anything about the
  108. // status quo. Authenticate throws an exception
  109. // via Fail if no identity information is present.
  110. //
  111. // With the present HTTP server we can't use the
  112. // builtin authentication mechanisms because they
  113. // would be enforced for all in-bound requests.
  114. // Instead we look at the headers ourselves and
  115. // handle authentication directly.
  116. try
  117. {
  118. if (!rdata.IsAuthenticated)
  119. {
  120. rdata.Fail(Rest.HttpStatusCodeNotAuthorized,
  121. String.Format("user \"{0}\" could not be authenticated", rdata.userName));
  122. }
  123. }
  124. catch (RestException e)
  125. {
  126. if (e.statusCode == Rest.HttpStatusCodeNotAuthorized)
  127. {
  128. Rest.Log.WarnFormat("{0} User not authenticated", MsgId);
  129. Rest.Log.DebugFormat("{0} Authorization header: {1}", MsgId, rdata.request.Headers.Get("Authorization"));
  130. }
  131. else
  132. {
  133. Rest.Log.ErrorFormat("{0} User authentication failed", MsgId);
  134. Rest.Log.DebugFormat("{0} Authorization header: {1}", MsgId, rdata.request.Headers.Get("Authorization"));
  135. }
  136. throw (e);
  137. }
  138. // Check that a test was specified
  139. if (rdata.Parameters.Length < 1)
  140. {
  141. Rest.Log.DebugFormat("{0} Insufficient parameters", MsgId);
  142. rdata.Fail(Rest.HttpStatusCodeBadRequest, "not enough parameters");
  143. }
  144. // Select the test
  145. foreach (ITest test in tests)
  146. {
  147. if (!rdata.handled)
  148. test.Execute(rdata);
  149. }
  150. }
  151. #endregion Interface
  152. private static bool testsLoaded = false;
  153. private static List<Type> classes = new List<Type>();
  154. private static List<ITest> tests = new List<ITest>();
  155. private static Type[] parms = new Type[0];
  156. private static Object[] args = new Object[0];
  157. static RestTestServices()
  158. {
  159. Module[] mods = Assembly.GetExecutingAssembly().GetModules();
  160. foreach (Module m in mods)
  161. {
  162. Type[] types = m.GetTypes();
  163. foreach (Type t in types)
  164. {
  165. try
  166. {
  167. if (t.GetInterface("ITest") != null)
  168. {
  169. classes.Add(t);
  170. }
  171. }
  172. catch (Exception e)
  173. {
  174. Rest.Log.WarnFormat("[STATIC-TEST] Unable to include test {0} : {1}", t, e.Message);
  175. }
  176. }
  177. }
  178. }
  179. /// <summary>
  180. /// This routine loads all of the handlers discovered during
  181. /// instance initialization. Each handler is responsible for
  182. /// registering itself with this handler.
  183. /// I was not able to make this code work in a constructor.
  184. /// </summary>
  185. private void loadTests()
  186. {
  187. lock (tests)
  188. {
  189. if (!testsLoaded)
  190. {
  191. ConstructorInfo ci;
  192. Object ht;
  193. foreach (Type t in classes)
  194. {
  195. try
  196. {
  197. if (t.GetInterface("ITest") != null)
  198. {
  199. ci = t.GetConstructor(parms);
  200. ht = ci.Invoke(args);
  201. tests.Add((ITest)ht);
  202. Rest.Log.InfoFormat("{0} Test {1} added", MsgId, t);
  203. }
  204. }
  205. catch (Exception e)
  206. {
  207. Rest.Log.WarnFormat("{0} Unable to load test {1} : {2}", MsgId, t, e.Message);
  208. }
  209. }
  210. testsLoaded = true;
  211. }
  212. }
  213. }
  214. }
  215. }