RestAssetServices.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 libsecondlife;
  29. using Nini.Config;
  30. using System;
  31. using System.Collections.Generic;
  32. using System.IO;
  33. using System.Threading;
  34. using System.Xml;
  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 RestAssetServices : IRest
  42. {
  43. private string key = "assets";
  44. private bool enabled = false;
  45. private string qPrefix = "assets";
  46. // A simple constructor is used to handle any once-only
  47. // initialization of working classes.
  48. public RestAssetServices(RestHandler p_rest)
  49. {
  50. Rest.Log.InfoFormat("{0} Asset services initializing", MsgId);
  51. Rest.Log.InfoFormat("{0} Using REST Implementation Version {1}", MsgId, Rest.Version);
  52. // Integrate domain
  53. if (!qPrefix.StartsWith(Rest.UrlPathSeparator))
  54. {
  55. qPrefix = Rest.Prefix + Rest.UrlPathSeparator + qPrefix;
  56. }
  57. // Authentication domain
  58. Rest.Domains.Add(key,Rest.Config.GetString("asset-domain",qPrefix));
  59. // Register interface
  60. Rest.Plugin.AddPathHandler(DoAsset, qPrefix, Allocate);
  61. // Activate
  62. enabled = true;
  63. Rest.Log.InfoFormat("{0} Asset 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. Rest.Log.InfoFormat("{0} Asset services closing down", MsgId);
  77. }
  78. // Properties
  79. internal string MsgId
  80. {
  81. get { return Rest.MsgId; }
  82. }
  83. #region Interface
  84. private RequestData Allocate(OSHttpRequest request, OSHttpResponse response)
  85. {
  86. return (RequestData) new AssetRequestData(request, response, qPrefix);
  87. }
  88. // Asset Handler
  89. private void DoAsset(RequestData rparm)
  90. {
  91. if (!enabled) return;
  92. AssetRequestData rdata = (AssetRequestData) rparm;
  93. Rest.Log.DebugFormat("{0} REST Asset handler ENTRY", MsgId);
  94. // Now that we know this is a serious attempt to
  95. // access inventory data, we should find out who
  96. // is asking, and make sure they are authorized
  97. // to do so. We need to validate the caller's
  98. // identity before revealing anything about the
  99. // status quo. Authenticate throws an exception
  100. // via Fail if no identity information is present.
  101. //
  102. // With the present HTTP server we can't use the
  103. // builtin authentication mechanisms because they
  104. // would be enforced for all in-bound requests.
  105. // Instead we look at the headers ourselves and
  106. // handle authentication directly.
  107. try
  108. {
  109. if (!rdata.IsAuthenticated)
  110. {
  111. rdata.Fail(Rest.HttpStatusCodeNotAuthorized, Rest.HttpStatusDescNotAuthorized);
  112. }
  113. }
  114. catch (RestException e)
  115. {
  116. if (e.statusCode == Rest.HttpStatusCodeNotAuthorized)
  117. {
  118. Rest.Log.WarnFormat("{0} User not authenticated", MsgId);
  119. Rest.Log.DebugFormat("{0} Authorization header: {1}", MsgId,
  120. rdata.request.Headers.Get("Authorization"));
  121. }
  122. else
  123. {
  124. Rest.Log.ErrorFormat("{0} User authentication failed", MsgId);
  125. Rest.Log.DebugFormat("{0} Authorization header: {1}", MsgId,
  126. rdata.request.Headers.Get("Authorization"));
  127. }
  128. throw (e);
  129. }
  130. // Remove the prefix and what's left are the parameters. If we don't have
  131. // the parameters we need, fail the request. Parameters do NOT include
  132. // any supplied query values.
  133. if (rdata.parameters.Length > 0)
  134. {
  135. switch (rdata.method)
  136. {
  137. case "get" :
  138. DoGet(rdata);
  139. break;
  140. case "put" :
  141. case "post" :
  142. case "delete" :
  143. default :
  144. Rest.Log.WarnFormat("{0} Asset: Method not supported: {1}",
  145. MsgId, rdata.method);
  146. rdata.Fail(Rest.HttpStatusCodeBadRequest,
  147. Rest.HttpStatusDescBadRequest);
  148. break;
  149. }
  150. }
  151. else
  152. {
  153. Rest.Log.WarnFormat("{0} Asset: No agent information provided", MsgId);
  154. rdata.Fail(Rest.HttpStatusCodeBadRequest, Rest.HttpStatusDescBadRequest);
  155. }
  156. Rest.Log.DebugFormat("{0} REST Asset handler EXIT", MsgId);
  157. }
  158. #endregion Interface
  159. private void DoGet(AssetRequestData rdata)
  160. {
  161. bool istexture = false;
  162. Rest.Log.DebugFormat("{0} REST Asset handler, Method = <{1}> ENTRY", MsgId, rdata.method);
  163. // The only parameter we accept is an LLUUID for
  164. // the asset
  165. if (rdata.parameters.Length == 1)
  166. {
  167. LLUUID uuid = new LLUUID(rdata.parameters[0]);
  168. AssetBase asset = Rest.AssetServices.GetAsset(uuid, istexture);
  169. if (asset != null)
  170. {
  171. Rest.Log.DebugFormat("{0} Asset located <{1}>", MsgId, rdata.parameters[0]);
  172. rdata.initXmlWriter();
  173. rdata.writer.WriteStartElement(String.Empty,"Asset",String.Empty);
  174. rdata.writer.WriteAttributeString("id", asset.ID.ToString());
  175. rdata.writer.WriteAttributeString("name", asset.Name);
  176. rdata.writer.WriteAttributeString("desc", asset.Description);
  177. rdata.writer.WriteAttributeString("type", asset.Type.ToString());
  178. rdata.writer.WriteAttributeString("local", asset.Local.ToString());
  179. rdata.writer.WriteAttributeString("temporary", asset.Temporary.ToString());
  180. rdata.writer.WriteBase64(asset.Data,0,asset.Data.Length);
  181. rdata.writer.WriteFullEndElement();
  182. }
  183. else
  184. {
  185. Rest.Log.DebugFormat("{0} Invalid parameters: <{1}>", MsgId, rdata.path);
  186. rdata.Fail(Rest.HttpStatusCodeNotFound,
  187. Rest.HttpStatusDescNotFound);
  188. }
  189. }
  190. rdata.Complete();
  191. rdata.Respond("Asset " + rdata.method + ": Normal completion");
  192. }
  193. internal class AssetRequestData : RequestData
  194. {
  195. internal AssetRequestData(OSHttpRequest request, OSHttpResponse response, string prefix)
  196. : base(request, response, prefix)
  197. {
  198. }
  199. }
  200. }
  201. }