clockwerk-opensim – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 vero 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 Nini.Config;
29 using log4net;
30 using System;
31 using System.IO;
32 using System.Reflection;
33 using System.Net;
34 using System.Text;
35 using System.Text.RegularExpressions;
36 using System.Xml;
37 using System.Xml.Serialization;
38 using OpenSim.Server.Base;
39 using OpenSim.Services.Interfaces;
40 using OpenSim.Framework;
41 using OpenSim.Framework.ServiceAuth;
42 using OpenSim.Framework.Servers.HttpServer;
43  
44 namespace OpenSim.Server.Handlers.Asset
45 {
46 public class AssetServerGetHandler : BaseStreamHandler
47 {
48 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
49  
50 private IAssetService m_AssetService;
51 private string m_RedirectURL;
52  
53 public AssetServerGetHandler(IAssetService service) :
54 base("GET", "/assets")
55 {
56 m_AssetService = service;
57 }
58  
59 public AssetServerGetHandler(IAssetService service, IServiceAuth auth, string redirectURL) :
60 base("GET", "/assets", auth)
61 {
62 m_AssetService = service;
63 m_RedirectURL = redirectURL;
64 if (!m_RedirectURL.EndsWith("/"))
65 m_RedirectURL = m_RedirectURL.TrimEnd('/');
66 }
67  
68 protected override byte[] ProcessRequest(string path, Stream request,
69 IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
70 {
71 byte[] result = new byte[0];
72  
73 string[] p = SplitParams(path);
74  
75 if (p.Length == 0)
76 return result;
77  
78 string id = string.Empty;
79 if (p.Length > 1)
80 {
81 id = p[0];
82 string cmd = p[1];
83  
84 if (cmd == "data")
85 {
86 result = m_AssetService.GetData(id);
87 if (result == null)
88 {
89 httpResponse.StatusCode = (int)HttpStatusCode.NotFound;
90 httpResponse.ContentType = "text/plain";
91 result = new byte[0];
92 }
93 else
94 {
95 httpResponse.StatusCode = (int)HttpStatusCode.OK;
96 httpResponse.ContentType = "application/octet-stream";
97 }
98 }
99 else if (cmd == "metadata")
100 {
101 AssetMetadata metadata = m_AssetService.GetMetadata(id);
102  
103 if (metadata != null)
104 {
105 XmlSerializer xs =
106 new XmlSerializer(typeof(AssetMetadata));
107 result = ServerUtils.SerializeResult(xs, metadata);
108  
109 httpResponse.StatusCode = (int)HttpStatusCode.OK;
110 httpResponse.ContentType =
111 SLUtil.SLAssetTypeToContentType(metadata.Type);
112 }
113 else
114 {
115 httpResponse.StatusCode = (int)HttpStatusCode.NotFound;
116 httpResponse.ContentType = "text/plain";
117 result = new byte[0];
118 }
119 }
120 else
121 {
122 // Unknown request
123 httpResponse.StatusCode = (int)HttpStatusCode.BadRequest;
124 httpResponse.ContentType = "text/plain";
125 result = new byte[0];
126 }
127 }
128 else if (p.Length == 1)
129 {
130 // Get the entire asset (metadata + data)
131  
132 id = p[0];
133 AssetBase asset = m_AssetService.Get(id);
134  
135 if (asset != null)
136 {
137 XmlSerializer xs = new XmlSerializer(typeof(AssetBase));
138 result = ServerUtils.SerializeResult(xs, asset);
139  
140 httpResponse.StatusCode = (int)HttpStatusCode.OK;
141 httpResponse.ContentType =
142 SLUtil.SLAssetTypeToContentType(asset.Type);
143 }
144 else
145 {
146 httpResponse.StatusCode = (int)HttpStatusCode.NotFound;
147 httpResponse.ContentType = "text/plain";
148 result = new byte[0];
149 }
150 }
151 else
152 {
153 // Unknown request
154 httpResponse.StatusCode = (int)HttpStatusCode.BadRequest;
155 httpResponse.ContentType = "text/plain";
156 result = new byte[0];
157 }
158  
159 if (httpResponse.StatusCode == (int)HttpStatusCode.NotFound && !string.IsNullOrEmpty(m_RedirectURL) && !string.IsNullOrEmpty(id))
160 {
161 httpResponse.StatusCode = (int)HttpStatusCode.Redirect;
162 string rurl = m_RedirectURL;
163 if (!path.StartsWith("/"))
164 rurl += "/";
165 rurl += path;
166 httpResponse.AddHeader("Location", rurl);
167 m_log.DebugFormat("[ASSET GET HANDLER]: Asset not found, redirecting to {0} ({1})", rurl, httpResponse.StatusCode);
168 }
169 return result;
170 }
171 }
172 }