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