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