AssetInventoryServer now compiles while using the standard OpenSim

console and HttpServer. It doesn't work though.
0.6.3-post-fixes
Mike Mazur 2009-02-16 02:27:17 +00:00
parent 1778f799ba
commit 07b8d51da8
8 changed files with 789 additions and 554 deletions

View File

@ -36,17 +36,19 @@ using System.Security.Cryptography.X509Certificates;
using System.ServiceProcess;
using ExtensionLoader;
using ExtensionLoader.Config;
using HttpServer;
//using HttpServer;
using log4net;
using OpenSim.Framework;
using OpenSim.Framework.Servers;
using OpenSim.Framework.Console;
namespace OpenSim.Grid.AssetInventoryServer
{
public class AssetInventoryServer : ServiceBase
public class AssetInventoryServer : BaseOpenSimServer//ServiceBase
{
public const string CONFIG_FILE = "AssetInventoryServer.ini";
public WebServer HttpServer;
//public WebServer HttpServer;
public IniConfigSource ConfigFile;
public IAssetStorageProvider StorageProvider;
@ -60,7 +62,9 @@ namespace OpenSim.Grid.AssetInventoryServer
public AssetInventoryServer()
{
this.ServiceName = "OpenSimAssetInventoryServer";
m_console = new ConsoleBase("Asset");
MainConsole.Instance = m_console;
//this.ServiceName = "OpenSimAssetInventoryServer";
}
public bool Start()
@ -123,7 +127,7 @@ namespace OpenSim.Grid.AssetInventoryServer
catch (Exception ex)
{
Logger.Log.Error("Initializing the HTTP server failed, shutting down: " + ex.Message);
Stop();
Shutdown();
return false;
}
@ -138,7 +142,17 @@ namespace OpenSim.Grid.AssetInventoryServer
return true;
}
public void Shutdown()
public void Work()
{
m_console.Notice("Enter help for a list of commands");
while (true)
{
m_console.Prompt();
}
}
public override void ShutdownSpecific()
{
foreach (IAssetInventoryServerPlugin plugin in frontends)
{
@ -162,26 +176,27 @@ namespace OpenSim.Grid.AssetInventoryServer
void InitHttpServer(int port, X509Certificate serverCert)
{
if (serverCert != null)
HttpServer = new WebServer(IPAddress.Any, port, serverCert, null, false);
else
HttpServer = new WebServer(IPAddress.Any, port);
//if (serverCert != null)
// HttpServer = new WebServer(IPAddress.Any, port, serverCert, null, false);
//else
// HttpServer = new WebServer(IPAddress.Any, port);
HttpServer.LogWriter = new log4netLogWriter(Logger.Log);
//HttpServer.LogWriter = new log4netLogWriter(Logger.Log);
HttpServer.Set404Handler(
delegate(IHttpClientContext client, IHttpRequest request, IHttpResponse response)
{
Logger.Log.Warn("Requested page was not found: " + request.Uri.PathAndQuery);
//HttpServer.Set404Handler(
// delegate(IHttpClientContext client, IHttpRequest request, IHttpResponse response)
// {
// Logger.Log.Warn("Requested page was not found: " + request.Uri.PathAndQuery);
string notFoundString = "<html><head><title>Page Not Found</title></head><body>The requested page or method was not found</body></html>";
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(notFoundString);
response.Body.Write(buffer, 0, buffer.Length);
response.Status = HttpStatusCode.NotFound;
return true;
}
);
// string notFoundString = "<html><head><title>Page Not Found</title></head><body>The requested page or method was not found</body></html>";
// byte[] buffer = System.Text.Encoding.UTF8.GetBytes(notFoundString);
// response.Body.Write(buffer, 0, buffer.Length);
// response.Status = HttpStatusCode.NotFound;
// return true;
// }
//);
m_httpServer = new BaseHttpServer(8003);
HttpServer.Start();
Logger.Log.Info("Asset server is listening on port " + port);
@ -189,14 +204,14 @@ namespace OpenSim.Grid.AssetInventoryServer
#region ServiceBase Overrides
protected override void OnStart(string[] args)
{
Start();
}
protected override void OnStop()
{
Shutdown();
}
//protected override void OnStart(string[] args)
//{
// Start();
//}
//protected override void OnStop()
//{
// Shutdown();
//}
#endregion
@ -231,36 +246,36 @@ namespace OpenSim.Grid.AssetInventoryServer
}
}
public class log4netLogWriter : ILogWriter
{
ILog Log;
//public class log4netLogWriter : ILogWriter
//{
// ILog Log;
public log4netLogWriter(ILog log)
{
Log = log;
}
// public log4netLogWriter(ILog log)
// {
// Log = log;
// }
public void Write(object source, LogPrio prio, string message)
{
switch (prio)
{
case LogPrio.Trace:
case LogPrio.Debug:
Log.DebugFormat("{0}: {1}", source, message);
break;
case LogPrio.Info:
Log.InfoFormat("{0}: {1}", source, message);
break;
case LogPrio.Warning:
Log.WarnFormat("{0}: {1}", source, message);
break;
case LogPrio.Error:
Log.ErrorFormat("{0}: {1}", source, message);
break;
case LogPrio.Fatal:
Log.FatalFormat("{0}: {1}", source, message);
break;
}
}
}
// public void Write(object source, LogPrio prio, string message)
// {
// switch (prio)
// {
// case LogPrio.Trace:
// case LogPrio.Debug:
// Log.DebugFormat("{0}: {1}", source, message);
// break;
// case LogPrio.Info:
// Log.InfoFormat("{0}: {1}", source, message);
// break;
// case LogPrio.Warning:
// Log.WarnFormat("{0}: {1}", source, message);
// break;
// case LogPrio.Error:
// Log.ErrorFormat("{0}: {1}", source, message);
// break;
// case LogPrio.Fatal:
// Log.FatalFormat("{0}: {1}", source, message);
// break;
// }
// }
//}
}

View File

@ -50,8 +50,9 @@ namespace OpenSim.Grid.AssetInventoryServer
Environment.Exit(0);
};
while (true)
Console.ReadLine();
server.Work();
//while (true)
// Console.ReadLine();
}
#else
ServiceBase[] servicesToRun = new ServiceBase[] { new AssetInventoryServer() };

View File

@ -28,20 +28,21 @@
*/
using System;
using System.IO;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Net;
using System.Text;
using System.Web;
using OpenMetaverse;
using HttpServer;
using OpenSim.Framework;
using OpenSim.Framework.Servers;
namespace OpenSim.Grid.AssetInventoryServer.Plugins
{
public class BrowseFrontendPlugin : IAssetInventoryServerPlugin
{
AssetInventoryServer server;
AssetInventoryServer m_server;
public BrowseFrontendPlugin()
{
@ -51,10 +52,11 @@ namespace OpenSim.Grid.AssetInventoryServer.Plugins
public void Initialise(AssetInventoryServer server)
{
this.server = server;
m_server = server;
// Request for / or /?...
server.HttpServer.AddHandler("get", null, @"(^/$)|(^/\?.*)", BrowseRequestHandler);
//server.HttpServer.AddHandler("get", null, @"(^/$)|(^/\?.*)", BrowseRequestHandler);
m_server.HttpServer.AddStreamHandler(new BrowseRequestHandler(server));
Logger.Log.Info("[ASSET] Browser Frontend loaded.");
}
@ -85,67 +87,102 @@ namespace OpenSim.Grid.AssetInventoryServer.Plugins
#endregion IPlugin implementation
bool BrowseRequestHandler(IHttpClientContext client, IHttpRequest request, IHttpResponse response)
public class BrowseRequestHandler : IStreamedRequestHandler
{
const int ASSETS_PER_PAGE = 25;
const string HEADER = "<html><head><title>Asset Server</title></head><body>";
const string TABLE_HEADER =
"<table><tr><th>Name</th><th>Description</th><th>Type</th><th>ID</th><th>Temporary</th><th>SHA-1</th></tr>";
const string TABLE_FOOTER = "</table>";
const string FOOTER = "</body></html>";
AssetInventoryServer m_server;
string m_contentType;
string m_httpMethod;
string m_path;
UUID authToken = Utils.GetAuthToken(request);
StringBuilder html = new StringBuilder();
int start = 0;
uint page = 0;
if (!String.IsNullOrEmpty(request.Uri.Query))
public BrowseRequestHandler(AssetInventoryServer server)
{
NameValueCollection query = HttpUtility.ParseQueryString(request.Uri.Query);
if (!String.IsNullOrEmpty(query["page"]) && UInt32.TryParse(query["page"], out page))
start = (int)page * ASSETS_PER_PAGE;
m_server = server;
m_contentType = null;
m_httpMethod = "GET";
m_path = @"(^/$)|(^/\?.*)";
}
html.AppendLine(HEADER);
#region IStreamedRequestHandler implementation
html.AppendLine("<p>");
if (page > 0)
html.AppendFormat("<a href=\"{0}?page={1}\">&lt; Previous Page</a> | ", request.Uri.AbsolutePath, page - 1);
html.AppendFormat("<a href=\"{0}?page={1}\">Next Page &gt;</a>", request.Uri.AbsolutePath, page + 1);
html.AppendLine("</p>");
public string ContentType
{
get { return m_contentType; }
}
html.AppendLine(TABLE_HEADER);
public string HttpMethod
{
get { return m_httpMethod; }
}
server.StorageProvider.ForEach(
delegate(Metadata data)
public string Path
{
get { return m_path; }
}
public byte[] Handle(string path, Stream request, OSHttpRequest httpRequest, OSHttpResponse httpResponse)
{
const int ASSETS_PER_PAGE = 25;
const string HEADER = "<html><head><title>Asset Server</title></head><body>";
const string TABLE_HEADER =
"<table><tr><th>Name</th><th>Description</th><th>Type</th><th>ID</th><th>Temporary</th><th>SHA-1</th></tr>";
const string TABLE_FOOTER = "</table>";
const string FOOTER = "</body></html>";
UUID authToken = Utils.GetAuthToken(httpRequest);
StringBuilder html = new StringBuilder();
int start = 0;
uint page = 0;
if (!String.IsNullOrEmpty(httpRequest.Url.Query))
{
if (server.AuthorizationProvider.IsMetadataAuthorized(authToken, data.ID))
NameValueCollection query = HttpUtility.ParseQueryString(httpRequest.Url.Query);
if (!String.IsNullOrEmpty(query["page"]) && UInt32.TryParse(query["page"], out page))
start = (int)page * ASSETS_PER_PAGE;
}
html.AppendLine(HEADER);
html.AppendLine("<p>");
if (page > 0)
html.AppendFormat("<a href=\"{0}?page={1}\">&lt; Previous Page</a> | ", httpRequest.RawUrl, page - 1);
html.AppendFormat("<a href=\"{0}?page={1}\">Next Page &gt;</a>", httpRequest.RawUrl, page + 1);
html.AppendLine("</p>");
html.AppendLine(TABLE_HEADER);
m_server.StorageProvider.ForEach(
delegate(Metadata data)
{
html.AppendLine(String.Format(
"<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td></tr>",
data.Name, data.Description, data.ContentType, data.ID, data.Temporary,
BitConverter.ToString(data.SHA1).Replace("-", String.Empty)));
}
else
{
html.AppendLine(String.Format(
"<tr><td>[Protected Asset]</td><td>&nbsp;</td><td>&nbsp;</td><td>{0}</td><td>{1}</td><td>&nbsp;</td></tr>",
data.ID, data.Temporary));
}
}, start, ASSETS_PER_PAGE
);
if (m_server.AuthorizationProvider.IsMetadataAuthorized(authToken, data.ID))
{
html.AppendLine(String.Format(
"<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td></tr>",
data.Name, data.Description, data.ContentType, data.ID, data.Temporary,
BitConverter.ToString(data.SHA1).Replace("-", String.Empty)));
}
else
{
html.AppendLine(String.Format(
"<tr><td>[Protected Asset]</td><td>&nbsp;</td><td>&nbsp;</td><td>{0}</td><td>{1}</td><td>&nbsp;</td></tr>",
data.ID, data.Temporary));
}
}, start, ASSETS_PER_PAGE
);
html.AppendLine(TABLE_FOOTER);
html.AppendLine(TABLE_FOOTER);
html.AppendLine(FOOTER);
html.AppendLine(FOOTER);
byte[] responseData = System.Text.Encoding.UTF8.GetBytes(html.ToString());
byte[] responseData = System.Text.Encoding.UTF8.GetBytes(html.ToString());
response.Status = HttpStatusCode.OK;
response.Body.Write(responseData, 0, responseData.Length);
response.Body.Flush();
return true;
httpResponse.StatusCode = (int) HttpStatusCode.OK;
httpResponse.Body.Write(responseData, 0, responseData.Length);
httpResponse.Body.Flush();
return responseData;
}
#endregion IStreamedRequestHandler implementation
}
}
}

View File

@ -37,6 +37,7 @@ using System.Xml.Serialization;
using OpenMetaverse;
using HttpServer;
using OpenSim.Framework;
using OpenSim.Framework.Servers;
namespace OpenSim.Grid.AssetInventoryServer.Plugins.OpenSim
{
@ -55,10 +56,10 @@ namespace OpenSim.Grid.AssetInventoryServer.Plugins.OpenSim
this.server = server;
// Asset request
server.HttpServer.AddHandler("get", null, @"^/assets/", AssetRequestHandler);
server.HttpServer.AddStreamHandler(new AssetRequestHandler(server));
// Asset creation
server.HttpServer.AddHandler("post", null, @"^/assets/", AssetPostHandler);
server.HttpServer.AddStreamHandler(new AssetPostHandler(server));
Logger.Log.Info("[ASSET] OpenSim Asset Frontend loaded.");
}
@ -89,100 +90,172 @@ namespace OpenSim.Grid.AssetInventoryServer.Plugins.OpenSim
#endregion IPlugin implementation
bool AssetRequestHandler(IHttpClientContext client, IHttpRequest request, IHttpResponse response)
public class AssetRequestHandler : IStreamedRequestHandler
{
UUID assetID;
// Split the URL up to get the asset ID out
string[] rawUrl = request.Uri.PathAndQuery.Split('/');
AssetInventoryServer m_server;
string m_contentType;
string m_httpMethod;
string m_path;
if (rawUrl.Length >= 3 && rawUrl[2].Length >= 36 && UUID.TryParse(rawUrl[2].Substring(0, 36), out assetID))
public AssetRequestHandler(AssetInventoryServer server)
{
Metadata metadata;
byte[] assetData;
BackendResponse dataResponse;
m_server = server;
m_contentType = null;
m_httpMethod = "GET";
m_path = @"^/assets/";
}
if ((dataResponse = server.StorageProvider.TryFetchDataMetadata(assetID, out metadata, out assetData)) == BackendResponse.Success)
#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[] buffer = null;
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))
{
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;
Metadata metadata;
byte[] assetData;
BackendResponse dataResponse;
XmlSerializer xs = new XmlSerializer(typeof (AssetBase));
MemoryStream ms = new MemoryStream();
XmlTextWriter xw = new XmlTextWriter(ms, Encoding.UTF8);
xs.Serialize(xw, asset);
xw.Flush();
if ((dataResponse = m_server.StorageProvider.TryFetchDataMetadata(assetID, out metadata, out assetData)) == 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;
ms.Seek(0, SeekOrigin.Begin);
byte[] buffer = ms.GetBuffer();
XmlSerializer xs = new XmlSerializer(typeof (AssetBase));
MemoryStream ms = new MemoryStream();
XmlTextWriter xw = new XmlTextWriter(ms, Encoding.UTF8);
xs.Serialize(xw, asset);
xw.Flush();
response.Status = HttpStatusCode.OK;
response.ContentType = "application/xml";
response.ContentLength = ms.Length;
response.Body.Write(buffer, 0, (int) ms.Length);
response.Body.Flush();
ms.Seek(0, SeekOrigin.Begin);
buffer = ms.GetBuffer();
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
{
Logger.Log.WarnFormat("Failed to fetch asset data or metadata for {0}: {1}", assetID, dataResponse);
httpResponse.StatusCode = (int) HttpStatusCode.NotFound;
}
}
else
{
Logger.Log.WarnFormat("Failed to fetch asset data or metadata for {0}: {1}", assetID, dataResponse);
response.Status = HttpStatusCode.NotFound;
Logger.Log.Warn("Unrecognized OpenSim asset request: " + httpRequest.Url.PathAndQuery);
}
}
else
{
Logger.Log.Warn("Unrecognized OpenSim asset request: " + request.Uri.PathAndQuery);
return buffer;
}
return true;
#endregion IStreamedRequestHandler implementation
}
bool AssetPostHandler(IHttpClientContext client, IHttpRequest request, IHttpResponse response)
public class AssetPostHandler : IStreamedRequestHandler
{
Metadata metadata = new Metadata();
AssetInventoryServer m_server;
string m_contentType;
string m_httpMethod;
string m_path;
try
public AssetPostHandler(AssetInventoryServer server)
{
AssetBase asset = (AssetBase) new XmlSerializer(typeof (AssetBase)).Deserialize(request.Body);
m_server = server;
m_contentType = null;
m_httpMethod = "POST";
m_path = @"^/assets/";
}
if (asset.Data != null && asset.Data.Length > 0)
#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)
{
Metadata metadata = new Metadata();
try
{
metadata.ID = asset.Metadata.FullID;
metadata.ContentType = Utils.SLAssetTypeToContentType((int) asset.Metadata.Type);
metadata.Name = asset.Metadata.Name;
metadata.Description = asset.Metadata.Description;
metadata.Temporary = asset.Metadata.Temporary;
AssetBase asset = (AssetBase) new XmlSerializer(typeof (AssetBase)).Deserialize(httpRequest.InputStream);
metadata.SHA1 = OpenMetaverse.Utils.SHA1(asset.Data);
metadata.CreationDate = DateTime.Now;
if (asset.Data != null && asset.Data.Length > 0)
{
metadata.ID = asset.Metadata.FullID;
metadata.ContentType = Utils.SLAssetTypeToContentType((int) asset.Metadata.Type);
metadata.Name = asset.Metadata.Name;
metadata.Description = asset.Metadata.Description;
metadata.Temporary = asset.Metadata.Temporary;
BackendResponse storageResponse = server.StorageProvider.TryCreateAsset(metadata, asset.Data);
metadata.SHA1 = OpenMetaverse.Utils.SHA1(asset.Data);
metadata.CreationDate = DateTime.Now;
if (storageResponse == BackendResponse.Success)
response.Status = HttpStatusCode.Created;
else if (storageResponse == BackendResponse.NotFound)
response.Status = HttpStatusCode.NotFound;
BackendResponse storageResponse = m_server.StorageProvider.TryCreateAsset(metadata, asset.Data);
if (storageResponse == BackendResponse.Success)
httpResponse.StatusCode = (int) HttpStatusCode.Created;
else if (storageResponse == BackendResponse.NotFound)
httpResponse.StatusCode = (int) HttpStatusCode.NotFound;
else
httpResponse.StatusCode = (int) HttpStatusCode.InternalServerError;
}
else
response.Status = HttpStatusCode.InternalServerError;
{
Logger.Log.Warn("AssetPostHandler called with no asset data");
httpResponse.StatusCode = (int) HttpStatusCode.BadRequest;
}
}
else
catch (Exception ex)
{
Logger.Log.Warn("AssetPostHandler called with no asset data");
response.Status = HttpStatusCode.BadRequest;
Logger.Log.Warn("Failed to parse POST data (expecting AssetBase): " + ex.Message);
httpResponse.StatusCode = (int) HttpStatusCode.BadRequest;
}
}
catch (Exception ex)
{
Logger.Log.Warn("Failed to parse POST data (expecting AssetBase): " + ex.Message);
response.Status = HttpStatusCode.BadRequest;
return null;
}
return true;
#endregion IStreamedRequestHandler implementation
}
}
}

View File

@ -56,16 +56,16 @@ namespace OpenSim.Grid.AssetInventoryServer.Plugins.OpenSim
{
this.server = server;
server.HttpServer.AddHandler("post", null, @"^/GetInventory/", GetInventoryHandler);
server.HttpServer.AddHandler("post", null, @"^/CreateInventory/", CreateInventoryHandler);
server.HttpServer.AddHandler("post", null, @"^/NewFolder/", NewFolderHandler);
server.HttpServer.AddHandler("post", null, @"^/UpdateFolder/", UpdateFolderHandler);
server.HttpServer.AddHandler("post", null, @"^/MoveFolder/", MoveFolderHandler);
server.HttpServer.AddHandler("post", null, @"^/PurgeFolder/", PurgeFolderHandler);
server.HttpServer.AddHandler("post", null, @"^/NewItem/", NewItemHandler);
server.HttpServer.AddHandler("post", null, @"^/DeleteItem/", DeleteItemHandler);
server.HttpServer.AddHandler("post", null, @"^/RootFolders/", RootFoldersHandler);
server.HttpServer.AddHandler("post", null, @"^/ActiveGestures/", ActiveGesturesHandler);
//server.HttpServer.AddHandler("post", null, @"^/GetInventory/", GetInventoryHandler);
//server.HttpServer.AddHandler("post", null, @"^/CreateInventory/", CreateInventoryHandler);
//server.HttpServer.AddHandler("post", null, @"^/NewFolder/", NewFolderHandler);
//server.HttpServer.AddHandler("post", null, @"^/UpdateFolder/", UpdateFolderHandler);
//server.HttpServer.AddHandler("post", null, @"^/MoveFolder/", MoveFolderHandler);
//server.HttpServer.AddHandler("post", null, @"^/PurgeFolder/", PurgeFolderHandler);
//server.HttpServer.AddHandler("post", null, @"^/NewItem/", NewItemHandler);
//server.HttpServer.AddHandler("post", null, @"^/DeleteItem/", DeleteItemHandler);
//server.HttpServer.AddHandler("post", null, @"^/RootFolders/", RootFoldersHandler);
//server.HttpServer.AddHandler("post", null, @"^/ActiveGestures/", ActiveGesturesHandler);
Logger.Log.Info("[INVENTORY] OpenSim Inventory Frontend loaded.");
}
@ -96,253 +96,253 @@ namespace OpenSim.Grid.AssetInventoryServer.Plugins.OpenSim
#endregion IPlugin implementation
bool GetInventoryHandler(IHttpClientContext client, IHttpRequest request, IHttpResponse response)
{
UUID sessionID, agentID;
UUID ownerID = DeserializeUUID(request.Body, out agentID, out sessionID);
//bool GetInventoryHandler(IHttpClientContext client, IHttpRequest request, IHttpResponse response)
//{
// UUID sessionID, agentID;
// UUID ownerID = DeserializeUUID(request.Body, out agentID, out sessionID);
if (ownerID != UUID.Zero)
{
Logger.Log.Warn("GetInventory is not scalable on some inventory backends, avoid calling it wherever possible");
// if (ownerID != UUID.Zero)
// {
// Logger.Log.Warn("GetInventory is not scalable on some inventory backends, avoid calling it wherever possible");
Uri owner = Utils.GetOpenSimUri(ownerID);
InventoryCollection inventory;
BackendResponse storageResponse = server.InventoryProvider.TryFetchInventory(owner, out inventory);
// Uri owner = Utils.GetOpenSimUri(ownerID);
// InventoryCollection inventory;
// BackendResponse storageResponse = server.InventoryProvider.TryFetchInventory(owner, out inventory);
if (storageResponse == BackendResponse.Success)
{
collectionSerializer.Serialize(response.Body, inventory);
response.Body.Flush();
}
else if (storageResponse == BackendResponse.NotFound)
{
// Return an empty inventory set to mimic OpenSim.Grid.InventoryServer.exe
inventory = new InventoryCollection();
inventory.UserID = ownerID;
inventory.Folders = new Dictionary<UUID, InventoryFolder>();
inventory.Items = new Dictionary<UUID, InventoryItem>();
collectionSerializer.Serialize(response.Body, inventory);
response.Body.Flush();
}
else
{
response.Status = HttpStatusCode.InternalServerError;
}
}
else
{
response.Status = HttpStatusCode.BadRequest;
}
// if (storageResponse == BackendResponse.Success)
// {
// collectionSerializer.Serialize(response.Body, inventory);
// response.Body.Flush();
// }
// else if (storageResponse == BackendResponse.NotFound)
// {
// // Return an empty inventory set to mimic OpenSim.Grid.InventoryServer.exe
// inventory = new InventoryCollection();
// inventory.UserID = ownerID;
// inventory.Folders = new Dictionary<UUID, InventoryFolder>();
// inventory.Items = new Dictionary<UUID, InventoryItem>();
// collectionSerializer.Serialize(response.Body, inventory);
// response.Body.Flush();
// }
// else
// {
// response.Status = HttpStatusCode.InternalServerError;
// }
// }
// else
// {
// response.Status = HttpStatusCode.BadRequest;
// }
return true;
}
// return true;
//}
bool CreateInventoryHandler(IHttpClientContext client, IHttpRequest request, IHttpResponse response)
{
UUID ownerID = DeserializeUUID(request.Body);
//bool CreateInventoryHandler(IHttpClientContext client, IHttpRequest request, IHttpResponse response)
//{
// UUID ownerID = DeserializeUUID(request.Body);
if (ownerID != UUID.Zero)
{
Uri owner = Utils.GetOpenSimUri(ownerID);
Logger.Log.DebugFormat("Created URI {0} for inventory creation", owner);
// if (ownerID != UUID.Zero)
// {
// Uri owner = Utils.GetOpenSimUri(ownerID);
// Logger.Log.DebugFormat("Created URI {0} for inventory creation", owner);
InventoryFolder rootFolder = new InventoryFolder("My Inventory", ownerID, UUID.Zero, (short)AssetType.Folder);
BackendResponse storageResponse = server.InventoryProvider.TryCreateInventory(owner, rootFolder);
if (storageResponse == BackendResponse.Success)
{
CreateFolder("Animations", ownerID, rootFolder.ID, AssetType.Animation);
CreateFolder("Body Parts", ownerID, rootFolder.ID, AssetType.Bodypart);
CreateFolder("Calling Cards", ownerID, rootFolder.ID, AssetType.CallingCard);
CreateFolder("Clothing", ownerID, rootFolder.ID, AssetType.Clothing);
CreateFolder("Gestures", ownerID, rootFolder.ID, AssetType.Gesture);
CreateFolder("Landmarks", ownerID, rootFolder.ID, AssetType.Landmark);
CreateFolder("Lost and Found", ownerID, rootFolder.ID, AssetType.LostAndFoundFolder);
CreateFolder("Notecards", ownerID, rootFolder.ID, AssetType.Notecard);
CreateFolder("Objects", ownerID, rootFolder.ID, AssetType.Object);
CreateFolder("Photo Album", ownerID, rootFolder.ID, AssetType.SnapshotFolder);
CreateFolder("Scripts", ownerID, rootFolder.ID, AssetType.LSLText);
CreateFolder("Sounds", ownerID, rootFolder.ID, AssetType.Sound);
CreateFolder("Textures", ownerID, rootFolder.ID, AssetType.Texture);
CreateFolder("Trash", ownerID, rootFolder.ID, AssetType.TrashFolder);
// InventoryFolder rootFolder = new InventoryFolder("My Inventory", ownerID, UUID.Zero, (short)AssetType.Folder);
// BackendResponse storageResponse = server.InventoryProvider.TryCreateInventory(owner, rootFolder);
// if (storageResponse == BackendResponse.Success)
// {
// CreateFolder("Animations", ownerID, rootFolder.ID, AssetType.Animation);
// CreateFolder("Body Parts", ownerID, rootFolder.ID, AssetType.Bodypart);
// CreateFolder("Calling Cards", ownerID, rootFolder.ID, AssetType.CallingCard);
// CreateFolder("Clothing", ownerID, rootFolder.ID, AssetType.Clothing);
// CreateFolder("Gestures", ownerID, rootFolder.ID, AssetType.Gesture);
// CreateFolder("Landmarks", ownerID, rootFolder.ID, AssetType.Landmark);
// CreateFolder("Lost and Found", ownerID, rootFolder.ID, AssetType.LostAndFoundFolder);
// CreateFolder("Notecards", ownerID, rootFolder.ID, AssetType.Notecard);
// CreateFolder("Objects", ownerID, rootFolder.ID, AssetType.Object);
// CreateFolder("Photo Album", ownerID, rootFolder.ID, AssetType.SnapshotFolder);
// CreateFolder("Scripts", ownerID, rootFolder.ID, AssetType.LSLText);
// CreateFolder("Sounds", ownerID, rootFolder.ID, AssetType.Sound);
// CreateFolder("Textures", ownerID, rootFolder.ID, AssetType.Texture);
// CreateFolder("Trash", ownerID, rootFolder.ID, AssetType.TrashFolder);
SerializeBool(response.Body, true);
return true;
}
}
// SerializeBool(response.Body, true);
// return true;
// }
// }
SerializeBool(response.Body, false);
return true;
}
// SerializeBool(response.Body, false);
// return true;
//}
bool NewFolderHandler(IHttpClientContext client, IHttpRequest request, IHttpResponse response)
{
UUID agentID, sessionID;
InventoryFolder folder = DeserializeFolder(request.Body, out agentID, out sessionID);
//bool NewFolderHandler(IHttpClientContext client, IHttpRequest request, IHttpResponse response)
//{
// UUID agentID, sessionID;
// InventoryFolder folder = DeserializeFolder(request.Body, out agentID, out sessionID);
if (folder != null)
{
Uri owner = Utils.GetOpenSimUri(folder.Owner);
// if (folder != null)
// {
// Uri owner = Utils.GetOpenSimUri(folder.Owner);
// Some calls that are moving or updating a folder instead of creating a new one
// will pass in an InventoryFolder without the name set. If this is the case we
// need to look up the name first
if (String.IsNullOrEmpty(folder.Name))
{
InventoryFolder oldFolder;
if (server.InventoryProvider.TryFetchFolder(owner, folder.ID, out oldFolder) == BackendResponse.Success)
folder.Name = oldFolder.Name;
}
// // Some calls that are moving or updating a folder instead of creating a new one
// // will pass in an InventoryFolder without the name set. If this is the case we
// // need to look up the name first
// if (String.IsNullOrEmpty(folder.Name))
// {
// InventoryFolder oldFolder;
// if (server.InventoryProvider.TryFetchFolder(owner, folder.ID, out oldFolder) == BackendResponse.Success)
// folder.Name = oldFolder.Name;
// }
BackendResponse storageResponse = server.InventoryProvider.TryCreateFolder(owner, folder);
// BackendResponse storageResponse = server.InventoryProvider.TryCreateFolder(owner, folder);
if (storageResponse == BackendResponse.Success)
{
SerializeBool(response.Body, true);
return true;
}
}
// if (storageResponse == BackendResponse.Success)
// {
// SerializeBool(response.Body, true);
// return true;
// }
// }
SerializeBool(response.Body, false);
return true;
}
// SerializeBool(response.Body, false);
// return true;
//}
bool UpdateFolderHandler(IHttpClientContext client, IHttpRequest request, IHttpResponse response)
{
return NewFolderHandler(client, request, response);
}
//bool UpdateFolderHandler(IHttpClientContext client, IHttpRequest request, IHttpResponse response)
//{
// return NewFolderHandler(client, request, response);
//}
bool MoveFolderHandler(IHttpClientContext client, IHttpRequest request, IHttpResponse response)
{
return NewFolderHandler(client, request, response);
}
//bool MoveFolderHandler(IHttpClientContext client, IHttpRequest request, IHttpResponse response)
//{
// return NewFolderHandler(client, request, response);
//}
bool PurgeFolderHandler(IHttpClientContext client, IHttpRequest request, IHttpResponse response)
{
UUID agentID, sessionID;
InventoryFolder folder = DeserializeFolder(request.Body, out agentID, out sessionID);
//bool PurgeFolderHandler(IHttpClientContext client, IHttpRequest request, IHttpResponse response)
//{
// UUID agentID, sessionID;
// InventoryFolder folder = DeserializeFolder(request.Body, out agentID, out sessionID);
if (folder != null)
{
Uri owner = Utils.GetOpenSimUri(folder.Owner);
BackendResponse storageResponse = server.InventoryProvider.TryPurgeFolder(owner, folder.ID);
// if (folder != null)
// {
// Uri owner = Utils.GetOpenSimUri(folder.Owner);
// BackendResponse storageResponse = server.InventoryProvider.TryPurgeFolder(owner, folder.ID);
if (storageResponse == BackendResponse.Success)
{
SerializeBool(response.Body, true);
return true;
}
}
// if (storageResponse == BackendResponse.Success)
// {
// SerializeBool(response.Body, true);
// return true;
// }
// }
SerializeBool(response.Body, false);
return true;
}
// SerializeBool(response.Body, false);
// return true;
//}
bool NewItemHandler(IHttpClientContext client, IHttpRequest request, IHttpResponse response)
{
UUID agentID, sessionID;
InventoryItem item = DeserializeItem(request.Body, out agentID, out sessionID);
//bool NewItemHandler(IHttpClientContext client, IHttpRequest request, IHttpResponse response)
//{
// UUID agentID, sessionID;
// InventoryItem item = DeserializeItem(request.Body, out agentID, out sessionID);
if (item != null)
{
Uri owner = Utils.GetOpenSimUri(agentID);
BackendResponse storageResponse = server.InventoryProvider.TryCreateItem(owner, item);
// if (item != null)
// {
// Uri owner = Utils.GetOpenSimUri(agentID);
// BackendResponse storageResponse = server.InventoryProvider.TryCreateItem(owner, item);
if (storageResponse == BackendResponse.Success)
{
SerializeBool(response.Body, true);
return true;
}
}
// if (storageResponse == BackendResponse.Success)
// {
// SerializeBool(response.Body, true);
// return true;
// }
// }
SerializeBool(response.Body, false);
return true;
}
// SerializeBool(response.Body, false);
// return true;
//}
bool DeleteItemHandler(IHttpClientContext client, IHttpRequest request, IHttpResponse response)
{
UUID agentID, sessionID;
InventoryItem item = DeserializeItem(request.Body, out agentID, out sessionID);
//bool DeleteItemHandler(IHttpClientContext client, IHttpRequest request, IHttpResponse response)
//{
// UUID agentID, sessionID;
// InventoryItem item = DeserializeItem(request.Body, out agentID, out sessionID);
if (item != null)
{
Uri owner = Utils.GetOpenSimUri(item.Owner);
BackendResponse storageResponse = server.InventoryProvider.TryDeleteItem(owner, item.ID);
// if (item != null)
// {
// Uri owner = Utils.GetOpenSimUri(item.Owner);
// BackendResponse storageResponse = server.InventoryProvider.TryDeleteItem(owner, item.ID);
if (storageResponse == BackendResponse.Success)
{
SerializeBool(response.Body, true);
return true;
}
}
// if (storageResponse == BackendResponse.Success)
// {
// SerializeBool(response.Body, true);
// return true;
// }
// }
SerializeBool(response.Body, false);
return true;
}
// SerializeBool(response.Body, false);
// return true;
//}
bool RootFoldersHandler(IHttpClientContext client, IHttpRequest request, IHttpResponse response)
{
UUID ownerID = DeserializeUUID(request.Body);
//bool RootFoldersHandler(IHttpClientContext client, IHttpRequest request, IHttpResponse response)
//{
// UUID ownerID = DeserializeUUID(request.Body);
if (ownerID != UUID.Zero)
{
Uri owner = Utils.GetOpenSimUri(ownerID);
List<InventoryFolder> skeleton;
BackendResponse storageResponse = server.InventoryProvider.TryFetchFolderList(owner, out skeleton);
// if (ownerID != UUID.Zero)
// {
// Uri owner = Utils.GetOpenSimUri(ownerID);
// List<InventoryFolder> skeleton;
// BackendResponse storageResponse = server.InventoryProvider.TryFetchFolderList(owner, out skeleton);
if (storageResponse == BackendResponse.Success)
{
SerializeFolderList(response.Body, skeleton);
}
else if (storageResponse == BackendResponse.NotFound)
{
// Return an empty set of inventory so the requester knows that
// an inventory needs to be created for this agent
SerializeFolderList(response.Body, new List<InventoryFolder>(0));
}
else
{
response.Status = HttpStatusCode.InternalServerError;
}
}
else
{
response.Status = HttpStatusCode.BadRequest;
}
// if (storageResponse == BackendResponse.Success)
// {
// SerializeFolderList(response.Body, skeleton);
// }
// else if (storageResponse == BackendResponse.NotFound)
// {
// // Return an empty set of inventory so the requester knows that
// // an inventory needs to be created for this agent
// SerializeFolderList(response.Body, new List<InventoryFolder>(0));
// }
// else
// {
// response.Status = HttpStatusCode.InternalServerError;
// }
// }
// else
// {
// response.Status = HttpStatusCode.BadRequest;
// }
return true;
}
// return true;
//}
bool ActiveGesturesHandler(IHttpClientContext client, IHttpRequest request, IHttpResponse response)
{
UUID ownerID = DeserializeUUID(request.Body);
//bool ActiveGesturesHandler(IHttpClientContext client, IHttpRequest request, IHttpResponse response)
//{
// UUID ownerID = DeserializeUUID(request.Body);
if (ownerID != UUID.Zero)
{
Uri owner = Utils.GetOpenSimUri(ownerID);
List<InventoryItem> gestures;
BackendResponse storageResponse = server.InventoryProvider.TryFetchActiveGestures(owner, out gestures);
// if (ownerID != UUID.Zero)
// {
// Uri owner = Utils.GetOpenSimUri(ownerID);
// List<InventoryItem> gestures;
// BackendResponse storageResponse = server.InventoryProvider.TryFetchActiveGestures(owner, out gestures);
if (storageResponse == BackendResponse.Success)
{
SerializeItemList(response.Body, gestures);
}
else if (storageResponse == BackendResponse.NotFound)
{
// Return an empty set of gestures to match OpenSim.Grid.InventoryServer.exe behavior
SerializeItemList(response.Body, new List<InventoryItem>(0));
}
else
{
response.Status = HttpStatusCode.InternalServerError;
}
}
else
{
response.Status = HttpStatusCode.BadRequest;
}
// if (storageResponse == BackendResponse.Success)
// {
// SerializeItemList(response.Body, gestures);
// }
// else if (storageResponse == BackendResponse.NotFound)
// {
// // Return an empty set of gestures to match OpenSim.Grid.InventoryServer.exe behavior
// SerializeItemList(response.Body, new List<InventoryItem>(0));
// }
// else
// {
// response.Status = HttpStatusCode.InternalServerError;
// }
// }
// else
// {
// response.Status = HttpStatusCode.BadRequest;
// }
return true;
}
// return true;
//}
BackendResponse CreateFolder(string name, UUID ownerID, UUID parentID, AssetType assetType)
{

View File

@ -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";
}
if (server.AuthorizationProvider.IsMetadataAuthorized(authToken, assetID))
#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[] 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 metadata;
BackendResponse storageResponse = m_server.StorageProvider.TryFetchMetadata(assetID, out metadata);
if (storageResponse == BackendResponse.Success)
{
metadata.Methods["data"] = new Uri(String.Format("{0}://{1}/{2}/data",
request.Uri.Scheme, request.Uri.Authority, 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",
httpRequest.Url.Scheme, httpRequest.Url.Authority, assetID));
}
serializedData = metadata.SerializeToBytes();
httpResponse.StatusCode = (int) HttpStatusCode.OK;
httpResponse.ContentType = "application/json";
httpResponse.ContentLength = serializedData.Length;
httpResponse.Body.Write(serializedData, 0, serializedData.Length);
}
byte[] serializedData = metadata.SerializeToBytes();
response.Status = HttpStatusCode.OK;
response.ContentType = "application/json";
response.ContentLength = serializedData.Length;
response.Body.Write(serializedData, 0, serializedData.Length);
}
else if (storageResponse == BackendResponse.NotFound)
{
Logger.Log.Warn("Could not find metadata for asset " + assetID.ToString());
response.Status = HttpStatusCode.NotFound;
}
else
{
response.Status = HttpStatusCode.InternalServerError;
}
}
else
{
response.Status = HttpStatusCode.Forbidden;
}
return true;
}
response.Status = HttpStatusCode.NotFound;
return true;
}
bool DataRequestHandler(IHttpClientContext client, IHttpRequest request, IHttpResponse response)
{
UUID assetID;
// Split the URL up into an AssetID and a method
string[] rawUrl = request.Uri.PathAndQuery.Split('/');
if (rawUrl.Length >= 3 && UUID.TryParse(rawUrl[1], out assetID))
{
UUID authToken = Utils.GetAuthToken(request);
if (server.AuthorizationProvider.IsDataAuthorized(authToken, assetID))
{
byte[] assetData;
BackendResponse storageResponse = server.StorageProvider.TryFetchData(assetID, out assetData);
if (storageResponse == BackendResponse.Success)
{
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;
}
else
{
response.Status = HttpStatusCode.InternalServerError;
}
}
else
{
response.Status = HttpStatusCode.Forbidden;
}
return true;
}
response.Status = HttpStatusCode.BadRequest;
return true;
}
bool CreateRequestHandler(IHttpClientContext client, IHttpRequest request, IHttpResponse response)
{
UUID authToken = Utils.GetAuthToken(request);
if (server.AuthorizationProvider.IsCreateAuthorized(authToken))
{
try
{
OSD osdata = OSDParser.DeserializeJson(request.Body);
if (osdata.Type == OSDType.Map)
{
OSDMap map = (OSDMap)osdata;
Metadata metadata = new Metadata();
metadata.Deserialize(map);
byte[] assetData = map["data"].AsBinary();
if (assetData != null && assetData.Length > 0)
else if (storageResponse == BackendResponse.NotFound)
{
BackendResponse storageResponse;
Logger.Log.Warn("Could not find metadata for asset " + assetID.ToString());
httpResponse.StatusCode = (int) HttpStatusCode.NotFound;
}
else
{
httpResponse.StatusCode = (int) HttpStatusCode.InternalServerError;
}
}
else
{
httpResponse.StatusCode = (int) HttpStatusCode.Forbidden;
}
if (metadata.ID != UUID.Zero)
storageResponse = server.StorageProvider.TryCreateAsset(metadata, assetData);
else
storageResponse = server.StorageProvider.TryCreateAsset(metadata, assetData, out metadata.ID);
return serializedData;
}
if (storageResponse == BackendResponse.Success)
httpResponse.StatusCode = (int) HttpStatusCode.NotFound;
return serializedData;
}
#endregion IStreamedRequestHandler implementation
}
public class DataRequestHandler : IStreamedRequestHandler
{
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; }
}
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[] 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))
{
UUID authToken = Utils.GetAuthToken(httpRequest);
if (m_server.AuthorizationProvider.IsDataAuthorized(authToken, assetID))
{
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
{
httpResponse.StatusCode = (int) HttpStatusCode.Forbidden;
}
return assetData;
}
httpResponse.StatusCode = (int) HttpStatusCode.BadRequest;
return assetData;
}
#endregion IStreamedRequestHandler implementation
}
public class CreateRequestHandler : IStreamedRequestHandler
{
AssetInventoryServer m_server;
string m_contentType;
string m_httpMethod;
string m_path;
public CreateRequestHandler(AssetInventoryServer server)
{
m_server = server;
m_contentType = null;
m_httpMethod = "POST";
m_path = "^/createasset";
}
#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);
if (m_server.AuthorizationProvider.IsCreateAuthorized(authToken))
{
try
{
OSD osdata = OSDParser.DeserializeJson(httpRequest.InputStream);
if (osdata.Type == OSDType.Map)
{
OSDMap map = (OSDMap)osdata;
Metadata metadata = new Metadata();
metadata.Deserialize(map);
byte[] assetData = map["data"].AsBinary();
if (assetData != null && assetData.Length > 0)
{
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)
{
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
}
}
}

View File

@ -36,12 +36,13 @@ using System.Xml.Serialization;
using OpenMetaverse;
using OpenMetaverse.StructuredData;
using HttpServer;
using OpenSim.Framework.Servers;
namespace OpenSim.Grid.AssetInventoryServer
{
public static class Utils
{
public static UUID GetAuthToken(IHttpRequest request)
public static UUID GetAuthToken(OSHttpRequest request)
{
UUID authToken = UUID.Zero;
@ -55,13 +56,13 @@ namespace OpenSim.Grid.AssetInventoryServer
UUID.TryParse(authHeaderParts[1], out authToken);
}
if (authToken == UUID.Zero && request.Cookies != null)
{
// Check for an authToken cookie to make logins browser-compatible
RequestCookie authCookie = request.Cookies["authToken"];
if (authCookie != null)
UUID.TryParse(authCookie.Value, out authToken);
}
//if (authToken == UUID.Zero && request.Cookies != null)
//{
// // Check for an authToken cookie to make logins browser-compatible
// RequestCookie authCookie = request.Cookies["authToken"];
// if (authCookie != null)
// UUID.TryParse(authCookie.Value, out authToken);
//}
return authToken;
}

View File

@ -764,10 +764,11 @@
<Reference name="System.Web"/>
<Reference name="System.Xml"/>
<Reference name="OpenSim.Framework"/>
<Reference name="OpenSim.Framework.Console"/>
<Reference name="OpenSim.Framework.Servers"/>
<Reference name="log4net2"/>
<Reference name="OpenMetaverseTypes"/>
<Reference name="OpenMetaverse.StructuredData2"/>
<Reference name="OpenMetaverse.Http"/>
<Reference name="ExtensionLoader"/>
<Reference name="HttpServer2"/>
@ -800,6 +801,7 @@
<Reference name="System.Web"/>
<Reference name="OpenSim.Grid.AssetInventoryServer" />
<Reference name="OpenSim.Framework"/>
<Reference name="OpenSim.Framework.Servers"/>
<Reference name="OpenMetaverse"/>
<Reference name="OpenMetaverseTypes"/>
<Reference name="OpenMetaverse.StructuredData2"/>
@ -859,6 +861,7 @@
<Reference name="System" />
<Reference name="System.Data" />
<Reference name="OpenSim.Framework" />
<Reference name="OpenSim.Framework.Servers"/>
<Reference name="OpenSim.Data" />
<Reference name="OpenSim.Grid.AssetInventoryServer" />
<Reference name="OpenMetaverseTypes"/>