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