diff options
Diffstat (limited to 'OpenSim/Grid/AssetInventoryServer/Plugins/ReferenceFrontendPlugin.cs')
-rw-r--r-- | OpenSim/Grid/AssetInventoryServer/Plugins/ReferenceFrontendPlugin.cs | 265 |
1 files changed, 265 insertions, 0 deletions
diff --git a/OpenSim/Grid/AssetInventoryServer/Plugins/ReferenceFrontendPlugin.cs b/OpenSim/Grid/AssetInventoryServer/Plugins/ReferenceFrontendPlugin.cs new file mode 100644 index 0000000..fddc385 --- /dev/null +++ b/OpenSim/Grid/AssetInventoryServer/Plugins/ReferenceFrontendPlugin.cs | |||
@@ -0,0 +1,265 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2008 Intel Corporation | ||
3 | * All rights reserved. | ||
4 | * Redistribution and use in source and binary forms, with or without | ||
5 | * modification, are permitted provided that the following conditions | ||
6 | * are met: | ||
7 | * | ||
8 | * -- Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * -- Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
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 | ||
15 | * this software without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
18 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
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 | ||
21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||
25 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
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. | ||
28 | */ | ||
29 | |||
30 | using System; | ||
31 | using System.Collections.Generic; | ||
32 | using System.Net; | ||
33 | using System.Xml; | ||
34 | using OpenMetaverse; | ||
35 | using OpenMetaverse.StructuredData; | ||
36 | using HttpServer; | ||
37 | using OpenSim.Framework; | ||
38 | |||
39 | namespace OpenSim.Grid.AssetInventoryServer.Plugins | ||
40 | { | ||
41 | public class ReferenceFrontendPlugin : IAssetInventoryServerPlugin | ||
42 | { | ||
43 | AssetInventoryServer server; | ||
44 | |||
45 | public ReferenceFrontendPlugin() | ||
46 | { | ||
47 | } | ||
48 | |||
49 | #region IPlugin implementation | ||
50 | |||
51 | public void Initialise(AssetInventoryServer server) | ||
52 | { | ||
53 | this.server = server; | ||
54 | |||
55 | // Asset metadata request | ||
56 | 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", | ||
57 | MetadataRequestHandler); | ||
58 | |||
59 | // Asset data request | ||
60 | 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", | ||
61 | DataRequestHandler); | ||
62 | |||
63 | // Asset creation | ||
64 | server.HttpServer.AddHandler("post", null, "^/createasset", CreateRequestHandler); | ||
65 | |||
66 | Logger.Log.Info("[ASSET] Reference Frontend loaded."); | ||
67 | } | ||
68 | |||
69 | /// <summary> | ||
70 | /// <para>Initialises asset interface</para> | ||
71 | /// </summary> | ||
72 | public void Initialise() | ||
73 | { | ||
74 | Logger.Log.InfoFormat("[ASSET]: {0} cannot be default-initialized!", Name); | ||
75 | throw new PluginNotInitialisedException(Name); | ||
76 | } | ||
77 | |||
78 | public void Dispose() | ||
79 | { | ||
80 | } | ||
81 | |||
82 | public string Version | ||
83 | { | ||
84 | // TODO: this should be something meaningful and not hardcoded? | ||
85 | get { return "0.1"; } | ||
86 | } | ||
87 | |||
88 | public string Name | ||
89 | { | ||
90 | get { return "AssetInventoryServer Reference asset frontend"; } | ||
91 | } | ||
92 | |||
93 | #endregion IPlugin implementation | ||
94 | |||
95 | bool MetadataRequestHandler(IHttpClientContext client, IHttpRequest request, IHttpResponse response) | ||
96 | { | ||
97 | UUID assetID; | ||
98 | // Split the URL up into an AssetID and a method | ||
99 | string[] rawUrl = request.Uri.PathAndQuery.Split('/'); | ||
100 | |||
101 | if (rawUrl.Length >= 3 && UUID.TryParse(rawUrl[1], out assetID)) | ||
102 | { | ||
103 | UUID authToken = Utils.GetAuthToken(request); | ||
104 | |||
105 | if (server.AuthorizationProvider.IsMetadataAuthorized(authToken, assetID)) | ||
106 | { | ||
107 | Metadata metadata; | ||
108 | BackendResponse storageResponse = server.StorageProvider.TryFetchMetadata(assetID, out metadata); | ||
109 | |||
110 | if (storageResponse == BackendResponse.Success) | ||
111 | { | ||
112 | // If the asset data location wasn't specified in the metadata, specify it | ||
113 | // manually here by pointing back to this asset server | ||
114 | if (!metadata.Methods.ContainsKey("data")) | ||
115 | { | ||
116 | metadata.Methods["data"] = new Uri(String.Format("{0}://{1}/{2}/data", | ||
117 | request.Uri.Scheme, request.Uri.Authority, assetID)); | ||
118 | } | ||
119 | |||
120 | byte[] serializedData = metadata.SerializeToBytes(); | ||
121 | |||
122 | response.Status = HttpStatusCode.OK; | ||
123 | response.ContentType = "application/json"; | ||
124 | response.ContentLength = serializedData.Length; | ||
125 | response.Body.Write(serializedData, 0, serializedData.Length); | ||
126 | |||
127 | } | ||
128 | else if (storageResponse == BackendResponse.NotFound) | ||
129 | { | ||
130 | Logger.Log.Warn("Could not find metadata for asset " + assetID.ToString()); | ||
131 | response.Status = HttpStatusCode.NotFound; | ||
132 | } | ||
133 | else | ||
134 | { | ||
135 | response.Status = HttpStatusCode.InternalServerError; | ||
136 | } | ||
137 | } | ||
138 | else | ||
139 | { | ||
140 | response.Status = HttpStatusCode.Forbidden; | ||
141 | } | ||
142 | |||
143 | return true; | ||
144 | } | ||
145 | |||
146 | response.Status = HttpStatusCode.NotFound; | ||
147 | return true; | ||
148 | } | ||
149 | |||
150 | bool DataRequestHandler(IHttpClientContext client, IHttpRequest request, IHttpResponse response) | ||
151 | { | ||
152 | UUID assetID; | ||
153 | // Split the URL up into an AssetID and a method | ||
154 | string[] rawUrl = request.Uri.PathAndQuery.Split('/'); | ||
155 | |||
156 | if (rawUrl.Length >= 3 && UUID.TryParse(rawUrl[1], out assetID)) | ||
157 | { | ||
158 | UUID authToken = Utils.GetAuthToken(request); | ||
159 | |||
160 | if (server.AuthorizationProvider.IsDataAuthorized(authToken, assetID)) | ||
161 | { | ||
162 | byte[] assetData; | ||
163 | BackendResponse storageResponse = server.StorageProvider.TryFetchData(assetID, out assetData); | ||
164 | |||
165 | if (storageResponse == BackendResponse.Success) | ||
166 | { | ||
167 | response.Status = HttpStatusCode.OK; | ||
168 | response.Status = HttpStatusCode.OK; | ||
169 | response.ContentType = "application/octet-stream"; | ||
170 | response.AddHeader("Content-Disposition", "attachment; filename=" + assetID.ToString()); | ||
171 | response.ContentLength = assetData.Length; | ||
172 | response.Body.Write(assetData, 0, assetData.Length); | ||
173 | } | ||
174 | else if (storageResponse == BackendResponse.NotFound) | ||
175 | { | ||
176 | response.Status = HttpStatusCode.NotFound; | ||
177 | } | ||
178 | else | ||
179 | { | ||
180 | response.Status = HttpStatusCode.InternalServerError; | ||
181 | } | ||
182 | } | ||
183 | else | ||
184 | { | ||
185 | response.Status = HttpStatusCode.Forbidden; | ||
186 | } | ||
187 | |||
188 | return true; | ||
189 | } | ||
190 | |||
191 | response.Status = HttpStatusCode.BadRequest; | ||
192 | return true; | ||
193 | } | ||
194 | |||
195 | bool CreateRequestHandler(IHttpClientContext client, IHttpRequest request, IHttpResponse response) | ||
196 | { | ||
197 | UUID authToken = Utils.GetAuthToken(request); | ||
198 | |||
199 | if (server.AuthorizationProvider.IsCreateAuthorized(authToken)) | ||
200 | { | ||
201 | try | ||
202 | { | ||
203 | OSD osdata = OSDParser.DeserializeJson(request.Body); | ||
204 | |||
205 | if (osdata.Type == OSDType.Map) | ||
206 | { | ||
207 | OSDMap map = (OSDMap)osdata; | ||
208 | Metadata metadata = new Metadata(); | ||
209 | metadata.Deserialize(map); | ||
210 | |||
211 | byte[] assetData = map["data"].AsBinary(); | ||
212 | |||
213 | if (assetData != null && assetData.Length > 0) | ||
214 | { | ||
215 | BackendResponse storageResponse; | ||
216 | |||
217 | if (metadata.ID != UUID.Zero) | ||
218 | storageResponse = server.StorageProvider.TryCreateAsset(metadata, assetData); | ||
219 | else | ||
220 | storageResponse = server.StorageProvider.TryCreateAsset(metadata, assetData, out metadata.ID); | ||
221 | |||
222 | if (storageResponse == BackendResponse.Success) | ||
223 | { | ||
224 | response.Status = HttpStatusCode.Created; | ||
225 | OSDMap responseMap = new OSDMap(1); | ||
226 | responseMap["id"] = OSD.FromUUID(metadata.ID); | ||
227 | LitJson.JsonData jsonData = OSDParser.SerializeJson(responseMap); | ||
228 | byte[] responseData = System.Text.Encoding.UTF8.GetBytes(jsonData.ToJson()); | ||
229 | response.Body.Write(responseData, 0, responseData.Length); | ||
230 | response.Body.Flush(); | ||
231 | } | ||
232 | else if (storageResponse == BackendResponse.NotFound) | ||
233 | { | ||
234 | response.Status = HttpStatusCode.NotFound; | ||
235 | } | ||
236 | else | ||
237 | { | ||
238 | response.Status = HttpStatusCode.InternalServerError; | ||
239 | } | ||
240 | } | ||
241 | else | ||
242 | { | ||
243 | response.Status = HttpStatusCode.BadRequest; | ||
244 | } | ||
245 | } | ||
246 | else | ||
247 | { | ||
248 | response.Status = HttpStatusCode.BadRequest; | ||
249 | } | ||
250 | } | ||
251 | catch (Exception ex) | ||
252 | { | ||
253 | response.Status = HttpStatusCode.InternalServerError; | ||
254 | response.Reason = ex.Message; | ||
255 | } | ||
256 | } | ||
257 | else | ||
258 | { | ||
259 | response.Status = HttpStatusCode.Forbidden; | ||
260 | } | ||
261 | |||
262 | return true; | ||
263 | } | ||
264 | } | ||
265 | } | ||