diff options
Diffstat (limited to 'OpenSim/Server/Handlers/Asset')
6 files changed, 802 insertions, 0 deletions
diff --git a/OpenSim/Server/Handlers/Asset/AssetServerConnector.cs b/OpenSim/Server/Handlers/Asset/AssetServerConnector.cs new file mode 100644 index 0000000..ab81dd6 --- /dev/null +++ b/OpenSim/Server/Handlers/Asset/AssetServerConnector.cs | |||
@@ -0,0 +1,221 @@ | |||
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 System; | ||
29 | using System.IO; | ||
30 | using Nini.Config; | ||
31 | using OpenMetaverse; | ||
32 | using OpenSim.Framework; | ||
33 | using OpenSim.Framework.ServiceAuth; | ||
34 | using OpenSim.Framework.Console; | ||
35 | using OpenSim.Server.Base; | ||
36 | using OpenSim.Services.Interfaces; | ||
37 | using OpenSim.Framework.Servers.HttpServer; | ||
38 | using OpenSim.Server.Handlers.Base; | ||
39 | |||
40 | namespace OpenSim.Server.Handlers.Asset | ||
41 | { | ||
42 | public class AssetServiceConnector : ServiceConnector | ||
43 | { | ||
44 | private IAssetService m_AssetService; | ||
45 | private string m_ConfigName = "AssetService"; | ||
46 | |||
47 | public AssetServiceConnector(IConfigSource config, IHttpServer server, string configName) : | ||
48 | base(config, server, configName) | ||
49 | { | ||
50 | if (configName != String.Empty) | ||
51 | m_ConfigName = configName; | ||
52 | |||
53 | IConfig serverConfig = config.Configs[m_ConfigName]; | ||
54 | if (serverConfig == null) | ||
55 | throw new Exception(String.Format("No section '{0}' in config file", m_ConfigName)); | ||
56 | |||
57 | string assetService = serverConfig.GetString("LocalServiceModule", | ||
58 | String.Empty); | ||
59 | |||
60 | if (assetService == String.Empty) | ||
61 | throw new Exception("No LocalServiceModule in config file"); | ||
62 | |||
63 | Object[] args = new Object[] { config, m_ConfigName }; | ||
64 | m_AssetService = | ||
65 | ServerUtils.LoadPlugin<IAssetService>(assetService, args); | ||
66 | |||
67 | if (m_AssetService == null) | ||
68 | throw new Exception(String.Format("Failed to load AssetService from {0}; config is {1}", assetService, m_ConfigName)); | ||
69 | |||
70 | bool allowDelete = serverConfig.GetBoolean("AllowRemoteDelete", false); | ||
71 | bool allowDeleteAllTypes = serverConfig.GetBoolean("AllowRemoteDeleteAllTypes", false); | ||
72 | |||
73 | string redirectURL = serverConfig.GetString("RedirectURL", string.Empty); | ||
74 | |||
75 | AllowedRemoteDeleteTypes allowedRemoteDeleteTypes; | ||
76 | |||
77 | if (!allowDelete) | ||
78 | { | ||
79 | allowedRemoteDeleteTypes = AllowedRemoteDeleteTypes.None; | ||
80 | } | ||
81 | else | ||
82 | { | ||
83 | if (allowDeleteAllTypes) | ||
84 | allowedRemoteDeleteTypes = AllowedRemoteDeleteTypes.All; | ||
85 | else | ||
86 | allowedRemoteDeleteTypes = AllowedRemoteDeleteTypes.MapTile; | ||
87 | } | ||
88 | |||
89 | IServiceAuth auth = ServiceAuth.Create(config, m_ConfigName); | ||
90 | |||
91 | server.AddStreamHandler(new AssetServerGetHandler(m_AssetService, auth, redirectURL)); | ||
92 | server.AddStreamHandler(new AssetServerPostHandler(m_AssetService, auth)); | ||
93 | server.AddStreamHandler(new AssetServerDeleteHandler(m_AssetService, allowedRemoteDeleteTypes, auth)); | ||
94 | server.AddStreamHandler(new AssetsExistHandler(m_AssetService)); | ||
95 | |||
96 | MainConsole.Instance.Commands.AddCommand("Assets", false, | ||
97 | "show asset", | ||
98 | "show asset <ID>", | ||
99 | "Show asset information", | ||
100 | HandleShowAsset); | ||
101 | |||
102 | MainConsole.Instance.Commands.AddCommand("Assets", false, | ||
103 | "delete asset", | ||
104 | "delete asset <ID>", | ||
105 | "Delete asset from database", | ||
106 | HandleDeleteAsset); | ||
107 | |||
108 | MainConsole.Instance.Commands.AddCommand("Assets", false, | ||
109 | "dump asset", | ||
110 | "dump asset <ID>", | ||
111 | "Dump asset to a file", | ||
112 | "The filename is the same as the ID given.", | ||
113 | HandleDumpAsset); | ||
114 | } | ||
115 | |||
116 | void HandleDeleteAsset(string module, string[] args) | ||
117 | { | ||
118 | if (args.Length < 3) | ||
119 | { | ||
120 | MainConsole.Instance.Output("Syntax: delete asset <ID>"); | ||
121 | return; | ||
122 | } | ||
123 | |||
124 | AssetBase asset = m_AssetService.Get(args[2]); | ||
125 | |||
126 | if (asset == null || asset.Data.Length == 0) | ||
127 | { | ||
128 | MainConsole.Instance.OutputFormat("Could not find asset with ID {0}", args[2]); | ||
129 | return; | ||
130 | } | ||
131 | |||
132 | if (!m_AssetService.Delete(asset.ID)) | ||
133 | MainConsole.Instance.OutputFormat("ERROR: Could not delete asset {0} {1}", asset.ID, asset.Name); | ||
134 | else | ||
135 | MainConsole.Instance.OutputFormat("Deleted asset {0} {1}", asset.ID, asset.Name); | ||
136 | } | ||
137 | |||
138 | void HandleDumpAsset(string module, string[] args) | ||
139 | { | ||
140 | if (args.Length < 3) | ||
141 | { | ||
142 | MainConsole.Instance.Output("Usage is dump asset <ID>"); | ||
143 | return; | ||
144 | } | ||
145 | |||
146 | UUID assetId; | ||
147 | string rawAssetId = args[2]; | ||
148 | |||
149 | if (!UUID.TryParse(rawAssetId, out assetId)) | ||
150 | { | ||
151 | MainConsole.Instance.OutputFormat("ERROR: {0} is not a valid ID format", rawAssetId); | ||
152 | return; | ||
153 | } | ||
154 | |||
155 | AssetBase asset = m_AssetService.Get(assetId.ToString()); | ||
156 | if (asset == null) | ||
157 | { | ||
158 | MainConsole.Instance.OutputFormat("ERROR: No asset found with ID {0}", assetId); | ||
159 | return; | ||
160 | } | ||
161 | |||
162 | string fileName = rawAssetId; | ||
163 | |||
164 | if (!ConsoleUtil.CheckFileDoesNotExist(MainConsole.Instance, fileName)) | ||
165 | return; | ||
166 | |||
167 | using (FileStream fs = new FileStream(fileName, FileMode.CreateNew)) | ||
168 | { | ||
169 | using (BinaryWriter bw = new BinaryWriter(fs)) | ||
170 | { | ||
171 | bw.Write(asset.Data); | ||
172 | } | ||
173 | } | ||
174 | |||
175 | MainConsole.Instance.OutputFormat("Asset dumped to file {0}", fileName); | ||
176 | } | ||
177 | |||
178 | void HandleShowAsset(string module, string[] args) | ||
179 | { | ||
180 | if (args.Length < 3) | ||
181 | { | ||
182 | MainConsole.Instance.Output("Syntax: show asset <ID>"); | ||
183 | return; | ||
184 | } | ||
185 | |||
186 | AssetBase asset = m_AssetService.Get(args[2]); | ||
187 | |||
188 | if (asset == null || asset.Data.Length == 0) | ||
189 | { | ||
190 | MainConsole.Instance.Output("Asset not found"); | ||
191 | return; | ||
192 | } | ||
193 | |||
194 | int i; | ||
195 | |||
196 | MainConsole.Instance.OutputFormat("Name: {0}", asset.Name); | ||
197 | MainConsole.Instance.OutputFormat("Description: {0}", asset.Description); | ||
198 | MainConsole.Instance.OutputFormat("Type: {0} (type number = {1})", (AssetType)asset.Type, asset.Type); | ||
199 | MainConsole.Instance.OutputFormat("Content-type: {0}", asset.Metadata.ContentType); | ||
200 | MainConsole.Instance.OutputFormat("Size: {0} bytes", asset.Data.Length); | ||
201 | MainConsole.Instance.OutputFormat("Temporary: {0}", asset.Temporary ? "yes" : "no"); | ||
202 | MainConsole.Instance.OutputFormat("Flags: {0}", asset.Metadata.Flags); | ||
203 | |||
204 | for (i = 0 ; i < 5 ; i++) | ||
205 | { | ||
206 | int off = i * 16; | ||
207 | if (asset.Data.Length <= off) | ||
208 | break; | ||
209 | int len = 16; | ||
210 | if (asset.Data.Length < off + len) | ||
211 | len = asset.Data.Length - off; | ||
212 | |||
213 | byte[] line = new byte[len]; | ||
214 | Array.Copy(asset.Data, off, line, 0, len); | ||
215 | |||
216 | string text = BitConverter.ToString(line); | ||
217 | MainConsole.Instance.Output(String.Format("{0:x4}: {1}", off, text)); | ||
218 | } | ||
219 | } | ||
220 | } | ||
221 | } | ||
diff --git a/OpenSim/Server/Handlers/Asset/AssetServerDeleteHandler.cs b/OpenSim/Server/Handlers/Asset/AssetServerDeleteHandler.cs new file mode 100644 index 0000000..d85d471 --- /dev/null +++ b/OpenSim/Server/Handlers/Asset/AssetServerDeleteHandler.cs | |||
@@ -0,0 +1,115 @@ | |||
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.Reflection; | ||
32 | using System.IO; | ||
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 | /// <summary> | ||
47 | /// Remote deletes allowed. | ||
48 | /// </summary> | ||
49 | public enum AllowedRemoteDeleteTypes | ||
50 | { | ||
51 | None, | ||
52 | MapTile, | ||
53 | All | ||
54 | } | ||
55 | |||
56 | public class AssetServerDeleteHandler : BaseStreamHandler | ||
57 | { | ||
58 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
59 | |||
60 | private IAssetService m_AssetService; | ||
61 | |||
62 | /// <summary> | ||
63 | /// Asset types that can be deleted remotely. | ||
64 | /// </summary> | ||
65 | private AllowedRemoteDeleteTypes m_allowedTypes; | ||
66 | |||
67 | public AssetServerDeleteHandler(IAssetService service, AllowedRemoteDeleteTypes allowedTypes) : | ||
68 | base("DELETE", "/assets") | ||
69 | { | ||
70 | m_AssetService = service; | ||
71 | m_allowedTypes = allowedTypes; | ||
72 | } | ||
73 | |||
74 | public AssetServerDeleteHandler(IAssetService service, AllowedRemoteDeleteTypes allowedTypes, IServiceAuth auth) : | ||
75 | base("DELETE", "/assets", auth) | ||
76 | { | ||
77 | m_AssetService = service; | ||
78 | m_allowedTypes = allowedTypes; | ||
79 | } | ||
80 | protected override byte[] ProcessRequest(string path, Stream request, | ||
81 | IOSHttpRequest httpRequest, IOSHttpResponse httpResponse) | ||
82 | { | ||
83 | bool result = false; | ||
84 | |||
85 | string[] p = SplitParams(path); | ||
86 | |||
87 | if (p.Length > 0) | ||
88 | { | ||
89 | if (m_allowedTypes != AllowedRemoteDeleteTypes.None) | ||
90 | { | ||
91 | string assetID = p[0]; | ||
92 | |||
93 | AssetBase asset = m_AssetService.Get(assetID); | ||
94 | if (asset != null) | ||
95 | { | ||
96 | if (m_allowedTypes == AllowedRemoteDeleteTypes.All | ||
97 | || (int)(asset.Flags & AssetFlags.Maptile) != 0) | ||
98 | { | ||
99 | result = m_AssetService.Delete(assetID); | ||
100 | } | ||
101 | else | ||
102 | { | ||
103 | m_log.DebugFormat( | ||
104 | "[ASSET SERVER DELETE HANDLER]: Request to delete asset {0}, but type is {1} and allowed remote delete types are {2}", | ||
105 | assetID, (AssetFlags)asset.Flags, m_allowedTypes); | ||
106 | } | ||
107 | } | ||
108 | } | ||
109 | } | ||
110 | |||
111 | XmlSerializer xs = new XmlSerializer(typeof(bool)); | ||
112 | return ServerUtils.SerializeResult(xs, result); | ||
113 | } | ||
114 | } | ||
115 | } \ No newline at end of file | ||
diff --git a/OpenSim/Server/Handlers/Asset/AssetServerGetHandler.cs b/OpenSim/Server/Handlers/Asset/AssetServerGetHandler.cs new file mode 100644 index 0000000..91c5c54 --- /dev/null +++ b/OpenSim/Server/Handlers/Asset/AssetServerGetHandler.cs | |||
@@ -0,0 +1,172 @@ | |||
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 | } | ||
diff --git a/OpenSim/Server/Handlers/Asset/AssetServerPostHandler.cs b/OpenSim/Server/Handlers/Asset/AssetServerPostHandler.cs new file mode 100644 index 0000000..1c706a7 --- /dev/null +++ b/OpenSim/Server/Handlers/Asset/AssetServerPostHandler.cs | |||
@@ -0,0 +1,98 @@ | |||
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.Reflection; | ||
32 | using System.IO; | ||
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 AssetServerPostHandler : BaseStreamHandler | ||
47 | { | ||
48 | // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
49 | |||
50 | private IAssetService m_AssetService; | ||
51 | |||
52 | public AssetServerPostHandler(IAssetService service) : | ||
53 | base("POST", "/assets") | ||
54 | { | ||
55 | m_AssetService = service; | ||
56 | } | ||
57 | |||
58 | public AssetServerPostHandler(IAssetService service, IServiceAuth auth) : | ||
59 | base("POST", "/assets", auth) | ||
60 | { | ||
61 | m_AssetService = service; | ||
62 | } | ||
63 | |||
64 | protected override byte[] ProcessRequest(string path, Stream request, | ||
65 | IOSHttpRequest httpRequest, IOSHttpResponse httpResponse) | ||
66 | { | ||
67 | AssetBase asset; | ||
68 | XmlSerializer xs = new XmlSerializer(typeof(AssetBase)); | ||
69 | |||
70 | try | ||
71 | { | ||
72 | asset = (AssetBase)xs.Deserialize(request); | ||
73 | } | ||
74 | catch (Exception) | ||
75 | { | ||
76 | httpResponse.StatusCode = (int)HttpStatusCode.BadRequest; | ||
77 | return null; | ||
78 | } | ||
79 | |||
80 | string[] p = SplitParams(path); | ||
81 | if (p.Length > 0) | ||
82 | { | ||
83 | string id = p[0]; | ||
84 | bool result = m_AssetService.UpdateContent(id, asset.Data); | ||
85 | |||
86 | xs = new XmlSerializer(typeof(bool)); | ||
87 | return ServerUtils.SerializeResult(xs, result); | ||
88 | } | ||
89 | else | ||
90 | { | ||
91 | string id = m_AssetService.Store(asset); | ||
92 | |||
93 | xs = new XmlSerializer(typeof(string)); | ||
94 | return ServerUtils.SerializeResult(xs, id); | ||
95 | } | ||
96 | } | ||
97 | } | ||
98 | } | ||
diff --git a/OpenSim/Server/Handlers/Asset/AssetsExistHandler.cs b/OpenSim/Server/Handlers/Asset/AssetsExistHandler.cs new file mode 100644 index 0000000..32901b3 --- /dev/null +++ b/OpenSim/Server/Handlers/Asset/AssetsExistHandler.cs | |||
@@ -0,0 +1,87 @@ | |||
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.Reflection; | ||
32 | using System.IO; | ||
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 | using OpenMetaverse; | ||
44 | |||
45 | namespace OpenSim.Server.Handlers.Asset | ||
46 | { | ||
47 | public class AssetsExistHandler : BaseStreamHandler | ||
48 | { | ||
49 | //private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
50 | |||
51 | private IAssetService m_AssetService; | ||
52 | |||
53 | public AssetsExistHandler(IAssetService service) : | ||
54 | base("POST", "/get_assets_exist") | ||
55 | { | ||
56 | m_AssetService = service; | ||
57 | } | ||
58 | |||
59 | public AssetsExistHandler(IAssetService service, IServiceAuth auth) : | ||
60 | base("POST", "/get_assets_exist", auth) | ||
61 | { | ||
62 | m_AssetService = service; | ||
63 | } | ||
64 | |||
65 | protected override byte[] ProcessRequest(string path, Stream request, IOSHttpRequest httpRequest, IOSHttpResponse httpResponse) | ||
66 | { | ||
67 | XmlSerializer xs; | ||
68 | |||
69 | string[] ids; | ||
70 | try | ||
71 | { | ||
72 | xs = new XmlSerializer(typeof(string[])); | ||
73 | ids = (string[])xs.Deserialize(request); | ||
74 | } | ||
75 | catch (Exception) | ||
76 | { | ||
77 | httpResponse.StatusCode = (int)HttpStatusCode.BadRequest; | ||
78 | return null; | ||
79 | } | ||
80 | |||
81 | bool[] exist = m_AssetService.AssetsExist(ids); | ||
82 | |||
83 | xs = new XmlSerializer(typeof(bool[])); | ||
84 | return ServerUtils.SerializeResult(xs, exist); | ||
85 | } | ||
86 | } | ||
87 | } | ||
diff --git a/OpenSim/Server/Handlers/Asset/Tests/AssetServerPostHandlerTests.cs b/OpenSim/Server/Handlers/Asset/Tests/AssetServerPostHandlerTests.cs new file mode 100644 index 0000000..faa6fb7 --- /dev/null +++ b/OpenSim/Server/Handlers/Asset/Tests/AssetServerPostHandlerTests.cs | |||
@@ -0,0 +1,109 @@ | |||
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 System; | ||
29 | using System.IO; | ||
30 | using System.Net; | ||
31 | using System.Text; | ||
32 | using System.Xml; | ||
33 | using System.Xml.Serialization; | ||
34 | using Nini.Config; | ||
35 | using NUnit.Framework; | ||
36 | using OpenMetaverse; | ||
37 | using OpenSim.Framework; | ||
38 | using OpenSim.Server.Handlers.Asset; | ||
39 | using OpenSim.Services.AssetService; | ||
40 | using OpenSim.Services.Interfaces; | ||
41 | using OpenSim.Tests.Common; | ||
42 | |||
43 | namespace OpenSim.Server.Handlers.Asset.Test | ||
44 | { | ||
45 | [TestFixture] | ||
46 | public class AssetServerPostHandlerTests : OpenSimTestCase | ||
47 | { | ||
48 | [Test] | ||
49 | public void TestGoodAssetStoreRequest() | ||
50 | { | ||
51 | TestHelpers.InMethod(); | ||
52 | |||
53 | UUID assetId = TestHelpers.ParseTail(0x1); | ||
54 | |||
55 | IConfigSource config = new IniConfigSource(); | ||
56 | config.AddConfig("AssetService"); | ||
57 | config.Configs["AssetService"].Set("StorageProvider", "OpenSim.Tests.Common.dll"); | ||
58 | |||
59 | AssetService assetService = new AssetService(config); | ||
60 | |||
61 | AssetServerPostHandler asph = new AssetServerPostHandler(assetService); | ||
62 | |||
63 | AssetBase asset = AssetHelpers.CreateNotecardAsset(assetId, "Hello World"); | ||
64 | |||
65 | MemoryStream buffer = new MemoryStream(); | ||
66 | |||
67 | XmlWriterSettings settings = new XmlWriterSettings(); | ||
68 | settings.Encoding = Encoding.UTF8; | ||
69 | |||
70 | using (XmlWriter writer = XmlWriter.Create(buffer, settings)) | ||
71 | { | ||
72 | XmlSerializer serializer = new XmlSerializer(typeof(AssetBase)); | ||
73 | serializer.Serialize(writer, asset); | ||
74 | writer.Flush(); | ||
75 | } | ||
76 | |||
77 | buffer.Position = 0; | ||
78 | asph.Handle(null, buffer, null, null); | ||
79 | |||
80 | AssetBase retrievedAsset = assetService.Get(assetId.ToString()); | ||
81 | |||
82 | Assert.That(retrievedAsset, Is.Not.Null); | ||
83 | } | ||
84 | |||
85 | [Test] | ||
86 | public void TestBadXmlAssetStoreRequest() | ||
87 | { | ||
88 | TestHelpers.InMethod(); | ||
89 | |||
90 | IConfigSource config = new IniConfigSource(); | ||
91 | config.AddConfig("AssetService"); | ||
92 | config.Configs["AssetService"].Set("StorageProvider", "OpenSim.Tests.Common.dll"); | ||
93 | |||
94 | AssetService assetService = new AssetService(config); | ||
95 | |||
96 | AssetServerPostHandler asph = new AssetServerPostHandler(assetService); | ||
97 | |||
98 | MemoryStream buffer = new MemoryStream(); | ||
99 | byte[] badData = new byte[] { 0x48, 0x65, 0x6c, 0x6c, 0x6f }; | ||
100 | buffer.Write(badData, 0, badData.Length); | ||
101 | buffer.Position = 0; | ||
102 | |||
103 | TestOSHttpResponse response = new TestOSHttpResponse(); | ||
104 | asph.Handle(null, buffer, null, response); | ||
105 | |||
106 | Assert.That(response.StatusCode, Is.EqualTo((int)HttpStatusCode.BadRequest)); | ||
107 | } | ||
108 | } | ||
109 | } \ No newline at end of file | ||