-
Notifications
You must be signed in to change notification settings - Fork 391
/
server_tmpl.go
148 lines (123 loc) · 4.11 KB
/
server_tmpl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package gowsdl
var serverTmpl = `
var WSDLUndefinedError = errors.New("Server was unable to process request. --> Object reference not set to an instance of an object.")
type SOAPEnvelopeRequest struct {
XMLName xml.Name ` + "`" + `xml:"http://schemas.xmlsoap.org/soap/envelope/ Envelope"` + "`" + `
Body SOAPBodyRequest
}
type SOAPBodyRequest struct {
XMLName xml.Name ` + "`" + `xml:"http://schemas.xmlsoap.org/soap/envelope/ Body"` + "`" + `
{{range .}}
{{range .Operations}}
{{$requestType := findType .Input.Message | replaceReservedWords | makePublic}} ` + `
{{$requestType}} *{{$requestType}} ` + "`" + `xml:,omitempty` + "`" + `
{{end}}
{{end}}
}
type SOAPEnvelopeResponse struct { ` + `
XMLName xml.Name` + "`" + `xml:"soap:Envelope"` + "`" + `
PrefixSoap string ` + "`" + `xml:"xmlns:soap,attr"` + "`" + `
PrefixXsi string ` + "`" + `xml:"xmlns:xsi,attr"` + "`" + `
PrefixXsd string ` + "`" + `xml:"xmlns:xsd,attr"` + "`" + `
Body SOAPBodyResponse
}
func NewSOAPEnvelopResponse() *SOAPEnvelopeResponse {
return &SOAPEnvelopeResponse{
PrefixSoap: "http://schemas.xmlsoap.org/soap/envelope/",
PrefixXsd: "http://www.w3.org/2001/XMLSchema",
PrefixXsi: "http://www.w3.org/2001/XMLSchema-instance",
}
}
type Fault struct { ` + `
XMLName xml.Name ` + "`" + `xml:"SOAP-ENV:Fault"` + "`" + `
Space string ` + "`" + `xml:"xmlns:SOAP-ENV,omitempty,attr"` + "`" + `
Code string ` + "`" + `xml:"faultcode,omitempty"` + "`" + `
String string ` + "`" + `xml:"faultstring,omitempty"` + "`" + `
Actor string ` + "`" + `xml:"faultactor,omitempty"` + "`" + `
Detail string ` + "`" + `xml:"detail,omitempty"` + "`" + `
}
type SOAPBodyResponse struct { ` + `
XMLName xml.Name ` + "`" + `xml:"soap:Body"` + "`" + `
Fault *Fault ` + "`" + `xml:",omitempty"` + "`" + `
{{range .}}
{{range .Operations}}
{{$responseType := findType .Output.Message | replaceReservedWords | makePublic}}
{{$requestType := findType .Input.Message | replaceReservedWords | makePublic}} ` + `
{{$requestType}} *{{$responseType}} ` + "`" + `xml:",omitempty"` + "`" + `
{{end}}
{{end}}
}
{{range .}}
{{range .Operations}}
{{$responseType := findType .Output.Message | replaceReservedWords | makePublic}}
{{$requestType := findType .Input.Message | replaceReservedWords | makePublic}}
{{$requestTypeSource := findType .Input.Message | replaceReservedWords }}
func (service *SOAPBodyRequest) {{$requestType}}Func(request *{{$requestType}}) (*{{$responseType}}, error) {
return nil, WSDLUndefinedError
}
{{end}}
{{end}}
func (service *SOAPEnvelopeRequest) call(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "text/xml; charset=utf-8")
val := reflect.ValueOf(&service.Body).Elem()
n := val.NumField()
var field reflect.Value
var name string
find := false
if r.Method == http.MethodGet {
w.Write([]byte(wsdl))
return
}
resp := NewSOAPEnvelopResponse()
defer func() {
if r := recover(); r != nil {
resp.Body.Fault = &Fault{}
resp.Body.Fault.Space = "http://schemas.xmlsoap.org/soap/envelope/"
resp.Body.Fault.Code = "soap:Server"
resp.Body.Fault.Detail = fmt.Sprintf("%v", r)
resp.Body.Fault.String = fmt.Sprintf("%v", r)
}
xml.NewEncoder(w).Encode(resp)
}()
header := r.Header.Get("Content-Type")
if strings.Index(header, "application/soap+xml") >= 0 {
panic("Could not find an appropriate Transport Binding to invoke.")
}
err := xml.NewDecoder(r.Body).Decode(service)
if err != nil {
panic(err)
}
for i := 0; i < n; i++ {
field = val.Field(i)
name = val.Type().Field(i).Name
if field.Kind() != reflect.Ptr {
continue
}
if field.IsNil() {
continue
}
if field.IsValid() {
find = true
break
}
}
if !find {
panic(WSDLUndefinedError)
} else {
m := val.Addr().MethodByName(name + "Func")
if !m.IsValid() {
panic(WSDLUndefinedError)
}
vals := m.Call([]reflect.Value{field})
if vals[1].IsNil() {
reflect.ValueOf(&resp.Body).Elem().FieldByName(name).Set(vals[0])
} else {
panic(vals[1].Interface())
}
}
}
func Endpoint(w http.ResponseWriter, r *http.Request) {
request := SOAPEnvelopeRequest{}
request.call(w, r)
}
`