Files

153 lines
3.7 KiB
Go

package internal
import (
"bytes"
"encoding/xml"
"io"
"strconv"
"strings"
)
const (
sendtpl = `<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Header>
<BHIA_CIIMS:AuthenticationToken xmlns:BHIA_CIIMS="http://ciims.bhia.itdcl.com/ExchangeService">
<BHIA_CIIMS:Username xmlns:BHIA_CIIMS="http://ciims.bhia.itdcl.com/ExchangeService">##user##</BHIA_CIIMS:Username>
<BHIA_CIIMS:Password xmlns:BHIA_CIIMS="http://ciims.bhia.itdcl.com/ExchangeService">##pass##</BHIA_CIIMS:Password>
</BHIA_CIIMS:AuthenticationToken>
</soap:Header>
<soap:Body>
<ns1:send xmlns:ns1="http://ciims.bhia.itdcl.com/ExchangeService">
<ns1:in0>##msg##</ns1:in0>
<ns2:in1 xmlns="http://msg.ciims.bhia.itdcl.com" xmlns:ns2="http://ciims.bhia.itdcl.com/ExchangeService">
<ack>false</ack>
<priority>##priority##</priority>
<routeId>##event##</routeId>
<valXml>##xml##</valXml>
</ns2:in1>
</ns1:send>
</soap:Body>
</soap:Envelope>
`
receivetpl = `<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Header>
<BHIA_CIIMS:AuthenticationToken xmlns:BHIA_CIIMS="http://ciims.bhia.itdcl.com/ExchangeService">
<BHIA_CIIMS:Username xmlns:BHIA_CIIMS="http://ciims.bhia.itdcl.com/ExchangeService">##user##</BHIA_CIIMS:Username>
<BHIA_CIIMS:Password xmlns:BHIA_CIIMS="http://ciims.bhia.itdcl.com/ExchangeService">##pass##</BHIA_CIIMS:Password>
</BHIA_CIIMS:AuthenticationToken>
</soap:Header>
<soap:Body>
<ns1:receive xmlns:ns1="http://ciims.bhia.itdcl.com/ExchangeService">
<ns1:in0>##count##</ns1:in0>
</ns1:receive>
</soap:Body>
</soap:Envelope>
`
)
// CreateSend xml string for sending
func CreateSend(user string, pass string, priority int, event string, valXML bool, message string) string {
var mb bytes.Buffer
xml.Escape(&mb, []byte(message))
msg := strings.Replace(sendtpl, "##user##", xmlEscape(user), -1)
msg = strings.Replace(msg, "##pass##", xmlEscape(pass), -1)
msg = strings.Replace(msg, "##event##", xmlEscape(event), -1)
msg = strings.Replace(msg, "##priority##", strconv.Itoa(priority), -1)
msg = strings.Replace(msg, "##xml##", strconv.FormatBool(valXML), -1)
msg = strings.Replace(msg, "##msg##", mb.String(), -1)
return msg
}
// CreateReceive message
func CreateReceive(user string, pass string, count int) string {
msg := strings.Replace(receivetpl, "##user##", xmlEscape(user), -1)
msg = strings.Replace(msg, "##pass##", xmlEscape(pass), -1)
msg = strings.Replace(msg, "##count##", strconv.Itoa(count), -1)
return msg
}
func xmlEscape(s string) string {
var buf bytes.Buffer
xml.Escape(&buf, []byte(s))
return buf.String()
}
// GetMsgs get message from soap response
func GetMsgs(soap string) []string {
return xmlElementTexts(soap, "string")
}
// GetErrMsg get error message
func GetErrMsg(soap string) string {
matches := xmlElementTexts(soap, "errorMessage")
if len(matches) > 0 {
return matches[0]
}
return ""
}
func xmlElementTexts(soap string, localName string) []string {
decoder := xml.NewDecoder(strings.NewReader(soap))
var result []string
for {
token, err := decoder.Token()
if err == io.EOF {
break
}
if err != nil {
return result
}
start, ok := token.(xml.StartElement)
if !ok || start.Name.Local != localName {
continue
}
var text string
if err := decoder.DecodeElement(&text, &start); err != nil {
return result
}
result = append(result, text)
}
return result
}