diff options
Diffstat (limited to 'OpenSim/Framework/Servers/HttpServer/SynchronousRestObjectRequester.cs')
-rw-r--r-- | OpenSim/Framework/Servers/HttpServer/SynchronousRestObjectRequester.cs | 137 |
1 files changed, 0 insertions, 137 deletions
diff --git a/OpenSim/Framework/Servers/HttpServer/SynchronousRestObjectRequester.cs b/OpenSim/Framework/Servers/HttpServer/SynchronousRestObjectRequester.cs deleted file mode 100644 index 077a1e8..0000000 --- a/OpenSim/Framework/Servers/HttpServer/SynchronousRestObjectRequester.cs +++ /dev/null | |||
@@ -1,137 +0,0 @@ | |||
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 | |||
35 | namespace OpenSim.Framework.Servers.HttpServer | ||
36 | { | ||
37 | public class SynchronousRestObjectPoster | ||
38 | { | ||
39 | [Obsolete] | ||
40 | public static TResponse BeginPostObject<TRequest, TResponse>(string verb, string requestUrl, TRequest obj) | ||
41 | { | ||
42 | return SynchronousRestObjectRequester.MakeRequest<TRequest, TResponse>(verb, requestUrl, obj); | ||
43 | } | ||
44 | } | ||
45 | |||
46 | public class SynchronousRestObjectRequester | ||
47 | { | ||
48 | /// <summary> | ||
49 | /// Perform a synchronous REST request. | ||
50 | /// </summary> | ||
51 | /// <param name="verb"></param> | ||
52 | /// <param name="requestUrl"></param> | ||
53 | /// <param name="obj"> </param> | ||
54 | /// <returns></returns> | ||
55 | /// | ||
56 | /// <exception cref="System.Net.WebException">Thrown if we encounter a network issue while posting | ||
57 | /// the request. You'll want to make sure you deal with this as they're not uncommon</exception> | ||
58 | public static TResponse MakeRequest<TRequest, TResponse>(string verb, string requestUrl, TRequest obj) | ||
59 | { | ||
60 | return MakeRequest<TRequest, TResponse>(verb, requestUrl, obj, 100); | ||
61 | } | ||
62 | /// <summary> | ||
63 | /// Perform a synchronous REST request. | ||
64 | /// </summary> | ||
65 | /// <param name="verb"></param> | ||
66 | /// <param name="requestUrl"></param> | ||
67 | /// <param name="obj"> </param> | ||
68 | /// <param name="timeout"> </param> | ||
69 | /// <returns></returns> | ||
70 | /// | ||
71 | /// <exception cref="System.Net.WebException">Thrown if we encounter a network issue while posting | ||
72 | /// the request. You'll want to make sure you deal with this as they're not uncommon</exception> | ||
73 | public static TResponse MakeRequest<TRequest, TResponse>(string verb, string requestUrl, TRequest obj, int pTimeout) | ||
74 | { | ||
75 | Type type = typeof (TRequest); | ||
76 | TResponse deserial = default(TResponse); | ||
77 | |||
78 | WebRequest request = WebRequest.Create(requestUrl); | ||
79 | request.Method = verb; | ||
80 | request.Timeout = pTimeout * 1000; | ||
81 | |||
82 | if ((verb == "POST") || (verb == "PUT")) | ||
83 | { | ||
84 | request.ContentType = "text/xml"; | ||
85 | |||
86 | MemoryStream buffer = new MemoryStream(); | ||
87 | |||
88 | XmlWriterSettings settings = new XmlWriterSettings(); | ||
89 | settings.Encoding = Encoding.UTF8; | ||
90 | |||
91 | using (XmlWriter writer = XmlWriter.Create(buffer, settings)) | ||
92 | { | ||
93 | XmlSerializer serializer = new XmlSerializer(type); | ||
94 | serializer.Serialize(writer, obj); | ||
95 | writer.Flush(); | ||
96 | } | ||
97 | |||
98 | int length = (int) buffer.Length; | ||
99 | request.ContentLength = length; | ||
100 | Stream requestStream = null; | ||
101 | try | ||
102 | { | ||
103 | requestStream = request.GetRequestStream(); | ||
104 | requestStream.Write(buffer.ToArray(), 0, length); | ||
105 | } | ||
106 | catch (Exception) | ||
107 | { | ||
108 | return deserial; | ||
109 | } | ||
110 | finally | ||
111 | { | ||
112 | if (requestStream != null) | ||
113 | requestStream.Close(); | ||
114 | } | ||
115 | } | ||
116 | |||
117 | try | ||
118 | { | ||
119 | using (WebResponse resp = request.GetResponse()) | ||
120 | { | ||
121 | if (resp.ContentLength != 0) | ||
122 | { | ||
123 | Stream respStream = resp.GetResponseStream(); | ||
124 | XmlSerializer deserializer = new XmlSerializer(typeof(TResponse)); | ||
125 | deserial = (TResponse)deserializer.Deserialize(respStream); | ||
126 | respStream.Close(); | ||
127 | } | ||
128 | } | ||
129 | } | ||
130 | catch (System.InvalidOperationException) | ||
131 | { | ||
132 | // This is what happens when there is invalid XML | ||
133 | } | ||
134 | return deserial; | ||
135 | } | ||
136 | } | ||
137 | } | ||