public string BuildXml(string command)
        {
            this.EnsureCanExecute();

            var attributes = new StringBuilder();

            foreach (var kvp in this.GetCommandAttributes())
            {
                attributes.AppendFormat(" {0}=" + "'{1}'", kvp.Key, kvp.Value);
            }

            string beginTag =
                string.Format(
                    "<{0} xmlns='http://schemas.microsoft.com/exchange/services/2006/messages' xmlns:m='http://schemas.microsoft.com/exchange/services/2006/messages' xmlns:t='http://schemas.microsoft.com/exchange/services/2006/types' {1}>",
                    command, attributes.ToString());
            string endTag = string.Format("</{0}>", command);

            string innerXml = this.BuildXmlCore();

            string xml = string.Format("{0}{1}{2}", beginTag, innerXml, endTag);

            string xmlValidationErrorMessage;

            if (!EwsXmlHelper.IsValidXml(xml, out xmlValidationErrorMessage))
            {
                throw new CommandRequestXmlException(string.Format("Invalid xml content: {0}", xmlValidationErrorMessage));
            }

            return(xml);
        }
Пример #2
0
        public void ParseResponse(string commandName, WebRequestResponse response)
        {
            if (response.Response.StatusCode == HttpStatusCode.Unauthorized)
            {
                throw new CommandAuthorizationException(string.Format("Authorization failed: {0}", response.Response.ReasonPhrase));
            }

            if (response.Response.IsSuccessStatusCode)
            {
                string xml = response.ResponseBody;

                string xmlValidationErrorMessage;
                if (!EwsXmlHelper.IsValidXml(xml, out xmlValidationErrorMessage))
                {
                    throw new CommandResponseXmlException(string.Format("Invalid xml content: {0}", xmlValidationErrorMessage));
                }

                XDocument xdoc = XDocument.Load(new StringReader(xml));

                var responseMessages    = xdoc.Root.XGetChildrenOf(string.Format("Body/{0}Response/ResponseMessages", commandName));
                var ewsResponseMessages = new List <EwsResponseMessage>();

                foreach (var responseMessage in responseMessages)
                {
                    var ewsResponseMessage = new EwsResponseMessage
                    {
                        Class   = responseMessage.XGetAttributeValue <EwsResponseClass>("ResponseClass"),
                        Code    = responseMessage.XGetChildValue <string>("ResponseCode", true),
                        Message = responseMessage.XGetChildValue <string>("MessageText", true),
                        Content = responseMessage
                    };

                    if (ewsResponseMessage.Class != EwsResponseClass.Success || ewsResponseMessage.Code != ResponseCodeNoError)
                    {
                        string message = string.Format("response is invalid (class: {0} code: {1} message: {2}) content: {3})", ewsResponseMessage.Class, ewsResponseMessage.Code, ewsResponseMessage.Message, responseMessage);
                        Log(commandName, message);

                        if (OnInvalidResponse != null)
                        {
                            OnInvalidResponse(this, new EventArgs <string>(message));
                        }
                    }

                    ewsResponseMessages.Add(ewsResponseMessage);
                }

                try
                {
                    this.ParseResponseCore(ewsResponseMessages);
                }
                catch (Exception ex)
                {
                    throw new CommandResponseException(string.Format("Failed to parse {0} response", commandName), ex);
                }
            }
            else
            {
                Log(commandName, string.Format("response does not have HTTP sucess status code (code: {0})", response.Response.StatusCode));
                EwsFault fault = null;
                string   xml;
                try
                {
                    xml = response.ResponseBody;
                    XDocument xdoc = XDocument.Load(new StringReader(xml));

                    fault = this.ExtractFault(xdoc);
                    Log(commandName, string.Format("fault: " + fault));
                }
                catch (Exception)
                {
                    response.Response.Content.ReadAsStringAsync().ContinueWith(r => Log(commandName, string.Format("response: " + r.Result)));
                }

                throw new CommandResponseException(string.Format("Failed to read {0} response, fault: {1}", commandName, fault != null ? fault.ToString() : "unknown fault"));
            }
        }