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.cs480
1 files changed, 240 insertions, 240 deletions
diff --git a/OpenSim/Grid/AssetInventoryServer/Plugins/OpenSim/OpenSimAssetFrontendPlugin.cs b/OpenSim/Grid/AssetInventoryServer/Plugins/OpenSim/OpenSimAssetFrontendPlugin.cs
index e76c8ee..1abd3f5 100644
--- a/OpenSim/Grid/AssetInventoryServer/Plugins/OpenSim/OpenSimAssetFrontendPlugin.cs
+++ b/OpenSim/Grid/AssetInventoryServer/Plugins/OpenSim/OpenSimAssetFrontendPlugin.cs
@@ -1,240 +1,240 @@
1/* 1/*
2 * Copyright (c) 2008 Intel Corporation 2 * Copyright (c) 2008 Intel Corporation
3 * All rights reserved. 3 * All rights reserved.
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 7 *
8 * -- Redistributions of source code must retain the above copyright 8 * -- Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * -- Redistributions in binary form must reproduce the above copyright 10 * -- Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the 11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution. 12 * documentation and/or other materials provided with the distribution.
13 * -- Neither the name of the Intel Corporation nor the names of its 13 * -- Neither the name of the Intel Corporation nor the names of its
14 * contributors may be used to endorse or promote products derived from 14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission. 15 * this software without specific prior written permission.
16 * 16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR ITS 20 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR ITS
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 24 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 25 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */ 28 */
29 29
30using System; 30using System;
31using System.Collections.Generic; 31using System.Collections.Generic;
32using System.Net; 32using System.Net;
33using System.IO; 33using System.IO;
34using System.Xml; 34using System.Xml;
35using ExtensionLoader; 35using ExtensionLoader;
36using OpenMetaverse; 36using OpenMetaverse;
37using HttpServer; 37using HttpServer;
38using OpenSim.Framework; 38using OpenSim.Framework;
39 39
40namespace OpenSim.Grid.AssetInventoryServer.Plugins 40namespace OpenSim.Grid.AssetInventoryServer.Plugins
41{ 41{
42 public class OpenSimAssetFrontendPlugin : IAssetInventoryServerPlugin 42 public class OpenSimAssetFrontendPlugin : IAssetInventoryServerPlugin
43 { 43 {
44 AssetInventoryServer server; 44 AssetInventoryServer server;
45 45
46 public OpenSimAssetFrontendPlugin() 46 public OpenSimAssetFrontendPlugin()
47 { 47 {
48 } 48 }
49 49
50 #region IPlugin implementation 50 #region IPlugin implementation
51 51
52 public void Initialise(AssetInventoryServer server) 52 public void Initialise(AssetInventoryServer server)
53 { 53 {
54 this.server = server; 54 this.server = server;
55 55
56 // Asset request 56 // Asset request
57 server.HttpServer.AddHandler("get", null, @"^/assets/", AssetRequestHandler); 57 server.HttpServer.AddHandler("get", null, @"^/assets/", AssetRequestHandler);
58 58
59 // Asset creation 59 // Asset creation
60 server.HttpServer.AddHandler("post", null, @"^/assets/", AssetPostHandler); 60 server.HttpServer.AddHandler("post", null, @"^/assets/", AssetPostHandler);
61 } 61 }
62 62
63 /// <summary> 63 /// <summary>
64 /// <para>Initialises asset interface</para> 64 /// <para>Initialises asset interface</para>
65 /// </summary> 65 /// </summary>
66 public void Initialise() 66 public void Initialise()
67 { 67 {
68 Logger.Log.InfoFormat("[ASSET]: {0} cannot be default-initialized!", Name); 68 Logger.Log.InfoFormat("[ASSET]: {0} cannot be default-initialized!", Name);
69 throw new PluginNotInitialisedException(Name); 69 throw new PluginNotInitialisedException(Name);
70 } 70 }
71 71
72 public void Dispose() 72 public void Dispose()
73 { 73 {
74 } 74 }
75 75
76 public string Version 76 public string Version
77 { 77 {
78 // TODO: this should be something meaningful and not hardcoded? 78 // TODO: this should be something meaningful and not hardcoded?
79 get { return "0.1"; } 79 get { return "0.1"; }
80 } 80 }
81 81
82 public string Name 82 public string Name
83 { 83 {
84 get { return "AssetInventoryServer OpenSim asset frontend"; } 84 get { return "AssetInventoryServer OpenSim asset frontend"; }
85 } 85 }
86 86
87 #endregion IPlugin implementation 87 #endregion IPlugin implementation
88 88
89 bool AssetRequestHandler(IHttpClientContext client, IHttpRequest request, IHttpResponse response) 89 bool AssetRequestHandler(IHttpClientContext client, IHttpRequest request, IHttpResponse response)
90 { 90 {
91 UUID assetID; 91 UUID assetID;
92 // Split the URL up to get the asset ID out 92 // Split the URL up to get the asset ID out
93 string[] rawUrl = request.Uri.PathAndQuery.Split('/'); 93 string[] rawUrl = request.Uri.PathAndQuery.Split('/');
94 94
95 if (rawUrl.Length >= 3 && rawUrl[2].Length >= 36 && UUID.TryParse(rawUrl[2].Substring(0, 36), out assetID)) 95 if (rawUrl.Length >= 3 && rawUrl[2].Length >= 36 && UUID.TryParse(rawUrl[2].Substring(0, 36), out assetID))
96 { 96 {
97 Metadata metadata; 97 Metadata metadata;
98 byte[] assetData; 98 byte[] assetData;
99 BackendResponse dataResponse; 99 BackendResponse dataResponse;
100 100
101 if ((dataResponse = server.StorageProvider.TryFetchDataMetadata(assetID, out metadata, out assetData)) == BackendResponse.Success) 101 if ((dataResponse = server.StorageProvider.TryFetchDataMetadata(assetID, out metadata, out assetData)) == BackendResponse.Success)
102 { 102 {
103 MemoryStream stream = new MemoryStream(); 103 MemoryStream stream = new MemoryStream();
104 104
105 XmlWriterSettings settings = new XmlWriterSettings(); 105 XmlWriterSettings settings = new XmlWriterSettings();
106 settings.Indent = true; 106 settings.Indent = true;
107 XmlWriter writer = XmlWriter.Create(stream, settings); 107 XmlWriter writer = XmlWriter.Create(stream, settings);
108 108
109 writer.WriteStartDocument(); 109 writer.WriteStartDocument();
110 writer.WriteStartElement("AssetBase"); 110 writer.WriteStartElement("AssetBase");
111 writer.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance"); 111 writer.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance");
112 writer.WriteAttributeString("xmlns", "xsd", null, "http://www.w3.org/2001/XMLSchema"); 112 writer.WriteAttributeString("xmlns", "xsd", null, "http://www.w3.org/2001/XMLSchema");
113 writer.WriteStartElement("FullID"); 113 writer.WriteStartElement("FullID");
114 writer.WriteStartElement("Guid"); 114 writer.WriteStartElement("Guid");
115 writer.WriteString(assetID.ToString()); 115 writer.WriteString(assetID.ToString());
116 writer.WriteEndElement(); 116 writer.WriteEndElement();
117 writer.WriteEndElement(); 117 writer.WriteEndElement();
118 writer.WriteStartElement("ID"); 118 writer.WriteStartElement("ID");
119 writer.WriteString(assetID.ToString()); 119 writer.WriteString(assetID.ToString());
120 writer.WriteEndElement(); 120 writer.WriteEndElement();
121 writer.WriteStartElement("Data"); 121 writer.WriteStartElement("Data");
122 writer.WriteBase64(assetData, 0, assetData.Length); 122 writer.WriteBase64(assetData, 0, assetData.Length);
123 writer.WriteEndElement(); 123 writer.WriteEndElement();
124 writer.WriteStartElement("Type"); 124 writer.WriteStartElement("Type");
125 writer.WriteValue(Utils.ContentTypeToSLAssetType(metadata.ContentType)); 125 writer.WriteValue(Utils.ContentTypeToSLAssetType(metadata.ContentType));
126 writer.WriteEndElement(); 126 writer.WriteEndElement();
127 writer.WriteStartElement("Name"); 127 writer.WriteStartElement("Name");
128 writer.WriteString(metadata.Name); 128 writer.WriteString(metadata.Name);
129 writer.WriteEndElement(); 129 writer.WriteEndElement();
130 writer.WriteStartElement("Description"); 130 writer.WriteStartElement("Description");
131 writer.WriteString(metadata.Description); 131 writer.WriteString(metadata.Description);
132 writer.WriteEndElement(); 132 writer.WriteEndElement();
133 writer.WriteStartElement("Local"); 133 writer.WriteStartElement("Local");
134 writer.WriteValue(false); 134 writer.WriteValue(false);
135 writer.WriteEndElement(); 135 writer.WriteEndElement();
136 writer.WriteStartElement("Temporary"); 136 writer.WriteStartElement("Temporary");
137 writer.WriteValue(metadata.Temporary); 137 writer.WriteValue(metadata.Temporary);
138 writer.WriteEndElement(); 138 writer.WriteEndElement();
139 writer.WriteEndElement(); 139 writer.WriteEndElement();
140 writer.WriteEndDocument(); 140 writer.WriteEndDocument();
141 141
142 writer.Flush(); 142 writer.Flush();
143 byte[] buffer = stream.GetBuffer(); 143 byte[] buffer = stream.GetBuffer();
144 144
145 response.Status = HttpStatusCode.OK; 145 response.Status = HttpStatusCode.OK;
146 response.ContentType = "application/xml"; 146 response.ContentType = "application/xml";
147 response.ContentLength = stream.Length; 147 response.ContentLength = stream.Length;
148 response.Body.Write(buffer, 0, (int)stream.Length); 148 response.Body.Write(buffer, 0, (int)stream.Length);
149 response.Body.Flush(); 149 response.Body.Flush();
150 } 150 }
151 else 151 else
152 { 152 {
153 Logger.Log.WarnFormat("Failed to fetch asset data or metadata for {0}: {1}", assetID, dataResponse); 153 Logger.Log.WarnFormat("Failed to fetch asset data or metadata for {0}: {1}", assetID, dataResponse);
154 response.Status = HttpStatusCode.NotFound; 154 response.Status = HttpStatusCode.NotFound;
155 } 155 }
156 } 156 }
157 else 157 else
158 { 158 {
159 Logger.Log.Warn("Unrecognized OpenSim asset request: " + request.Uri.PathAndQuery); 159 Logger.Log.Warn("Unrecognized OpenSim asset request: " + request.Uri.PathAndQuery);
160 } 160 }
161 161
162 return true; 162 return true;
163 } 163 }
164 164
165 bool AssetPostHandler(IHttpClientContext client, IHttpRequest request, IHttpResponse response) 165 bool AssetPostHandler(IHttpClientContext client, IHttpRequest request, IHttpResponse response)
166 { 166 {
167 byte[] assetData = null; 167 byte[] assetData = null;
168 Metadata metadata = new Metadata(); 168 Metadata metadata = new Metadata();
169 169
170 Logger.Log.Debug("Handling OpenSim asset upload"); 170 Logger.Log.Debug("Handling OpenSim asset upload");
171 171
172 try 172 try
173 { 173 {
174 using (XmlReader reader = XmlReader.Create(request.Body)) 174 using (XmlReader reader = XmlReader.Create(request.Body))
175 { 175 {
176 reader.MoveToContent(); 176 reader.MoveToContent();
177 reader.ReadStartElement("AssetBase"); 177 reader.ReadStartElement("AssetBase");
178 178
179 reader.ReadStartElement("FullID"); 179 reader.ReadStartElement("FullID");
180 UUID.TryParse(reader.ReadElementContentAsString("Guid", String.Empty), out metadata.ID); 180 UUID.TryParse(reader.ReadElementContentAsString("Guid", String.Empty), out metadata.ID);
181 reader.ReadEndElement(); 181 reader.ReadEndElement();
182 reader.ReadStartElement("ID"); 182 reader.ReadStartElement("ID");
183 reader.Skip(); 183 reader.Skip();
184 reader.ReadEndElement(); 184 reader.ReadEndElement();
185 185
186 // HACK: Broken on Mono. https://bugzilla.novell.com/show_bug.cgi?id=464229 186 // HACK: Broken on Mono. https://bugzilla.novell.com/show_bug.cgi?id=464229
187 //int readBytes = 0; 187 //int readBytes = 0;
188 //byte[] buffer = new byte[1024]; 188 //byte[] buffer = new byte[1024];
189 //MemoryStream stream = new MemoryStream(); 189 //MemoryStream stream = new MemoryStream();
190 //BinaryWriter writer = new BinaryWriter(stream); 190 //BinaryWriter writer = new BinaryWriter(stream);
191 //while ((readBytes = reader.ReadElementContentAsBase64(buffer, 0, buffer.Length)) > 0) 191 //while ((readBytes = reader.ReadElementContentAsBase64(buffer, 0, buffer.Length)) > 0)
192 // writer.Write(buffer, 0, readBytes); 192 // writer.Write(buffer, 0, readBytes);
193 //writer.Flush(); 193 //writer.Flush();
194 //assetData = stream.GetBuffer(); 194 //assetData = stream.GetBuffer();
195 //Array.Resize<byte>(ref assetData, (int)stream.Length); 195 //Array.Resize<byte>(ref assetData, (int)stream.Length);
196 196
197 assetData = Convert.FromBase64String(reader.ReadElementContentAsString()); 197 assetData = Convert.FromBase64String(reader.ReadElementContentAsString());
198 198
199 int type; 199 int type;
200 Int32.TryParse(reader.ReadElementContentAsString("Type", String.Empty), out type); 200 Int32.TryParse(reader.ReadElementContentAsString("Type", String.Empty), out type);
201 metadata.ContentType = Utils.SLAssetTypeToContentType(type); 201 metadata.ContentType = Utils.SLAssetTypeToContentType(type);
202 metadata.Name = reader.ReadElementContentAsString("Name", String.Empty); 202 metadata.Name = reader.ReadElementContentAsString("Name", String.Empty);
203 metadata.Description = reader.ReadElementContentAsString("Description", String.Empty); 203 metadata.Description = reader.ReadElementContentAsString("Description", String.Empty);
204 Boolean.TryParse(reader.ReadElementContentAsString("Local", String.Empty), out metadata.Temporary); 204 Boolean.TryParse(reader.ReadElementContentAsString("Local", String.Empty), out metadata.Temporary);
205 Boolean.TryParse(reader.ReadElementContentAsString("Temporary", String.Empty), out metadata.Temporary); 205 Boolean.TryParse(reader.ReadElementContentAsString("Temporary", String.Empty), out metadata.Temporary);
206 206
207 reader.ReadEndElement(); 207 reader.ReadEndElement();
208 } 208 }
209 209
210 if (assetData != null && assetData.Length > 0) 210 if (assetData != null && assetData.Length > 0)
211 { 211 {
212 metadata.SHA1 = OpenMetaverse.Utils.SHA1(assetData); 212 metadata.SHA1 = OpenMetaverse.Utils.SHA1(assetData);
213 metadata.CreationDate = DateTime.Now; 213 metadata.CreationDate = DateTime.Now;
214 214
215 BackendResponse storageResponse = server.StorageProvider.TryCreateAsset(metadata, assetData); 215 BackendResponse storageResponse = server.StorageProvider.TryCreateAsset(metadata, assetData);
216 216
217 if (storageResponse == BackendResponse.Success) 217 if (storageResponse == BackendResponse.Success)
218 response.Status = HttpStatusCode.Created; 218 response.Status = HttpStatusCode.Created;
219 else if (storageResponse == BackendResponse.NotFound) 219 else if (storageResponse == BackendResponse.NotFound)
220 response.Status = HttpStatusCode.NotFound; 220 response.Status = HttpStatusCode.NotFound;
221 else 221 else
222 response.Status = HttpStatusCode.InternalServerError; 222 response.Status = HttpStatusCode.InternalServerError;
223 } 223 }
224 else 224 else
225 { 225 {
226 Logger.Log.Warn("AssetPostHandler called with no asset data"); 226 Logger.Log.Warn("AssetPostHandler called with no asset data");
227 response.Status = HttpStatusCode.BadRequest; 227 response.Status = HttpStatusCode.BadRequest;
228 } 228 }
229 } 229 }
230 catch (Exception ex) 230 catch (Exception ex)
231 { 231 {
232 Logger.Log.Warn("Failed to parse POST data (expecting AssetBase): " + ex.Message); 232 Logger.Log.Warn("Failed to parse POST data (expecting AssetBase): " + ex.Message);
233 response.Status = HttpStatusCode.BadRequest; 233 response.Status = HttpStatusCode.BadRequest;
234 } 234 }
235 235
236 Logger.Log.Debug("Finished handling OpenSim asset upload, Status: " + response.Status.ToString()); 236 Logger.Log.Debug("Finished handling OpenSim asset upload, Status: " + response.Status.ToString());
237 return true; 237 return true;
238 } 238 }
239 } 239 }
240} 240}