diff options
author | Mike Mazur | 2009-02-16 02:25:44 +0000 |
---|---|---|
committer | Mike Mazur | 2009-02-16 02:25:44 +0000 |
commit | 16fa7f516a484e9f21fac34262214b3d9d761823 (patch) | |
tree | 925c10831c04d053b707857ffa785482316d4dcb /OpenSim/Grid/AssetInventoryServer/Extensions/OpenSimFrontend.cs | |
parent | Added OpenSim asset frontend plugin. (diff) | |
download | opensim-SC-16fa7f516a484e9f21fac34262214b3d9d761823.zip opensim-SC-16fa7f516a484e9f21fac34262214b3d9d761823.tar.gz opensim-SC-16fa7f516a484e9f21fac34262214b3d9d761823.tar.bz2 opensim-SC-16fa7f516a484e9f21fac34262214b3d9d761823.tar.xz |
Converted to Linux newlines.
Diffstat (limited to 'OpenSim/Grid/AssetInventoryServer/Extensions/OpenSimFrontend.cs')
-rw-r--r-- | OpenSim/Grid/AssetInventoryServer/Extensions/OpenSimFrontend.cs | 430 |
1 files changed, 215 insertions, 215 deletions
diff --git a/OpenSim/Grid/AssetInventoryServer/Extensions/OpenSimFrontend.cs b/OpenSim/Grid/AssetInventoryServer/Extensions/OpenSimFrontend.cs index 4fff5e9..e34a235 100644 --- a/OpenSim/Grid/AssetInventoryServer/Extensions/OpenSimFrontend.cs +++ b/OpenSim/Grid/AssetInventoryServer/Extensions/OpenSimFrontend.cs | |||
@@ -1,215 +1,215 @@ | |||
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 | ||
30 | using System; | 30 | using System; |
31 | using System.Collections.Generic; | 31 | using System.Collections.Generic; |
32 | using System.Net; | 32 | using System.Net; |
33 | using System.IO; | 33 | using System.IO; |
34 | using System.Xml; | 34 | using System.Xml; |
35 | using ExtensionLoader; | 35 | using ExtensionLoader; |
36 | using OpenMetaverse; | 36 | using OpenMetaverse; |
37 | using HttpServer; | 37 | using HttpServer; |
38 | 38 | ||
39 | namespace OpenSim.Grid.AssetInventoryServer.Extensions | 39 | namespace OpenSim.Grid.AssetInventoryServer.Extensions |
40 | { | 40 | { |
41 | public class OpenSimFrontend : IExtension<AssetInventoryServer> | 41 | public class OpenSimFrontend : IExtension<AssetInventoryServer> |
42 | { | 42 | { |
43 | AssetInventoryServer server; | 43 | AssetInventoryServer server; |
44 | 44 | ||
45 | public OpenSimFrontend() | 45 | public OpenSimFrontend() |
46 | { | 46 | { |
47 | } | 47 | } |
48 | 48 | ||
49 | public void Start(AssetInventoryServer server) | 49 | public void Start(AssetInventoryServer server) |
50 | { | 50 | { |
51 | this.server = server; | 51 | this.server = server; |
52 | 52 | ||
53 | // Asset request | 53 | // Asset request |
54 | server.HttpServer.AddHandler("get", null, @"^/assets/", AssetRequestHandler); | 54 | server.HttpServer.AddHandler("get", null, @"^/assets/", AssetRequestHandler); |
55 | 55 | ||
56 | // Asset creation | 56 | // Asset creation |
57 | server.HttpServer.AddHandler("post", null, @"^/assets/", AssetPostHandler); | 57 | server.HttpServer.AddHandler("post", null, @"^/assets/", AssetPostHandler); |
58 | } | 58 | } |
59 | 59 | ||
60 | public void Stop() | 60 | public void Stop() |
61 | { | 61 | { |
62 | } | 62 | } |
63 | 63 | ||
64 | bool AssetRequestHandler(IHttpClientContext client, IHttpRequest request, IHttpResponse response) | 64 | bool AssetRequestHandler(IHttpClientContext client, IHttpRequest request, IHttpResponse response) |
65 | { | 65 | { |
66 | UUID assetID; | 66 | UUID assetID; |
67 | // Split the URL up to get the asset ID out | 67 | // Split the URL up to get the asset ID out |
68 | string[] rawUrl = request.Uri.PathAndQuery.Split('/'); | 68 | string[] rawUrl = request.Uri.PathAndQuery.Split('/'); |
69 | 69 | ||
70 | if (rawUrl.Length >= 3 && rawUrl[2].Length >= 36 && UUID.TryParse(rawUrl[2].Substring(0, 36), out assetID)) | 70 | if (rawUrl.Length >= 3 && rawUrl[2].Length >= 36 && UUID.TryParse(rawUrl[2].Substring(0, 36), out assetID)) |
71 | { | 71 | { |
72 | Metadata metadata; | 72 | Metadata metadata; |
73 | byte[] assetData; | 73 | byte[] assetData; |
74 | BackendResponse dataResponse; | 74 | BackendResponse dataResponse; |
75 | 75 | ||
76 | if ((dataResponse = server.StorageProvider.TryFetchDataMetadata(assetID, out metadata, out assetData)) == BackendResponse.Success) | 76 | if ((dataResponse = server.StorageProvider.TryFetchDataMetadata(assetID, out metadata, out assetData)) == BackendResponse.Success) |
77 | { | 77 | { |
78 | MemoryStream stream = new MemoryStream(); | 78 | MemoryStream stream = new MemoryStream(); |
79 | 79 | ||
80 | XmlWriterSettings settings = new XmlWriterSettings(); | 80 | XmlWriterSettings settings = new XmlWriterSettings(); |
81 | settings.Indent = true; | 81 | settings.Indent = true; |
82 | XmlWriter writer = XmlWriter.Create(stream, settings); | 82 | XmlWriter writer = XmlWriter.Create(stream, settings); |
83 | 83 | ||
84 | writer.WriteStartDocument(); | 84 | writer.WriteStartDocument(); |
85 | writer.WriteStartElement("AssetBase"); | 85 | writer.WriteStartElement("AssetBase"); |
86 | writer.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance"); | 86 | writer.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance"); |
87 | writer.WriteAttributeString("xmlns", "xsd", null, "http://www.w3.org/2001/XMLSchema"); | 87 | writer.WriteAttributeString("xmlns", "xsd", null, "http://www.w3.org/2001/XMLSchema"); |
88 | writer.WriteStartElement("FullID"); | 88 | writer.WriteStartElement("FullID"); |
89 | writer.WriteStartElement("Guid"); | 89 | writer.WriteStartElement("Guid"); |
90 | writer.WriteString(assetID.ToString()); | 90 | writer.WriteString(assetID.ToString()); |
91 | writer.WriteEndElement(); | 91 | writer.WriteEndElement(); |
92 | writer.WriteEndElement(); | 92 | writer.WriteEndElement(); |
93 | writer.WriteStartElement("ID"); | 93 | writer.WriteStartElement("ID"); |
94 | writer.WriteString(assetID.ToString()); | 94 | writer.WriteString(assetID.ToString()); |
95 | writer.WriteEndElement(); | 95 | writer.WriteEndElement(); |
96 | writer.WriteStartElement("Data"); | 96 | writer.WriteStartElement("Data"); |
97 | writer.WriteBase64(assetData, 0, assetData.Length); | 97 | writer.WriteBase64(assetData, 0, assetData.Length); |
98 | writer.WriteEndElement(); | 98 | writer.WriteEndElement(); |
99 | writer.WriteStartElement("Type"); | 99 | writer.WriteStartElement("Type"); |
100 | writer.WriteValue(Utils.ContentTypeToSLAssetType(metadata.ContentType)); | 100 | writer.WriteValue(Utils.ContentTypeToSLAssetType(metadata.ContentType)); |
101 | writer.WriteEndElement(); | 101 | writer.WriteEndElement(); |
102 | writer.WriteStartElement("Name"); | 102 | writer.WriteStartElement("Name"); |
103 | writer.WriteString(metadata.Name); | 103 | writer.WriteString(metadata.Name); |
104 | writer.WriteEndElement(); | 104 | writer.WriteEndElement(); |
105 | writer.WriteStartElement("Description"); | 105 | writer.WriteStartElement("Description"); |
106 | writer.WriteString(metadata.Description); | 106 | writer.WriteString(metadata.Description); |
107 | writer.WriteEndElement(); | 107 | writer.WriteEndElement(); |
108 | writer.WriteStartElement("Local"); | 108 | writer.WriteStartElement("Local"); |
109 | writer.WriteValue(false); | 109 | writer.WriteValue(false); |
110 | writer.WriteEndElement(); | 110 | writer.WriteEndElement(); |
111 | writer.WriteStartElement("Temporary"); | 111 | writer.WriteStartElement("Temporary"); |
112 | writer.WriteValue(metadata.Temporary); | 112 | writer.WriteValue(metadata.Temporary); |
113 | writer.WriteEndElement(); | 113 | writer.WriteEndElement(); |
114 | writer.WriteEndElement(); | 114 | writer.WriteEndElement(); |
115 | writer.WriteEndDocument(); | 115 | writer.WriteEndDocument(); |
116 | 116 | ||
117 | writer.Flush(); | 117 | writer.Flush(); |
118 | byte[] buffer = stream.GetBuffer(); | 118 | byte[] buffer = stream.GetBuffer(); |
119 | 119 | ||
120 | response.Status = HttpStatusCode.OK; | 120 | response.Status = HttpStatusCode.OK; |
121 | response.ContentType = "application/xml"; | 121 | response.ContentType = "application/xml"; |
122 | response.ContentLength = stream.Length; | 122 | response.ContentLength = stream.Length; |
123 | response.Body.Write(buffer, 0, (int)stream.Length); | 123 | response.Body.Write(buffer, 0, (int)stream.Length); |
124 | response.Body.Flush(); | 124 | response.Body.Flush(); |
125 | } | 125 | } |
126 | else | 126 | else |
127 | { | 127 | { |
128 | Logger.Log.WarnFormat("Failed to fetch asset data or metadata for {0}: {1}", assetID, dataResponse); | 128 | Logger.Log.WarnFormat("Failed to fetch asset data or metadata for {0}: {1}", assetID, dataResponse); |
129 | response.Status = HttpStatusCode.NotFound; | 129 | response.Status = HttpStatusCode.NotFound; |
130 | } | 130 | } |
131 | } | 131 | } |
132 | else | 132 | else |
133 | { | 133 | { |
134 | Logger.Log.Warn("Unrecognized OpenSim asset request: " + request.Uri.PathAndQuery); | 134 | Logger.Log.Warn("Unrecognized OpenSim asset request: " + request.Uri.PathAndQuery); |
135 | } | 135 | } |
136 | 136 | ||
137 | return true; | 137 | return true; |
138 | } | 138 | } |
139 | 139 | ||
140 | bool AssetPostHandler(IHttpClientContext client, IHttpRequest request, IHttpResponse response) | 140 | bool AssetPostHandler(IHttpClientContext client, IHttpRequest request, IHttpResponse response) |
141 | { | 141 | { |
142 | byte[] assetData = null; | 142 | byte[] assetData = null; |
143 | Metadata metadata = new Metadata(); | 143 | Metadata metadata = new Metadata(); |
144 | 144 | ||
145 | Logger.Log.Debug("Handling OpenSim asset upload"); | 145 | Logger.Log.Debug("Handling OpenSim asset upload"); |
146 | 146 | ||
147 | try | 147 | try |
148 | { | 148 | { |
149 | using (XmlReader reader = XmlReader.Create(request.Body)) | 149 | using (XmlReader reader = XmlReader.Create(request.Body)) |
150 | { | 150 | { |
151 | reader.MoveToContent(); | 151 | reader.MoveToContent(); |
152 | reader.ReadStartElement("AssetBase"); | 152 | reader.ReadStartElement("AssetBase"); |
153 | 153 | ||
154 | reader.ReadStartElement("FullID"); | 154 | reader.ReadStartElement("FullID"); |
155 | UUID.TryParse(reader.ReadElementContentAsString("Guid", String.Empty), out metadata.ID); | 155 | UUID.TryParse(reader.ReadElementContentAsString("Guid", String.Empty), out metadata.ID); |
156 | reader.ReadEndElement(); | 156 | reader.ReadEndElement(); |
157 | reader.ReadStartElement("ID"); | 157 | reader.ReadStartElement("ID"); |
158 | reader.Skip(); | 158 | reader.Skip(); |
159 | reader.ReadEndElement(); | 159 | reader.ReadEndElement(); |
160 | 160 | ||
161 | // HACK: Broken on Mono. https://bugzilla.novell.com/show_bug.cgi?id=464229 | 161 | // HACK: Broken on Mono. https://bugzilla.novell.com/show_bug.cgi?id=464229 |
162 | //int readBytes = 0; | 162 | //int readBytes = 0; |
163 | //byte[] buffer = new byte[1024]; | 163 | //byte[] buffer = new byte[1024]; |
164 | //MemoryStream stream = new MemoryStream(); | 164 | //MemoryStream stream = new MemoryStream(); |
165 | //BinaryWriter writer = new BinaryWriter(stream); | 165 | //BinaryWriter writer = new BinaryWriter(stream); |
166 | //while ((readBytes = reader.ReadElementContentAsBase64(buffer, 0, buffer.Length)) > 0) | 166 | //while ((readBytes = reader.ReadElementContentAsBase64(buffer, 0, buffer.Length)) > 0) |
167 | // writer.Write(buffer, 0, readBytes); | 167 | // writer.Write(buffer, 0, readBytes); |
168 | //writer.Flush(); | 168 | //writer.Flush(); |
169 | //assetData = stream.GetBuffer(); | 169 | //assetData = stream.GetBuffer(); |
170 | //Array.Resize<byte>(ref assetData, (int)stream.Length); | 170 | //Array.Resize<byte>(ref assetData, (int)stream.Length); |
171 | 171 | ||
172 | assetData = Convert.FromBase64String(reader.ReadElementContentAsString()); | 172 | assetData = Convert.FromBase64String(reader.ReadElementContentAsString()); |
173 | 173 | ||
174 | int type; | 174 | int type; |
175 | Int32.TryParse(reader.ReadElementContentAsString("Type", String.Empty), out type); | 175 | Int32.TryParse(reader.ReadElementContentAsString("Type", String.Empty), out type); |
176 | metadata.ContentType = Utils.SLAssetTypeToContentType(type); | 176 | metadata.ContentType = Utils.SLAssetTypeToContentType(type); |
177 | metadata.Name = reader.ReadElementContentAsString("Name", String.Empty); | 177 | metadata.Name = reader.ReadElementContentAsString("Name", String.Empty); |
178 | metadata.Description = reader.ReadElementContentAsString("Description", String.Empty); | 178 | metadata.Description = reader.ReadElementContentAsString("Description", String.Empty); |
179 | Boolean.TryParse(reader.ReadElementContentAsString("Local", String.Empty), out metadata.Temporary); | 179 | Boolean.TryParse(reader.ReadElementContentAsString("Local", String.Empty), out metadata.Temporary); |
180 | Boolean.TryParse(reader.ReadElementContentAsString("Temporary", String.Empty), out metadata.Temporary); | 180 | Boolean.TryParse(reader.ReadElementContentAsString("Temporary", String.Empty), out metadata.Temporary); |
181 | 181 | ||
182 | reader.ReadEndElement(); | 182 | reader.ReadEndElement(); |
183 | } | 183 | } |
184 | 184 | ||
185 | if (assetData != null && assetData.Length > 0) | 185 | if (assetData != null && assetData.Length > 0) |
186 | { | 186 | { |
187 | metadata.SHA1 = OpenMetaverse.Utils.SHA1(assetData); | 187 | metadata.SHA1 = OpenMetaverse.Utils.SHA1(assetData); |
188 | metadata.CreationDate = DateTime.Now; | 188 | metadata.CreationDate = DateTime.Now; |
189 | 189 | ||
190 | BackendResponse storageResponse = server.StorageProvider.TryCreateAsset(metadata, assetData); | 190 | BackendResponse storageResponse = server.StorageProvider.TryCreateAsset(metadata, assetData); |
191 | 191 | ||
192 | if (storageResponse == BackendResponse.Success) | 192 | if (storageResponse == BackendResponse.Success) |
193 | response.Status = HttpStatusCode.Created; | 193 | response.Status = HttpStatusCode.Created; |
194 | else if (storageResponse == BackendResponse.NotFound) | 194 | else if (storageResponse == BackendResponse.NotFound) |
195 | response.Status = HttpStatusCode.NotFound; | 195 | response.Status = HttpStatusCode.NotFound; |
196 | else | 196 | else |
197 | response.Status = HttpStatusCode.InternalServerError; | 197 | response.Status = HttpStatusCode.InternalServerError; |
198 | } | 198 | } |
199 | else | 199 | else |
200 | { | 200 | { |
201 | Logger.Log.Warn("AssetPostHandler called with no asset data"); | 201 | Logger.Log.Warn("AssetPostHandler called with no asset data"); |
202 | response.Status = HttpStatusCode.BadRequest; | 202 | response.Status = HttpStatusCode.BadRequest; |
203 | } | 203 | } |
204 | } | 204 | } |
205 | catch (Exception ex) | 205 | catch (Exception ex) |
206 | { | 206 | { |
207 | Logger.Log.Warn("Failed to parse POST data (expecting AssetBase): " + ex.Message); | 207 | Logger.Log.Warn("Failed to parse POST data (expecting AssetBase): " + ex.Message); |
208 | response.Status = HttpStatusCode.BadRequest; | 208 | response.Status = HttpStatusCode.BadRequest; |
209 | } | 209 | } |
210 | 210 | ||
211 | Logger.Log.Debug("Finished handling OpenSim asset upload, Status: " + response.Status.ToString()); | 211 | Logger.Log.Debug("Finished handling OpenSim asset upload, Status: " + response.Status.ToString()); |
212 | return true; | 212 | return true; |
213 | } | 213 | } |
214 | } | 214 | } |
215 | } | 215 | } |