From 07b8d51da8186e537da35c1d56d176b4843d8eaa Mon Sep 17 00:00:00 2001 From: Mike Mazur Date: Mon, 16 Feb 2009 02:27:17 +0000 Subject: AssetInventoryServer now compiles while using the standard OpenSim console and HttpServer. It doesn't work though. --- .../Plugins/ReferenceFrontendPlugin.cs | 329 ++++++++++++++------- 1 file changed, 217 insertions(+), 112 deletions(-) (limited to 'OpenSim/Grid/AssetInventoryServer/Plugins/ReferenceFrontendPlugin.cs') diff --git a/OpenSim/Grid/AssetInventoryServer/Plugins/ReferenceFrontendPlugin.cs b/OpenSim/Grid/AssetInventoryServer/Plugins/ReferenceFrontendPlugin.cs index fddc385..d1ea201 100644 --- a/OpenSim/Grid/AssetInventoryServer/Plugins/ReferenceFrontendPlugin.cs +++ b/OpenSim/Grid/AssetInventoryServer/Plugins/ReferenceFrontendPlugin.cs @@ -28,6 +28,7 @@ */ using System; +using System.IO; using System.Collections.Generic; using System.Net; using System.Xml; @@ -35,12 +36,13 @@ using OpenMetaverse; using OpenMetaverse.StructuredData; using HttpServer; using OpenSim.Framework; +using OpenSim.Framework.Servers; namespace OpenSim.Grid.AssetInventoryServer.Plugins { public class ReferenceFrontendPlugin : IAssetInventoryServerPlugin { - AssetInventoryServer server; + AssetInventoryServer m_server; public ReferenceFrontendPlugin() { @@ -50,18 +52,16 @@ namespace OpenSim.Grid.AssetInventoryServer.Plugins public void Initialise(AssetInventoryServer server) { - this.server = server; + m_server = server; // Asset metadata request - server.HttpServer.AddHandler("get", null, @"^/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/metadata", - MetadataRequestHandler); + m_server.HttpServer.AddStreamHandler(new MetadataRequestHandler(server)); // Asset data request - server.HttpServer.AddHandler("get", null, @"^/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/data", - DataRequestHandler); + m_server.HttpServer.AddStreamHandler(new DataRequestHandler(server)); // Asset creation - server.HttpServer.AddHandler("post", null, "^/createasset", CreateRequestHandler); + m_server.HttpServer.AddStreamHandler(new CreateRequestHandler(server)); Logger.Log.Info("[ASSET] Reference Frontend loaded."); } @@ -92,174 +92,279 @@ namespace OpenSim.Grid.AssetInventoryServer.Plugins #endregion IPlugin implementation - bool MetadataRequestHandler(IHttpClientContext client, IHttpRequest request, IHttpResponse response) + public class MetadataRequestHandler : IStreamedRequestHandler { - UUID assetID; - // Split the URL up into an AssetID and a method - string[] rawUrl = request.Uri.PathAndQuery.Split('/'); + AssetInventoryServer m_server; + string m_contentType; + string m_httpMethod; + string m_path; - if (rawUrl.Length >= 3 && UUID.TryParse(rawUrl[1], out assetID)) + public MetadataRequestHandler(AssetInventoryServer server) { - UUID authToken = Utils.GetAuthToken(request); + m_server = server; + m_contentType = null; + m_httpMethod = "GET"; + m_path = @"^/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/metadata"; + } + + #region IStreamedRequestHandler implementation + + public string ContentType + { + get { return m_contentType; } + } + + public string HttpMethod + { + get { return m_httpMethod; } + } + + public string Path + { + get { return m_path; } + } - if (server.AuthorizationProvider.IsMetadataAuthorized(authToken, assetID)) + public byte[] Handle(string path, Stream request, OSHttpRequest httpRequest, OSHttpResponse httpResponse) + { + byte[] serializedData = null; + UUID assetID; + // Split the URL up into an AssetID and a method + string[] rawUrl = httpRequest.Url.PathAndQuery.Split('/'); + + if (rawUrl.Length >= 3 && UUID.TryParse(rawUrl[1], out assetID)) { - Metadata metadata; - BackendResponse storageResponse = server.StorageProvider.TryFetchMetadata(assetID, out metadata); + UUID authToken = Utils.GetAuthToken(httpRequest); - if (storageResponse == BackendResponse.Success) + if (m_server.AuthorizationProvider.IsMetadataAuthorized(authToken, assetID)) { - // If the asset data location wasn't specified in the metadata, specify it - // manually here by pointing back to this asset server - if (!metadata.Methods.ContainsKey("data")) - { - metadata.Methods["data"] = new Uri(String.Format("{0}://{1}/{2}/data", - request.Uri.Scheme, request.Uri.Authority, assetID)); - } + Metadata metadata; + BackendResponse storageResponse = m_server.StorageProvider.TryFetchMetadata(assetID, out metadata); - byte[] serializedData = metadata.SerializeToBytes(); + if (storageResponse == BackendResponse.Success) + { + // If the asset data location wasn't specified in the metadata, specify it + // manually here by pointing back to this asset server + if (!metadata.Methods.ContainsKey("data")) + { + metadata.Methods["data"] = new Uri(String.Format("{0}://{1}/{2}/data", + httpRequest.Url.Scheme, httpRequest.Url.Authority, assetID)); + } - response.Status = HttpStatusCode.OK; - response.ContentType = "application/json"; - response.ContentLength = serializedData.Length; - response.Body.Write(serializedData, 0, serializedData.Length); + serializedData = metadata.SerializeToBytes(); - } - else if (storageResponse == BackendResponse.NotFound) - { - Logger.Log.Warn("Could not find metadata for asset " + assetID.ToString()); - response.Status = HttpStatusCode.NotFound; + httpResponse.StatusCode = (int) HttpStatusCode.OK; + httpResponse.ContentType = "application/json"; + httpResponse.ContentLength = serializedData.Length; + httpResponse.Body.Write(serializedData, 0, serializedData.Length); + } + else if (storageResponse == BackendResponse.NotFound) + { + Logger.Log.Warn("Could not find metadata for asset " + assetID.ToString()); + httpResponse.StatusCode = (int) HttpStatusCode.NotFound; + } + else + { + httpResponse.StatusCode = (int) HttpStatusCode.InternalServerError; + } } else { - response.Status = HttpStatusCode.InternalServerError; + httpResponse.StatusCode = (int) HttpStatusCode.Forbidden; } - } - else - { - response.Status = HttpStatusCode.Forbidden; + + return serializedData; } - return true; + httpResponse.StatusCode = (int) HttpStatusCode.NotFound; + return serializedData; } - response.Status = HttpStatusCode.NotFound; - return true; + #endregion IStreamedRequestHandler implementation } - bool DataRequestHandler(IHttpClientContext client, IHttpRequest request, IHttpResponse response) + public class DataRequestHandler : IStreamedRequestHandler { - UUID assetID; - // Split the URL up into an AssetID and a method - string[] rawUrl = request.Uri.PathAndQuery.Split('/'); + AssetInventoryServer m_server; + string m_contentType; + string m_httpMethod; + string m_path; + + public DataRequestHandler(AssetInventoryServer server) + { + m_server = server; + m_contentType = null; + m_httpMethod = "GET"; + m_path = @"^/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/data"; + } + + #region IStreamedRequestHandler implementation + + public string ContentType + { + get { return m_contentType; } + } - if (rawUrl.Length >= 3 && UUID.TryParse(rawUrl[1], out assetID)) + public string HttpMethod { - UUID authToken = Utils.GetAuthToken(request); + get { return m_httpMethod; } + } + + public string Path + { + get { return m_path; } + } - if (server.AuthorizationProvider.IsDataAuthorized(authToken, assetID)) + public byte[] Handle(string path, Stream request, OSHttpRequest httpRequest, OSHttpResponse httpResponse) + { + byte[] assetData = null; + UUID assetID; + // Split the URL up into an AssetID and a method + string[] rawUrl = httpRequest.Url.PathAndQuery.Split('/'); + + if (rawUrl.Length >= 3 && UUID.TryParse(rawUrl[1], out assetID)) { - byte[] assetData; - BackendResponse storageResponse = server.StorageProvider.TryFetchData(assetID, out assetData); + UUID authToken = Utils.GetAuthToken(httpRequest); - if (storageResponse == BackendResponse.Success) + if (m_server.AuthorizationProvider.IsDataAuthorized(authToken, assetID)) { - response.Status = HttpStatusCode.OK; - response.Status = HttpStatusCode.OK; - response.ContentType = "application/octet-stream"; - response.AddHeader("Content-Disposition", "attachment; filename=" + assetID.ToString()); - response.ContentLength = assetData.Length; - response.Body.Write(assetData, 0, assetData.Length); - } - else if (storageResponse == BackendResponse.NotFound) - { - response.Status = HttpStatusCode.NotFound; + BackendResponse storageResponse = m_server.StorageProvider.TryFetchData(assetID, out assetData); + + if (storageResponse == BackendResponse.Success) + { + httpResponse.StatusCode = (int) HttpStatusCode.OK; + httpResponse.ContentType = "application/octet-stream"; + httpResponse.AddHeader("Content-Disposition", "attachment; filename=" + assetID.ToString()); + httpResponse.ContentLength = assetData.Length; + httpResponse.Body.Write(assetData, 0, assetData.Length); + } + else if (storageResponse == BackendResponse.NotFound) + { + httpResponse.StatusCode = (int) HttpStatusCode.NotFound; + } + else + { + httpResponse.StatusCode = (int) HttpStatusCode.InternalServerError; + } } else { - response.Status = HttpStatusCode.InternalServerError; + httpResponse.StatusCode = (int) HttpStatusCode.Forbidden; } - } - else - { - response.Status = HttpStatusCode.Forbidden; + + return assetData; } - return true; + httpResponse.StatusCode = (int) HttpStatusCode.BadRequest; + return assetData; } - response.Status = HttpStatusCode.BadRequest; - return true; + #endregion IStreamedRequestHandler implementation } - bool CreateRequestHandler(IHttpClientContext client, IHttpRequest request, IHttpResponse response) + public class CreateRequestHandler : IStreamedRequestHandler { - UUID authToken = Utils.GetAuthToken(request); + AssetInventoryServer m_server; + string m_contentType; + string m_httpMethod; + string m_path; - if (server.AuthorizationProvider.IsCreateAuthorized(authToken)) + public CreateRequestHandler(AssetInventoryServer server) { - try - { - OSD osdata = OSDParser.DeserializeJson(request.Body); + m_server = server; + m_contentType = null; + m_httpMethod = "POST"; + m_path = "^/createasset"; + } - if (osdata.Type == OSDType.Map) - { - OSDMap map = (OSDMap)osdata; - Metadata metadata = new Metadata(); - metadata.Deserialize(map); + #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) + { + byte[] responseData = null; + UUID authToken = Utils.GetAuthToken(httpRequest); - byte[] assetData = map["data"].AsBinary(); + if (m_server.AuthorizationProvider.IsCreateAuthorized(authToken)) + { + try + { + OSD osdata = OSDParser.DeserializeJson(httpRequest.InputStream); - if (assetData != null && assetData.Length > 0) + if (osdata.Type == OSDType.Map) { - BackendResponse storageResponse; + OSDMap map = (OSDMap)osdata; + Metadata metadata = new Metadata(); + metadata.Deserialize(map); - if (metadata.ID != UUID.Zero) - storageResponse = server.StorageProvider.TryCreateAsset(metadata, assetData); - else - storageResponse = server.StorageProvider.TryCreateAsset(metadata, assetData, out metadata.ID); + byte[] assetData = map["data"].AsBinary(); - if (storageResponse == BackendResponse.Success) - { - response.Status = HttpStatusCode.Created; - OSDMap responseMap = new OSDMap(1); - responseMap["id"] = OSD.FromUUID(metadata.ID); - LitJson.JsonData jsonData = OSDParser.SerializeJson(responseMap); - byte[] responseData = System.Text.Encoding.UTF8.GetBytes(jsonData.ToJson()); - response.Body.Write(responseData, 0, responseData.Length); - response.Body.Flush(); - } - else if (storageResponse == BackendResponse.NotFound) + if (assetData != null && assetData.Length > 0) { - response.Status = HttpStatusCode.NotFound; + BackendResponse storageResponse; + + if (metadata.ID != UUID.Zero) + storageResponse = m_server.StorageProvider.TryCreateAsset(metadata, assetData); + else + storageResponse = m_server.StorageProvider.TryCreateAsset(metadata, assetData, out metadata.ID); + + if (storageResponse == BackendResponse.Success) + { + httpResponse.StatusCode = (int) HttpStatusCode.Created; + OSDMap responseMap = new OSDMap(1); + responseMap["id"] = OSD.FromUUID(metadata.ID); + LitJson.JsonData jsonData = OSDParser.SerializeJson(responseMap); + responseData = System.Text.Encoding.UTF8.GetBytes(jsonData.ToJson()); + httpResponse.Body.Write(responseData, 0, responseData.Length); + httpResponse.Body.Flush(); + } + else if (storageResponse == BackendResponse.NotFound) + { + httpResponse.StatusCode = (int) HttpStatusCode.NotFound; + } + else + { + httpResponse.StatusCode = (int) HttpStatusCode.InternalServerError; + } } else { - response.Status = HttpStatusCode.InternalServerError; + httpResponse.StatusCode = (int) HttpStatusCode.BadRequest; } } else { - response.Status = HttpStatusCode.BadRequest; + httpResponse.StatusCode = (int) HttpStatusCode.BadRequest; } } - else + catch (Exception ex) { - response.Status = HttpStatusCode.BadRequest; + httpResponse.StatusCode = (int) HttpStatusCode.InternalServerError; + httpResponse.StatusDescription = ex.Message; } } - catch (Exception ex) + else { - response.Status = HttpStatusCode.InternalServerError; - response.Reason = ex.Message; + httpResponse.StatusCode = (int) HttpStatusCode.Forbidden; } - } - else - { - response.Status = HttpStatusCode.Forbidden; + + return responseData; } - return true; + #endregion IStreamedRequestHandler implementation } } } -- cgit v1.1