From b3d29aaeb3816fdb963a038a2df840a69ccd21e8 Mon Sep 17 00:00:00 2001 From: Melanie Thielker Date: Sat, 9 May 2009 04:03:32 +0000 Subject: Make remote assets work through the new server system --- .../HttpServer/SynchronousRestObjectPoster.cs | 46 ++++++++++------- .../Asset/RemoteAssetServiceConnector.cs | 58 ++++++++++++++++++++-- OpenSim/Servers/Asset/AssetServerPostHandler.cs | 2 +- 3 files changed, 83 insertions(+), 23 deletions(-) diff --git a/OpenSim/Framework/Servers/HttpServer/SynchronousRestObjectPoster.cs b/OpenSim/Framework/Servers/HttpServer/SynchronousRestObjectPoster.cs index b754c36..71e5b69 100644 --- a/OpenSim/Framework/Servers/HttpServer/SynchronousRestObjectPoster.cs +++ b/OpenSim/Framework/Servers/HttpServer/SynchronousRestObjectPoster.cs @@ -52,30 +52,42 @@ namespace OpenSim.Framework.Servers.HttpServer WebRequest request = WebRequest.Create(requestUrl); request.Method = verb; - request.ContentType = "text/xml"; - MemoryStream buffer = new MemoryStream(); + if (verb == "POST") + { + request.ContentType = "text/xml"; - XmlWriterSettings settings = new XmlWriterSettings(); - settings.Encoding = Encoding.UTF8; + MemoryStream buffer = new MemoryStream(); - using (XmlWriter writer = XmlWriter.Create(buffer, settings)) - { - XmlSerializer serializer = new XmlSerializer(type); - serializer.Serialize(writer, obj); - writer.Flush(); - } + XmlWriterSettings settings = new XmlWriterSettings(); + settings.Encoding = Encoding.UTF8; + + using (XmlWriter writer = XmlWriter.Create(buffer, settings)) + { + XmlSerializer serializer = new XmlSerializer(type); + serializer.Serialize(writer, obj); + writer.Flush(); + } - int length = (int) buffer.Length; - request.ContentLength = length; + int length = (int) buffer.Length; + request.ContentLength = length; + + Stream requestStream = request.GetRequestStream(); + requestStream.Write(buffer.ToArray(), 0, length); + } - Stream requestStream = request.GetRequestStream(); - requestStream.Write(buffer.ToArray(), 0, length); TResponse deserial = default(TResponse); - using (WebResponse resp = request.GetResponse()) + try + { + using (WebResponse resp = request.GetResponse()) + { + XmlSerializer deserializer = new XmlSerializer(typeof (TResponse)); + deserial = (TResponse) deserializer.Deserialize(resp.GetResponseStream()); + } + } + catch (System.InvalidOperationException) { - XmlSerializer deserializer = new XmlSerializer(typeof (TResponse)); - deserial = (TResponse) deserializer.Deserialize(resp.GetResponseStream()); + // This is what happens when there is invalid XML } return deserial; } diff --git a/OpenSim/Region/CoreModules/ServiceConnectors/Asset/RemoteAssetServiceConnector.cs b/OpenSim/Region/CoreModules/ServiceConnectors/Asset/RemoteAssetServiceConnector.cs index 715cf6e..2cc2962 100644 --- a/OpenSim/Region/CoreModules/ServiceConnectors/Asset/RemoteAssetServiceConnector.cs +++ b/OpenSim/Region/CoreModules/ServiceConnectors/Asset/RemoteAssetServiceConnector.cs @@ -27,12 +27,15 @@ using log4net; using System; +using System.IO; using System.Reflection; using Nini.Config; using OpenSim.Framework; +using OpenSim.Framework.Servers.HttpServer; using OpenSim.Region.Framework.Interfaces; using OpenSim.Region.Framework.Scenes; using OpenSim.Services.Interfaces; +using OpenSim.Framework.Communications; namespace OpenSim.Region.CoreModules.ServiceConnectors.Asset { @@ -76,6 +79,8 @@ namespace OpenSim.Region.CoreModules.ServiceConnectors.Asset } m_Enabled = true; m_ServerURI = serviceURI; + + m_log.Info("[ASSET CONNECTOR]: Remote assets enabled"); } } } @@ -106,31 +111,74 @@ namespace OpenSim.Region.CoreModules.ServiceConnectors.Asset public AssetBase Get(string id) { - return null; + string uri = m_ServerURI + "/assets/" + id; + + AssetBase asset = SynchronousRestObjectPoster. + BeginPostObject("GET", uri, 0); + return asset; } public AssetMetadata GetMetadata(string id) { - return null; + string uri = m_ServerURI + "/assets/" + id + "/metadata"; + + AssetMetadata asset = SynchronousRestObjectPoster. + BeginPostObject("GET", uri, 0); + return asset; } public byte[] GetData(string id) { - return new byte[0]; + RestClient rc = new RestClient(m_ServerURI); + rc.AddResourcePath("assets"); + rc.AddResourcePath(id); + rc.AddResourcePath("data"); + + rc.RequestMethod = "GET"; + + Stream s = rc.Request(); + + if (s == null) + return null; + + if (s.Length > 0) + { + byte[] ret = new byte[s.Length]; + s.Read(ret, 0, (int)s.Length); + + return ret; + } + + return null; } public string Store(AssetBase asset) { - return String.Empty; + string uri = m_ServerURI + "/assets/"; + + string newID = SynchronousRestObjectPoster. + BeginPostObject("POST", uri, asset); + return newID; } public bool UpdateContent(string id, byte[] data) { - return false; + AssetBase asset = new AssetBase(); + asset.ID = id; + asset.Data = data; + + string uri = m_ServerURI + "/assets/" + id; + + return SynchronousRestObjectPoster. + BeginPostObject("POST", uri, asset); } public bool Delete(string id) { + string uri = m_ServerURI + "/assets/" + id; + + return SynchronousRestObjectPoster. + BeginPostObject("DELETE", uri, 0); return false; } } diff --git a/OpenSim/Servers/Asset/AssetServerPostHandler.cs b/OpenSim/Servers/Asset/AssetServerPostHandler.cs index 70cb16a..3bc7267 100644 --- a/OpenSim/Servers/Asset/AssetServerPostHandler.cs +++ b/OpenSim/Servers/Asset/AssetServerPostHandler.cs @@ -66,7 +66,7 @@ namespace OpenSim.Servers.AssetServer if (p.Length > 1) { bool result = - m_AssetService.UpdateContent(asset.ID, asset.Data); + m_AssetService.UpdateContent(p[1], asset.Data); xs = new XmlSerializer(typeof(bool)); return ServerUtils.SerializeResult(xs, result); -- cgit v1.1