diff options
Diffstat (limited to '')
-rw-r--r-- | OpenSim/Framework/Servers/CachedGetAssetStreamHandler.cs | 217 | ||||
-rw-r--r-- | OpenSim/Region/CoreModules/Framework/Services/RegionAssetService.cs | 23 |
2 files changed, 235 insertions, 5 deletions
diff --git a/OpenSim/Framework/Servers/CachedGetAssetStreamHandler.cs b/OpenSim/Framework/Servers/CachedGetAssetStreamHandler.cs new file mode 100644 index 0000000..7a9030c --- /dev/null +++ b/OpenSim/Framework/Servers/CachedGetAssetStreamHandler.cs | |||
@@ -0,0 +1,217 @@ | |||
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 | |||
28 | using System; | ||
29 | using System.IO; | ||
30 | using System.Reflection; | ||
31 | using System.Text; | ||
32 | using System.Text.RegularExpressions; | ||
33 | using System.Xml; | ||
34 | using System.Xml.Serialization; | ||
35 | using log4net; | ||
36 | using OpenMetaverse; | ||
37 | using OpenSim.Data; | ||
38 | using OpenSim.Framework; | ||
39 | using OpenSim.Framework.Servers; | ||
40 | using OpenSim.Framework.Statistics; | ||
41 | using System.Net; | ||
42 | |||
43 | namespace OpenSim.Framework.Servers | ||
44 | { | ||
45 | public class CachedGetAssetStreamHandler : BaseStreamHandler | ||
46 | { | ||
47 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
48 | |||
49 | // private OpenAsset_Main m_assetManager; | ||
50 | private IAssetCache m_assetProvider; | ||
51 | |||
52 | /// <summary> | ||
53 | /// Constructor. | ||
54 | /// </summary> | ||
55 | /// <param name="assetManager"></param> | ||
56 | /// <param name="assetProvider"></param> | ||
57 | public CachedGetAssetStreamHandler(IAssetCache assetProvider) | ||
58 | : base("GET", "/assets") | ||
59 | { | ||
60 | m_log.Info("[REST]: In Get Request"); | ||
61 | // m_assetManager = assetManager; | ||
62 | m_assetProvider = assetProvider; | ||
63 | } | ||
64 | |||
65 | public override byte[] Handle(string path, Stream request, | ||
66 | OSHttpRequest httpRequest, OSHttpResponse httpResponse) | ||
67 | { | ||
68 | string param = GetParam(path); | ||
69 | byte[] result = new byte[] { }; | ||
70 | |||
71 | string[] p = param.Split(new char[] { '/', '?', '&' }, StringSplitOptions.RemoveEmptyEntries); | ||
72 | |||
73 | if (p.Length > 0) | ||
74 | { | ||
75 | UUID assetID = UUID.Zero; | ||
76 | |||
77 | if (!UUID.TryParse(p[0], out assetID)) | ||
78 | { | ||
79 | m_log.InfoFormat( | ||
80 | "[REST]: GET:/asset ignoring request with malformed UUID {0}", p[0]); | ||
81 | return result; | ||
82 | } | ||
83 | |||
84 | if (StatsManager.AssetStats != null) | ||
85 | StatsManager.AssetStats.AddRequest(); | ||
86 | |||
87 | AssetBase asset = m_assetProvider.GetAsset(assetID,true); // TODO IsTexture should be deduced from loaded asset. It is not used in this case. | ||
88 | |||
89 | if (asset != null) | ||
90 | { | ||
91 | // if (asset.ContainsReferences) | ||
92 | // { | ||
93 | // asset.Data = ProcessOutgoingAssetData(asset.Data); | ||
94 | // } | ||
95 | if (p.Length > 1 && p[1] == "data") | ||
96 | { | ||
97 | httpResponse.StatusCode = (int)HttpStatusCode.OK; | ||
98 | httpResponse.ContentType = SLAssetTypeToContentType(asset.Type); | ||
99 | result = asset.Data; | ||
100 | } | ||
101 | else | ||
102 | { | ||
103 | XmlSerializer xs = new XmlSerializer(typeof(AssetBase)); | ||
104 | MemoryStream ms = new MemoryStream(); | ||
105 | XmlTextWriter xw = new XmlTextWriter(ms, Encoding.UTF8); | ||
106 | xw.Formatting = Formatting.Indented; | ||
107 | xs.Serialize(xw, asset); | ||
108 | xw.Flush(); | ||
109 | |||
110 | ms.Seek(0, SeekOrigin.Begin); | ||
111 | //StreamReader sr = new StreamReader(ms); | ||
112 | |||
113 | result = ms.GetBuffer(); | ||
114 | |||
115 | Array.Resize<byte>(ref result, (int)ms.Length); | ||
116 | } | ||
117 | } | ||
118 | else | ||
119 | { | ||
120 | if (StatsManager.AssetStats != null) | ||
121 | StatsManager.AssetStats.AddNotFoundRequest(); | ||
122 | |||
123 | m_log.InfoFormat("[REST]: GET:/asset failed to find {0}", assetID); | ||
124 | } | ||
125 | } | ||
126 | |||
127 | return result; | ||
128 | } | ||
129 | |||
130 | // private byte[] ProcessOutgoingAssetData(byte[] assetData) | ||
131 | // { | ||
132 | // string data = Encoding.ASCII.GetString(assetData); | ||
133 | |||
134 | // data = ProcessAssetDataString(data); | ||
135 | |||
136 | // return Encoding.ASCII.GetBytes(data); | ||
137 | // } | ||
138 | |||
139 | public string ProcessAssetDataString(string data) | ||
140 | { | ||
141 | Regex regex = new Regex("(creator_id|owner_id)\\s+(\\S+)"); | ||
142 | |||
143 | // IUserService userService = null; | ||
144 | |||
145 | data = regex.Replace(data, delegate(Match m) | ||
146 | { | ||
147 | string result = String.Empty; | ||
148 | |||
149 | // string key = m.Groups[1].Captures[0].Value; | ||
150 | // | ||
151 | // string value = m.Groups[2].Captures[0].Value; | ||
152 | // | ||
153 | // Guid userUri; | ||
154 | // | ||
155 | // switch (key) | ||
156 | // { | ||
157 | // case "creator_id": | ||
158 | // userUri = new Guid(value); | ||
159 | // // result = "creator_url " + userService(userService, userUri); | ||
160 | // break; | ||
161 | // | ||
162 | // case "owner_id": | ||
163 | // userUri = new Guid(value); | ||
164 | // // result = "owner_url " + ResolveUserUri(userService, userUri); | ||
165 | // break; | ||
166 | // } | ||
167 | |||
168 | return result; | ||
169 | }); | ||
170 | |||
171 | return data; | ||
172 | } | ||
173 | |||
174 | private string SLAssetTypeToContentType(int assetType) | ||
175 | { | ||
176 | switch (assetType) | ||
177 | { | ||
178 | case 0: | ||
179 | return "image/jp2"; | ||
180 | case 1: | ||
181 | return "application/ogg"; | ||
182 | case 2: | ||
183 | return "application/x-metaverse-callingcard"; | ||
184 | case 3: | ||
185 | return "application/x-metaverse-landmark"; | ||
186 | case 5: | ||
187 | return "application/x-metaverse-clothing"; | ||
188 | case 6: | ||
189 | return "application/x-metaverse-primitive"; | ||
190 | case 7: | ||
191 | return "application/x-metaverse-notecard"; | ||
192 | case 8: | ||
193 | return "application/x-metaverse-folder"; | ||
194 | case 10: | ||
195 | return "application/x-metaverse-lsl"; | ||
196 | case 11: | ||
197 | return "application/x-metaverse-lso"; | ||
198 | case 12: | ||
199 | return "image/tga"; | ||
200 | case 13: | ||
201 | return "application/x-metaverse-bodypart"; | ||
202 | case 17: | ||
203 | return "audio/x-wav"; | ||
204 | case 19: | ||
205 | return "image/jpeg"; | ||
206 | case 20: | ||
207 | return "application/x-metaverse-animation"; | ||
208 | case 21: | ||
209 | return "application/x-metaverse-gesture"; | ||
210 | case 22: | ||
211 | return "application/x-metaverse-simstate"; | ||
212 | default: | ||
213 | return "application/octet-stream"; | ||
214 | } | ||
215 | } | ||
216 | } | ||
217 | } | ||
diff --git a/OpenSim/Region/CoreModules/Framework/Services/RegionAssetService.cs b/OpenSim/Region/CoreModules/Framework/Services/RegionAssetService.cs index 7fac319..dd0223a 100644 --- a/OpenSim/Region/CoreModules/Framework/Services/RegionAssetService.cs +++ b/OpenSim/Region/CoreModules/Framework/Services/RegionAssetService.cs | |||
@@ -45,7 +45,8 @@ namespace OpenSim.Region.CoreModules.Framework.Services | |||
45 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | 45 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); |
46 | private static bool initialized = false; | 46 | private static bool initialized = false; |
47 | private static bool enabled = false; | 47 | private static bool enabled = false; |
48 | 48 | ||
49 | private bool m_gridMode = false; | ||
49 | Scene m_scene; | 50 | Scene m_scene; |
50 | 51 | ||
51 | #region IRegionModule interface | 52 | #region IRegionModule interface |
@@ -58,9 +59,10 @@ namespace OpenSim.Region.CoreModules.Framework.Services | |||
58 | m_scene = scene; | 59 | m_scene = scene; |
59 | 60 | ||
60 | // This module is only on for standalones in hypergrid mode | 61 | // This module is only on for standalones in hypergrid mode |
61 | enabled = ((!config.Configs["Startup"].GetBoolean("gridmode", true)) && | 62 | enabled = ((!config.Configs["Startup"].GetBoolean("gridmode", true)) && |
62 | config.Configs["Startup"].GetBoolean("hypergrid", true)) || | 63 | config.Configs["Startup"].GetBoolean("hypergrid", true)) || |
63 | ((config.Configs["MXP"] != null) && config.Configs["MXP"].GetBoolean("Enabled", true)); | 64 | ((config.Configs["MXP"] != null) && config.Configs["MXP"].GetBoolean("Enabled", true)); |
65 | m_gridMode = config.Configs["Startup"].GetBoolean("gridmode", true); | ||
64 | } | 66 | } |
65 | } | 67 | } |
66 | 68 | ||
@@ -70,7 +72,7 @@ namespace OpenSim.Region.CoreModules.Framework.Services | |||
70 | { | 72 | { |
71 | m_log.Info("[RegionAssetService]: Starting..."); | 73 | m_log.Info("[RegionAssetService]: Starting..."); |
72 | 74 | ||
73 | new AssetService(m_scene); | 75 | new AssetService(m_scene,m_gridMode); |
74 | } | 76 | } |
75 | } | 77 | } |
76 | 78 | ||
@@ -96,6 +98,7 @@ namespace OpenSim.Region.CoreModules.Framework.Services | |||
96 | { | 98 | { |
97 | // private IUserService m_userService; | 99 | // private IUserService m_userService; |
98 | private bool m_doLookup = false; | 100 | private bool m_doLookup = false; |
101 | private bool m_gridMode = false; | ||
99 | 102 | ||
100 | public bool DoLookup | 103 | public bool DoLookup |
101 | { | 104 | { |
@@ -105,8 +108,9 @@ namespace OpenSim.Region.CoreModules.Framework.Services | |||
105 | // private static readonly ILog m_log | 108 | // private static readonly ILog m_log |
106 | // = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | 109 | // = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); |
107 | 110 | ||
108 | public AssetService(Scene m_scene) | 111 | public AssetService(Scene m_scene, bool gridMode) |
109 | { | 112 | { |
113 | m_gridMode = gridMode; | ||
110 | AddHttpHandlers(m_scene); | 114 | AddHttpHandlers(m_scene); |
111 | // m_userService = m_scene.CommsManager.UserService; | 115 | // m_userService = m_scene.CommsManager.UserService; |
112 | } | 116 | } |
@@ -117,7 +121,16 @@ namespace OpenSim.Region.CoreModules.Framework.Services | |||
117 | = ((AssetServerBase)m_scene.CommsManager.AssetCache.AssetServer).AssetProviderPlugin; | 121 | = ((AssetServerBase)m_scene.CommsManager.AssetCache.AssetServer).AssetProviderPlugin; |
118 | 122 | ||
119 | IHttpServer httpServer = m_scene.CommsManager.HttpServer; | 123 | IHttpServer httpServer = m_scene.CommsManager.HttpServer; |
120 | httpServer.AddStreamHandler(new GetAssetStreamHandler(m_assetProvider)); | 124 | |
125 | if (m_gridMode) | ||
126 | { | ||
127 | httpServer.AddStreamHandler(new CachedGetAssetStreamHandler(m_scene.CommsManager.AssetCache)); | ||
128 | } | ||
129 | else | ||
130 | { | ||
131 | httpServer.AddStreamHandler(new GetAssetStreamHandler(m_assetProvider)); | ||
132 | } | ||
133 | |||
121 | httpServer.AddStreamHandler(new PostAssetStreamHandler(m_assetProvider)); | 134 | httpServer.AddStreamHandler(new PostAssetStreamHandler(m_assetProvider)); |
122 | 135 | ||
123 | } | 136 | } |