diff options
Diffstat (limited to 'OpenSim/Framework/Servers/HttpServer/AsynchronousRestObjectRequester.cs')
-rw-r--r-- | OpenSim/Framework/Servers/HttpServer/AsynchronousRestObjectRequester.cs | 192 |
1 files changed, 0 insertions, 192 deletions
diff --git a/OpenSim/Framework/Servers/HttpServer/AsynchronousRestObjectRequester.cs b/OpenSim/Framework/Servers/HttpServer/AsynchronousRestObjectRequester.cs deleted file mode 100644 index 03c12dd..0000000 --- a/OpenSim/Framework/Servers/HttpServer/AsynchronousRestObjectRequester.cs +++ /dev/null | |||
@@ -1,192 +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.Reflection; | ||
32 | using System.Text; | ||
33 | using System.Xml; | ||
34 | using System.Xml.Serialization; | ||
35 | using log4net; | ||
36 | |||
37 | namespace OpenSim.Framework.Servers.HttpServer | ||
38 | { | ||
39 | public class AsynchronousRestObjectRequester | ||
40 | { | ||
41 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
42 | |||
43 | /// <summary> | ||
44 | /// Perform an asynchronous REST request. | ||
45 | /// </summary> | ||
46 | /// <param name="verb">GET or POST</param> | ||
47 | /// <param name="requestUrl"></param> | ||
48 | /// <param name="obj"></param> | ||
49 | /// <param name="action"></param> | ||
50 | /// <returns></returns> | ||
51 | /// | ||
52 | /// <exception cref="System.Net.WebException">Thrown if we encounter a | ||
53 | /// network issue while posting the request. You'll want to make | ||
54 | /// sure you deal with this as they're not uncommon</exception> | ||
55 | // | ||
56 | public static void MakeRequest<TRequest, TResponse>(string verb, | ||
57 | string requestUrl, TRequest obj, Action<TResponse> action) | ||
58 | { | ||
59 | // m_log.DebugFormat("[ASYNC REQUEST]: Starting {0} {1}", verb, requestUrl); | ||
60 | |||
61 | Type type = typeof (TRequest); | ||
62 | |||
63 | WebRequest request = WebRequest.Create(requestUrl); | ||
64 | WebResponse response = null; | ||
65 | TResponse deserial = default(TResponse); | ||
66 | XmlSerializer deserializer = new XmlSerializer(typeof (TResponse)); | ||
67 | |||
68 | request.Method = verb; | ||
69 | |||
70 | if (verb == "POST") | ||
71 | { | ||
72 | request.ContentType = "text/xml"; | ||
73 | |||
74 | MemoryStream buffer = new MemoryStream(); | ||
75 | |||
76 | XmlWriterSettings settings = new XmlWriterSettings(); | ||
77 | settings.Encoding = Encoding.UTF8; | ||
78 | |||
79 | using (XmlWriter writer = XmlWriter.Create(buffer, settings)) | ||
80 | { | ||
81 | XmlSerializer serializer = new XmlSerializer(type); | ||
82 | serializer.Serialize(writer, obj); | ||
83 | writer.Flush(); | ||
84 | } | ||
85 | |||
86 | int length = (int) buffer.Length; | ||
87 | request.ContentLength = length; | ||
88 | |||
89 | request.BeginGetRequestStream(delegate(IAsyncResult res) | ||
90 | { | ||
91 | Stream requestStream = request.EndGetRequestStream(res); | ||
92 | |||
93 | requestStream.Write(buffer.ToArray(), 0, length); | ||
94 | requestStream.Close(); | ||
95 | |||
96 | request.BeginGetResponse(delegate(IAsyncResult ar) | ||
97 | { | ||
98 | response = request.EndGetResponse(ar); | ||
99 | Stream respStream = null; | ||
100 | try | ||
101 | { | ||
102 | respStream = response.GetResponseStream(); | ||
103 | deserial = (TResponse)deserializer.Deserialize( | ||
104 | respStream); | ||
105 | } | ||
106 | catch (System.InvalidOperationException) | ||
107 | { | ||
108 | } | ||
109 | finally | ||
110 | { | ||
111 | // Let's not close this | ||
112 | //buffer.Close(); | ||
113 | respStream.Close(); | ||
114 | response.Close(); | ||
115 | } | ||
116 | |||
117 | action(deserial); | ||
118 | |||
119 | }, null); | ||
120 | }, null); | ||
121 | |||
122 | |||
123 | return; | ||
124 | } | ||
125 | |||
126 | request.BeginGetResponse(delegate(IAsyncResult res2) | ||
127 | { | ||
128 | try | ||
129 | { | ||
130 | // If the server returns a 404, this appears to trigger a System.Net.WebException even though that isn't | ||
131 | // documented in MSDN | ||
132 | response = request.EndGetResponse(res2); | ||
133 | |||
134 | Stream respStream = null; | ||
135 | try | ||
136 | { | ||
137 | respStream = response.GetResponseStream(); | ||
138 | deserial = (TResponse)deserializer.Deserialize(respStream); | ||
139 | } | ||
140 | catch (System.InvalidOperationException) | ||
141 | { | ||
142 | } | ||
143 | finally | ||
144 | { | ||
145 | respStream.Close(); | ||
146 | response.Close(); | ||
147 | } | ||
148 | } | ||
149 | catch (WebException e) | ||
150 | { | ||
151 | if (e.Status == WebExceptionStatus.ProtocolError) | ||
152 | { | ||
153 | if (e.Response is HttpWebResponse) | ||
154 | { | ||
155 | HttpWebResponse httpResponse = (HttpWebResponse)e.Response; | ||
156 | |||
157 | if (httpResponse.StatusCode != HttpStatusCode.NotFound) | ||
158 | { | ||
159 | // We don't appear to be handling any other status codes, so log these feailures to that | ||
160 | // people don't spend unnecessary hours hunting phantom bugs. | ||
161 | m_log.DebugFormat( | ||
162 | "[ASYNC REQUEST]: Request {0} {1} failed with unexpected status code {2}", | ||
163 | verb, requestUrl, httpResponse.StatusCode); | ||
164 | } | ||
165 | } | ||
166 | } | ||
167 | else | ||
168 | { | ||
169 | m_log.ErrorFormat("[ASYNC REQUEST]: Request {0} {1} failed with status {2} and message {3}", verb, requestUrl, e.Status, e.Message); | ||
170 | } | ||
171 | } | ||
172 | catch (Exception e) | ||
173 | { | ||
174 | m_log.ErrorFormat("[ASYNC REQUEST]: Request {0} {1} failed with exception {2}", verb, requestUrl, e); | ||
175 | } | ||
176 | |||
177 | // m_log.DebugFormat("[ASYNC REQUEST]: Received {0}", deserial.ToString()); | ||
178 | |||
179 | try | ||
180 | { | ||
181 | action(deserial); | ||
182 | } | ||
183 | catch (Exception e) | ||
184 | { | ||
185 | m_log.ErrorFormat( | ||
186 | "[ASYNC REQUEST]: Request {0} {1} callback failed with exception {2}", verb, requestUrl, e); | ||
187 | } | ||
188 | |||
189 | }, null); | ||
190 | } | ||
191 | } | ||
192 | } | ||