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