-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsoaptypes.go
More file actions
226 lines (198 loc) · 7.38 KB
/
soaptypes.go
File metadata and controls
226 lines (198 loc) · 7.38 KB
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
package xroad
import (
"encoding/xml"
"errors"
"fmt"
"strings"
)
// Value only struct
type SOAPEnvelope struct {
XMLName xml.Name `xml:"http://schemas.xmlsoap.org/soap/envelope/ Envelope"`
Header SOAPHeader `xml:""`
Body interface{} `xml:"http://schemas.xmlsoap.org/soap/envelope/ Body"`
XOP *XOP `xml:"-"`
}
func NewEnvelope(h SOAPHeader, b interface{}) SOAPEnvelope {
return SOAPEnvelope{
Header: h,
Body: b,
}
}
func (x SOAPEnvelope) NewResponseEnvelope(body interface{}) SOAPEnvelope {
res := x
res.Body = body
res.XOP = nil
return res
}
func (x SOAPEnvelope) String() string {
return fmt.Sprintf("header: [%s], body: [%s]", x.Header, x.Body)
}
// https://github.com/nordic-institute/X-Road/blob/develop/doc/Protocols/pr-mess_x-road_message_protocol.md#22-message-headers
type SOAPHeader struct {
XMLName xml.Name `xml:"http://schemas.xmlsoap.org/soap/envelope/ Header" json:"-"`
ProtocolVersion string `xml:"http://x-road.eu/xsd/xroad.xsd protocolVersion" json:"protocolVersion,omitempty"`
Id string `xml:"http://x-road.eu/xsd/xroad.xsd id" json:"id"`
UserId string `xml:"http://x-road.eu/xsd/xroad.xsd userId" json:"userId"`
TargetUserId string `xml:"http://xsd.planetcross.net/planetcross.xsd targetUserId,omitempty" json:"targetUserId"`
TargetUserIdXrd string `xml:"http://x-road.eu/xsd/xroad.xsd targetUserId,omitempty" json:"-"`
Issue string `xml:"http://x-road.eu/xsd/xroad.xsd issue,omitempty" json:"issue,omitempty"`
Service *XroadService `xml:"service" json:"service" mapstructure:"service"`
CentralService *XroadCentralService `xml:"centralService" json:"centralService" mapstructure:"centralService"`
Client XroadClient `xml:"client" json:"client" mapstructure:"client"`
}
func (x SOAPHeader) String() string {
return fmt.Sprintf("v: %s, id: %s, userId: %s, targetUserId: %s, issue: %s, service: [%s], client: [%s]",
x.ProtocolVersion, x.Id, x.UserId, x.TargetUserId, x.Issue, x.Service, x.Client)
}
func (h *SOAPHeader) fillDefaults() {
// fill defaults
if h.ProtocolVersion == "" {
h.ProtocolVersion = "4.0"
}
if service := h.Service; service != nil && service.ObjectType == "" {
service.ObjectType = "SERVICE"
}
if centralService := h.CentralService; centralService != nil && centralService.ObjectType == "" {
centralService.ObjectType = "CENTRALSERVICE"
}
if h.Client.ObjectType == "" {
h.Client.ObjectType = "SUBSYSTEM"
}
}
type XroadService struct {
XroadClient `mapstructure:",squash"`
XMLName xml.Name `xml:"http://x-road.eu/xsd/xroad.xsd service" json:"-"`
ServiceCode string `xml:"http://x-road.eu/xsd/identifiers serviceCode" json:"serviceCode"`
ServiceVersion string `xml:"http://x-road.eu/xsd/identifiers serviceVersion" json:"serviceVersion"`
}
// Create a new XroadService from service FQDN.
// Reading code like FiVRKSignCertificateProfileInfo.java , we assume all the parts don't include a '/'
func NewXroadService(fqdn string) (*XroadService, error) {
parts := strings.Split(fqdn, ".")
if len(parts) != 6 {
return nil, WrapError(errors.New("invalid service fqdn"))
}
return &XroadService{
XroadClient: XroadClient{
ObjectType: "",
XRoadInstance: parts[0],
MemberClass: parts[1],
MemberCode: parts[2],
SubsystemCode: parts[3],
},
ServiceCode: parts[4],
ServiceVersion: parts[5],
}, nil
}
func (x XroadService) Equal(y XroadService) bool {
if x.XroadClient.Equal(y.XroadClient) &&
x.ServiceCode == y.ServiceCode &&
x.ServiceVersion == y.ServiceVersion {
return true
}
return false
}
func (x XroadService) Fqdn() string {
return fmt.Sprintf("%s.%s.%s", x.XroadClient.Fqdn(), x.ServiceCode, x.ServiceVersion)
}
func (x XroadService) String() string {
return x.Fqdn()
}
type XroadCentralService struct {
XMLName xml.Name `xml:"http://x-road.eu/xsd/xroad.xsd centralService" json:"-"`
ObjectType string `xml:"http://x-road.eu/xsd/identifiers objectType,attr" json:"objectType,omitempty"`
XRoadInstance string `xml:"http://x-road.eu/xsd/identifiers xRoadInstance" json:"xRoadInstance"`
ServiceCode string `xml:"http://x-road.eu/xsd/identifiers serviceCode" json:"serviceCode"`
}
type XroadClient struct {
XMLName xml.Name `xml:"http://x-road.eu/xsd/xroad.xsd client" json:"-"`
ObjectType string `xml:"http://x-road.eu/xsd/identifiers objectType,attr" json:"objectType,omitempty"`
XRoadInstance string `xml:"http://x-road.eu/xsd/identifiers xRoadInstance" json:"xRoadInstance"`
MemberClass string `xml:"http://x-road.eu/xsd/identifiers memberClass" json:"memberClass"`
MemberCode string `xml:"http://x-road.eu/xsd/identifiers memberCode" json:"memberCode"`
SubsystemCode string `xml:"http://x-road.eu/xsd/identifiers subsystemCode" json:"subsystemCode"`
}
// Create a new XroadClient from subsystem FQDN.
// Reading code like FiVRKSignCertificateProfileInfo.java , we assume all the parts don't include a '.'
// ex: JP-TEST.COM.12973914.librarian
func NewXroadClient(fqdn string) (*XroadClient, error) {
parts := strings.Split(fqdn, ".")
if len(parts) != 4 {
return nil, WrapError(errors.New("invalid client fqdn"))
}
return &XroadClient{
ObjectType: "",
XRoadInstance: parts[0],
MemberClass: parts[1],
MemberCode: parts[2],
SubsystemCode: parts[3],
}, nil
}
func (x XroadClient) SameMember(y XroadClient) bool {
if x.XRoadInstance == y.XRoadInstance &&
x.MemberClass == y.MemberClass &&
x.MemberCode == y.MemberCode {
return true
}
return false
}
func (x XroadClient) Equal(y XroadClient) bool {
if x.XRoadInstance == y.XRoadInstance &&
x.MemberClass == y.MemberClass &&
x.MemberCode == y.MemberCode &&
x.SubsystemCode == y.SubsystemCode {
return true
}
return false
}
func (x XroadClient) Fqdn() string {
return fmt.Sprintf("%s.%s.%s.%s", x.XRoadInstance, x.MemberClass, x.MemberCode, x.SubsystemCode)
}
func (x XroadClient) String() string {
return x.Fqdn()
}
type SOAPFaultBody struct {
XMLName xml.Name `xml:"http://schemas.xmlsoap.org/soap/envelope/ Body"`
Fault SOAPFault `xml:""`
}
type SOAPFault struct {
XMLName xml.Name `xml:"http://schemas.xmlsoap.org/soap/envelope/ Fault"`
Code string `xml:"faultcode,omitempty"`
String string `xml:"faultstring,omitempty"`
Actor string `xml:"faultactor,omitempty"`
Detail *SOAPFaultDetail `xml:""`
Cause error `xml:"-"` // only used for logging
}
func (s SOAPFault) Error() string {
cause := "-"
if s.Cause != nil {
cause = s.Cause.Error()
}
return fmt.Sprintf("faultcode: %s, faultstring: %s, faultactor: %s, %s, cause: %s", s.Code, s.String, s.Actor, s.Detail, cause)
}
// reuse http status codes here
func NewSOAPFault(fault string) SOAPFault {
return SOAPFault{
Code: "Server",
String: fault,
Detail: nil,
}
}
func NewSOAPFaultWithCause(fault string, cause error) SOAPFault {
return SOAPFault{
Code: "Server",
String: fault,
Cause: cause,
}
}
type SOAPFaultDetail struct {
XMLName xml.Name `xml:"http://schemas.xmlsoap.org/soap/envelope/ detail"`
FaultDetail string `xml:"faultDetail,omitempty"`
}
func (s SOAPFaultDetail) Error() string {
return fmt.Sprintf("faultDetail: %s", s.FaultDetail)
}
type XOPInclude struct {
XMLName xml.Name `xml:"http://www.w3.org/2004/08/xop/include Include"`
Href string `xml:"href,attr"`
}