diff options
Diffstat (limited to 'OpenSim/Services/HypergridService/HGAssetService.cs')
-rw-r--r-- | OpenSim/Services/HypergridService/HGAssetService.cs | 193 |
1 files changed, 193 insertions, 0 deletions
diff --git a/OpenSim/Services/HypergridService/HGAssetService.cs b/OpenSim/Services/HypergridService/HGAssetService.cs new file mode 100644 index 0000000..b83fb1e --- /dev/null +++ b/OpenSim/Services/HypergridService/HGAssetService.cs | |||
@@ -0,0 +1,193 @@ | |||
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 | using System; | ||
28 | using System.Collections.Generic; | ||
29 | using System.IO; | ||
30 | using System.Reflection; | ||
31 | using System.Xml; | ||
32 | |||
33 | using Nini.Config; | ||
34 | using log4net; | ||
35 | using OpenMetaverse; | ||
36 | |||
37 | using OpenSim.Framework; | ||
38 | using OpenSim.Framework.Serialization.External; | ||
39 | using OpenSim.Server.Base; | ||
40 | using OpenSim.Services.Interfaces; | ||
41 | using OpenSim.Services.AssetService; | ||
42 | |||
43 | namespace OpenSim.Services.HypergridService | ||
44 | { | ||
45 | /// <summary> | ||
46 | /// Hypergrid asset service. It serves the IAssetService interface, | ||
47 | /// but implements it in ways that are appropriate for inter-grid | ||
48 | /// asset exchanges. | ||
49 | /// </summary> | ||
50 | public class HGAssetService : OpenSim.Services.AssetService.AssetService, IAssetService | ||
51 | { | ||
52 | private static readonly ILog m_log = | ||
53 | LogManager.GetLogger( | ||
54 | MethodBase.GetCurrentMethod().DeclaringType); | ||
55 | |||
56 | private string m_HomeURL; | ||
57 | private IUserAccountService m_UserAccountService; | ||
58 | |||
59 | private UserAccountCache m_Cache; | ||
60 | |||
61 | private AssetPermissions m_AssetPerms; | ||
62 | |||
63 | public HGAssetService(IConfigSource config, string configName) : base(config, configName) | ||
64 | { | ||
65 | m_log.Debug("[HGAsset Service]: Starting"); | ||
66 | IConfig assetConfig = config.Configs[configName]; | ||
67 | if (assetConfig == null) | ||
68 | throw new Exception("No HGAssetService configuration"); | ||
69 | |||
70 | string userAccountsDll = assetConfig.GetString("UserAccountsService", string.Empty); | ||
71 | if (userAccountsDll == string.Empty) | ||
72 | throw new Exception("Please specify UserAccountsService in HGAssetService configuration"); | ||
73 | |||
74 | Object[] args = new Object[] { config }; | ||
75 | m_UserAccountService = ServerUtils.LoadPlugin<IUserAccountService>(userAccountsDll, args); | ||
76 | if (m_UserAccountService == null) | ||
77 | throw new Exception(String.Format("Unable to create UserAccountService from {0}", userAccountsDll)); | ||
78 | |||
79 | m_HomeURL = Util.GetConfigVarFromSections<string>(config, "HomeURI", | ||
80 | new string[] { "Startup", "Hypergrid", configName }, string.Empty); | ||
81 | if (m_HomeURL == string.Empty) | ||
82 | throw new Exception("[HGAssetService] No HomeURI specified"); | ||
83 | |||
84 | m_Cache = UserAccountCache.CreateUserAccountCache(m_UserAccountService); | ||
85 | |||
86 | // Permissions | ||
87 | m_AssetPerms = new AssetPermissions(assetConfig); | ||
88 | |||
89 | } | ||
90 | |||
91 | #region IAssetService overrides | ||
92 | public override AssetBase Get(string id) | ||
93 | { | ||
94 | AssetBase asset = base.Get(id); | ||
95 | |||
96 | if (asset == null) | ||
97 | return null; | ||
98 | |||
99 | if (!m_AssetPerms.AllowedExport(asset.Type)) | ||
100 | return null; | ||
101 | |||
102 | if (asset.Metadata.Type == (sbyte)AssetType.Object) | ||
103 | asset.Data = AdjustIdentifiers(asset.Data); | ||
104 | |||
105 | AdjustIdentifiers(asset.Metadata); | ||
106 | |||
107 | return asset; | ||
108 | } | ||
109 | |||
110 | public override AssetMetadata GetMetadata(string id) | ||
111 | { | ||
112 | AssetMetadata meta = base.GetMetadata(id); | ||
113 | |||
114 | if (meta == null) | ||
115 | return null; | ||
116 | |||
117 | AdjustIdentifiers(meta); | ||
118 | |||
119 | return meta; | ||
120 | } | ||
121 | |||
122 | public override byte[] GetData(string id) | ||
123 | { | ||
124 | AssetBase asset = Get(id); | ||
125 | |||
126 | if (asset == null) | ||
127 | return null; | ||
128 | |||
129 | if (!m_AssetPerms.AllowedExport(asset.Type)) | ||
130 | return null; | ||
131 | |||
132 | // Deal with bug introduced in Oct. 20 (1eb3e6cc43e2a7b4053bc1185c7c88e22356c5e8) | ||
133 | // Fix bad assets before sending them elsewhere | ||
134 | if (asset.Type == (int)AssetType.Object && asset.Data != null) | ||
135 | { | ||
136 | string xml = ExternalRepresentationUtils.SanitizeXml(Utils.BytesToString(asset.Data)); | ||
137 | asset.Data = Utils.StringToBytes(xml); | ||
138 | } | ||
139 | |||
140 | return asset.Data; | ||
141 | } | ||
142 | |||
143 | //public virtual bool Get(string id, Object sender, AssetRetrieved handler) | ||
144 | |||
145 | public override string Store(AssetBase asset) | ||
146 | { | ||
147 | if (!m_AssetPerms.AllowedImport(asset.Type)) | ||
148 | return string.Empty; | ||
149 | |||
150 | // Deal with bug introduced in Oct. 20 (1eb3e6cc43e2a7b4053bc1185c7c88e22356c5e8) | ||
151 | // Fix bad assets before storing on this server | ||
152 | if (asset.Type == (int)AssetType.Object && asset.Data != null) | ||
153 | { | ||
154 | string xml = ExternalRepresentationUtils.SanitizeXml(Utils.BytesToString(asset.Data)); | ||
155 | asset.Data = Utils.StringToBytes(xml); | ||
156 | } | ||
157 | |||
158 | return base.Store(asset); | ||
159 | } | ||
160 | |||
161 | public override bool Delete(string id) | ||
162 | { | ||
163 | // NOGO | ||
164 | return false; | ||
165 | } | ||
166 | |||
167 | #endregion | ||
168 | |||
169 | protected void AdjustIdentifiers(AssetMetadata meta) | ||
170 | { | ||
171 | if (meta == null || m_Cache == null) | ||
172 | return; | ||
173 | |||
174 | UserAccount creator = m_Cache.GetUser(meta.CreatorID); | ||
175 | if (creator != null) | ||
176 | meta.CreatorID = meta.CreatorID + ";" + m_HomeURL + "/" + creator.FirstName + " " + creator.LastName; | ||
177 | } | ||
178 | |||
179 | // Only for Object | ||
180 | protected byte[] AdjustIdentifiers(byte[] data) | ||
181 | { | ||
182 | string xml = Utils.BytesToString(data); | ||
183 | |||
184 | // Deal with bug introduced in Oct. 20 (1eb3e6cc43e2a7b4053bc1185c7c88e22356c5e8) | ||
185 | // Fix bad assets before sending them elsewhere | ||
186 | xml = ExternalRepresentationUtils.SanitizeXml(xml); | ||
187 | |||
188 | return Utils.StringToBytes(ExternalRepresentationUtils.RewriteSOP(xml, "HGAssetService", m_HomeURL, m_Cache, UUID.Zero)); | ||
189 | } | ||
190 | |||
191 | } | ||
192 | |||
193 | } | ||