View of /trunk/Extensions/ReferenceInterface.cs
Parent Directory
|
Revision Log
Revision 25 -
(download)
(annotate)
Sat Nov 15 03:56:39 2008 UTC (4 years, 6 months ago) by jhurliman
File size: 11291 byte(s)
Sat Nov 15 03:56:39 2008 UTC (4 years, 6 months ago) by jhurliman
File size: 11291 byte(s)
* Added MemcacheStorage, sits on top of another storage provider and uses memcached to cache metadata and asset data * Implemented Robert's suggestion to replace AssetURL with a list of method to URI mappings that contains data=>uri * Upgraded to latest ExtensionLoader with simplified extension starting * Simplified SimpleStorage by removing SimpleMetadata and using a dictionary of UUID to filenames * Moved Metadata into its own file, added serialization and deserialization methods * Added LICENSES.txt file for third party libraries (not complete yet)
/*
* Copyright (c) 2008 Intel Corporation
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* -- Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* -- Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* -- Neither the name of the Intel Corporation nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR ITS
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.Collections.Generic;
using System.Net;
using System.Xml;
using ExtensionLoader;
using OpenMetaverse;
using OpenMetaverse.Capabilities;
using OpenMetaverse.StructuredData;
namespace AssetServer.Extensions
{
public class ReferenceInterface : IExtension<AssetServer>
{
AssetServer server;
public ReferenceInterface()
{
}
public void Start(AssetServer server)
{
this.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);
// 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);
// Asset creation
server.HttpServer.AddHandler("post", null, "^/createasset", CreateRequestHandler);
}
public void Stop()
{
}
void MetadataRequestHandler(HttpRequestSignature signature, ref HttpListenerContext context)
{
UUID assetID;
// Split the URL up into an AssetID and a method
string[] rawUrl = context.Request.RawUrl.Split('/');
if (rawUrl.Length >= 3 && UUID.TryParse(rawUrl[1], out assetID))
{
UUID authToken = Utils.GetAuthToken(context.Request);
Metadata metadata;
StorageResponse response = server.StorageProvider.TryFetchMetadata(assetID, authToken, out metadata);
switch (response)
{
case StorageResponse.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",
context.Request.Url.Scheme, context.Request.Url.Authority, assetID));
}
byte[] serializedData = metadata.SerializeToBinary();
context.Response.StatusCode = (int)HttpStatusCode.OK;
context.Response.ContentType = "application/xml";
context.Response.ContentLength64 = serializedData.Length;
context.Response.OutputStream.Write(serializedData, 0, serializedData.Length);
break;
case StorageResponse.NotFound:
Logger.Log("Could not find metadata for asset " + assetID.ToString(), Helpers.LogLevel.Warning);
context.Response.StatusCode = (int)HttpStatusCode.NotFound;
context.Response.StatusDescription = "Asset not found";
break;
case StorageResponse.AuthNeeded:
context.Response.StatusCode = (int)HttpStatusCode.Forbidden;
context.Response.StatusDescription = "Authorization denied";
break;
case StorageResponse.Failure:
default:
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
context.Response.StatusDescription = "Failed to fetch metadata";
break;
}
context.Response.Close();
return;
}
context.Response.StatusCode = (int)HttpStatusCode.NotFound;
context.Response.StatusDescription = "Requests take the form of /AssetID/metadata";
context.Response.Close();
}
void DataRequestHandler(HttpRequestSignature signature, ref HttpListenerContext context)
{
UUID assetID;
// Split the URL up into an AssetID and a method
string[] rawUrl = context.Request.RawUrl.Split('/');
if (rawUrl.Length >= 3 && UUID.TryParse(rawUrl[1], out assetID))
{
UUID authToken = Utils.GetAuthToken(context.Request);
byte[] assetData;
StorageResponse response = server.StorageProvider.TryFetchData(assetID, authToken, out assetData);
switch (response)
{
case StorageResponse.Success:
context.Response.StatusCode = (int)HttpStatusCode.OK;
context.Response.StatusCode = (int)HttpStatusCode.OK;
context.Response.ContentType = "application/octet-stream";
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + assetID.ToString());
context.Response.ContentLength64 = assetData.Length;
context.Response.OutputStream.Write(assetData, 0, assetData.Length);
break;
case StorageResponse.NotFound:
context.Response.StatusCode = (int)HttpStatusCode.NotFound;
context.Response.StatusDescription = "Asset not found";
break;
case StorageResponse.AuthNeeded:
context.Response.StatusCode = (int)HttpStatusCode.Forbidden;
context.Response.StatusDescription = "Authorization denied";
break;
case StorageResponse.Failure:
default:
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
context.Response.StatusDescription = "Failed to retrieve asset";
break;
}
context.Response.Close();
return;
}
context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
context.Response.StatusDescription = "Requests take the form of /AssetID/data";
context.Response.Close();
}
void CreateRequestHandler(HttpRequestSignature signature, ref HttpListenerContext context)
{
UUID authToken = Utils.GetAuthToken(context.Request);
try
{
XmlTextReader reader = new XmlTextReader(context.Request.InputStream);
OSD osdata = OSDParser.DeserializeLLSDXml(reader);
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)
{
StorageResponse response;
if (metadata.ID != UUID.Zero)
response = server.StorageProvider.TryCreateAsset(metadata, assetData, authToken);
else
response = server.StorageProvider.TryCreateAsset(metadata, assetData, authToken, out metadata.ID);
switch (response)
{
case StorageResponse.Success:
context.Response.StatusCode = (int)HttpStatusCode.Created;
OSDMap responseMap = new OSDMap(1);
responseMap["id"] = OSD.FromUUID(metadata.ID);
byte[] responseData = OSDParser.SerializeLLSDXmlBytes(responseMap);
context.Response.OutputStream.Write(responseData, 0, responseData.Length);
break;
case StorageResponse.NotFound:
context.Response.StatusCode = (int)HttpStatusCode.NotFound;
break;
case StorageResponse.AuthNeeded:
context.Response.StatusCode = (int)HttpStatusCode.Forbidden;
context.Response.StatusDescription = "Authorization denied";
break;
case StorageResponse.Failure:
default:
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
context.Response.StatusDescription = "Failed to create asset " + metadata.ID.ToString();
break;
}
}
else
{
context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
}
}
else
{
context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
}
}
catch (Exception ex)
{
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
context.Response.StatusDescription = ex.Message;
}
context.Response.Close();
}
}
}
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |

