summaryrefslogtreecommitdiff
path: root/vendor/github.com/mitchellh/packer/vendor/github.com/masterzen/winrm/soap/header.go
blob: 1a2ac088194ae828889377330c3495b563005e02 (plain)
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
package soap

import (
	"github.com/masterzen/simplexml/dom"
	"strconv"
)

type HeaderOption struct {
	key   string
	value string
}

func NewHeaderOption(name string, value string) *HeaderOption {
	return &HeaderOption{key: name, value: value}
}

type SoapHeader struct {
	to              string
	replyTo         string
	maxEnvelopeSize string
	timeout         string
	locale          string
	id              string
	action          string
	shellId         string
	resourceURI     string
	options         []HeaderOption
	message         *SoapMessage
}

type HeaderBuilder interface {
	To(string) *SoapHeader
	ReplyTo(string) *SoapHeader
	MaxEnvelopeSize(int) *SoapHeader
	Timeout(string) *SoapHeader
	Locale(string) *SoapHeader
	Id(string) *SoapHeader
	Action(string) *SoapHeader
	ShellId(string) *SoapHeader
	resourceURI(string) *SoapHeader
	AddOption(*HeaderOption) *SoapHeader
	Options([]HeaderOption) *SoapHeader
	Build(*SoapMessage) *SoapMessage
}

func (self *SoapHeader) To(uri string) *SoapHeader {
	self.to = uri
	return self
}

func (self *SoapHeader) ReplyTo(uri string) *SoapHeader {
	self.replyTo = uri
	return self
}

func (self *SoapHeader) MaxEnvelopeSize(size int) *SoapHeader {
	self.maxEnvelopeSize = strconv.Itoa(size)
	return self
}

func (self *SoapHeader) Timeout(timeout string) *SoapHeader {
	self.timeout = timeout
	return self
}

func (self *SoapHeader) Id(id string) *SoapHeader {
	self.id = id
	return self
}

func (self *SoapHeader) Action(action string) *SoapHeader {
	self.action = action
	return self
}

func (self *SoapHeader) Locale(locale string) *SoapHeader {
	self.locale = locale
	return self
}

func (self *SoapHeader) ShellId(shellId string) *SoapHeader {
	self.shellId = shellId
	return self
}

func (self *SoapHeader) ResourceURI(resourceURI string) *SoapHeader {
	self.resourceURI = resourceURI
	return self
}

func (self *SoapHeader) AddOption(option *HeaderOption) *SoapHeader {
	self.options = append(self.options, *option)
	return self
}

func (self *SoapHeader) Options(options []HeaderOption) *SoapHeader {
	self.options = options
	return self
}

func (self *SoapHeader) Build() *SoapMessage {
	header := self.createElement(self.message.envelope, "Header", NS_SOAP_ENV)

	if self.to != "" {
		to := self.createElement(header, "To", NS_ADDRESSING)
		to.SetContent(self.to)
	}

	if self.replyTo != "" {
		replyTo := self.createElement(header, "ReplyTo", NS_ADDRESSING)
		a := self.createMUElement(replyTo, "Address", NS_ADDRESSING, true)
		a.SetContent(self.replyTo)
	}

	if self.maxEnvelopeSize != "" {
		envelope := self.createMUElement(header, "MaxEnvelopeSize", NS_WSMAN_DMTF, true)
		envelope.SetContent(self.maxEnvelopeSize)
	}

	if self.timeout != "" {
		timeout := self.createElement(header, "OperationTimeout", NS_WSMAN_DMTF)
		timeout.SetContent(self.timeout)
	}

	if self.id != "" {
		id := self.createElement(header, "MessageID", NS_ADDRESSING)
		id.SetContent(self.id)
	}

	if self.locale != "" {
		locale := self.createMUElement(header, "Locale", NS_WSMAN_DMTF, false)
		locale.SetAttr("xml:lang", self.locale)
		datalocale := self.createMUElement(header, "DataLocale", NS_WSMAN_MSFT, false)
		datalocale.SetAttr("xml:lang", self.locale)
	}

	if self.action != "" {
		action := self.createMUElement(header, "Action", NS_ADDRESSING, true)
		action.SetContent(self.action)
	}

	if self.shellId != "" {
		selectorSet := self.createElement(header, "SelectorSet", NS_WSMAN_DMTF)
		selector := self.createElement(selectorSet, "Selector", NS_WSMAN_DMTF)
		selector.SetAttr("Name", "ShellId")
		selector.SetContent(self.shellId)
	}

	if self.resourceURI != "" {
		resource := self.createMUElement(header, "ResourceURI", NS_WSMAN_DMTF, true)
		resource.SetContent(self.resourceURI)
	}

	if len(self.options) > 0 {
		set := self.createElement(header, "OptionSet", NS_WSMAN_DMTF)
		for _, option := range self.options {
			e := self.createElement(set, "Option", NS_WSMAN_DMTF)
			e.SetAttr("Name", option.key)
			e.SetContent(option.value)
		}
	}

	return self.message
}

func (self *SoapHeader) createElement(parent *dom.Element, name string, ns dom.Namespace) (element *dom.Element) {
	element = dom.CreateElement(name)
	parent.AddChild(element)
	ns.SetTo(element)
	return
}

func (self *SoapHeader) createMUElement(parent *dom.Element, name string, ns dom.Namespace, mustUnderstand bool) (element *dom.Element) {
	element = self.createElement(parent, name, ns)
	value := "false"
	if mustUnderstand {
		value = "true"
	}
	element.SetAttr("mustUnderstand", value)
	return
}