Imports System.Xml
Imports CharterDotCom.CommonEntities.Library.EntityMemberServices
Imports System
Imports System.IO
Imports System.Net
Imports System.Text
Imports System.Xml.XmlDocument





Public Class OSSApiClient
    Private _url As String
    Private _credentials As Net.ICredentials

    Public Property Url() As String
        Get
            Return _url
        End Get
        Set(ByVal value As String)
            _url = value
        End Set
    End Property

    Public Property Credentials() As Net.ICredentials
        Get
            Return _credentials
        End Get
        Set(ByVal value As Net.ICredentials)
            _credentials = value
        End Set
    End Property

    Public Sub New(ByVal url As String)
        _url = url
    End Sub
    'SAMPLE getEncryptString Response:
    '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    '    <soap:Body xmlns:th="http://toolhost.corp.chartercom.com/ossapi">
    '        <OSSResponse>
    '            <th:result>53616c7465645f5f7142335f64349a24f2dc13d3b031f27a459b4321866bf5582efb62b7373c5ce321fcd6c0efc3eef69c5db86c6288bf7f50f40906b6b7dabd573a3e240750a4fcbf580b5f9432b91fe92e21ede6713630eaef011cb56ce7c65f7744a9589abdde5df983d84068d0d7b16a257da026eb0e22cdaabc2fef778fae580ce544e4d19e22291b4c202d4233bace9723c5733521525556c5a601c33c953db3efc704a24aa46be199a1819410</th:result>
    '            <th:returnCode>0</th:returnCode>
    '            <th:returnString>Success</th:returnString>
    '            <th:roundTrip>0.386405</th:roundTrip>
    '        </OSSResponse>
    '    </soap:Body>
    '</soap:Envelope>
    Public Function GetEncryptString(ByVal request As GetEncryptStringRequest) As GetEncryptStringResponse
        Const GET_ENCR_STR_SOAP_REQ As String = _
         "<soap:Envelope " + _
          "xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" " + _
          "xmlns:soapenc=""http://schemas.xmlsoap.org/soap/encoding/"" " + _
          "xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" " + _
          "xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">" + _
            "<soap:Body>" + _
            "<getEncryptStringRequest " + _
              "xmlns=""messaging.ossapi.toolhost.corp.chartercom.com"">" + _
                "<accountNumber>{0}</accountNumber>" + _
                "<auditUser>{1}</auditUser>" + _
                "<clientIP>{2}</clientIP>" + _
                "<source>dotcom</source>" + _
                "<userName>{3}</userName>" + _
                "<target>ist:link=accounts</target>" + _
            "</getEncryptStringRequest>" + _
            "</soap:Body>" + _
        "</soap:Envelope>"

        'inject request parameters into soap request string
        Dim soapRequest As String = _
       String.Format(GET_ENCR_STR_SOAP_REQ, _
            request.AccountNumber, request.AuditUser, _
            request.SourceIPAddress, request.UserName)

        Dim response As New GetEncryptStringResponse

        'create web request, 
        Dim webRequest As Net.HttpWebRequest = Net.WebRequest.Create(Url)

        webRequest.ContentType = "text/xml;charset=""utf-8"""
        webRequest.Accept = "text/xml"
        webRequest.Method = "POST"

        'Add SOAPAction HTTP header to request:
        webRequest.Headers.Add("SOAPAction", "OSSRequest")

        'write soap envelope contents to request:
        Using s As IO.Stream = webRequest.GetRequestStream()
            Using x As New IO.StreamWriter(s, Text.Encoding.ASCII)
                x.Write(soapRequest)
            End Using
        End Using

        'get web response:
        Using webResponse As Net.WebResponse = webRequest.GetResponse
            Using rd As New XmlTextReader(webResponse.GetResponseStream())
                While rd.Read()
                    'parse response xml and translate into GetEncryptStringResponse elements
                    If rd.NodeType = XmlNodeType.Element Then
                        Select Case rd.Name
                            Case "th:result"
                                response.AuthString = rd.ReadString().Trim
                            Case "th:returnCode"
                                response.ReturnCode = rd.ReadElementContentAsInt()
                            Case "th:returnString"
                                response.Status = rd.ReadString().Trim
                            Case "th:roundTrip"
                                response.RoundTrip = rd.ReadElementContentAsDecimal()
                        End Select
                    End If
                End While
            End Using
        End Using

        'if response was in error, indicate an exception occured, and populate exception's status message 
        'into the response ExceptionMessage field.
        If response.ReturnCode <> 0 Then
            response.ExceptionMessage = response.Status
            response.Exception = True
        End If

        Return response
    End Function

    'GetAccountInfo requestString"= "<?xml version="1.0" encoding="utf-8"?><env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope"><env:Header/><env:Body><OSSRequest method="getLogin" getAnswer="true" system="mslogin" accountNumber="8345780100262751"/></env:Body></env:Envelope>"	
    'getAccountInfo  "responseString"= "<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope"><env:Body xmlns:th="http://toolhost.corp.chartercom.com/ossapi"><OSSResponse><th:logins><th:loginsItem><th:accountNumber>8345780100262751</th:accountNumber><th:contactEmail>srenaud@charter.net</th:contactEmail><th:domain></th:domain><th:role></th:role><th:secAnswer>riley</th:secAnswer><th:secQuestion>Favorite pet's name?</th:secQuestion><th:status>ACTIVE</th:status><th:system>member services</th:system><th:username>srenaud</th:username></th:loginsItem></th:logins><th:returnCode>0</th:returnCode><th:returnString>Success</th:returnString><th:roundTrip>1.001978</th:roundTrip></OSSResponse></env:Body></env:Envelope>"	
    Public Function GetLoginIDResponseXMLdoc(ByVal request As GetLoginRequest) As XmlDocument
        Dim xmlDoc As New XmlDocument
        '###########################################################
        'MyAccount- Forgot UserName Sriman kanuri April 07. 2010
        'this procedure fetches userName using getLogin webservice call
        'on OSSAPI tier
        '##########################################################

        'OSS API needs soap requests to be build from scratch
        'Build the Soap Envelope in the format below 

        Dim requestString As String = _
        "<soap:Envelope " + _
         "xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" " + _
         "xmlns:soapenc=""http://schemas.xmlsoap.org/soap/encoding/"" " + _
         "xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" " + _
         "xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">" + _
           "<soap:Body>" + _
           "<OSSRequest method=""getLogin""  getAnswer=""true"" system=""mslogin""  accountNumber=""" + _
           request.AccountNumber + """/>" + _
                 "</soap:Body>" + _
       "</soap:Envelope>"


        ' Create a request using a URL that can receive a post. 
        'create web request, 
        Dim webRequest As Net.HttpWebRequest = Net.WebRequest.Create(Url)

        webRequest.ContentType = "text/xml;charset=""utf-8"""
        webRequest.Accept = "text/xml"
        webRequest.Method = "POST"

        'Add SOAPAction HTTP header to request:
        webRequest.Headers.Add("SOAPAction", "OSSRequest")


        'write soap envelope contents to request string:
        Using s As IO.Stream = webRequest.GetRequestStream()
            Using x As New IO.StreamWriter(s, Text.Encoding.ASCII)
                x.Write(requestString)
            End Using
        End Using



        'get web response:
        'convert it to xmlDoc and pass it back
        Using webResponse As Net.WebResponse = webRequest.GetResponse

            xmlDoc.Load(webResponse.GetResponseStream())

            Return xmlDoc
        End Using
    End Function
   

    Public Function GetLoginIDByUserNameResponseXMLdoc(ByVal request As GetLoginRequest) As XmlDocument
        Dim xmlDoc As New XmlDocument
        '###########################################################
        'MyAccount- Forgot UserName Sriman kanuri April 07. 2010
        'this procedure fetches userName using getLogin webservice call
        'on OSSAPI tier
        '##########################################################

        'OSS API needs soap requests to be build from scratch
        'Build the Soap Envelope in the format below 

        Dim requestString As String = _
        "<soap:Envelope " + _
         "xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" " + _
         "xmlns:soapenc=""http://schemas.xmlsoap.org/soap/encoding/"" " + _
         "xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" " + _
         "xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">" + _
           "<soap:Body>" + _
           "<OSSRequest method=""getLogin""  getAnswer=""true"" system=""mslogin""  username=""" + _
           request.AccountNumber + """/>" + _
                 "</soap:Body>" + _
       "</soap:Envelope>"


        ' Create a request using a URL that can receive a post. 
        'create web request, 
        Dim webRequest As Net.HttpWebRequest = Net.WebRequest.Create(Url)

        webRequest.ContentType = "text/xml;charset=""utf-8"""
        webRequest.Accept = "text/xml"
        webRequest.Method = "POST"

        'Add SOAPAction HTTP header to request:
        webRequest.Headers.Add("SOAPAction", "OSSRequest")


        'write soap envelope contents to request string:
        Using s As IO.Stream = webRequest.GetRequestStream()
            Using x As New IO.StreamWriter(s, Text.Encoding.ASCII)
                x.Write(requestString)
            End Using
        End Using



        'get web response:
        'convert it to xmlDoc and pass it back
        Using webResponse As Net.WebResponse = webRequest.GetResponse

            xmlDoc.Load(webResponse.GetResponseStream())

            Return xmlDoc
        End Using
    End Function

End Class
