Exemplo n.º 1
0
        public MFTestResults XmlTest5_WsXmlNodeList()
        {
            /// <summary>
            /// 1. Verifies the properties of a WsWsaEndpointRefs object
            /// 2. Adds elements to it
            /// 3. Re-verifies
            /// 4. Empties the object
            /// 5. Re-verifies
            /// </summary>
            ///
            bool testResult = true;

            try
            {
                WsXmlNodeList testWXNs = new WsXmlNodeList();

                if (testWXNs.Count != 0)
                {
                    throw new Exception("Count did not set correctly on new");
                }

                testWXNs.Add(new WsXmlNode());

                WsXmlNode testWXN = new WsXmlNode();
                testWXN.LocalName = "testWXN local name";
                testWXNs.Add(testWXN);

                if (testWXNs.Count != 2)
                {
                    throw new Exception("Count did not set correctly on new");
                }
            }
            catch (Exception e)
            {
                testResult = false;
                Log.Comment("Incorrect exception caught: " + e.Message);
            }
            return(testResult ? MFTestResults.Pass : MFTestResults.Fail);
        }
Exemplo n.º 2
0
        internal void ParseHeader(XmlReader reader, ProtocolVersion version)
        {
            RequiredHeaderElement headerElements = RequiredHeaderElement.None;

            ProtocolVersion altVer = version is ProtocolVersion10 ? (ProtocolVersion) new ProtocolVersion11() : (ProtocolVersion) new ProtocolVersion10();

            bool isFault = false;

            try
            {
                reader.ReadStartElement("Header", WsWellKnownUri.SoapNamespaceUri);

                while (true)
                {
                    if (reader.IsStartElement())
                    {
                        if (reader.NamespaceURI == version.AddressingNamespace ||
                            reader.NamespaceURI == altVer.AddressingNamespace)
                        {
                            switch (reader.LocalName)
                            {
                            case "MessageID":
                                if ((_messageId = reader.ReadElementString()) == String.Empty)
                                {
                                    throw new XmlException("MessageID");
                                }

                                headerElements |= RequiredHeaderElement.MessageID;
                                break;

                            case "RelatesTo":
                                if ((_relatesTo = reader.ReadElementString()) == String.Empty)
                                {
                                    throw new XmlException("RelatesTo");
                                }
                                break;

                            case "To":
                                if ((_to = reader.ReadElementString()) == String.Empty)
                                {
                                    throw new XmlException("To");
                                }

                                // If this is a URI peal off the transport address
                                if (To.IndexOf("http://") == 0)
                                {
                                    int pathIndex = _to.Substring(7).IndexOf('/');
                                    if (pathIndex != -1)
                                    {
                                        _to = _to.Substring(pathIndex + 8);
                                    }
                                }

                                headerElements |= RequiredHeaderElement.To;
                                break;

                            case "Action":
                                if ((_action = reader.ReadElementString()) == String.Empty)
                                {
                                    throw new XmlException("Action");
                                }

                                if (_action == version.AddressingNamespace + "/fault")
                                {
                                    isFault = true;
                                }

                                headerElements |= RequiredHeaderElement.Action;
                                break;

                            case "From":
#if DEBUG
                                int depth = reader.Depth;
#endif
                                _from = new WsWsaEndpointRef(reader, reader.NamespaceURI);
#if DEBUG
                                Debug.Assert(XmlReaderHelper.HasReadCompleteNode(depth, reader));
#endif
                                break;

                            case "ReplyTo":
#if DEBUG
                                depth = reader.Depth;
#endif
                                _replyTo = new WsWsaEndpointRef(reader, reader.NamespaceURI);
#if DEBUG
                                Debug.Assert(XmlReaderHelper.HasReadCompleteNode(depth, reader));
#endif
                                break;

                            case "FaultTo":
#if DEBUG
                                depth = reader.Depth;
#endif
                                _faultTo = new WsWsaEndpointRef(reader, reader.NamespaceURI);
#if DEBUG
                                Debug.Assert(XmlReaderHelper.HasReadCompleteNode(depth, reader));
#endif
                                break;

                            default:
                                reader.Skip();     //unknown item from WSA namespace, should we store it?
                                break;
                            }
                        }
                        else
                        {
                            // unknown item, need to store it for future reference
#if DEBUG
                            int depth = reader.Depth;
#endif
                            _any.Add(new WsXmlNode(reader));
#if DEBUG
                            Debug.Assert(XmlReaderHelper.HasReadCompleteNode(depth, reader));
#endif
                        }
                    }
                    else
                    {
                        break;
                    }
                }

                reader.ReadEndElement(); // Header
            }
            catch (XmlException e)
            {
                throw new WsFaultException(this, WsFaultType.WsaInvalidMessageInformationHeader, e.ToString());
            }

            if (isFault)
            {
                WsFault.ThrowFaultException(reader, this);
            }

            // add more logic for determining required headers?  it is not this simple
            //if (headerElements != RequiredHeaderElement.All)
            //{
            //    throw new WsFaultException(this, WsFaultType.WsaMessageInformationHeaderRequired);
            //}
        }
Exemplo n.º 3
0
        /// <summary>
        /// Use to get the status of an event subscription.
        /// </summary>
        /// <param name="endpointAddress">
        /// A Uri containing the endpoint address of the service or subscription manager that is currently
        /// maintaining this event subscription on behalf of the device. This address is an http uri
        /// (i.e. http://ip_address:port/serviceID).
        /// </param>
        /// <param name="subscriptionID">
        /// A subscription ID returned from a previous subscribe response. The device uses this ID
        /// to identify a specific event source subscription.
        /// </param>
        /// <returns>
        /// A WsDuration object containing the remaining subscription time for this event subscription, null = infinite.
        /// </returns>
        public WsDuration GetStatus(Uri endpointAddress, string subscriptionID)
        {
            // Performance debugging
            DebugTiming timeDebuger = new DebugTiming();
            long        startTime   = timeDebuger.ResetStartTime("");

            // Build Renew request
            using (XmlMemoryWriter xmlWriter = XmlMemoryWriter.Create())
            {
                WsXmlNodeList nodeList = new WsXmlNodeList();
                nodeList.Add(new WsXmlNode(WsNamespacePrefix.Wse, "Identifier", null, subscriptionID));

                WsWsaHeader header = new WsWsaHeader(
                    m_version.EventingNamespace + "/GetStatus",  // Action
                    null,                                        // RelatesTo
                    endpointAddress.AbsoluteUri,                 // To
                    null, null, nodeList);                       // ReplyTo, From, Any

                WsMessage msg = new WsMessage(header, null, WsPrefix.Wse, null, null);

                WsSoapMessageWriter smw = new WsSoapMessageWriter(m_version);
                String messageID        = smw.WriteSoapMessageStart(xmlWriter, msg);

                // Performance debuging
                timeDebuger.PrintElapsedTime("*****Write Header Took");

                // write body
                xmlWriter.WriteStartElement(WsNamespacePrefix.Soap, "Body", null);
                xmlWriter.WriteStartElement(WsNamespacePrefix.Wse, "GetStatus", null);
                xmlWriter.WriteString("");
                xmlWriter.WriteEndElement(); // End GetStatus

                // Performance debuging
                timeDebuger.PrintElapsedTime("*****Write Body Took");

                smw.WriteSoapMessageEnd(xmlWriter);

                // Performance debuging
                timeDebuger.PrintTotalTime(startTime, "***Renew Message Build Took");

                // Create an Http client and send GetStatus request
                WsHttpClient httpClient = new WsHttpClient(m_version);

                msg.Body = xmlWriter.ToString();

                WsMessage getStatusResponse = httpClient.SendRequest(msg, endpointAddress);

                // If a GetStatus response is received validate the messageID and action and get the remaining
                // event subscription time. If a fault is received print exception and go on.
                if (getStatusResponse == null)
                {
                    System.Ext.Console.Write("");
                    System.Ext.Console.Write("Renew response is null.");
                    return(null);
                }
                else
                {
                    System.Ext.Console.Write("");
                    System.Ext.Console.Write("Response From: " + endpointAddress.Host);
                    System.Ext.Console.Write(getStatusResponse.Body as byte[]);

                    // Note: Since the response is the same for GetStatus ans it is for Renew reuse the
                    // Renew response parser.
                    try
                    {
                        return(ProcessRenewResponse((byte[])getStatusResponse.Body, messageID));
                    }
                    catch (Exception e)
                    {
                        System.Ext.Console.Write("");
                        System.Ext.Console.Write("Unsubscribe response parsing failed.");
                        System.Ext.Console.Write(e.Message);
                    }
                }
            }

            return(null);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Use to unsubscribe from a devices event source.
        /// </summary>
        /// <param name="endpointAddress">
        /// A Uri containing the endpoint address of the service or subscription manager that is currently
        /// maintaining this event subscription on behalf of the device. This address is an http uri
        /// (i.e. http://ip_address:port/serviceID).
        /// </param>
        /// <param name="subscription">An event subscription returned from a previous subscribe call.
        /// The subscription contains among other things a subscription ID used by the subscription manager
        /// to identify a specific event source subscription and the endpoint address of the subscription manager.
        /// </param>
        /// <returns>True if the Unsubscribe request was successful.</returns>
        public bool Unsubscribe(Uri endpointAddress, DpwsEventSubscription subscription)
        {
            // Performance debugging
            DebugTiming timeDebuger = new DebugTiming();
            long        startTime   = timeDebuger.ResetStartTime("");

            // Build Unsubscribe Request
            using (XmlMemoryWriter xmlWriter = XmlMemoryWriter.Create())
            {
                WsXmlNodeList nodeList = new WsXmlNodeList();
                nodeList.Add(new WsXmlNode(null, "identifier", WsWellKnownUri.WseNamespaceUri, subscription.SubscriptionID));

                WsWsaHeader header = new WsWsaHeader(
                    m_version.EventingNamespace + "/Unsubscribe",            // Action
                    null,                                                    // RelatesTo
                    endpointAddress.AbsoluteUri,                             // To
                    m_version.AddressingNamespace,                           // ReplyTo
                    subscription.SubscriptionManager.Address.AbsoluteUri,    // From
                    nodeList);                                               // Identifier

                WsMessage msg = new WsMessage(header, null, WsPrefix.Wse, null, null);

                WsSoapMessageWriter smw = new WsSoapMessageWriter(m_version);
                String messageID        = smw.WriteSoapMessageStart(xmlWriter, msg);

                // Performance debuging
                timeDebuger.PrintElapsedTime("*****Write Header Took");

                // write body
                xmlWriter.WriteStartElement(WsNamespacePrefix.Wse, "Unsubscribe", null);
                xmlWriter.WriteString("");
                xmlWriter.WriteEndElement(); // End Unsubscribe

                // Performance debuging
                timeDebuger.PrintElapsedTime("*****Write Body Took");

                smw.WriteSoapMessageEnd(xmlWriter);

                // Performance debuging
                timeDebuger.PrintTotalTime(startTime, "***Unsubscribe Message Build Took");

                // Create an Http client and send Unsubscribe request
                WsHttpClient httpClient = new WsHttpClient(m_version);

                msg.Body = xmlWriter.ToArray();

                WsMessage unsubscribeResponse = httpClient.SendRequest(msg, endpointAddress);

                // If a unsubscribe response is received simple validate that the messageID and action are correct and
                // If a parsing fault is received print exception and go on.
                if (unsubscribeResponse == null)
                {
                    System.Ext.Console.Write("");
                    System.Ext.Console.Write("Unsubscribe response is null.");
                    return(false);
                }
                else
                {
                    System.Ext.Console.Write("");
                    System.Ext.Console.Write("Response From: " + endpointAddress.Host);
                    System.Ext.Console.Write(unsubscribeResponse.Body as byte[]);
                    try
                    {
                        return(ProcessUnsubscribeResponse((byte[])unsubscribeResponse.Body, messageID));
                    }
                    catch (Exception e)
                    {
                        System.Ext.Console.Write("");
                        System.Ext.Console.Write("Unsubscribe response parsing failed.");
                        System.Ext.Console.Write(e.Message);
                        return(false);
                    }
                }
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Use to get the status of an event subscription.
        /// </summary>
        /// <param name="endpointAddress">
        /// A Uri containing the endpoint address of the service or subscription manager that is currently
        /// maintaining this event subscription on behalf of the device. This address is an http uri
        /// (i.e. http://ip_address:port/serviceID).
        /// </param>
        /// <param name="subscriptionID">
        /// A subscription ID returned from a previous subscribe response. The device uses this ID
        /// to identify a specific event source subscription.
        /// </param>
        /// <returns>
        /// A WsDuration object containing the remaining subscription time for this event subscription, null = infinite.
        /// </returns>
        public WsDuration GetStatus(Uri endpointAddress, string subscriptionID)
        {
            // Performance debugging
            DebugTiming timeDebuger = new DebugTiming();
            long        startTime   = timeDebuger.ResetStartTime("");

            // Build Renew request
            MemoryStream soapStream = new MemoryStream();
            XmlWriter    xmlWriter  = XmlWriter.Create(soapStream);

            WsXmlNodeList nodeList = new WsXmlNodeList();

            nodeList.Add(new WsXmlNode("wse", "Identifier", null, subscriptionID));

            WsWsaHeader header = new WsWsaHeader(
                WsWellKnownUri.WseNamespaceUri + "/GetStatus",  // Action
                null,                                           // RelatesTo
                endpointAddress.AbsoluteUri,                    // To
                null, null, nodeList);                          // ReplyTo, From, Any

            String messageID = WsSoapMessageWriter.WriteSoapMessageStart(xmlWriter,
                                                                         WsSoapMessageWriter.Prefixes.Wse, null, header, null);

            // Performance debuging
            timeDebuger.PrintElapsedTime("*****Write Header Took");

            // write body
            xmlWriter.WriteStartElement("soap", "Body", null);
            xmlWriter.WriteStartElement("wse", "GetStatus", null);
            xmlWriter.WriteString("");
            xmlWriter.WriteEndElement(); // End GetStatus

            // Performance debuging
            timeDebuger.PrintElapsedTime("*****Write Body Took");

            WsSoapMessageWriter.WriteSoapMessageEnd(xmlWriter);

            // Performance debuging
            timeDebuger.PrintTotalTime(startTime, "***Renew Message Build Took");

            // Flush and close writer
            xmlWriter.Flush();
            xmlWriter.Close();

            // Create an Http client and send GetStatus request
            WsHttpClient httpClient = new WsHttpClient();

            byte[] getStatusResponse = null;
            try
            {
                getStatusResponse = httpClient.SendRequest(soapStream.ToArray(), endpointAddress.ToString(), false, false);
            }
            catch (Exception e)
            {
                System.Ext.Console.Write("");
                System.Ext.Console.Write("GetStatus failed. " + e.Message);
                return(null);
            }

            // If a GetStatus response is received validate the messageID and action and get the remaining
            // event subscription time. If a fault is received print exception and go on.
            if (getStatusResponse == null)
            {
                System.Ext.Console.Write("");
                System.Ext.Console.Write("Renew response is null.");
                return(null);
            }
            else
            {
                System.Ext.Console.Write("");
                System.Ext.Console.Write("Response From: " + endpointAddress.Host);
                System.Ext.Console.Write(new String(System.Text.Encoding.UTF8.GetChars(getStatusResponse)));

                // Note: Since the response is the same for GetStatus ans it is for Renew reuse the
                // Renew response parser.
                try
                {
                    return(ProcessRenewResponse(getStatusResponse, messageID));
                }
                catch (Exception e)
                {
                    System.Ext.Console.Write("");
                    System.Ext.Console.Write("Unsubscribe response parsing failed.");
                    System.Ext.Console.Write(e.Message);
                }
            }

            return(null);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Use to unsubscribe from a devices event source.
        /// </summary>
        /// <param name="endpointAddress">
        /// A Uri containing the endpoint address of the service or subscription manager that is currently
        /// maintaining this event subscription on behalf of the device. This address is an http uri
        /// (i.e. http://ip_address:port/serviceID).
        /// </param>
        /// <param name="subscription">An event subscription returned from a previous subscribe call.
        /// The subscription contains among other things a subscription ID used by the subscription manager
        /// to identify a specific event source subscription and the endpoint address of the subscription manager.
        /// </param>
        /// <returns>True if the Unsubscribe request was successful.</returns>
        public bool Unsubscribe(Uri endpointAddress, DpwsEventSubscription subscription)
        {
            // Performance debugging
            DebugTiming timeDebuger = new DebugTiming();
            long        startTime   = timeDebuger.ResetStartTime("");

            // Build Unsubscribe Request
            MemoryStream soapStream = new MemoryStream();
            XmlWriter    xmlWriter  = XmlWriter.Create(soapStream);

            WsXmlNodeList nodeList = new WsXmlNodeList();

            nodeList.Add(new WsXmlNode(null, "identifier", WsWellKnownUri.WseNamespaceUri, subscription.SubscriptionID));

            WsWsaHeader header = new WsWsaHeader(
                WsWellKnownUri.WseNamespaceUri + "/Unsubscribe",            // Action
                null,                                                       // RelatesTo
                endpointAddress.AbsoluteUri,                                // To
                WsWellKnownUri.WsaAnonymousUri,                             // ReplyTo
                subscription.SubscriptionManager.Address.AbsoluteUri,       // From
                nodeList);                                                  // Identifier

            String messageID = WsSoapMessageWriter.WriteSoapMessageStart(xmlWriter,
                                                                         WsSoapMessageWriter.Prefixes.Wse, null, header, null);

            // Performance debuging
            timeDebuger.PrintElapsedTime("*****Write Header Took");

            // write body
            xmlWriter.WriteStartElement("wse", "Unsubscribe", null);
            xmlWriter.WriteString("");
            xmlWriter.WriteEndElement(); // End Unsubscribe

            // Performance debuging
            timeDebuger.PrintElapsedTime("*****Write Body Took");

            WsSoapMessageWriter.WriteSoapMessageEnd(xmlWriter);

            // Performance debuging
            timeDebuger.PrintTotalTime(startTime, "***Unsubscribe Message Build Took");

            // Flush and close writer
            xmlWriter.Flush();
            xmlWriter.Close();

            // Create an Http client and send Unsubscribe request
            WsHttpClient httpClient = new WsHttpClient();

            byte[] unsubscribeResponse = null;
            try
            {
                unsubscribeResponse = httpClient.SendRequest(soapStream.ToArray(), endpointAddress.ToString(), false, false);
            }
            catch (Exception e)
            {
                System.Ext.Console.Write("");
                System.Ext.Console.Write("Unsubscribe failed. " + e.Message);
                return(false);
            }

            // If a unsubscribe response is received simple validate that the messageID and action are correct and
            // If a parsing fault is received print exception and go on.
            if (unsubscribeResponse == null)
            {
                System.Ext.Console.Write("");
                System.Ext.Console.Write("Unsubscribe response is null.");
                return(false);
            }
            else
            {
                System.Ext.Console.Write("");
                System.Ext.Console.Write("Response From: " + endpointAddress.Host);
                System.Ext.Console.Write(new String(System.Text.Encoding.UTF8.GetChars(unsubscribeResponse)));
                try
                {
                    return(ProcessUnsubscribeResponse(unsubscribeResponse, messageID));
                }
                catch (Exception e)
                {
                    System.Ext.Console.Write("");
                    System.Ext.Console.Write("Unsubscribe response parsing failed.");
                    System.Ext.Console.Write(e.Message);
                    return(false);
                }
            }
        }
Exemplo n.º 7
0
        internal void ParseHeader(XmlReader reader)
        {
            RequiredHeaderElement headerElements = RequiredHeaderElement.None;

            try
            {
                reader.ReadStartElement("Header", WsWellKnownUri.SoapNamespaceUri);

                while (true)
                {
                    if (reader.IsStartElement())
                    {
                        if (
                            (reader.NamespaceURI == WsWellKnownUri.WsaNamespaceUri_2005_08) ||
                            (reader.NamespaceURI == WsWellKnownUri.WsaNamespaceUri_2004_08)
                            )
                        {
                            switch (reader.LocalName)
                            {
                            case "MessageID":
                                if ((_messageId = reader.ReadElementString()) == String.Empty)
                                {
                                    throw new XmlException("MessageID");
                                }

                                headerElements |= RequiredHeaderElement.MessageID;
                                break;

                            case "RelatesTo":
                                if ((_relatesTo = reader.ReadElementString()) == String.Empty)
                                {
                                    throw new XmlException("RelatesTo");
                                }
                                break;

                            case "To":
                                if ((_to = reader.ReadElementString()) == String.Empty)
                                {
                                    throw new XmlException("To");
                                }

                                // If this is a URI peal off the transport address
                                if (To.IndexOf("http://") == 0)
                                {
                                    int pathIndex = _to.Substring(7).IndexOf('/');
                                    if (pathIndex != -1)
                                    {
                                        _to = _to.Substring(pathIndex + 8);
                                    }
                                }

                                headerElements |= RequiredHeaderElement.To;
                                break;

                            case "Action":
                                if ((_action = reader.ReadElementString()) == String.Empty)
                                {
                                    throw new XmlException("Action");
                                }

                                headerElements |= RequiredHeaderElement.Action;
                                break;

                            case "From":
#if DEBUG
                                int depth = reader.Depth;
#endif
                                _from = new WsWsaEndpointRef(reader);
#if DEBUG
                                Debug.Assert(XmlReaderHelper.HasReadCompleteNode(depth, reader));
#endif
                                break;

                            case "ReplyTo":
#if DEBUG
                                depth = reader.Depth;
#endif
                                _replyTo = new WsWsaEndpointRef(reader);
#if DEBUG
                                Debug.Assert(XmlReaderHelper.HasReadCompleteNode(depth, reader));
#endif
                                if (ReplyTo.Address.AbsoluteUri != WsWellKnownUri.WsaAnonymousUri &&
                                    ReplyTo.Address.AbsoluteUri != WsWellKnownUri.WsaAnonymousRoleUri)
                                {
                                    throw new WsFaultException(this, WsFaultType.WsaInvalidMessageInformationHeader, "R0031");
                                }
                                break;

                            case "FaultTo":
#if DEBUG
                                depth = reader.Depth;
#endif
                                _faultTo = new WsWsaEndpointRef(reader);
#if DEBUG
                                Debug.Assert(XmlReaderHelper.HasReadCompleteNode(depth, reader));
#endif
                                break;

                            default:
                                reader.Skip();     //unknown item from WSA namespace, should we store it?
                                break;
                            }
                        }
                        else
                        {
                            // unknown item, need to store it for future reference
#if DEBUG
                            int depth = reader.Depth;
#endif
                            _any.Add(new WsXmlNode(reader));
#if DEBUG
                            Debug.Assert(XmlReaderHelper.HasReadCompleteNode(depth, reader));
#endif
                        }
                    }
                    else
                    {
                        break;
                    }
                }

                reader.ReadEndElement(); // Header
            }
            catch (XmlException e)
            {
                throw new WsFaultException(this, WsFaultType.WsaInvalidMessageInformationHeader, e.ToString());
            }

            //if (headerElements != RequiredHeaderElement.All)
            //{
            //    throw new WsFaultException(this, WsFaultType.WsaMessageInformationHeaderRequired);
            //}
        }