forked from tiaguinho/gosoap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wsdl.go
267 lines (224 loc) · 7.51 KB
/
wsdl.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package gosoap
import (
"encoding/xml"
"fmt"
"io"
"net/http"
"net/url"
"os"
"strings"
"golang.org/x/net/html/charset"
)
type wsdlDefinitions struct {
Name string `xml:"name,attr"`
TargetNamespace string `xml:"targetNamespace,attr"`
Imports []*wsdlImport `xml:"http://schemas.xmlsoap.org/wsdl/ import"`
Types []*wsdlTypes `xml:"http://schemas.xmlsoap.org/wsdl/ types"`
Messages []*wsdlMessage `xml:"http://schemas.xmlsoap.org/wsdl/ message"`
PortTypes []*wsdlPortTypes `xml:"http://schemas.xmlsoap.org/wsdl/ portType"`
Services []*wsdlService `xml:"http://schemas.xmlsoap.org/wsdl/ service"`
Bindings []*wsdlBinding `xml:"http://schemas.xmlsoap.org/wsdl/ binding"`
}
type wsdlBinding struct {
Name string `xml:"name,attr"`
Type string `xml:"type,attr"`
Operations []*wsdlOperation `xml:"http://schemas.xmlsoap.org/wsdl/ operation"`
SoapBindings []*soapBinding `xml:"http://schemas.xmlsoap.org/wsdl/soap/ binding"`
}
type soapBinding struct {
Transport string `xml:"transport,attr"`
}
type wsdlTypes struct {
XsdSchema []*xsdSchema `xml:"http://www.w3.org/2001/XMLSchema schema"`
}
type wsdlImport struct {
Namespace string `xml:"namespace,attr"`
Location string `xml:"location,attr"`
}
type wsdlMessage struct {
Name string `xml:"name,attr"`
Parts []*wsdlMessagePart `xml:"http://schemas.xmlsoap.org/wsdl/ part"`
}
type wsdlMessagePart struct {
Name string `xml:"name,attr"`
Element string `xml:"element,attr"`
}
type wsdlPortTypes struct {
Name string `xml:"name,attr"`
Operations []*wsdlOperation `xml:"http://schemas.xmlsoap.org/wsdl/ operation"`
}
type wsdlOperation struct {
Name string `xml:"name,attr"`
Inputs []*wsdlOperationInput `xml:"http://schemas.xmlsoap.org/wsdl/ input"`
Outputs []*wsdlOperationOutput `xml:"http://schemas.xmlsoap.org/wsdl/ output"`
Faults []*wsdlOperationFault `xml:"http://schemas.xmlsoap.org/wsdl/ fault"`
SoapOperations []*soapOperation `xml:"http://schemas.xmlsoap.org/wsdl/soap/ operation"`
}
type wsdlOperationInput struct {
Message string `xml:"message,attr"`
WsawAction string `xml:"http://www.w3.org/2006/05/addressing/wsdl Action,attr"`
}
type wsdlOperationOutput struct {
Message string `xml:"message,attr"`
WsawAction string `xml:"http://www.w3.org/2006/05/addressing/wsdl Action,attr"`
}
type wsdlOperationFault struct {
Name string `xml:"name,attr"`
Message string `xml:"message,attr"`
WsawAction string `xml:"http://www.w3.org/2006/05/addressing/wsdl Action,attr"`
}
type wsdlService struct {
Name string `xml:"name,attr"`
Ports []*wsdlPort `xml:"http://schemas.xmlsoap.org/wsdl/ port"`
}
type wsdlPort struct {
Name string `xml:"name,attr"`
Binding string `xml:"binding,attr"`
SoapAddresses []*soapAddress `xml:"http://schemas.xmlsoap.org/wsdl/soap/ address"`
}
type soapAddress struct {
Location string `xml:"location,attr"`
}
type soapOperation struct {
SoapAction string `xml:"soapAction,attr"`
Style string `xml:"style,attr"`
}
type xsdSchema struct {
TargetNamespace string `xml:"targetNamespace,attr"`
ElementFormDefault string `xml:"elementFormDefault,attr"`
Imports []*xsdImport `xml:"http://www.w3.org/2001/XMLSchema import"`
Elements []*xsdElement `xml:"http://www.w3.org/2001/XMLSchema element"`
ComplexTypes []*xsdComplexType `xml:"http://www.w3.org/2001/XMLSchema complexType"`
}
type xsdImport struct {
SchemaLocation string `xml:"schemaLocation,attr"`
Namespace string `xml:"namespace,attr"`
}
type xsdElement struct {
Name string `xml:"name,attr"`
Nillable bool `xml:"nillable,attr"`
Type string `xml:"type,attr"`
MinOccurs string `xml:"minOccurs,attr"`
MaxOccurs string `xml:"maxOccurs,attr"`
ComplexType *xsdComplexType `xml:"http://www.w3.org/2001/XMLSchema complexType"`
SimpleType *xsdSimpleType `xml:"http://www.w3.org/2001/XMLSchema simpleType"`
}
type xsdComplexType struct {
Name string `xml:"name,attr"`
Sequence *xsdSequence `xml:"http://www.w3.org/2001/XMLSchema sequence"`
}
type xsdSimpleType struct {
Name string `xml:"name,attr"`
Sequence *xsdRestriction `xml:"http://www.w3.org/2001/XMLSchema restriction"`
}
type xsdSequence struct {
Elements []*xsdElement `xml:"http://www.w3.org/2001/XMLSchema element"`
}
type xsdRestriction struct {
Base string `xml:"base,attr"`
Pattern *xsdPattern `xml:"http://www.w3.org/2001/XMLSchema pattern"`
MinInclusive *xsdMinInclusive `xml:"http://www.w3.org/2001/XMLSchema minInclusive"`
MaxInclusive *xsdMaxInclusive `xml:"http://www.w3.org/2001/XMLSchema maxInclusive"`
}
type xsdPattern struct {
Value string `xml:"value,attr"`
}
type xsdMinInclusive struct {
Value string `xml:"value,attr"`
}
type xsdMaxInclusive struct {
Value string `xml:"value,attr"`
}
func getWsdlBody(u string, c *http.Client) (reader io.ReadCloser, err error) {
parse, err := url.Parse(u)
if err != nil {
return nil, err
}
if parse.Scheme == "file" {
outFile, err := os.Open(parse.Path)
if err != nil {
return nil, err
}
return outFile, nil
}
if c == nil {
c = &http.Client{}
}
r, err := c.Get(u)
if err != nil {
return nil, err
}
return r.Body, nil
}
// getWsdlDefinitions sent request to the wsdl url and set definitions on struct
func getWsdlDefinitions(u string, c *http.Client) (wsdl *wsdlDefinitions, err error) {
reader, err := getWsdlBody(u, c)
if err != nil {
return nil, err
}
defer reader.Close()
decoder := xml.NewDecoder(reader)
decoder.CharsetReader = charset.NewReaderLabel
err = decoder.Decode(&wsdl)
return wsdl, err
}
// the SoapAction of an operation might differ from the action wsdl-operation name
// if any SoapAction name is set in the wsdlOperation binding, use that.
func (wsdl *wsdlDefinitions) GetSoapActionFromWsdlOperation(operation string) string {
// in the future it would be nice to have Operations be map[string]*wsdlOperation,
// where the map key is the wsdlOperation name
if wsdl.Bindings[0] != nil {
for _, o := range wsdl.Bindings[0].Operations {
if o.Name == operation {
if len(o.SoapOperations) > 0 && o.SoapOperations[0] != nil {
return o.SoapOperations[0].SoapAction
}
}
}
}
return ""
}
// if any input message part name is set in the wsdlOperation binding, use that.
func (wsdl *wsdlDefinitions) GetInputMessagePartFromWsdlOperation(operation string) string {
portTypeName := trimNamespace(wsdl.Bindings[0].Type)
var portType *wsdlPortTypes
var message string
for _, o := range wsdl.PortTypes {
if o.Name == portTypeName {
portType = o
}
}
if portType == nil {
return ""
}
for _, o := range portType.Operations {
if o.Name == operation {
message = trimNamespace(o.Inputs[0].Message)
}
}
if len(message) > 0 {
for _, o := range wsdl.Messages {
if o.Name == message {
return o.Parts[0].Element
}
}
}
return ""
}
func trimNamespace(str string) string {
colonIndex := strings.Index(str, ":")
if colonIndex != -1 {
return str[colonIndex+1:]
}
return str
}
// Fault response
// Fault implements Stringer interface
type Fault struct {
Code string `xml:"faultcode"`
Description string `xml:"faultstring"`
Detail string `xml:"detail"`
}
func (f *Fault) String() string {
return fmt.Sprintf("[%s]: %s | Detail: %s", f.Code, f.Description, f.Detail)
}