From f8ea2740909cbe33c7729be0333260f0567e7867 Mon Sep 17 00:00:00 2001
From: Mike Mazur
Date: Mon, 16 Feb 2009 02:27:25 +0000
Subject: - asset server functionality works with OpenSim's HttpServer - start
 of removal of AssetInventoryServer.Metadata class

---
 OpenSim/Grid/AssetInventoryServer/Interfaces.cs    |  2 +-
 .../Plugins/OpenSim/OpenSimAssetFrontendPlugin.cs  | 97 ++++------------------
 .../Plugins/OpenSim/OpenSimAssetStoragePlugin.cs   | 23 +----
 .../Plugins/Simple/SimpleAssetStoragePlugin.cs     | 16 +++-
 4 files changed, 34 insertions(+), 104 deletions(-)

(limited to 'OpenSim')

diff --git a/OpenSim/Grid/AssetInventoryServer/Interfaces.cs b/OpenSim/Grid/AssetInventoryServer/Interfaces.cs
index 19298c5..d934b14 100644
--- a/OpenSim/Grid/AssetInventoryServer/Interfaces.cs
+++ b/OpenSim/Grid/AssetInventoryServer/Interfaces.cs
@@ -77,7 +77,7 @@ namespace OpenSim.Grid.AssetInventoryServer
     {
         BackendResponse TryFetchMetadata(UUID assetID, out Metadata metadata);
         BackendResponse TryFetchData(UUID assetID, out byte[] assetData);
-        BackendResponse TryFetchDataMetadata(UUID assetID, out Metadata metadata, out byte[] assetData);
+        BackendResponse TryFetchDataMetadata(UUID assetID, out AssetBase asset);
         BackendResponse TryCreateAsset(Metadata metadata, byte[] assetData);
         BackendResponse TryCreateAsset(Metadata metadata, byte[] assetData, out UUID assetID);
         int ForEach(Action<Metadata> action, int start, int count);
diff --git a/OpenSim/Grid/AssetInventoryServer/Plugins/OpenSim/OpenSimAssetFrontendPlugin.cs b/OpenSim/Grid/AssetInventoryServer/Plugins/OpenSim/OpenSimAssetFrontendPlugin.cs
index 3431ff3..b8f48cb 100644
--- a/OpenSim/Grid/AssetInventoryServer/Plugins/OpenSim/OpenSimAssetFrontendPlugin.cs
+++ b/OpenSim/Grid/AssetInventoryServer/Plugins/OpenSim/OpenSimAssetFrontendPlugin.cs
@@ -43,7 +43,7 @@ namespace OpenSim.Grid.AssetInventoryServer.Plugins.OpenSim
 {
     public class OpenSimAssetFrontendPlugin : IAssetInventoryServerPlugin
     {
-        private AssetInventoryServer server;
+        private AssetInventoryServer m_server;
 
         public OpenSimAssetFrontendPlugin()
         {
@@ -53,13 +53,13 @@ namespace OpenSim.Grid.AssetInventoryServer.Plugins.OpenSim
 
         public void Initialise(AssetInventoryServer server)
         {
-            this.server = server;
+            m_server = server;
 
             // Asset request
-            server.HttpServer.AddStreamHandler(new AssetRequestHandler(server));
+            m_server.HttpServer.AddStreamHandler(new AssetRequestHandler(server));
 
             // Asset creation
-            server.HttpServer.AddStreamHandler(new AssetPostHandler(server));
+            m_server.HttpServer.AddStreamHandler(new AssetPostHandler(server));
 
             Logger.Log.Info("[ASSET] OpenSim Asset Frontend loaded.");
         }
@@ -90,63 +90,31 @@ namespace OpenSim.Grid.AssetInventoryServer.Plugins.OpenSim
 
         #endregion IPlugin implementation
 
-        public class AssetRequestHandler : IStreamedRequestHandler
+        public class AssetRequestHandler : BaseStreamHandler
         {
             AssetInventoryServer m_server;
-            string m_contentType;
-            string m_httpMethod;
-            string m_path;
 
-            public AssetRequestHandler(AssetInventoryServer server)
+            //public AssetRequestHandler(AssetInventoryServer server) : base("GET", "^/assets")
+            public AssetRequestHandler(AssetInventoryServer server) : base("GET", "/assets")
             {
+                Logger.Log.Info("[REST]: In Get Request");
                 m_server = server;
-                m_contentType = null;
-                m_httpMethod = "GET";
-                m_path = @"^/assets/";
             }
 
-            #region IStreamedRequestHandler implementation
-
-            public string ContentType
-            {
-                get { return m_contentType; }
-            }
-
-            public string HttpMethod
-            {
-                get { return m_httpMethod; }
-            }
-
-            public string Path
-            {
-                get { return m_path; }
-            }
-
-            public byte[] Handle(string path, Stream request, OSHttpRequest httpRequest, OSHttpResponse httpResponse)
+            public override byte[] Handle(string path, Stream request, OSHttpRequest httpRequest, OSHttpResponse httpResponse)
             {
-                byte[] buffer = null;
+                byte[] buffer = new byte[] {};
                 UUID assetID;
                 // Split the URL up to get the asset ID out
                 string[] rawUrl = httpRequest.Url.PathAndQuery.Split('/');
 
                 if (rawUrl.Length >= 3 && rawUrl[2].Length >= 36 && UUID.TryParse(rawUrl[2].Substring(0, 36), out assetID))
                 {
-                    Metadata metadata;
-                    byte[] assetData;
                     BackendResponse dataResponse;
 
-                    if ((dataResponse = m_server.StorageProvider.TryFetchDataMetadata(assetID, out metadata, out assetData)) == BackendResponse.Success)
+                    AssetBase asset = new AssetBase();
+                    if ((dataResponse = m_server.StorageProvider.TryFetchDataMetadata(assetID, out asset)) == BackendResponse.Success)
                     {
-                        AssetBase asset = new AssetBase();
-                        asset.Data = assetData;
-                        asset.Metadata.FullID = metadata.ID;
-                        asset.Metadata.Name = metadata.Name;
-                        asset.Metadata.Description = metadata.Description;
-                        asset.Metadata.CreationDate = metadata.CreationDate;
-                        asset.Metadata.Type = (sbyte) Utils.ContentTypeToSLAssetType(metadata.ContentType);
-                        asset.Metadata.Local = false;
-                        asset.Metadata.Temporary = metadata.Temporary;
-
                         XmlSerializer xs = new XmlSerializer(typeof (AssetBase));
                         MemoryStream ms = new MemoryStream();
                         XmlTextWriter xw = new XmlTextWriter(ms, Encoding.UTF8);
@@ -155,12 +123,8 @@ namespace OpenSim.Grid.AssetInventoryServer.Plugins.OpenSim
 
                         ms.Seek(0, SeekOrigin.Begin);
                         buffer = ms.GetBuffer();
-
+                        Array.Resize<byte>(ref buffer, (int) ms.Length);
                         httpResponse.StatusCode = (int) HttpStatusCode.OK;
-                        httpResponse.ContentType = "application/xml";
-                        httpResponse.ContentLength = ms.Length;
-                        httpResponse.Body.Write(buffer, 0, (int) ms.Length);
-                        httpResponse.Body.Flush();
                     }
                     else
                     {
@@ -175,44 +139,19 @@ namespace OpenSim.Grid.AssetInventoryServer.Plugins.OpenSim
 
                 return buffer;
             }
-
-            #endregion IStreamedRequestHandler implementation
         }
 
-        public class AssetPostHandler : IStreamedRequestHandler
+        public class AssetPostHandler : BaseStreamHandler
         {
             AssetInventoryServer m_server;
-            string m_contentType;
-            string m_httpMethod;
-            string m_path;
 
-            public AssetPostHandler(AssetInventoryServer server)
+            //public AssetPostHandler(AssetInventoryServer server) : base("POST", "/^assets")
+            public AssetPostHandler(AssetInventoryServer server) : base("POST", "/assets")
             {
                 m_server = server;
-                m_contentType = null;
-                m_httpMethod = "POST";
-                m_path = @"^/assets/";
-            }
-
-            #region IStreamedRequestHandler implementation
-
-            public string ContentType
-            {
-                get { return m_contentType; }
-            }
-
-            public string HttpMethod
-            {
-                get { return m_httpMethod; }
-            }
-
-            public string Path
-            {
-                get { return m_path; }
             }
 
-            public byte[] Handle(string path, Stream request, OSHttpRequest httpRequest, OSHttpResponse httpResponse)
-        //bool AssetPostHandler(IHttpClientContext client, IHttpRequest request, IHttpResponse response)
+            public override byte[] Handle(string path, Stream request, OSHttpRequest httpRequest, OSHttpResponse httpResponse)
             {
                 Metadata metadata = new Metadata();
 
@@ -254,8 +193,6 @@ namespace OpenSim.Grid.AssetInventoryServer.Plugins.OpenSim
 
                 return null;
             }
-
-            #endregion IStreamedRequestHandler implementation
         }
     }
 }
diff --git a/OpenSim/Grid/AssetInventoryServer/Plugins/OpenSim/OpenSimAssetStoragePlugin.cs b/OpenSim/Grid/AssetInventoryServer/Plugins/OpenSim/OpenSimAssetStoragePlugin.cs
index b5bc70e..e35092b 100644
--- a/OpenSim/Grid/AssetInventoryServer/Plugins/OpenSim/OpenSimAssetStoragePlugin.cs
+++ b/OpenSim/Grid/AssetInventoryServer/Plugins/OpenSim/OpenSimAssetStoragePlugin.cs
@@ -137,28 +137,11 @@ namespace OpenSim.Grid.AssetInventoryServer.Plugins.OpenSim
             return ret;
         }
 
-        public BackendResponse TryFetchDataMetadata(UUID assetID, out Metadata metadata, out byte[] assetData)
+        public BackendResponse TryFetchDataMetadata(UUID assetID, out AssetBase asset)
         {
-            metadata = null;
-            assetData = null;
-            //BackendResponse ret;
-
-            AssetBase asset = m_assetProvider.FetchAsset(assetID);
+            asset = m_assetProvider.FetchAsset(assetID);
 
-            if (asset != null)
-            {
-                metadata = new Metadata();
-                metadata.ID = asset.Metadata.FullID;
-                metadata.CreationDate = OpenMetaverse.Utils.Epoch;
-                metadata.SHA1 = null;
-                metadata.Name = asset.Metadata.Name;
-                metadata.Description = asset.Metadata.Description;
-                metadata.ContentType = Utils.SLAssetTypeToContentType(asset.Metadata.Type);
-                metadata.Temporary = asset.Metadata.Temporary;
-
-                assetData = asset.Data;
-            }
-            else return BackendResponse.NotFound;
+            if (asset == null) return BackendResponse.NotFound;
 
             return BackendResponse.Success;
         }
diff --git a/OpenSim/Grid/AssetInventoryServer/Plugins/Simple/SimpleAssetStoragePlugin.cs b/OpenSim/Grid/AssetInventoryServer/Plugins/Simple/SimpleAssetStoragePlugin.cs
index 301a55f..5a718f2 100644
--- a/OpenSim/Grid/AssetInventoryServer/Plugins/Simple/SimpleAssetStoragePlugin.cs
+++ b/OpenSim/Grid/AssetInventoryServer/Plugins/Simple/SimpleAssetStoragePlugin.cs
@@ -95,10 +95,10 @@ namespace OpenSim.Grid.AssetInventoryServer.Plugins.Simple
             return ret;
         }
 
-        public BackendResponse TryFetchDataMetadata(UUID assetID, out Metadata metadata, out byte[] assetData)
+        public BackendResponse TryFetchDataMetadata(UUID assetID, out AssetBase asset)
         {
-            metadata = null;
-            assetData = null;
+            Metadata metadata = null;
+            byte[] assetData = null;
             string filename;
             BackendResponse ret;
 
@@ -121,6 +121,16 @@ namespace OpenSim.Grid.AssetInventoryServer.Plugins.Simple
                 ret = BackendResponse.NotFound;
             }
 
+            asset = new AssetBase();
+            asset.Data = assetData;
+            asset.Metadata.FullID = metadata.ID;
+            asset.Metadata.Name = metadata.Name;
+            asset.Metadata.Description = metadata.Description;
+            asset.Metadata.CreationDate = metadata.CreationDate;
+            asset.Metadata.Type = (sbyte) Utils.ContentTypeToSLAssetType(metadata.ContentType);
+            asset.Metadata.Local = false;
+            asset.Metadata.Temporary = metadata.Temporary;
+
             server.MetricsProvider.LogAssetMetadataFetch(EXTENSION_NAME, ret, assetID, DateTime.Now);
             server.MetricsProvider.LogAssetDataFetch(EXTENSION_NAME, ret, assetID, (assetData != null ? assetData.Length : 0), DateTime.Now);
             return ret;
-- 
cgit v1.1