123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- using Nini.Config;
- using log4net;
- using System;
- using System.IO;
- using System.Reflection;
- using System.Net;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Xml;
- using System.Xml.Serialization;
- using OpenSim.Server.Base;
- using OpenSim.Services.Interfaces;
- using OpenSim.Framework;
- using OpenSim.Framework.ServiceAuth;
- using OpenSim.Framework.Servers.HttpServer;
- namespace OpenSim.Server.Handlers.Asset
- {
- public class AssetServerGetHandler : BaseStreamHandler
- {
- private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
- private IAssetService m_AssetService;
- private string m_RedirectURL;
- public AssetServerGetHandler(IAssetService service) :
- base("GET", "/assets")
- {
- m_AssetService = service;
- }
- public AssetServerGetHandler(IAssetService service, IServiceAuth auth, string redirectURL) :
- base("GET", "/assets", auth)
- {
- m_AssetService = service;
- m_RedirectURL = redirectURL;
- if (!m_RedirectURL.EndsWith("/"))
- m_RedirectURL = m_RedirectURL.TrimEnd('/');
- }
- protected override byte[] ProcessRequest(string path, Stream request,
- IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
- {
- byte[] result = new byte[0];
- string[] p = SplitParams(path);
- if (p.Length == 0)
- return result;
- string id = string.Empty;
- if (p.Length > 1)
- {
- id = p[0];
- string cmd = p[1];
- if (cmd == "data")
- {
- result = m_AssetService.GetData(id);
- if (result == null)
- {
- httpResponse.StatusCode = (int)HttpStatusCode.NotFound;
- httpResponse.ContentType = "text/plain";
- result = new byte[0];
- }
- else
- {
- httpResponse.StatusCode = (int)HttpStatusCode.OK;
- httpResponse.ContentType = "application/octet-stream";
- }
- }
- else if (cmd == "metadata")
- {
- AssetMetadata metadata = m_AssetService.GetMetadata(id);
- if (metadata != null)
- {
- XmlSerializer xs =
- new XmlSerializer(typeof(AssetMetadata));
- result = ServerUtils.SerializeResult(xs, metadata);
- httpResponse.StatusCode = (int)HttpStatusCode.OK;
- httpResponse.ContentType =
- SLUtil.SLAssetTypeToContentType(metadata.Type);
- }
- else
- {
- httpResponse.StatusCode = (int)HttpStatusCode.NotFound;
- httpResponse.ContentType = "text/plain";
- result = new byte[0];
- }
- }
- else
- {
-
- httpResponse.StatusCode = (int)HttpStatusCode.BadRequest;
- httpResponse.ContentType = "text/plain";
- result = new byte[0];
- }
- }
- else if (p.Length == 1)
- {
-
- id = p[0];
- AssetBase asset = m_AssetService.Get(id);
- if (asset != null)
- {
- XmlSerializer xs = new XmlSerializer(typeof(AssetBase));
- result = ServerUtils.SerializeResult(xs, asset);
- httpResponse.StatusCode = (int)HttpStatusCode.OK;
- httpResponse.ContentType =
- SLUtil.SLAssetTypeToContentType(asset.Type);
- }
- else
- {
- httpResponse.StatusCode = (int)HttpStatusCode.NotFound;
- httpResponse.ContentType = "text/plain";
- result = new byte[0];
- }
- }
- else
- {
-
- httpResponse.StatusCode = (int)HttpStatusCode.BadRequest;
- httpResponse.ContentType = "text/plain";
- result = new byte[0];
- }
- if (httpResponse.StatusCode == (int)HttpStatusCode.NotFound && !string.IsNullOrEmpty(m_RedirectURL) && !string.IsNullOrEmpty(id))
- {
- httpResponse.StatusCode = (int)HttpStatusCode.Redirect;
- string rurl = m_RedirectURL;
- if (!path.StartsWith("/"))
- rurl += "/";
- rurl += path;
- httpResponse.AddHeader("Location", rurl);
- m_log.DebugFormat("[ASSET GET HANDLER]: Asset not found, redirecting to {0} ({1})", rurl, httpResponse.StatusCode);
- }
- return result;
- }
- }
- }
|