aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Grid/AssetInventoryServer/Extensions/ReferenceFrontend.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Grid/AssetInventoryServer/Extensions/ReferenceFrontend.cs')
-rw-r--r--OpenSim/Grid/AssetInventoryServer/Extensions/ReferenceFrontend.cs239
1 files changed, 239 insertions, 0 deletions
diff --git a/OpenSim/Grid/AssetInventoryServer/Extensions/ReferenceFrontend.cs b/OpenSim/Grid/AssetInventoryServer/Extensions/ReferenceFrontend.cs
new file mode 100644
index 0000000..454984e
--- /dev/null
+++ b/OpenSim/Grid/AssetInventoryServer/Extensions/ReferenceFrontend.cs
@@ -0,0 +1,239 @@
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
30using System;
31using System.Collections.Generic;
32using System.Net;
33using System.Xml;
34using ExtensionLoader;
35using OpenMetaverse;
36using OpenMetaverse.StructuredData;
37using HttpServer;
38
39namespace OpenSim.Grid.AssetInventoryServer.Extensions
40{
41 public class ReferenceFrontend : IExtension<AssetInventoryServer>
42 {
43 AssetInventoryServer server;
44
45 public ReferenceFrontend()
46 {
47 }
48
49 public void Start(AssetInventoryServer server)
50 {
51 this.server = server;
52
53 // Asset metadata request
54 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",
55 MetadataRequestHandler);
56
57 // Asset data request
58 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",
59 DataRequestHandler);
60
61 // Asset creation
62 server.HttpServer.AddHandler("post", null, "^/createasset", CreateRequestHandler);
63 }
64
65 public void Stop()
66 {
67 }
68
69 bool MetadataRequestHandler(IHttpClientContext client, IHttpRequest request, IHttpResponse response)
70 {
71 UUID assetID;
72 // Split the URL up into an AssetID and a method
73 string[] rawUrl = request.Uri.PathAndQuery.Split('/');
74
75 if (rawUrl.Length >= 3 && UUID.TryParse(rawUrl[1], out assetID))
76 {
77 UUID authToken = Utils.GetAuthToken(request);
78
79 if (server.AuthorizationProvider.IsMetadataAuthorized(authToken, assetID))
80 {
81 Metadata metadata;
82 BackendResponse storageResponse = server.StorageProvider.TryFetchMetadata(assetID, out metadata);
83
84 if (storageResponse == BackendResponse.Success)
85 {
86 // If the asset data location wasn't specified in the metadata, specify it
87 // manually here by pointing back to this asset server
88 if (!metadata.Methods.ContainsKey("data"))
89 {
90 metadata.Methods["data"] = new Uri(String.Format("{0}://{1}/{2}/data",
91 request.Uri.Scheme, request.Uri.Authority, assetID));
92 }
93
94 byte[] serializedData = metadata.SerializeToBytes();
95
96 response.Status = HttpStatusCode.OK;
97 response.ContentType = "application/json";
98 response.ContentLength = serializedData.Length;
99 response.Body.Write(serializedData, 0, serializedData.Length);
100
101 }
102 else if (storageResponse == BackendResponse.NotFound)
103 {
104 Logger.Log.Warn("Could not find metadata for asset " + assetID.ToString());
105 response.Status = HttpStatusCode.NotFound;
106 }
107 else
108 {
109 response.Status = HttpStatusCode.InternalServerError;
110 }
111 }
112 else
113 {
114 response.Status = HttpStatusCode.Forbidden;
115 }
116
117 return true;
118 }
119
120 response.Status = HttpStatusCode.NotFound;
121 return true;
122 }
123
124 bool DataRequestHandler(IHttpClientContext client, IHttpRequest request, IHttpResponse response)
125 {
126 UUID assetID;
127 // Split the URL up into an AssetID and a method
128 string[] rawUrl = request.Uri.PathAndQuery.Split('/');
129
130 if (rawUrl.Length >= 3 && UUID.TryParse(rawUrl[1], out assetID))
131 {
132 UUID authToken = Utils.GetAuthToken(request);
133
134 if (server.AuthorizationProvider.IsDataAuthorized(authToken, assetID))
135 {
136 byte[] assetData;
137 BackendResponse storageResponse = server.StorageProvider.TryFetchData(assetID, out assetData);
138
139 if (storageResponse == BackendResponse.Success)
140 {
141 response.Status = HttpStatusCode.OK;
142 response.Status = HttpStatusCode.OK;
143 response.ContentType = "application/octet-stream";
144 response.AddHeader("Content-Disposition", "attachment; filename=" + assetID.ToString());
145 response.ContentLength = assetData.Length;
146 response.Body.Write(assetData, 0, assetData.Length);
147 }
148 else if (storageResponse == BackendResponse.NotFound)
149 {
150 response.Status = HttpStatusCode.NotFound;
151 }
152 else
153 {
154 response.Status = HttpStatusCode.InternalServerError;
155 }
156 }
157 else
158 {
159 response.Status = HttpStatusCode.Forbidden;
160 }
161
162 return true;
163 }
164
165 response.Status = HttpStatusCode.BadRequest;
166 return true;
167 }
168
169 bool CreateRequestHandler(IHttpClientContext client, IHttpRequest request, IHttpResponse response)
170 {
171 UUID authToken = Utils.GetAuthToken(request);
172
173 if (server.AuthorizationProvider.IsCreateAuthorized(authToken))
174 {
175 try
176 {
177 OSD osdata = OSDParser.DeserializeJson(request.Body);
178
179 if (osdata.Type == OSDType.Map)
180 {
181 OSDMap map = (OSDMap)osdata;
182 Metadata metadata = new Metadata();
183 metadata.Deserialize(map);
184
185 byte[] assetData = map["data"].AsBinary();
186
187 if (assetData != null && assetData.Length > 0)
188 {
189 BackendResponse storageResponse;
190
191 if (metadata.ID != UUID.Zero)
192 storageResponse = server.StorageProvider.TryCreateAsset(metadata, assetData);
193 else
194 storageResponse = server.StorageProvider.TryCreateAsset(metadata, assetData, out metadata.ID);
195
196 if (storageResponse == BackendResponse.Success)
197 {
198 response.Status = HttpStatusCode.Created;
199 OSDMap responseMap = new OSDMap(1);
200 responseMap["id"] = OSD.FromUUID(metadata.ID);
201 LitJson.JsonData jsonData = OSDParser.SerializeJson(responseMap);
202 byte[] responseData = System.Text.Encoding.UTF8.GetBytes(jsonData.ToJson());
203 response.Body.Write(responseData, 0, responseData.Length);
204 response.Body.Flush();
205 }
206 else if (storageResponse == BackendResponse.NotFound)
207 {
208 response.Status = HttpStatusCode.NotFound;
209 }
210 else
211 {
212 response.Status = HttpStatusCode.InternalServerError;
213 }
214 }
215 else
216 {
217 response.Status = HttpStatusCode.BadRequest;
218 }
219 }
220 else
221 {
222 response.Status = HttpStatusCode.BadRequest;
223 }
224 }
225 catch (Exception ex)
226 {
227 response.Status = HttpStatusCode.InternalServerError;
228 response.Reason = ex.Message;
229 }
230 }
231 else
232 {
233 response.Status = HttpStatusCode.Forbidden;
234 }
235
236 return true;
237 }
238 }
239}