aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Servers
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Servers')
-rw-r--r--OpenSim/Servers/Connectors/Asset/AssetServiceConnector.cs252
1 files changed, 252 insertions, 0 deletions
diff --git a/OpenSim/Servers/Connectors/Asset/AssetServiceConnector.cs b/OpenSim/Servers/Connectors/Asset/AssetServiceConnector.cs
new file mode 100644
index 0000000..efd5eea
--- /dev/null
+++ b/OpenSim/Servers/Connectors/Asset/AssetServiceConnector.cs
@@ -0,0 +1,252 @@
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 OpenSim 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 log4net;
29using System;
30using System.Collections.Generic;
31using System.IO;
32using System.Reflection;
33using Nini.Config;
34using OpenSim.Framework;
35using OpenSim.Framework.Communications;
36using OpenSim.Framework.Servers.HttpServer;
37using OpenSim.Services.Interfaces;
38
39namespace OpenSim.Servers.Connectors
40{
41 public class AssetServicesConnector : IAssetService
42 {
43 private static readonly ILog m_log =
44 LogManager.GetLogger(
45 MethodBase.GetCurrentMethod().DeclaringType);
46
47 private string m_ServerURI = String.Empty;
48 private IImprovedAssetCache m_Cache = null;
49
50 public AssetServicesConnector()
51 {
52 }
53
54 public AssetServicesConnector(IConfigSource source)
55 {
56 Initialise(source);
57 }
58
59 public virtual void Initialise(IConfigSource source)
60 {
61 IConfig assetConfig = source.Configs["AssetService"];
62 if (assetConfig == null)
63 {
64 m_log.Error("[ASSET CONNECTOR]: AssetService missing from OpanSim.ini");
65 throw new Exception("Asset connector init error");
66 }
67
68 string serviceURI = assetConfig.GetString("AssetServerURI",
69 String.Empty);
70
71 if (serviceURI == String.Empty)
72 {
73 m_log.Error("[ASSET CONNECTOR]: No Server URI named in section AssetService");
74 throw new Exception("Asset connector init error");
75 }
76 m_ServerURI = serviceURI;
77 }
78
79 protected void SetCache(IImprovedAssetCache cache)
80 {
81 m_Cache = cache;
82 }
83
84 public AssetBase Get(string id)
85 {
86 string uri = m_ServerURI + "/assets/" + id;
87
88 AssetBase asset = null;
89 if (m_Cache != null)
90 asset = m_Cache.Get(id);
91
92 if (asset == null)
93 {
94 asset = SynchronousRestObjectRequester.
95 MakeRequest<int, AssetBase>("GET", uri, 0);
96
97 if (m_Cache != null)
98 m_Cache.Cache(asset);
99 }
100 return asset;
101 }
102
103 public AssetMetadata GetMetadata(string id)
104 {
105 if (m_Cache != null)
106 {
107 AssetBase fullAsset = m_Cache.Get(id);
108
109 if (fullAsset != null)
110 return fullAsset.Metadata;
111 }
112
113 string uri = m_ServerURI + "/assets/" + id + "/metadata";
114
115 AssetMetadata asset = SynchronousRestObjectRequester.
116 MakeRequest<int, AssetMetadata>("GET", uri, 0);
117 return asset;
118 }
119
120 public byte[] GetData(string id)
121 {
122 if (m_Cache != null)
123 {
124 AssetBase fullAsset = m_Cache.Get(id);
125
126 if (fullAsset != null)
127 return fullAsset.Data;
128 }
129
130 RestClient rc = new RestClient(m_ServerURI);
131 rc.AddResourcePath("assets");
132 rc.AddResourcePath(id);
133 rc.AddResourcePath("data");
134
135 rc.RequestMethod = "GET";
136
137 Stream s = rc.Request();
138
139 if (s == null)
140 return null;
141
142 if (s.Length > 0)
143 {
144 byte[] ret = new byte[s.Length];
145 s.Read(ret, 0, (int)s.Length);
146
147 return ret;
148 }
149
150 return null;
151 }
152
153 public bool Get(string id, Object sender, AssetRetrieved handler)
154 {
155 string uri = m_ServerURI + "/assets/" + id;
156
157 AssetBase asset = null;
158 if (m_Cache != null)
159 asset = m_Cache.Get(id);
160
161 if (asset == null)
162 {
163 AsynchronousRestObjectRequester.
164 MakeRequest<int, AssetBase>("GET", uri, 0,
165 delegate(AssetBase a)
166 {
167 if (m_Cache != null)
168 m_Cache.Cache(a);
169 handler(id, sender, a);
170 });
171
172 }
173 else
174 {
175 handler(id, sender, asset);
176 }
177
178 return true;
179 }
180
181 public string Store(AssetBase asset)
182 {
183 if (asset.Temporary || asset.Local)
184 {
185 if (m_Cache != null)
186 m_Cache.Cache(asset);
187
188 return asset.ID;
189 }
190
191 string uri = m_ServerURI + "/assets/";
192
193 string newID = SynchronousRestObjectRequester.
194 MakeRequest<AssetBase, string>("POST", uri, asset);
195
196 if (newID != String.Empty)
197 {
198 asset.ID = newID;
199
200 if (m_Cache != null)
201 m_Cache.Cache(asset);
202 }
203 return newID;
204 }
205
206 public bool UpdateContent(string id, byte[] data)
207 {
208 AssetBase asset = null;
209
210 if (m_Cache != null)
211 asset = m_Cache.Get(id);
212
213 if (asset == null)
214 {
215 AssetMetadata metadata = GetMetadata(id);
216 if (metadata == null)
217 return false;
218
219 asset = new AssetBase();
220 asset.Metadata = metadata;
221 }
222 asset.Data = data;
223
224 string uri = m_ServerURI + "/assets/" + id;
225
226 if (SynchronousRestObjectRequester.
227 MakeRequest<AssetBase, bool>("POST", uri, asset))
228 {
229 if (m_Cache != null)
230 m_Cache.Cache(asset);
231
232 return true;
233 }
234 return false;
235 }
236
237 public bool Delete(string id)
238 {
239 string uri = m_ServerURI + "/assets/" + id;
240
241 if (SynchronousRestObjectRequester.
242 MakeRequest<int, bool>("DELETE", uri, 0))
243 {
244 if (m_Cache != null)
245 m_Cache.Expire(id);
246
247 return true;
248 }
249 return false;
250 }
251 }
252}