aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Framework/Servers/HttpServer
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--OpenSim/Framework/Servers/HttpServer/AsynchronousRestObjectRequester.cs126
-rw-r--r--OpenSim/Framework/Servers/HttpServer/SynchronousRestObjectRequester.cs (renamed from OpenSim/Framework/Servers/HttpServer/SynchronousRestObjectPoster.cs)0
2 files changed, 126 insertions, 0 deletions
diff --git a/OpenSim/Framework/Servers/HttpServer/AsynchronousRestObjectRequester.cs b/OpenSim/Framework/Servers/HttpServer/AsynchronousRestObjectRequester.cs
new file mode 100644
index 0000000..a63fd41
--- /dev/null
+++ b/OpenSim/Framework/Servers/HttpServer/AsynchronousRestObjectRequester.cs
@@ -0,0 +1,126 @@
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
28using System;
29using System.IO;
30using System.Net;
31using System.Text;
32using System.Xml;
33using System.Xml.Serialization;
34
35namespace OpenSim.Framework.Servers.HttpServer
36{
37 public class AsynchronousRestObjectRequester
38 {
39 /// <summary>
40 /// Perform an asynchronous REST request.
41 /// </summary>
42 /// <param name="verb"></param>
43 /// <param name="requestUrl"></param>
44 /// <param name="obj"></param>
45 /// <param name="action"></param>
46 /// <returns></returns>
47 ///
48 /// <exception cref="System.Net.WebException">Thrown if we encounter a
49 /// network issue while posting the request. You'll want to make
50 /// sure you deal with this as they're not uncommon</exception>
51 //
52 public static void MakeRequest<TRequest, TResponse>(string verb,
53 string requestUrl, TRequest obj, Action<TResponse> action)
54 {
55 Type type = typeof (TRequest);
56
57 WebRequest request = WebRequest.Create(requestUrl);
58 WebResponse response = null;
59 TResponse deserial = default(TResponse);
60 XmlSerializer deserializer = new XmlSerializer(typeof (TResponse));
61
62 request.Method = verb;
63
64 if (verb == "POST")
65 {
66 request.ContentType = "text/xml";
67
68 MemoryStream buffer = new MemoryStream();
69
70 XmlWriterSettings settings = new XmlWriterSettings();
71 settings.Encoding = Encoding.UTF8;
72
73 using (XmlWriter writer = XmlWriter.Create(buffer, settings))
74 {
75 XmlSerializer serializer = new XmlSerializer(type);
76 serializer.Serialize(writer, obj);
77 writer.Flush();
78 }
79
80 int length = (int) buffer.Length;
81 request.ContentLength = length;
82
83 request.BeginGetRequestStream(delegate(IAsyncResult res)
84 {
85 Stream requestStream = request.EndGetRequestStream(res);
86
87 requestStream.Write(buffer.ToArray(), 0, length);
88
89 request.BeginGetResponse(delegate(IAsyncResult ar)
90 {
91 response = request.EndGetResponse(ar);
92
93 try
94 {
95 deserial = (TResponse) deserializer.Deserialize(
96 response.GetResponseStream());
97 }
98 catch (System.InvalidOperationException)
99 {
100 }
101
102 action(deserial);
103 }, null);
104 }, null);
105
106 return;
107 }
108
109 request.BeginGetResponse(delegate(IAsyncResult res2)
110 {
111 response = request.EndGetResponse(res2);
112
113 try
114 {
115 deserial = (TResponse) deserializer.Deserialize(
116 response.GetResponseStream());
117 }
118 catch (System.InvalidOperationException)
119 {
120 }
121
122 action(deserial);
123 }, null);
124 }
125 }
126}
diff --git a/OpenSim/Framework/Servers/HttpServer/SynchronousRestObjectPoster.cs b/OpenSim/Framework/Servers/HttpServer/SynchronousRestObjectRequester.cs
index 71e5b69..71e5b69 100644
--- a/OpenSim/Framework/Servers/HttpServer/SynchronousRestObjectPoster.cs
+++ b/OpenSim/Framework/Servers/HttpServer/SynchronousRestObjectRequester.cs