aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Grid/AssetInventoryServer/Plugins/OpenSim/OpenSimAssetFrontendPlugin.cs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--OpenSim/Grid/AssetInventoryServer/Plugins/OpenSim/OpenSimAssetFrontendPlugin.cs198
1 files changed, 0 insertions, 198 deletions
diff --git a/OpenSim/Grid/AssetInventoryServer/Plugins/OpenSim/OpenSimAssetFrontendPlugin.cs b/OpenSim/Grid/AssetInventoryServer/Plugins/OpenSim/OpenSimAssetFrontendPlugin.cs
deleted file mode 100644
index e4be55e..0000000
--- a/OpenSim/Grid/AssetInventoryServer/Plugins/OpenSim/OpenSimAssetFrontendPlugin.cs
+++ /dev/null
@@ -1,198 +0,0 @@
1/*
2 * Copyright (c) Contributors, http://opensimulator.org/
3 * See CONTRIBUTORS.TXT for a full list of copyright holders.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the OpenSimulator Project nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28using System;
29using System.Reflection;
30using System.Net;
31using System.IO;
32using System.Text;
33using System.Xml;
34using System.Xml.Serialization;
35using OpenMetaverse;
36using OpenSim.Framework;
37using OpenSim.Framework.Servers;
38using OpenSim.Framework.Servers.HttpServer;
39using log4net;
40
41namespace OpenSim.Grid.AssetInventoryServer.Plugins.OpenSim
42{
43 public class OpenSimAssetFrontendPlugin : IAssetInventoryServerPlugin
44 {
45 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
46 private AssetInventoryServer m_server;
47
48 public OpenSimAssetFrontendPlugin()
49 {
50 }
51
52 #region IPlugin implementation
53
54 public void Initialise(AssetInventoryServer server)
55 {
56 m_server = server;
57
58 // Asset request
59 m_server.HttpServer.AddStreamHandler(new AssetRequestHandler(server));
60
61 // Asset creation
62 m_server.HttpServer.AddStreamHandler(new AssetPostHandler(server));
63
64 m_log.Info("[OPENSIMASSETFRONTEND]: OpenSim Asset Frontend loaded.");
65 }
66
67 /// <summary>
68 /// <para>Initialises asset interface</para>
69 /// </summary>
70 public void Initialise()
71 {
72 m_log.InfoFormat("[OPENSIMASSETFRONTEND]: {0} cannot be default-initialized!", Name);
73 throw new PluginNotInitialisedException(Name);
74 }
75
76 public void Dispose()
77 {
78 }
79
80 public string Version
81 {
82 // TODO: this should be something meaningful and not hardcoded?
83 get { return "0.1"; }
84 }
85
86 public string Name
87 {
88 get { return "OpenSimAssetFrontend"; }
89 }
90
91 #endregion IPlugin implementation
92
93 public class AssetRequestHandler : BaseStreamHandler
94 {
95 AssetInventoryServer m_server;
96
97 //public AssetRequestHandler(AssetInventoryServer server) : base("GET", "^/assets")
98 public AssetRequestHandler(AssetInventoryServer server) : base("GET", "/assets")
99 {
100 m_server = server;
101 }
102
103 public override byte[] Handle(string path, Stream request, OSHttpRequest httpRequest, OSHttpResponse httpResponse)
104 {
105 byte[] buffer = new byte[] {};
106 UUID assetID;
107 // Split the URL up to get the asset ID out
108 string[] rawUrl = httpRequest.Url.PathAndQuery.Split('/');
109
110 if (rawUrl.Length >= 3 && rawUrl[2].Length >= 36 && UUID.TryParse(rawUrl[2].Substring(0, 36), out assetID))
111 {
112 BackendResponse dataResponse;
113
114 AssetBase asset = new AssetBase();
115 if ((dataResponse = m_server.StorageProvider.TryFetchDataMetadata(assetID, out asset)) == BackendResponse.Success)
116 {
117 if (rawUrl.Length >= 4 && rawUrl[3] == "data")
118 {
119 httpResponse.StatusCode = (int)HttpStatusCode.OK;
120 httpResponse.ContentType = Utils.SLAssetTypeToContentType(asset.Type);
121 buffer=asset.Data;
122 }
123 else
124 {
125 XmlSerializer xs = new XmlSerializer(typeof(AssetBase));
126 MemoryStream ms = new MemoryStream();
127 XmlTextWriter xw = new XmlTextWriter(ms, Encoding.UTF8);
128 xs.Serialize(xw, asset);
129 xw.Flush();
130
131 ms.Seek(0, SeekOrigin.Begin);
132 buffer = ms.GetBuffer();
133 Array.Resize<byte>(ref buffer, (int)ms.Length);
134 ms.Close();
135 httpResponse.StatusCode = (int)HttpStatusCode.OK;
136 }
137 }
138 else
139 {
140 m_log.WarnFormat("[OPENSIMASSETFRONTEND]: Failed to fetch asset data or metadata for {0}: {1}", assetID, dataResponse);
141 httpResponse.StatusCode = (int) HttpStatusCode.NotFound;
142 }
143 }
144 else
145 {
146 m_log.Warn("[OPENSIMASSETFRONTEND]: Unrecognized OpenSim asset request: " + httpRequest.Url.PathAndQuery);
147 }
148
149 return buffer;
150 }
151 }
152
153 public class AssetPostHandler : BaseStreamHandler
154 {
155 AssetInventoryServer m_server;
156
157 //public AssetPostHandler(AssetInventoryServer server) : base("POST", "/^assets")
158 public AssetPostHandler(AssetInventoryServer server) : base("POST", "/assets")
159 {
160 m_server = server;
161 }
162
163 public override byte[] Handle(string path, Stream request, OSHttpRequest httpRequest, OSHttpResponse httpResponse)
164 {
165 AssetBase asset = null;
166
167 try
168 {
169 asset = (AssetBase) new XmlSerializer(typeof (AssetBase)).Deserialize(httpRequest.InputStream);
170 }
171 catch (Exception ex)
172 {
173 m_log.Warn("[OPENSIMASSETFRONTEND]: Failed to parse POST data (expecting AssetBase): " + ex.Message);
174 httpResponse.StatusCode = (int) HttpStatusCode.BadRequest;
175 }
176
177 if (asset != null && asset.Data != null && asset.Data.Length > 0)
178 {
179 BackendResponse storageResponse = m_server.StorageProvider.TryCreateAsset(asset);
180
181 if (storageResponse == BackendResponse.Success)
182 httpResponse.StatusCode = (int) HttpStatusCode.Created;
183 else if (storageResponse == BackendResponse.NotFound)
184 httpResponse.StatusCode = (int) HttpStatusCode.NotFound;
185 else
186 httpResponse.StatusCode = (int) HttpStatusCode.InternalServerError;
187 }
188 else
189 {
190 m_log.Warn("[OPENSIMASSETFRONTEND]: AssetPostHandler called with no asset data");
191 httpResponse.StatusCode = (int) HttpStatusCode.BadRequest;
192 }
193
194 return new byte[] {};
195 }
196 }
197 }
198}