aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Grid/AssetInventoryServer/Plugins/OpenSim/OpenSimAssetFrontendPlugin.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Grid/AssetInventoryServer/Plugins/OpenSim/OpenSimAssetFrontendPlugin.cs')
-rw-r--r--OpenSim/Grid/AssetInventoryServer/Plugins/OpenSim/OpenSimAssetFrontendPlugin.cs211
1 files changed, 142 insertions, 69 deletions
diff --git a/OpenSim/Grid/AssetInventoryServer/Plugins/OpenSim/OpenSimAssetFrontendPlugin.cs b/OpenSim/Grid/AssetInventoryServer/Plugins/OpenSim/OpenSimAssetFrontendPlugin.cs
index f831e24..3431ff3 100644
--- a/OpenSim/Grid/AssetInventoryServer/Plugins/OpenSim/OpenSimAssetFrontendPlugin.cs
+++ b/OpenSim/Grid/AssetInventoryServer/Plugins/OpenSim/OpenSimAssetFrontendPlugin.cs
@@ -37,6 +37,7 @@ using System.Xml.Serialization;
37using OpenMetaverse; 37using OpenMetaverse;
38using HttpServer; 38using HttpServer;
39using OpenSim.Framework; 39using OpenSim.Framework;
40using OpenSim.Framework.Servers;
40 41
41namespace OpenSim.Grid.AssetInventoryServer.Plugins.OpenSim 42namespace OpenSim.Grid.AssetInventoryServer.Plugins.OpenSim
42{ 43{
@@ -55,10 +56,10 @@ namespace OpenSim.Grid.AssetInventoryServer.Plugins.OpenSim
55 this.server = server; 56 this.server = server;
56 57
57 // Asset request 58 // Asset request
58 server.HttpServer.AddHandler("get", null, @"^/assets/", AssetRequestHandler); 59 server.HttpServer.AddStreamHandler(new AssetRequestHandler(server));
59 60
60 // Asset creation 61 // Asset creation
61 server.HttpServer.AddHandler("post", null, @"^/assets/", AssetPostHandler); 62 server.HttpServer.AddStreamHandler(new AssetPostHandler(server));
62 63
63 Logger.Log.Info("[ASSET] OpenSim Asset Frontend loaded."); 64 Logger.Log.Info("[ASSET] OpenSim Asset Frontend loaded.");
64 } 65 }
@@ -89,100 +90,172 @@ namespace OpenSim.Grid.AssetInventoryServer.Plugins.OpenSim
89 90
90 #endregion IPlugin implementation 91 #endregion IPlugin implementation
91 92
92 bool AssetRequestHandler(IHttpClientContext client, IHttpRequest request, IHttpResponse response) 93 public class AssetRequestHandler : IStreamedRequestHandler
93 { 94 {
94 UUID assetID; 95 AssetInventoryServer m_server;
95 // Split the URL up to get the asset ID out 96 string m_contentType;
96 string[] rawUrl = request.Uri.PathAndQuery.Split('/'); 97 string m_httpMethod;
98 string m_path;
97 99
98 if (rawUrl.Length >= 3 && rawUrl[2].Length >= 36 && UUID.TryParse(rawUrl[2].Substring(0, 36), out assetID)) 100 public AssetRequestHandler(AssetInventoryServer server)
99 { 101 {
100 Metadata metadata; 102 m_server = server;
101 byte[] assetData; 103 m_contentType = null;
102 BackendResponse dataResponse; 104 m_httpMethod = "GET";
105 m_path = @"^/assets/";
106 }
107
108 #region IStreamedRequestHandler implementation
109
110 public string ContentType
111 {
112 get { return m_contentType; }
113 }
114
115 public string HttpMethod
116 {
117 get { return m_httpMethod; }
118 }
119
120 public string Path
121 {
122 get { return m_path; }
123 }
124
125 public byte[] Handle(string path, Stream request, OSHttpRequest httpRequest, OSHttpResponse httpResponse)
126 {
127 byte[] buffer = null;
128 UUID assetID;
129 // Split the URL up to get the asset ID out
130 string[] rawUrl = httpRequest.Url.PathAndQuery.Split('/');
103 131
104 if ((dataResponse = server.StorageProvider.TryFetchDataMetadata(assetID, out metadata, out assetData)) == BackendResponse.Success) 132 if (rawUrl.Length >= 3 && rawUrl[2].Length >= 36 && UUID.TryParse(rawUrl[2].Substring(0, 36), out assetID))
105 { 133 {
106 AssetBase asset = new AssetBase(); 134 Metadata metadata;
107 asset.Data = assetData; 135 byte[] assetData;
108 asset.Metadata.FullID = metadata.ID; 136 BackendResponse dataResponse;
109 asset.Metadata.Name = metadata.Name; 137
110 asset.Metadata.Description = metadata.Description; 138 if ((dataResponse = m_server.StorageProvider.TryFetchDataMetadata(assetID, out metadata, out assetData)) == BackendResponse.Success)
111 asset.Metadata.CreationDate = metadata.CreationDate; 139 {
112 asset.Metadata.Type = (sbyte) Utils.ContentTypeToSLAssetType(metadata.ContentType); 140 AssetBase asset = new AssetBase();
113 asset.Metadata.Local = false; 141 asset.Data = assetData;
114 asset.Metadata.Temporary = metadata.Temporary; 142 asset.Metadata.FullID = metadata.ID;
115 143 asset.Metadata.Name = metadata.Name;
116 XmlSerializer xs = new XmlSerializer(typeof (AssetBase)); 144 asset.Metadata.Description = metadata.Description;
117 MemoryStream ms = new MemoryStream(); 145 asset.Metadata.CreationDate = metadata.CreationDate;
118 XmlTextWriter xw = new XmlTextWriter(ms, Encoding.UTF8); 146 asset.Metadata.Type = (sbyte) Utils.ContentTypeToSLAssetType(metadata.ContentType);
119 xs.Serialize(xw, asset); 147 asset.Metadata.Local = false;
120 xw.Flush(); 148 asset.Metadata.Temporary = metadata.Temporary;
121 149
122 ms.Seek(0, SeekOrigin.Begin); 150 XmlSerializer xs = new XmlSerializer(typeof (AssetBase));
123 byte[] buffer = ms.GetBuffer(); 151 MemoryStream ms = new MemoryStream();
124 152 XmlTextWriter xw = new XmlTextWriter(ms, Encoding.UTF8);
125 response.Status = HttpStatusCode.OK; 153 xs.Serialize(xw, asset);
126 response.ContentType = "application/xml"; 154 xw.Flush();
127 response.ContentLength = ms.Length; 155
128 response.Body.Write(buffer, 0, (int) ms.Length); 156 ms.Seek(0, SeekOrigin.Begin);
129 response.Body.Flush(); 157 buffer = ms.GetBuffer();
158
159 httpResponse.StatusCode = (int) HttpStatusCode.OK;
160 httpResponse.ContentType = "application/xml";
161 httpResponse.ContentLength = ms.Length;
162 httpResponse.Body.Write(buffer, 0, (int) ms.Length);
163 httpResponse.Body.Flush();
164 }
165 else
166 {
167 Logger.Log.WarnFormat("Failed to fetch asset data or metadata for {0}: {1}", assetID, dataResponse);
168 httpResponse.StatusCode = (int) HttpStatusCode.NotFound;
169 }
130 } 170 }
131 else 171 else
132 { 172 {
133 Logger.Log.WarnFormat("Failed to fetch asset data or metadata for {0}: {1}", assetID, dataResponse); 173 Logger.Log.Warn("Unrecognized OpenSim asset request: " + httpRequest.Url.PathAndQuery);
134 response.Status = HttpStatusCode.NotFound;
135 } 174 }
136 } 175
137 else 176 return buffer;
138 {
139 Logger.Log.Warn("Unrecognized OpenSim asset request: " + request.Uri.PathAndQuery);
140 } 177 }
141 178
142 return true; 179 #endregion IStreamedRequestHandler implementation
143 } 180 }
144 181
145 bool AssetPostHandler(IHttpClientContext client, IHttpRequest request, IHttpResponse response) 182 public class AssetPostHandler : IStreamedRequestHandler
146 { 183 {
147 Metadata metadata = new Metadata(); 184 AssetInventoryServer m_server;
185 string m_contentType;
186 string m_httpMethod;
187 string m_path;
188
189 public AssetPostHandler(AssetInventoryServer server)
190 {
191 m_server = server;
192 m_contentType = null;
193 m_httpMethod = "POST";
194 m_path = @"^/assets/";
195 }
196
197 #region IStreamedRequestHandler implementation
148 198
149 try 199 public string ContentType
150 { 200 {
151 AssetBase asset = (AssetBase) new XmlSerializer(typeof (AssetBase)).Deserialize(request.Body); 201 get { return m_contentType; }
202 }
152 203
153 if (asset.Data != null && asset.Data.Length > 0) 204 public string HttpMethod
205 {
206 get { return m_httpMethod; }
207 }
208
209 public string Path
210 {
211 get { return m_path; }
212 }
213
214 public byte[] Handle(string path, Stream request, OSHttpRequest httpRequest, OSHttpResponse httpResponse)
215 //bool AssetPostHandler(IHttpClientContext client, IHttpRequest request, IHttpResponse response)
216 {
217 Metadata metadata = new Metadata();
218
219 try
154 { 220 {
155 metadata.ID = asset.Metadata.FullID; 221 AssetBase asset = (AssetBase) new XmlSerializer(typeof (AssetBase)).Deserialize(httpRequest.InputStream);
156 metadata.ContentType = Utils.SLAssetTypeToContentType((int) asset.Metadata.Type); 222
157 metadata.Name = asset.Metadata.Name; 223 if (asset.Data != null && asset.Data.Length > 0)
158 metadata.Description = asset.Metadata.Description; 224 {
159 metadata.Temporary = asset.Metadata.Temporary; 225 metadata.ID = asset.Metadata.FullID;
226 metadata.ContentType = Utils.SLAssetTypeToContentType((int) asset.Metadata.Type);
227 metadata.Name = asset.Metadata.Name;
228 metadata.Description = asset.Metadata.Description;
229 metadata.Temporary = asset.Metadata.Temporary;
160 230
161 metadata.SHA1 = OpenMetaverse.Utils.SHA1(asset.Data); 231 metadata.SHA1 = OpenMetaverse.Utils.SHA1(asset.Data);
162 metadata.CreationDate = DateTime.Now; 232 metadata.CreationDate = DateTime.Now;
163 233
164 BackendResponse storageResponse = server.StorageProvider.TryCreateAsset(metadata, asset.Data); 234 BackendResponse storageResponse = m_server.StorageProvider.TryCreateAsset(metadata, asset.Data);
165 235
166 if (storageResponse == BackendResponse.Success) 236 if (storageResponse == BackendResponse.Success)
167 response.Status = HttpStatusCode.Created; 237 httpResponse.StatusCode = (int) HttpStatusCode.Created;
168 else if (storageResponse == BackendResponse.NotFound) 238 else if (storageResponse == BackendResponse.NotFound)
169 response.Status = HttpStatusCode.NotFound; 239 httpResponse.StatusCode = (int) HttpStatusCode.NotFound;
240 else
241 httpResponse.StatusCode = (int) HttpStatusCode.InternalServerError;
242 }
170 else 243 else
171 response.Status = HttpStatusCode.InternalServerError; 244 {
245 Logger.Log.Warn("AssetPostHandler called with no asset data");
246 httpResponse.StatusCode = (int) HttpStatusCode.BadRequest;
247 }
172 } 248 }
173 else 249 catch (Exception ex)
174 { 250 {
175 Logger.Log.Warn("AssetPostHandler called with no asset data"); 251 Logger.Log.Warn("Failed to parse POST data (expecting AssetBase): " + ex.Message);
176 response.Status = HttpStatusCode.BadRequest; 252 httpResponse.StatusCode = (int) HttpStatusCode.BadRequest;
177 } 253 }
178 } 254
179 catch (Exception ex) 255 return null;
180 {
181 Logger.Log.Warn("Failed to parse POST data (expecting AssetBase): " + ex.Message);
182 response.Status = HttpStatusCode.BadRequest;
183 } 256 }
184 257
185 return true; 258 #endregion IStreamedRequestHandler implementation
186 } 259 }
187 } 260 }
188} 261}