diff options
Diffstat (limited to 'OpenSim/Grid/AssetServer/RestService.cs')
-rw-r--r-- | OpenSim/Grid/AssetServer/RestService.cs | 156 |
1 files changed, 156 insertions, 0 deletions
diff --git a/OpenSim/Grid/AssetServer/RestService.cs b/OpenSim/Grid/AssetServer/RestService.cs new file mode 100644 index 0000000..1e01c44 --- /dev/null +++ b/OpenSim/Grid/AssetServer/RestService.cs | |||
@@ -0,0 +1,156 @@ | |||
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 OpenSim 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.IO; | ||
30 | using System.Reflection; | ||
31 | using System.Text; | ||
32 | using System.Xml; | ||
33 | using System.Xml.Serialization; | ||
34 | using log4net; | ||
35 | using OpenMetaverse; | ||
36 | using OpenSim.Data; | ||
37 | using OpenSim.Framework; | ||
38 | using OpenSim.Framework.Servers; | ||
39 | using OpenSim.Framework.Statistics; | ||
40 | |||
41 | namespace OpenSim.Grid.AssetServer | ||
42 | { | ||
43 | public class GetAssetStreamHandler : BaseStreamHandler | ||
44 | { | ||
45 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
46 | |||
47 | // private OpenAsset_Main m_assetManager; | ||
48 | private IAssetDataPlugin m_assetProvider; | ||
49 | |||
50 | /// <summary> | ||
51 | /// Constructor. | ||
52 | /// </summary> | ||
53 | /// <param name="assetManager"></param> | ||
54 | /// <param name="assetProvider"></param> | ||
55 | public GetAssetStreamHandler(IAssetDataPlugin assetProvider) | ||
56 | : base("GET", "/assets") | ||
57 | { | ||
58 | m_log.Info("[REST]: In Get Request"); | ||
59 | // m_assetManager = assetManager; | ||
60 | m_assetProvider = assetProvider; | ||
61 | } | ||
62 | |||
63 | public override byte[] Handle(string path, Stream request, | ||
64 | OSHttpRequest httpRequest, OSHttpResponse httpResponse) | ||
65 | { | ||
66 | string param = GetParam(path); | ||
67 | byte[] result = new byte[] {}; | ||
68 | |||
69 | string[] p = param.Split(new char[] {'/', '?', '&'}, StringSplitOptions.RemoveEmptyEntries); | ||
70 | |||
71 | if (p.Length > 0) | ||
72 | { | ||
73 | UUID assetID = UUID.Zero; | ||
74 | |||
75 | if (!UUID.TryParse(p[0], out assetID)) | ||
76 | { | ||
77 | m_log.InfoFormat( | ||
78 | "[REST]: GET:/asset ignoring request with malformed UUID {0}", p[0]); | ||
79 | return result; | ||
80 | } | ||
81 | |||
82 | if (StatsManager.AssetStats != null) | ||
83 | StatsManager.AssetStats.AddRequest(); | ||
84 | |||
85 | AssetBase asset = m_assetProvider.FetchAsset(assetID); | ||
86 | if (asset != null) | ||
87 | { | ||
88 | XmlSerializer xs = new XmlSerializer(typeof (AssetBase)); | ||
89 | MemoryStream ms = new MemoryStream(); | ||
90 | XmlTextWriter xw = new XmlTextWriter(ms, Encoding.UTF8); | ||
91 | xw.Formatting = Formatting.Indented; | ||
92 | xs.Serialize(xw, asset); | ||
93 | xw.Flush(); | ||
94 | |||
95 | ms.Seek(0, SeekOrigin.Begin); | ||
96 | //StreamReader sr = new StreamReader(ms); | ||
97 | |||
98 | result = ms.GetBuffer(); | ||
99 | |||
100 | //Ckrinke 1/11/09 Commenting out the succesful REST message as under heavy use there | ||
101 | //are multiple messages in a second and that is usually (in my experience) meaning | ||
102 | //the logging itself is slowing down the program. Leaving the unsuccesful message | ||
103 | //as we need to know about that path. | ||
104 | // m_log.InfoFormat( | ||
105 | // "[REST]: GET:/asset found {0} with name {1}, size {2} bytes", | ||
106 | // assetID, asset.Name, result.Length); | ||
107 | |||
108 | Array.Resize<byte>(ref result, (int) ms.Length); | ||
109 | } | ||
110 | else | ||
111 | { | ||
112 | if (StatsManager.AssetStats != null) | ||
113 | StatsManager.AssetStats.AddNotFoundRequest(); | ||
114 | |||
115 | m_log.InfoFormat("[REST]: GET:/asset failed to find {0}", assetID); | ||
116 | } | ||
117 | } | ||
118 | |||
119 | return result; | ||
120 | } | ||
121 | } | ||
122 | |||
123 | public class PostAssetStreamHandler : BaseStreamHandler | ||
124 | { | ||
125 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
126 | |||
127 | // private OpenAsset_Main m_assetManager; | ||
128 | private IAssetDataPlugin m_assetProvider; | ||
129 | |||
130 | public override byte[] Handle(string path, Stream request, | ||
131 | OSHttpRequest httpRequest, OSHttpResponse httpResponse) | ||
132 | { | ||
133 | string param = GetParam(path); | ||
134 | |||
135 | UUID assetId; | ||
136 | if (param.Length > 0) | ||
137 | UUID.TryParse(param, out assetId); | ||
138 | // byte[] txBuffer = new byte[4096]; | ||
139 | |||
140 | XmlSerializer xs = new XmlSerializer(typeof (AssetBase)); | ||
141 | AssetBase asset = (AssetBase) xs.Deserialize(request); | ||
142 | |||
143 | m_log.InfoFormat("[REST]: Creating asset {0}", asset.FullID); | ||
144 | m_assetProvider.CreateAsset(asset); | ||
145 | |||
146 | return new byte[] {}; | ||
147 | } | ||
148 | |||
149 | public PostAssetStreamHandler(IAssetDataPlugin assetProvider) | ||
150 | : base("POST", "/assets") | ||
151 | { | ||
152 | // m_assetManager = assetManager; | ||
153 | m_assetProvider = assetProvider; | ||
154 | } | ||
155 | } | ||
156 | } | ||