aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
-rw-r--r--OpenSim/Capabilities/Handlers/CapsServerConnector.cs73
-rw-r--r--OpenSim/Capabilities/Handlers/GetTextureHandler.cs356
-rw-r--r--OpenSim/Region/ClientStack/Linden/Caps/GetTextureModule.cs315
-rw-r--r--prebuild.xml46
4 files changed, 476 insertions, 314 deletions
diff --git a/OpenSim/Capabilities/Handlers/CapsServerConnector.cs b/OpenSim/Capabilities/Handlers/CapsServerConnector.cs
new file mode 100644
index 0000000..561d767
--- /dev/null
+++ b/OpenSim/Capabilities/Handlers/CapsServerConnector.cs
@@ -0,0 +1,73 @@
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
28using System;
29using Nini.Config;
30using OpenSim.Server.Base;
31using OpenSim.Services.Interfaces;
32using OpenSim.Framework.Servers.HttpServer;
33using OpenSim.Server.Handlers.Base;
34using OpenMetaverse;
35using Caps = OpenSim.Framework.Capabilities.Caps;
36
37namespace OpenSim.Capabilities.Handlers
38{
39 public class CapsServerConnector : ServiceConnector
40 {
41 private IAssetService m_AssetService;
42 private string m_ConfigName = "CapsService";
43
44 public CapsServerConnector(IConfigSource config, IHttpServer server, string configName) :
45 base(config, server, configName)
46 {
47 if (configName != String.Empty)
48 m_ConfigName = configName;
49
50 IConfig serverConfig = config.Configs[m_ConfigName];
51 if (serverConfig == null)
52 throw new Exception(String.Format("No section '{0}' in config file", m_ConfigName));
53
54 string assetService = serverConfig.GetString("LocalServiceModule",
55 String.Empty);
56
57 if (assetService == String.Empty)
58 throw new Exception("No LocalServiceModule in config file");
59
60 Object[] args = new Object[] { config };
61 m_AssetService =
62 ServerUtils.LoadPlugin<IAssetService>(assetService, args);
63
64 if (m_AssetService == null)
65 throw new Exception(String.Format("Failed to load AssetService from {0}; config is {1}", assetService, m_ConfigName));
66
67 bool allowDelete = serverConfig.GetBoolean("AllowRemoteDelete", false);
68
69 server.AddStreamHandler(new GetTextureHandler("/CAPS/" + UUID.Random() + "/", m_AssetService));
70 }
71
72 }
73}
diff --git a/OpenSim/Capabilities/Handlers/GetTextureHandler.cs b/OpenSim/Capabilities/Handlers/GetTextureHandler.cs
new file mode 100644
index 0000000..00186ee
--- /dev/null
+++ b/OpenSim/Capabilities/Handlers/GetTextureHandler.cs
@@ -0,0 +1,356 @@
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
28using System;
29using System.Collections;
30using System.Collections.Specialized;
31using System.Drawing;
32using System.Drawing.Imaging;
33using System.Reflection;
34using System.IO;
35using System.Web;
36using log4net;
37using Nini.Config;
38using OpenMetaverse;
39using OpenMetaverse.StructuredData;
40using OpenMetaverse.Imaging;
41using OpenSim.Framework;
42using OpenSim.Framework.Servers;
43using OpenSim.Framework.Servers.HttpServer;
44using OpenSim.Region.Framework.Interfaces;
45using OpenSim.Services.Interfaces;
46using Caps = OpenSim.Framework.Capabilities.Caps;
47
48namespace OpenSim.Capabilities.Handlers
49{
50
51 public class GetTextureHandler : BaseStreamHandler
52 {
53 private static readonly ILog m_log =
54 LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
55 private IAssetService m_assetService;
56
57 public const string DefaultFormat = "x-j2c";
58
59 // TODO: Change this to a config option
60 const string REDIRECT_URL = null;
61
62 public GetTextureHandler(string path, IAssetService assService) :
63 base("GET", path)
64 {
65 m_assetService = assService;
66 }
67
68 public override byte[] Handle(string path, Stream request, OSHttpRequest httpRequest, OSHttpResponse httpResponse)
69 {
70 //m_log.DebugFormat("[GETTEXTURE]: called in {0}", m_scene.RegionInfo.RegionName);
71
72 // Try to parse the texture ID from the request URL
73 NameValueCollection query = HttpUtility.ParseQueryString(httpRequest.Url.Query);
74 string textureStr = query.GetOne("texture_id");
75 string format = query.GetOne("format");
76
77 if (m_assetService == null)
78 {
79 m_log.Error("[GETTEXTURE]: Cannot fetch texture " + textureStr + " without an asset service");
80 httpResponse.StatusCode = (int)System.Net.HttpStatusCode.NotFound;
81 return null;
82 }
83
84 UUID textureID;
85 if (!String.IsNullOrEmpty(textureStr) && UUID.TryParse(textureStr, out textureID))
86 {
87 string[] formats;
88 if (format != null && format != string.Empty)
89 {
90 formats = new string[1] { format.ToLower() };
91 }
92 else
93 {
94 formats = WebUtil.GetPreferredImageTypes(httpRequest.Headers.Get("Accept"));
95 if (formats.Length == 0)
96 formats = new string[1] { DefaultFormat }; // default
97
98 }
99 // OK, we have an array with preferred formats, possibly with only one entry
100
101 httpResponse.StatusCode = (int)System.Net.HttpStatusCode.NotFound;
102 foreach (string f in formats)
103 {
104 if (FetchTexture(httpRequest, httpResponse, textureID, f))
105 break;
106 }
107
108 }
109 else
110 {
111 m_log.Warn("[GETTEXTURE]: Failed to parse a texture_id from GetTexture request: " + httpRequest.Url);
112 }
113
114 httpResponse.Send();
115 return null;
116 }
117
118 /// <summary>
119 ///
120 /// </summary>
121 /// <param name="httpRequest"></param>
122 /// <param name="httpResponse"></param>
123 /// <param name="textureID"></param>
124 /// <param name="format"></param>
125 /// <returns>False for "caller try another codec"; true otherwise</returns>
126 private bool FetchTexture(OSHttpRequest httpRequest, OSHttpResponse httpResponse, UUID textureID, string format)
127 {
128// m_log.DebugFormat("[GETTEXTURE]: {0} with requested format {1}", textureID, format);
129 AssetBase texture;
130
131 string fullID = textureID.ToString();
132 if (format != DefaultFormat)
133 fullID = fullID + "-" + format;
134
135 if (!String.IsNullOrEmpty(REDIRECT_URL))
136 {
137 // Only try to fetch locally cached textures. Misses are redirected
138 texture = m_assetService.GetCached(fullID);
139
140 if (texture != null)
141 {
142 if (texture.Type != (sbyte)AssetType.Texture)
143 {
144 httpResponse.StatusCode = (int)System.Net.HttpStatusCode.NotFound;
145 return true;
146 }
147 WriteTextureData(httpRequest, httpResponse, texture, format);
148 }
149 else
150 {
151 string textureUrl = REDIRECT_URL + textureID.ToString();
152 m_log.Debug("[GETTEXTURE]: Redirecting texture request to " + textureUrl);
153 httpResponse.RedirectLocation = textureUrl;
154 return true;
155 }
156 }
157 else // no redirect
158 {
159 // try the cache
160 texture = m_assetService.GetCached(fullID);
161
162 if (texture == null)
163 {
164 //m_log.DebugFormat("[GETTEXTURE]: texture was not in the cache");
165
166 // Fetch locally or remotely. Misses return a 404
167 texture = m_assetService.Get(textureID.ToString());
168
169 if (texture != null)
170 {
171 if (texture.Type != (sbyte)AssetType.Texture)
172 {
173 httpResponse.StatusCode = (int)System.Net.HttpStatusCode.NotFound;
174 return true;
175 }
176 if (format == DefaultFormat)
177 {
178 WriteTextureData(httpRequest, httpResponse, texture, format);
179 return true;
180 }
181 else
182 {
183 AssetBase newTexture = new AssetBase(texture.ID + "-" + format, texture.Name, (sbyte)AssetType.Texture, texture.Metadata.CreatorID);
184 newTexture.Data = ConvertTextureData(texture, format);
185 if (newTexture.Data.Length == 0)
186 return false; // !!! Caller try another codec, please!
187
188 newTexture.Flags = AssetFlags.Collectable;
189 newTexture.Temporary = true;
190 m_assetService.Store(newTexture);
191 WriteTextureData(httpRequest, httpResponse, newTexture, format);
192 return true;
193 }
194 }
195 }
196 else // it was on the cache
197 {
198 //m_log.DebugFormat("[GETTEXTURE]: texture was in the cache");
199 WriteTextureData(httpRequest, httpResponse, texture, format);
200 return true;
201 }
202 }
203
204 // not found
205// m_log.Warn("[GETTEXTURE]: Texture " + textureID + " not found");
206 httpResponse.StatusCode = (int)System.Net.HttpStatusCode.NotFound;
207 return true;
208 }
209
210 private void WriteTextureData(OSHttpRequest request, OSHttpResponse response, AssetBase texture, string format)
211 {
212 string range = request.Headers.GetOne("Range");
213 //m_log.DebugFormat("[GETTEXTURE]: Range {0}", range);
214 if (!String.IsNullOrEmpty(range)) // JP2's only
215 {
216 // Range request
217 int start, end;
218 if (TryParseRange(range, out start, out end))
219 {
220 // Before clamping start make sure we can satisfy it in order to avoid
221 // sending back the last byte instead of an error status
222 if (start >= texture.Data.Length)
223 {
224 response.StatusCode = (int)System.Net.HttpStatusCode.RequestedRangeNotSatisfiable;
225 return;
226 }
227
228 end = Utils.Clamp(end, 0, texture.Data.Length - 1);
229 start = Utils.Clamp(start, 0, end);
230 int len = end - start + 1;
231
232 //m_log.Debug("Serving " + start + " to " + end + " of " + texture.Data.Length + " bytes for texture " + texture.ID);
233
234 if (len < texture.Data.Length)
235 response.StatusCode = (int)System.Net.HttpStatusCode.PartialContent;
236
237 response.ContentLength = len;
238 response.ContentType = texture.Metadata.ContentType;
239 response.AddHeader("Content-Range", String.Format("bytes {0}-{1}/{2}", start, end, texture.Data.Length));
240
241 response.Body.Write(texture.Data, start, len);
242 }
243 else
244 {
245 m_log.Warn("[GETTEXTURE]: Malformed Range header: " + range);
246 response.StatusCode = (int)System.Net.HttpStatusCode.BadRequest;
247 }
248 }
249 else // JP2's or other formats
250 {
251 // Full content request
252 response.StatusCode = (int)System.Net.HttpStatusCode.OK;
253 response.ContentLength = texture.Data.Length;
254 if (format == DefaultFormat)
255 response.ContentType = texture.Metadata.ContentType;
256 else
257 response.ContentType = "image/" + format;
258 response.Body.Write(texture.Data, 0, texture.Data.Length);
259 }
260 }
261
262 private bool TryParseRange(string header, out int start, out int end)
263 {
264 if (header.StartsWith("bytes="))
265 {
266 string[] rangeValues = header.Substring(6).Split('-');
267 if (rangeValues.Length == 2)
268 {
269 if (Int32.TryParse(rangeValues[0], out start) && Int32.TryParse(rangeValues[1], out end))
270 return true;
271 }
272 }
273
274 start = end = 0;
275 return false;
276 }
277
278
279 private byte[] ConvertTextureData(AssetBase texture, string format)
280 {
281 m_log.DebugFormat("[GETTEXTURE]: Converting texture {0} to {1}", texture.ID, format);
282 byte[] data = new byte[0];
283
284 MemoryStream imgstream = new MemoryStream();
285 Bitmap mTexture = new Bitmap(1, 1);
286 ManagedImage managedImage;
287 Image image = (Image)mTexture;
288
289 try
290 {
291 // Taking our jpeg2000 data, decoding it, then saving it to a byte array with regular data
292
293 imgstream = new MemoryStream();
294
295 // Decode image to System.Drawing.Image
296 if (OpenJPEG.DecodeToImage(texture.Data, out managedImage, out image))
297 {
298 // Save to bitmap
299 mTexture = new Bitmap(image);
300
301 EncoderParameters myEncoderParameters = new EncoderParameters();
302 myEncoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, 95L);
303
304 // Save bitmap to stream
305 ImageCodecInfo codec = GetEncoderInfo("image/" + format);
306 if (codec != null)
307 {
308 mTexture.Save(imgstream, codec, myEncoderParameters);
309 // Write the stream to a byte array for output
310 data = imgstream.ToArray();
311 }
312 else
313 m_log.WarnFormat("[GETTEXTURE]: No such codec {0}", format);
314
315 }
316 }
317 catch (Exception e)
318 {
319 m_log.WarnFormat("[GETTEXTURE]: Unable to convert texture {0} to {1}: {2}", texture.ID, format, e.Message);
320 }
321 finally
322 {
323 // Reclaim memory, these are unmanaged resources
324 // If we encountered an exception, one or more of these will be null
325 if (mTexture != null)
326 mTexture.Dispose();
327
328 if (image != null)
329 image.Dispose();
330
331 if (imgstream != null)
332 {
333 imgstream.Close();
334 imgstream.Dispose();
335 }
336 }
337
338 return data;
339 }
340
341 // From msdn
342 private static ImageCodecInfo GetEncoderInfo(String mimeType)
343 {
344 ImageCodecInfo[] encoders;
345 encoders = ImageCodecInfo.GetImageEncoders();
346 for (int j = 0; j < encoders.Length; ++j)
347 {
348 if (encoders[j].MimeType == mimeType)
349 return encoders[j];
350 }
351 return null;
352 }
353
354
355 }
356}
diff --git a/OpenSim/Region/ClientStack/Linden/Caps/GetTextureModule.cs b/OpenSim/Region/ClientStack/Linden/Caps/GetTextureModule.cs
index 58dbc14..341240d 100644
--- a/OpenSim/Region/ClientStack/Linden/Caps/GetTextureModule.cs
+++ b/OpenSim/Region/ClientStack/Linden/Caps/GetTextureModule.cs
@@ -46,30 +46,10 @@ using OpenSim.Region.Framework.Interfaces;
46using OpenSim.Region.Framework.Scenes; 46using OpenSim.Region.Framework.Scenes;
47using OpenSim.Services.Interfaces; 47using OpenSim.Services.Interfaces;
48using Caps = OpenSim.Framework.Capabilities.Caps; 48using Caps = OpenSim.Framework.Capabilities.Caps;
49using OpenSim.Capabilities.Handlers;
49 50
50namespace OpenSim.Region.ClientStack.Linden 51namespace OpenSim.Region.ClientStack.Linden
51{ 52{
52 #region Stream Handler
53
54 public delegate byte[] StreamHandlerCallback(string path, Stream request, OSHttpRequest httpRequest, OSHttpResponse httpResponse);
55
56 public class StreamHandler : BaseStreamHandler
57 {
58 StreamHandlerCallback m_callback;
59
60 public StreamHandler(string httpMethod, string path, StreamHandlerCallback callback)
61 : base(httpMethod, path)
62 {
63 m_callback = callback;
64 }
65
66 public override byte[] Handle(string path, Stream request, OSHttpRequest httpRequest, OSHttpResponse httpResponse)
67 {
68 return m_callback(path, request, httpRequest, httpResponse);
69 }
70 }
71
72 #endregion Stream Handler
73 53
74 [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule")] 54 [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule")]
75 public class GetTextureModule : IRegionModule 55 public class GetTextureModule : IRegionModule
@@ -107,298 +87,11 @@ namespace OpenSim.Region.ClientStack.Linden
107 { 87 {
108 UUID capID = UUID.Random(); 88 UUID capID = UUID.Random();
109 89
110// m_log.InfoFormat("[GETTEXTURE]: /CAPS/{0} in region {1}", capID, m_scene.RegionInfo.RegionName); 90 // m_log.InfoFormat("[GETTEXTURE]: /CAPS/{0} in region {1}", capID, m_scene.RegionInfo.RegionName);
111 caps.RegisterHandler("GetTexture", new StreamHandler("GET", "/CAPS/" + capID, ProcessGetTexture)); 91 //caps.RegisterHandler("GetTexture", new StreamHandler("GET", "/CAPS/" + capID, ProcessGetTexture));
92 caps.RegisterHandler("GetTexture", new GetTextureHandler("/CAPS/" + capID + "/", m_assetService));
112 } 93 }
113 94
114 #endregion 95 #endregion
115
116 private byte[] ProcessGetTexture(string path, Stream request, OSHttpRequest httpRequest, OSHttpResponse httpResponse)
117 {
118 //m_log.DebugFormat("[GETTEXTURE]: called in {0}", m_scene.RegionInfo.RegionName);
119
120 // Try to parse the texture ID from the request URL
121 NameValueCollection query = HttpUtility.ParseQueryString(httpRequest.Url.Query);
122 string textureStr = query.GetOne("texture_id");
123 string format = query.GetOne("format");
124
125 if (m_assetService == null)
126 {
127 m_log.Error("[GETTEXTURE]: Cannot fetch texture " + textureStr + " without an asset service");
128 httpResponse.StatusCode = (int)System.Net.HttpStatusCode.NotFound;
129 return null;
130 }
131
132 UUID textureID;
133 if (!String.IsNullOrEmpty(textureStr) && UUID.TryParse(textureStr, out textureID))
134 {
135 string[] formats;
136 if (format != null && format != string.Empty)
137 {
138 formats = new string[1] { format.ToLower() };
139 }
140 else
141 {
142 formats = WebUtil.GetPreferredImageTypes(httpRequest.Headers.Get("Accept"));
143 if (formats.Length == 0)
144 formats = new string[1] { DefaultFormat }; // default
145
146 }
147 // OK, we have an array with preferred formats, possibly with only one entry
148
149 httpResponse.StatusCode = (int)System.Net.HttpStatusCode.NotFound;
150 foreach (string f in formats)
151 {
152 if (FetchTexture(httpRequest, httpResponse, textureID, f))
153 break;
154 }
155
156 }
157 else
158 {
159 m_log.Warn("[GETTEXTURE]: Failed to parse a texture_id from GetTexture request: " + httpRequest.Url);
160 }
161
162 httpResponse.Send();
163 return null;
164 }
165
166 /// <summary>
167 ///
168 /// </summary>
169 /// <param name="httpRequest"></param>
170 /// <param name="httpResponse"></param>
171 /// <param name="textureID"></param>
172 /// <param name="format"></param>
173 /// <returns>False for "caller try another codec"; true otherwise</returns>
174 private bool FetchTexture(OSHttpRequest httpRequest, OSHttpResponse httpResponse, UUID textureID, string format)
175 {
176// m_log.DebugFormat("[GETTEXTURE]: {0} with requested format {1}", textureID, format);
177 AssetBase texture;
178
179 string fullID = textureID.ToString();
180 if (format != DefaultFormat)
181 fullID = fullID + "-" + format;
182
183 if (!String.IsNullOrEmpty(REDIRECT_URL))
184 {
185 // Only try to fetch locally cached textures. Misses are redirected
186 texture = m_assetService.GetCached(fullID);
187
188 if (texture != null)
189 {
190 if (texture.Type != (sbyte)AssetType.Texture)
191 {
192 httpResponse.StatusCode = (int)System.Net.HttpStatusCode.NotFound;
193 return true;
194 }
195 WriteTextureData(httpRequest, httpResponse, texture, format);
196 }
197 else
198 {
199 string textureUrl = REDIRECT_URL + textureID.ToString();
200 m_log.Debug("[GETTEXTURE]: Redirecting texture request to " + textureUrl);
201 httpResponse.RedirectLocation = textureUrl;
202 return true;
203 }
204 }
205 else // no redirect
206 {
207 // try the cache
208 texture = m_assetService.GetCached(fullID);
209
210 if (texture == null)
211 {
212 //m_log.DebugFormat("[GETTEXTURE]: texture was not in the cache");
213
214 // Fetch locally or remotely. Misses return a 404
215 texture = m_assetService.Get(textureID.ToString());
216
217 if (texture != null)
218 {
219 if (texture.Type != (sbyte)AssetType.Texture)
220 {
221 httpResponse.StatusCode = (int)System.Net.HttpStatusCode.NotFound;
222 return true;
223 }
224 if (format == DefaultFormat)
225 {
226 WriteTextureData(httpRequest, httpResponse, texture, format);
227 return true;
228 }
229 else
230 {
231 AssetBase newTexture = new AssetBase(texture.ID + "-" + format, texture.Name, (sbyte)AssetType.Texture, texture.Metadata.CreatorID);
232 newTexture.Data = ConvertTextureData(texture, format);
233 if (newTexture.Data.Length == 0)
234 return false; // !!! Caller try another codec, please!
235
236 newTexture.Flags = AssetFlags.Collectable;
237 newTexture.Temporary = true;
238 m_assetService.Store(newTexture);
239 WriteTextureData(httpRequest, httpResponse, newTexture, format);
240 return true;
241 }
242 }
243 }
244 else // it was on the cache
245 {
246 //m_log.DebugFormat("[GETTEXTURE]: texture was in the cache");
247 WriteTextureData(httpRequest, httpResponse, texture, format);
248 return true;
249 }
250 }
251
252 // not found
253// m_log.Warn("[GETTEXTURE]: Texture " + textureID + " not found");
254 httpResponse.StatusCode = (int)System.Net.HttpStatusCode.NotFound;
255 return true;
256 }
257
258 private void WriteTextureData(OSHttpRequest request, OSHttpResponse response, AssetBase texture, string format)
259 {
260 string range = request.Headers.GetOne("Range");
261 //m_log.DebugFormat("[GETTEXTURE]: Range {0}", range);
262 if (!String.IsNullOrEmpty(range)) // JP2's only
263 {
264 // Range request
265 int start, end;
266 if (TryParseRange(range, out start, out end))
267 {
268 // Before clamping start make sure we can satisfy it in order to avoid
269 // sending back the last byte instead of an error status
270 if (start >= texture.Data.Length)
271 {
272 response.StatusCode = (int)System.Net.HttpStatusCode.RequestedRangeNotSatisfiable;
273 return;
274 }
275
276 end = Utils.Clamp(end, 0, texture.Data.Length - 1);
277 start = Utils.Clamp(start, 0, end);
278 int len = end - start + 1;
279
280 //m_log.Debug("Serving " + start + " to " + end + " of " + texture.Data.Length + " bytes for texture " + texture.ID);
281
282 if (len < texture.Data.Length)
283 response.StatusCode = (int)System.Net.HttpStatusCode.PartialContent;
284
285 response.ContentLength = len;
286 response.ContentType = texture.Metadata.ContentType;
287 response.AddHeader("Content-Range", String.Format("bytes {0}-{1}/{2}", start, end, texture.Data.Length));
288
289 response.Body.Write(texture.Data, start, len);
290 }
291 else
292 {
293 m_log.Warn("[GETTEXTURE]: Malformed Range header: " + range);
294 response.StatusCode = (int)System.Net.HttpStatusCode.BadRequest;
295 }
296 }
297 else // JP2's or other formats
298 {
299 // Full content request
300 response.StatusCode = (int)System.Net.HttpStatusCode.OK;
301 response.ContentLength = texture.Data.Length;
302 if (format == DefaultFormat)
303 response.ContentType = texture.Metadata.ContentType;
304 else
305 response.ContentType = "image/" + format;
306 response.Body.Write(texture.Data, 0, texture.Data.Length);
307 }
308 }
309
310 private bool TryParseRange(string header, out int start, out int end)
311 {
312 if (header.StartsWith("bytes="))
313 {
314 string[] rangeValues = header.Substring(6).Split('-');
315 if (rangeValues.Length == 2)
316 {
317 if (Int32.TryParse(rangeValues[0], out start) && Int32.TryParse(rangeValues[1], out end))
318 return true;
319 }
320 }
321
322 start = end = 0;
323 return false;
324 }
325
326
327 private byte[] ConvertTextureData(AssetBase texture, string format)
328 {
329 m_log.DebugFormat("[GETTEXTURE]: Converting texture {0} to {1}", texture.ID, format);
330 byte[] data = new byte[0];
331
332 MemoryStream imgstream = new MemoryStream();
333 Bitmap mTexture = new Bitmap(1, 1);
334 ManagedImage managedImage;
335 Image image = (Image)mTexture;
336
337 try
338 {
339 // Taking our jpeg2000 data, decoding it, then saving it to a byte array with regular data
340
341 imgstream = new MemoryStream();
342
343 // Decode image to System.Drawing.Image
344 if (OpenJPEG.DecodeToImage(texture.Data, out managedImage, out image))
345 {
346 // Save to bitmap
347 mTexture = new Bitmap(image);
348
349 EncoderParameters myEncoderParameters = new EncoderParameters();
350 myEncoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, 95L);
351
352 // Save bitmap to stream
353 ImageCodecInfo codec = GetEncoderInfo("image/" + format);
354 if (codec != null)
355 {
356 mTexture.Save(imgstream, codec, myEncoderParameters);
357 // Write the stream to a byte array for output
358 data = imgstream.ToArray();
359 }
360 else
361 m_log.WarnFormat("[GETTEXTURE]: No such codec {0}", format);
362
363 }
364 }
365 catch (Exception e)
366 {
367 m_log.WarnFormat("[GETTEXTURE]: Unable to convert texture {0} to {1}: {2}", texture.ID, format, e.Message);
368 }
369 finally
370 {
371 // Reclaim memory, these are unmanaged resources
372 // If we encountered an exception, one or more of these will be null
373 if (mTexture != null)
374 mTexture.Dispose();
375
376 if (image != null)
377 image.Dispose();
378
379 if (imgstream != null)
380 {
381 imgstream.Close();
382 imgstream.Dispose();
383 }
384 }
385
386 return data;
387 }
388
389 // From msdn
390 private static ImageCodecInfo GetEncoderInfo(String mimeType)
391 {
392 ImageCodecInfo[] encoders;
393 encoders = ImageCodecInfo.GetImageEncoders();
394 for (int j = 0; j < encoders.Length; ++j)
395 {
396 if (encoders[j].MimeType == mimeType)
397 return encoders[j];
398 }
399 return null;
400 }
401
402
403 } 96 }
404} 97}
diff --git a/prebuild.xml b/prebuild.xml
index 6a0b07a..4fa48d1 100644
--- a/prebuild.xml
+++ b/prebuild.xml
@@ -687,7 +687,7 @@
687 <Reference name="log4net" path="../../../bin/"/> 687 <Reference name="log4net" path="../../../bin/"/>
688 688
689 <Files> 689 <Files>
690 <Match pattern="*.cs" recurse="true"> 690 <Match pattern="*.cs" recurse="false">
691 <Exclude name="Tests" pattern="Tests"/> 691 <Exclude name="Tests" pattern="Tests"/>
692 </Match> 692 </Match>
693 <Match buildAction="EmbeddedResource" path="Resources" pattern="*.addin.xml" recurse="true"/> 693 <Match buildAction="EmbeddedResource" path="Resources" pattern="*.addin.xml" recurse="true"/>
@@ -1268,7 +1268,6 @@
1268 </Files> 1268 </Files>
1269 </Project> 1269 </Project>
1270 1270
1271
1272 <Project frameworkVersion="v3_5" name="OpenSim.Server.Handlers" path="OpenSim/Server/Handlers" type="Library"> 1271 <Project frameworkVersion="v3_5" name="OpenSim.Server.Handlers" path="OpenSim/Server/Handlers" type="Library">
1273 <Configuration name="Debug"> 1272 <Configuration name="Debug">
1274 <Options> 1273 <Options>
@@ -1306,6 +1305,47 @@
1306 </Files> 1305 </Files>
1307 </Project> 1306 </Project>
1308 1307
1308
1309 <Project frameworkVersion="v3_5" name="OpenSim.Capabilities.Handlers" path="OpenSim/Capabilities/Handlers" type="Library">
1310 <Configuration name="Debug">
1311 <Options>
1312 <OutputPath>../../../bin/</OutputPath>
1313 </Options>
1314 </Configuration>
1315 <Configuration name="Release">
1316 <Options>
1317 <OutputPath>../../../bin/</OutputPath>
1318 </Options>
1319 </Configuration>
1320
1321 <ReferencePath>../../../bin/</ReferencePath>
1322 <Reference name="System"/>
1323 <Reference name="System.Drawing"/>
1324 <Reference name="System.Xml"/>
1325 <Reference name="System.Web"/>
1326 <Reference name="OpenMetaverse" path="../../../bin/"/>
1327 <Reference name="OpenMetaverseTypes" path="../../../bin/"/>
1328 <Reference name="OpenMetaverse.StructuredData" path="../../../bin/"/>
1329 <Reference name="OpenSim.Capabilities"/>
1330 <Reference name="OpenSim.Framework"/>
1331 <Reference name="OpenSim.Framework.Console"/>
1332 <Reference name="OpenSim.Framework.Servers.HttpServer"/>
1333 <Reference name="OpenSim.Server.Base"/>
1334 <Reference name="OpenSim.Server.Handlers"/>
1335 <Reference name="OpenSim.Services.Base"/>
1336 <Reference name="OpenSim.Services.Interfaces"/>
1337 <Reference name="Nini" path="../../../bin/"/>
1338 <Reference name="log4net" path="../../../bin/"/>
1339 <Reference name="DotNetOpenId" path="../../../bin/"/>
1340
1341 <Files>
1342 <Match pattern="*.cs" recurse="true">
1343 <Exclude pattern="Tests"/>
1344 </Match>
1345 </Files>
1346 </Project>
1347
1348
1309 <Project frameworkVersion="v3_5" name="Robust" path="OpenSim/Server" type="Exe"> 1349 <Project frameworkVersion="v3_5" name="Robust" path="OpenSim/Server" type="Exe">
1310 <Configuration name="Debug"> 1350 <Configuration name="Debug">
1311 <Options> 1351 <Options>
@@ -1532,6 +1572,7 @@
1532 <Reference name="OpenMetaverse" path="../../../../bin/"/> 1572 <Reference name="OpenMetaverse" path="../../../../bin/"/>
1533 <Reference name="OpenSim.Region.Framework"/> 1573 <Reference name="OpenSim.Region.Framework"/>
1534 <Reference name="OpenSim.Capabilities"/> 1574 <Reference name="OpenSim.Capabilities"/>
1575 <Reference name="OpenSim.Capabilities.Handlers"/>
1535 <Reference name="OpenSim.Framework"/> 1576 <Reference name="OpenSim.Framework"/>
1536 <Reference name="OpenSim.Data"/> 1577 <Reference name="OpenSim.Data"/>
1537 <Reference name="OpenSim.Framework.Servers"/> 1578 <Reference name="OpenSim.Framework.Servers"/>
@@ -2355,7 +2396,6 @@
2355 <Reference name="OpenSim.Region.CoreModules"/> 2396 <Reference name="OpenSim.Region.CoreModules"/>
2356 <Reference name="OpenSim.Framework.Console"/> 2397 <Reference name="OpenSim.Framework.Console"/>
2357 <Reference name="OpenSim.Framework.Servers"/> 2398 <Reference name="OpenSim.Framework.Servers"/>
2358 <Reference name="OpenSim.Capabilities"/>
2359 <Reference name="OpenSim.Framework.Servers.HttpServer"/> 2399 <Reference name="OpenSim.Framework.Servers.HttpServer"/>
2360 <Reference name="OpenSim.Framework.Statistics"/> 2400 <Reference name="OpenSim.Framework.Statistics"/>
2361 <Reference name="OpenSim.Region.Physics.Manager"/> 2401 <Reference name="OpenSim.Region.Physics.Manager"/>