diff options
* Extracted common superclass for GetAssetStreamHandler and CachedGetAssetStreamHandler
* Added some more tests
Diffstat (limited to 'OpenSim/Framework/Servers/BaseGetAssetStreamHandler.cs')
-rw-r--r-- | OpenSim/Framework/Servers/BaseGetAssetStreamHandler.cs | 196 |
1 files changed, 196 insertions, 0 deletions
diff --git a/OpenSim/Framework/Servers/BaseGetAssetStreamHandler.cs b/OpenSim/Framework/Servers/BaseGetAssetStreamHandler.cs new file mode 100644 index 0000000..6e03ea8 --- /dev/null +++ b/OpenSim/Framework/Servers/BaseGetAssetStreamHandler.cs | |||
@@ -0,0 +1,196 @@ | |||
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.Collections.Generic; | ||
30 | using System.IO; | ||
31 | using System.Net; | ||
32 | using System.Reflection; | ||
33 | using System.Text; | ||
34 | using System.Text.RegularExpressions; | ||
35 | using System.Xml; | ||
36 | using System.Xml.Serialization; | ||
37 | using log4net; | ||
38 | using OpenMetaverse; | ||
39 | using OpenSim.Framework.Servers.HttpServer; | ||
40 | using OpenSim.Framework.Statistics; | ||
41 | |||
42 | namespace OpenSim.Framework.Servers | ||
43 | { | ||
44 | public abstract class BaseGetAssetStreamHandler : BaseStreamHandler | ||
45 | { | ||
46 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
47 | |||
48 | protected BaseGetAssetStreamHandler(string httpMethod, string path) : base(httpMethod, path) | ||
49 | { | ||
50 | } | ||
51 | |||
52 | protected abstract AssetBase GetAsset(UUID assetID); | ||
53 | |||
54 | public override byte[] Handle(string path, Stream request, | ||
55 | OSHttpRequest httpRequest, OSHttpResponse httpResponse) | ||
56 | { | ||
57 | byte[] result = new byte[] { }; | ||
58 | |||
59 | string[] p = SplitParams(path); | ||
60 | |||
61 | if (p.Length > 0) | ||
62 | { | ||
63 | UUID assetID = UUID.Zero; | ||
64 | |||
65 | if (!UUID.TryParse(p[0], out assetID)) | ||
66 | { | ||
67 | m_log.InfoFormat( | ||
68 | "[REST]: GET:/asset ignoring request with malformed UUID {0}", p[0]); | ||
69 | return result; | ||
70 | } | ||
71 | |||
72 | if (StatsManager.AssetStats != null) | ||
73 | StatsManager.AssetStats.AddRequest(); | ||
74 | |||
75 | AssetBase asset = GetAsset(assetID); | ||
76 | |||
77 | if (asset != null) | ||
78 | { | ||
79 | // if (asset.ContainsReferences) | ||
80 | // { | ||
81 | // asset.Data = ProcessOutgoingAssetData(asset.Data); | ||
82 | // } | ||
83 | if (p.Length > 1 && p[1] == "data") | ||
84 | { | ||
85 | httpResponse.StatusCode = (int)HttpStatusCode.OK; | ||
86 | httpResponse.ContentType = SLAssetTypeToContentType(asset.Type); | ||
87 | result = asset.Data; | ||
88 | } | ||
89 | else | ||
90 | { | ||
91 | XmlSerializer xs = new XmlSerializer(typeof(AssetBase)); | ||
92 | MemoryStream ms = new MemoryStream(); | ||
93 | XmlTextWriter xw = new XmlTextWriter(ms, Encoding.UTF8); | ||
94 | xw.Formatting = Formatting.Indented; | ||
95 | xs.Serialize(xw, asset); | ||
96 | xw.Flush(); | ||
97 | |||
98 | ms.Seek(0, SeekOrigin.Begin); | ||
99 | //StreamReader sr = new StreamReader(ms); | ||
100 | |||
101 | result = ms.GetBuffer(); | ||
102 | |||
103 | Array.Resize<byte>(ref result, (int)ms.Length); | ||
104 | } | ||
105 | } | ||
106 | else | ||
107 | { | ||
108 | if (StatsManager.AssetStats != null) | ||
109 | StatsManager.AssetStats.AddNotFoundRequest(); | ||
110 | |||
111 | m_log.InfoFormat("[REST]: GET:/asset failed to find {0}", assetID); | ||
112 | } | ||
113 | } | ||
114 | |||
115 | return result; | ||
116 | } | ||
117 | |||
118 | public string ProcessAssetDataString(string data) | ||
119 | { | ||
120 | Regex regex = new Regex("(creator_id|owner_id)\\s+(\\S+)"); | ||
121 | |||
122 | // IUserService userService = null; | ||
123 | |||
124 | data = regex.Replace(data, delegate(Match m) | ||
125 | { | ||
126 | string result = String.Empty; | ||
127 | |||
128 | // string key = m.Groups[1].Captures[0].Value; | ||
129 | // | ||
130 | // string value = m.Groups[2].Captures[0].Value; | ||
131 | // | ||
132 | // Guid userUri; | ||
133 | // | ||
134 | // switch (key) | ||
135 | // { | ||
136 | // case "creator_id": | ||
137 | // userUri = new Guid(value); | ||
138 | // // result = "creator_url " + userService(userService, userUri); | ||
139 | // break; | ||
140 | // | ||
141 | // case "owner_id": | ||
142 | // userUri = new Guid(value); | ||
143 | // // result = "owner_url " + ResolveUserUri(userService, userUri); | ||
144 | // break; | ||
145 | // } | ||
146 | |||
147 | return result; | ||
148 | }); | ||
149 | |||
150 | return data; | ||
151 | } | ||
152 | |||
153 | private string SLAssetTypeToContentType(int assetType) | ||
154 | { | ||
155 | switch (assetType) | ||
156 | { | ||
157 | case 0: | ||
158 | return "image/jp2"; | ||
159 | case 1: | ||
160 | return "application/ogg"; | ||
161 | case 2: | ||
162 | return "application/x-metaverse-callingcard"; | ||
163 | case 3: | ||
164 | return "application/x-metaverse-landmark"; | ||
165 | case 5: | ||
166 | return "application/x-metaverse-clothing"; | ||
167 | case 6: | ||
168 | return "application/x-metaverse-primitive"; | ||
169 | case 7: | ||
170 | return "application/x-metaverse-notecard"; | ||
171 | case 8: | ||
172 | return "application/x-metaverse-folder"; | ||
173 | case 10: | ||
174 | return "application/x-metaverse-lsl"; | ||
175 | case 11: | ||
176 | return "application/x-metaverse-lso"; | ||
177 | case 12: | ||
178 | return "image/tga"; | ||
179 | case 13: | ||
180 | return "application/x-metaverse-bodypart"; | ||
181 | case 17: | ||
182 | return "audio/x-wav"; | ||
183 | case 19: | ||
184 | return "image/jpeg"; | ||
185 | case 20: | ||
186 | return "application/x-metaverse-animation"; | ||
187 | case 21: | ||
188 | return "application/x-metaverse-gesture"; | ||
189 | case 22: | ||
190 | return "application/x-metaverse-simstate"; | ||
191 | default: | ||
192 | return "application/octet-stream"; | ||
193 | } | ||
194 | } | ||
195 | } | ||
196 | } | ||