Files
ciimsproxy/internal/codec.go
T

133 lines
3.2 KiB
Go
Raw Normal View History

2020-05-11 14:58:22 +08:00
package internal
import (
"bytes"
"encoding/xml"
"regexp"
"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>
`
msgExp = `<ns1:string>.*<\/ns1:string>`
)
// 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##", user, -1)
msg = strings.Replace(msg, "##pass##", pass, -1)
msg = strings.Replace(msg, "##event##", 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##", user, -1)
msg = strings.Replace(msg, "##pass##", pass, -1)
msg = strings.Replace(msg, "##count##", strconv.Itoa(count), -1)
return msg
}
func Map(vs []string, f func(string) string) []string {
vsm := make([]string, len(vs))
for i, v := range vs {
vsm[i] = f(v)
}
return vsm
}
//GetMsgs get message from soap response
func GetMsgs(soap string) []string {
r, _ := regexp.Compile(msgExp)
msgs := r.FindAllString(soap, -1)
return Map(msgs, split)
}
func split(msg string) string {
if len(msg) > 12 {
return msg[12 : len(msg)-13]
} else {
return msg
}
}