-
Notifications
You must be signed in to change notification settings - Fork 64
/
activity.go
358 lines (277 loc) · 6.89 KB
/
activity.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package rest
import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
"github.com/project-flogo/core/activity"
"github.com/project-flogo/core/data/metadata"
"github.com/project-flogo/core/support/ssl"
)
func init() {
_ = activity.Register(&Activity{}, New)
}
const (
methodPOST = "POST"
methodPUT = "PUT"
methodPATCH = "PATCH"
)
var activityMd = activity.ToMetadata(&Settings{}, &Input{}, &Output{})
func New(ctx activity.InitContext) (activity.Activity, error) {
s := &Settings{}
err := metadata.MapToStruct(ctx.Settings(), s, true)
if err != nil {
return nil, err
}
act := &Activity{settings: s}
act.containsParam = strings.Index(s.Uri, "/:") > -1
client := &http.Client{}
httpTransportSettings := &http.Transport{}
if s.Timeout > 0 {
httpTransportSettings.ResponseHeaderTimeout = time.Second * time.Duration(s.Timeout)
}
logger := ctx.Logger()
// Set the proxy server to use, if supplied
if len(s.Proxy) > 0 {
proxyURL, err := url.Parse(s.Proxy)
if err != nil {
logger.Debugf("Error parsing proxy url '%s': %s", s.Proxy, err)
return nil, err
}
logger.Debug("Setting proxy server:", s.Proxy)
httpTransportSettings.Proxy = http.ProxyURL(proxyURL)
}
if strings.HasPrefix(s.Uri, "https") {
cfg := &ssl.Config{}
if len(s.SSLConfig) != 0 {
err := cfg.FromMap(s.SSLConfig)
if err != nil {
return nil, err
}
if _, set := s.SSLConfig["skipVerify"]; !set {
cfg.SkipVerify = true
}
if _, set := s.SSLConfig["useSystemCert"]; !set {
cfg.UseSystemCert = true
}
} else {
//using ssl but not configured, use defaults
cfg.SkipVerify = true
cfg.UseSystemCert = true
}
tlsConfig, err := ssl.NewClientTLSConfig(cfg)
if err != nil {
return nil, err
}
httpTransportSettings.TLSClientConfig = tlsConfig
}
client.Transport = httpTransportSettings
act.client = client
return act, nil
}
// Activity is an activity that is used to invoke a REST Operation
// settings : {method, uri, headers, proxy, skipSSL}
// input : {pathParams, queryParams, headers, content}
// outputs : {status, result}
type Activity struct {
settings *Settings
containsParam bool
client *http.Client
}
func (a *Activity) Metadata() *activity.Metadata {
return activityMd
}
// Eval implements api.Activity.Eval - Invokes a REST Operation
func (a *Activity) Eval(ctx activity.Context) (done bool, err error) {
input := &Input{}
err = ctx.GetInputObject(input)
if err != nil {
return false, err
}
uri := a.settings.Uri
if a.containsParam {
if len(input.PathParams) == 0 {
err := activity.NewError("Path Params not specified, required for URI: "+uri, "", nil)
return false, err
}
uri = BuildURI(a.settings.Uri, input.PathParams)
}
if len(input.QueryParams) > 0 {
qp := url.Values{}
for key, value := range input.QueryParams {
qp.Set(key, value)
}
uri = uri + "?" + qp.Encode()
}
logger := ctx.Logger()
if logger.DebugEnabled() {
logger.Debugf("REST Call: [%s] %s", a.settings.Method, uri)
}
var reqBody io.Reader
contentType := "application/json; charset=UTF-8"
method := a.settings.Method
if method == methodPOST || method == methodPUT || method == methodPATCH {
contentType = getContentType(input.Content)
if input.Content != nil {
if str, ok := input.Content.(string); ok {
reqBody = bytes.NewBuffer([]byte(str))
} else {
b, _ := json.Marshal(input.Content) //todo handle error
reqBody = bytes.NewBuffer([]byte(b))
}
}
} else {
reqBody = nil
}
req, err := http.NewRequest(method, uri, reqBody)
if err != nil {
return false, err
}
if reqBody != nil {
req.Header.Set("Content-Type", contentType)
}
headers := a.getHeaders(input.Headers)
// Set headers
if len(headers) > 0 {
if logger.DebugEnabled() {
logger.Debug("Setting HTTP request headers...")
}
for key, value := range headers {
if logger.TraceEnabled() {
logger.Trace("%s: %s", key, value)
}
req.Header.Set(key, value)
}
}
resp, err := a.client.Do(req)
if err != nil {
return false, err
}
if resp == nil {
logger.Trace("Empty response")
return true, nil
}
defer func() {
if resp.Body != nil {
_ = resp.Body.Close()
}
}()
if logger.DebugEnabled() {
logger.Debug("Response status:", resp.Status)
}
respHeaders := make(map[string]string, len(resp.Header))
for key := range resp.Header {
respHeaders[key] = resp.Header.Get(key)
}
var cookies []interface{}
for _, cookie := range resp.Header["Set-Cookie"] {
cookies = append(cookies, cookie)
}
var result interface{}
// Check the HTTP Header Content-Type
respContentType := resp.Header.Get("Content-Type")
switch respContentType {
case "application/json":
d := json.NewDecoder(resp.Body)
d.UseNumber()
err = d.Decode(&result)
if err != nil {
switch {
case err == io.EOF:
// empty body
default:
return false, err
}
}
default:
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return false, err
}
result = string(b)
}
if logger.TraceEnabled() {
logger.Trace("Response body:", result)
}
output := &Output{Status: resp.StatusCode, Data: result, Headers:respHeaders, Cookies:cookies}
err = ctx.SetOutputObject(output)
if err != nil {
return false, err
}
return true, nil
}
////////////////////////////////////////////////////////////////////////////////////////
// Utils
func (a *Activity) getHeaders(inputHeaders map[string]string) map[string]string {
if len(inputHeaders) == 0 {
return a.settings.Headers
}
if len(a.settings.Headers) == 0 {
return inputHeaders
}
headers := make(map[string]string)
for key, value := range a.settings.Headers {
headers[key] = value
}
for key, value := range inputHeaders {
headers[key] = value
}
return headers
}
//todo just make contentType a setting
func getContentType(replyData interface{}) string {
contentType := "application/json; charset=UTF-8"
switch v := replyData.(type) {
case string:
if !strings.HasPrefix(v, "{") && !strings.HasPrefix(v, "[") {
contentType = "text/plain; charset=UTF-8"
}
case int, int64, float64, bool, json.Number:
contentType = "text/plain; charset=UTF-8"
default:
contentType = "application/json; charset=UTF-8"
}
return contentType
}
// BuildURI is a temporary crude URI builder
func BuildURI(uri string, values map[string]string) string {
var buffer bytes.Buffer
buffer.Grow(len(uri))
addrStart := strings.Index(uri, "://")
i := addrStart + 3
for i < len(uri) {
if uri[i] == '/' {
break
}
i++
}
buffer.WriteString(uri[0:i])
for i < len(uri) {
if uri[i] == ':' {
j := i + 1
for j < len(uri) && uri[j] != '/' {
j++
}
if i+1 == j {
buffer.WriteByte(uri[i])
i++
} else {
param := uri[i+1 : j]
value := values[param]
buffer.WriteString(value)
if j < len(uri) {
buffer.WriteString("/")
}
i = j + 1
}
} else {
buffer.WriteByte(uri[i])
i++
}
}
return buffer.String()
}