-
Notifications
You must be signed in to change notification settings - Fork 22
/
ServiceRequest.cs
306 lines (271 loc) · 12.6 KB
/
ServiceRequest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/* Software License Agreement (BSD License)
*
* Copyright (c) 2010-2011, Rustici Software, LLC
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL Rustici Software, LLC BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Web;
using System.Xml;
using System.Threading;
using System.Collections.Specialized;
namespace RusticiSoftware.HostedEngine.Client
{
/// <summary>
/// Main class used to invoke Hosted Engine web service methods.
/// </summary>
public class ServiceRequest
{
private IDictionary<string, object> methodParameters = new Dictionary<string, object>();
private string fileToPost = null;
private NameValueCollection dataToPost = null;
private Configuration configuration = null;
private String engineServiceUrl = null;
/// <summary>
/// Constructor
/// </summary>
/// <param name="configuration">Application Configuration</param>
public ServiceRequest(Configuration configuration)
{
this.configuration = configuration;
//Keep local copy of services url in case we need to change
//it (in just this request), like setting a particular server
this.engineServiceUrl = configuration.ScormEngineServiceUrl;
}
/// <summary>
/// Method-specific parameters for the web service method
/// </summary>
public IDictionary<string, object> Parameters
{
get { return methodParameters; }
set { methodParameters = value; }
}
/// <summary>
/// Optional full file path to the source file if required for the
/// web method
/// </summary>
public string FileToPost
{
get
{
return fileToPost;
}
set
{
if (!File.Exists(value))
{
throw new ArgumentException(
String.Format("Path provided for FileToPost does not point to an existing file. Value: '{0}'", value));
}
fileToPost = value;
}
}
public NameValueCollection DataToPost
{
get { return dataToPost; }
set { dataToPost = value; }
}
/// <summary>
/// Server at which to make the request
/// </summary>
public string Server
{
get
{
String serviceUrl = this.configuration.ScormEngineServiceUrl;
int beginIndex = serviceUrl.IndexOf("://");
String server = serviceUrl.Substring(beginIndex + 3);
int endIndex = server.IndexOf("/");
if (endIndex == -1) {
endIndex = server.Length;
}
return server.Substring(0, endIndex);
}
set
{
this.engineServiceUrl = this.engineServiceUrl.Replace(this.Server, value);
}
}
/// <summary>
/// Main method for invoking HostedEngine web services based on the
/// HostedEngine.WebServicesCore engine
/// </summary>
/// <param name="methodName">Method name passed on the call to the api</param>
/// <returns>Response string unless response indicstes an error. If the web service
/// response indicates a failure, a ServiceException is thrown</returns>
public XmlDocument CallService(string methodName)
{
return GetXmlResponseFromUrl(ConstructUrl(methodName));
}
public string GetFileFromService(string toFileName, string methodName)
{
return GetFileResponseFromUrl(toFileName, ConstructUrl(methodName));
}
public byte[] GetFileFromService(string methodName)
{
string url = ConstructUrl(methodName);
return GetResponseFromUrl(url);
}
public string GetFileResponseFromUrl(string toFileName, string url)
{
byte[] responseBytes = GetResponseFromUrl(url);
FileStream fs = File.Create(toFileName);
fs.Write(responseBytes, 0, responseBytes.Length);
fs.Close();
return toFileName;
}
/// <summary>
/// Get the xml response from a fully prepared (signed) service call URL
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public XmlDocument GetXmlResponseFromUrl(string url)
{
byte[] responseBytes = GetResponseFromUrl(url);
string responseText = Encoding.GetEncoding("utf-8").GetString(responseBytes);
XmlDocument response = AssertNoErrorAndReturnXmlDoc(responseText);
// // debug!
// string path = @"C:\Documents and Settings\jhayden\Desktop\response.xml";
// if (File.Exists(path)) File.Delete(path);
// using (TextWriter tw = new StreamWriter(path))
// tw.Write(response.OuterXml);
return response;
}
public byte[] GetResponseFromUrl(string url)
{
// If we have a file to post, do a POST with the file as the payload, otherwise
// do a simple GET
byte[] responseBytes;
int retries = 6;
int msWait = 200;
while (retries > 0) {
try {
//TODO: WebClient nicely wraps this functionaly but if we need more power
// or diagnostics we might move to other WebRequest types that are available.
using (CustomWebClient wc = new CustomWebClient()) {
if (fileToPost != null)
responseBytes = wc.UploadFile(url, fileToPost);
else if (dataToPost != null)
responseBytes = wc.UploadValues(url, dataToPost);
else
responseBytes = wc.DownloadData(url);
}
return responseBytes;
}
catch (WebException) {
Thread.Sleep(msWait);
retries--;
msWait *= 2;
if (retries == 0) {
throw;
}
}
}
throw new Exception("Could not retrieve a response from " + this.Server);
}
/// <summary>
/// This method will evaluate the reponse string and manually validate
/// the top-level structure. If an err is present, this will be turned
/// into a Service Exception.
/// </summary>
/// <param name="xmlString">Response from web service as xml</param>
/// <returns>XML document from the given string, provided no service errors are present</returns>
private static XmlDocument AssertNoErrorAndReturnXmlDoc(string xmlString)
{
if (!xmlString.StartsWith("<?xml"))
{
throw new ServiceException(ErrorCode.INVALID_WEB_SERVICE_RESPONSE, "Expecting XML Response from web service call, instead received: " + xmlString);
}
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlString);
XmlNodeList rspElements = xmlDoc.GetElementsByTagName("rsp");
if (rspElements.Count == 0)
{
throw new ServiceException(ErrorCode.INVALID_WEB_SERVICE_RESPONSE, "Invalid XML Response from web service call, expected <rsp> tag, instead received: " + xmlString);
}
XmlAttribute statusAttr = rspElements[0].Attributes["stat"];
if (statusAttr == null || statusAttr.Value == null)
{
throw new ServiceException(ErrorCode.INVALID_WEB_SERVICE_RESPONSE, "Invalid XML Response from web service call, expected 'stat' attribute on <rsp> tag, instead received: " + xmlString);
}
string status = statusAttr.Value.ToLower();
if (status != "ok")
{
if (status!= "fail")
{
throw new ServiceException(ErrorCode.INVALID_WEB_SERVICE_RESPONSE, "Invalid XML Response from web service call, expected 'stat' value of 'ok' or 'fail' attribute on <rsp> tag, instead received stat value: " + status);
}
XmlNode errNode = rspElements[0].FirstChild;
if (errNode.Name != "err")
{
throw new ServiceException(ErrorCode.INVALID_WEB_SERVICE_RESPONSE, "Invalid XML Response from web service call, expected <err> node since stat='fail', instead received : " + xmlString);
}
throw new ServiceException(Convert.ToInt32(errNode.Attributes["code"].Value), errNode.Attributes["msg"].Value);
}
return xmlDoc;
}
/// <summary>
/// Given the method name and the parameters and configuration associated
/// with this object, generate the full URL for the web service invocation.
/// </summary>
/// <param name="methodName">Method name for the HOSTED Engine api call</param>
/// <returns>Fully qualified URL to be used for invocation</returns>
public string ConstructUrl(string methodName)
{
// The local parameter map just contains method methodParameters. We'll
// now create a complete parameter map that contains the web-service
// params as well the actual method params.
IDictionary<string, object> parameterMap = new Dictionary<string, object>();
parameterMap.Add("method", methodName);
parameterMap.Add("appid", configuration.AppId);
parameterMap.Add("origin", configuration.Origin);
parameterMap.Add("ts", DateTime.UtcNow.ToString("yyyyMMddHHmmss"));
parameterMap.Add("applib", "net");
foreach(string key in methodParameters.Keys)
{
parameterMap.Add(key, methodParameters[key]);
}
// Construct the url, concatonate all parameters as query string parameters
string url = this.engineServiceUrl + "/api";
int cnt = 0;
foreach(string key in parameterMap.Keys)
{
// Create a query string with URL-encoded values
url += (cnt++ == 0 ? "?" : "&") + key + "=" + HttpUtility.UrlEncode(parameterMap[key].ToString());
}
if (dataToPost != null) {
foreach(string key in dataToPost)
{
parameterMap.Add(key, dataToPost[key]);
}
}
url += "&sig=" + RequestSigner.GetSignatureForRequest(configuration.SecurityKey, parameterMap);
return url;
}
}
}