1 using CPE.App.Web.Code;
3 using System.Collections.Specialized;
6 using System.Text.RegularExpressions;
10 namespace CPE.App.Web.Connect {
13 /// Handles a basic request to the Connect service.
15 public class Request {
18 private XmlDocument _xmlResults;
19 private NameValueCollection _parameters = new NameValueCollection();
20 private Session _session = new Session();
22 private static string CleanHost(string url) {
23 if (!url.StartsWith("http"))
24 url = "http://" + url;
29 /// The action to perform against the service.
31 /// <example>common-info</example>
32 public string Action { get; set; }
35 /// The parameters to pass with the action for this request.
37 public NameValueCollection Parameters {
47 /// The Session settings to contact the service.
49 public Session Session {
59 /// Returns the raw XmlDocument that was returned from the service.
61 public XmlDocument XmlResults { get { return _xmlResults; }}
64 /// Returns the current status of the request.
66 public Status Status {
70 Status status = Status.Unknown;
72 // Has a response been received?
75 // Parse the response for the status
76 string statusCode = _xmlResults.SelectSingleNode("//status").Attributes["code"].Value;
83 status = Status.Invalid;
86 status = Status.NoAccess;
89 status = Status.NoData;
92 status = Status.TooMuchData;
102 /// Returns whether a response has been received for this request.
104 public bool HasResponse { get { return (_xmlResults != null); } }
107 /// Returns the response headers for this request. Useful for retrieving the session key from a login request.
109 public WebHeaderCollection ResponseHeaders { get; private set; }
112 /// Initializes an empty request.
117 /// Initializes a request.
119 /// <param name="hostname">The hostname to connect to.</param>
120 /// <param name="action">The action to perform.</param>
121 public Request(string hostname, string action) {
122 Session.Hostname = hostname;
127 /// Initializes the request.
129 /// <param name="session">The session object to connect with.</param>
130 /// <param name="action">The action to perform.</param>
131 public Request(Session session, string action) {
137 private string _url = null;
147 /// Sends the request to the service and retrieves the results.
149 /// <returns>Returns true if the request came back or false if an exception was thrown.</returns>
150 public virtual bool Execute() {
154 // Example URL: http://connectpro41408666.acrobat.com/api/xml?action=common-info
157 var host = CleanHost(Session.Hostname);
158 var url = new StringBuilder(string.Format("{0}/api/xml?action={1}", host, Action));
160 // Is a session key set?
161 if(Session.SessionKey != null)
162 Parameters.Add("session", Session.SessionKey);
164 // Add parameters to querystring
165 for(int i = 0; i < Parameters.Count; i++) {
166 for(int x = 0; x < Parameters.GetValues(i).Length; x++) {
167 url.AppendFormat("&{0}={1}", HttpUtility.UrlEncode(Parameters.Keys[i]), HttpUtility.UrlEncode(Parameters.GetValues(i)[x]));
171 // Ignore SSL validation errors
172 //ServicePointManager.ServerCertificateValidationCallback = IgnoreInvalidSSL;
177 using(var request = new WebClient() { UseDefaultCredentials = true, Encoding = Encoding.UTF8 }) {
179 // Is a proxy required?
180 if(Session.Proxy != null)
181 request.Proxy = new WebProxy(Session.Proxy, true) { UseDefaultCredentials = true };
183 //Extensions.LogServiceCall("[Request][Execute]", String.Format("url = {0}", url.ToString()));
187 // System.IO.File.AppendAllText("adobeconnect_log.txt", url.ToString() + Environment.NewLine);
196 xml = request.DownloadString(url.ToString());
197 ResponseHeaders = request.ResponseHeaders;
201 //var log = new ExceptionLogController();
202 //log.AddLog(ex, ExceptionLogController.ExceptionLogType.GENERAL_EXCEPTION);
203 //log.AddLog(new Exception(url.ToString()), ExceptionLogController.ExceptionLogType.GENERAL_EXCEPTION);
204 Extensions.LogServiceError("[Request][Execute]", ex);
210 _xmlResults = new XmlDocument();
211 _xmlResults.LoadXml(xml);
212 _url = url.ToString();
222 ResponseHeaders = null;
227 /// Overrides the default vertifcation for SSL certificates so that they will always be accepted.
229 private static bool IgnoreInvalidSsl(object sender, System.Security.Cryptography.X509Certificates.X509Certificate cert, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors errors) {
234 /// Returns the session key that is returned in the ResponseHeaders from Connect.
236 /// <returns>The session key, if found. Returns an empty string if not found.</returns>
237 public string ExtractSessionKey() {
239 // Default to returning an empty string if the key isn't found
240 string session = string.Empty;
242 // Does the cookie header exist?
243 if(ResponseHeaders != null && ResponseHeaders["Set-Cookie"] != null) {
245 // Example session cookie
246 // BREEZESESSION=Sbreez9u75m9zfau4pi8m8;HttpOnly;domain=.acrobat.com;path=/
248 // Capture session key
249 var regex = new Regex(@"BREEZESESSION=(?<session>\w*);");
250 Match match = regex.Match(ResponseHeaders["Set-Cookie"]);
253 session = match.Groups["session"].Value;
261 /// Specifies the status of the request.
272 public static class ScoTypes
298 public static Types TryParse(string type)
305 return Types.Archive;
307 return Types.Attachment;
309 return Types.Authorware;
311 return Types.Captivate;
313 return Types.Content;
317 return Types.Curriculum;
320 case "external-event":
321 return Types.ExternalEvent;
331 return Types.Meeting;
333 return Types.Presentation;
335 return Types.Producer;
337 return Types.Session;
343 return Types.Content;
350 return Types.Unknown;