/
wsdl.go
254 lines (217 loc) · 7.65 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
package gosoap
import (
"bytes"
"context"
"encoding/xml"
"fmt"
"io"
"net/http"
"net/url"
"os"
"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"`
// https://www.w3.org/TR/2001/NOTE-wsdl-20010315#_types
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"`
}
type WSDLSource func(config *Config) ([]byte, error)
func SourceFromURI(ctx context.Context, uri string) WSDLSource {
return func(config *Config) ([]byte, error) {
res, err := getWsdlBody(ctx, uri, config.Client)
if err != nil {
return nil, fmt.Errorf("could not fetch WSDL resource: %w", err)
}
defer res.Close()
return io.ReadAll(res)
}
}
func SourceFromBytes(raw []byte) WSDLSource {
return func(config *Config) ([]byte, error) {
return raw, nil
}
}
func getWsdlBody(ctx context.Context, 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{}
}
req, err := http.NewRequest(http.MethodGet, u, nil)
if err != nil {
return nil, err
}
r, err := c.Do(req.WithContext(ctx))
if err != nil {
return nil, err
}
if r.StatusCode >= 400 {
return nil, fmt.Errorf("got error response when fetching WSDL: %v", r.StatusCode)
}
return r.Body, nil
}
// getWSDLDefinitions sent request to the wsdl url and set definitions on struct
func getWSDLDefinitions(source WSDLSource, c *Config) (wsdl *wsdlDefinitions, err error) {
raw, err := source(c)
if err != nil {
return nil, fmt.Errorf("could not load WSDL: %w", err)
}
decoder := xml.NewDecoder(bytes.NewReader(raw))
decoder.CharsetReader = charset.NewReaderLabel
err = decoder.Decode(&wsdl)
return wsdl, err
}
// GetSoapActionFromWsdlOperation 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 (b *wsdlBinding) GetSoapActionFromWsdlOperation(operation string) (string, error) {
if b == nil {
return "", fmt.Errorf("WSDL binding is nil")
}
// in the future it would be nice to have Operations be map[string]*wsdlOperation,
// where the map key is the wsdlOperation name
for _, o := range b.Operations {
if o.Name == operation {
if len(o.SoapOperations) > 0 {
return o.SoapOperations[0].SoapAction, nil
}
}
}
return "", fmt.Errorf("could not find operating matching %q in binding %q", operation, b.Name)
}
// Fault see https://www.w3.org/TR/2000/NOTE-SOAP-20000508/#_Toc478383507
type Fault struct {
Code string `xml:"faultcode"`
Description string `xml:"faultstring"`
Detail string `xml:"detail"`
}
func (f *Fault) String() string {
if f.Detail != "" {
return fmt.Sprintf("[%s]: %s | Detail: %s", f.Code, f.Description, f.Detail)
}
return fmt.Sprintf("[%s]: %s", f.Code, f.Description)
}