forked from TIBCOSoftware/flogo-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactivity.go
executable file
·282 lines (215 loc) · 5.85 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
package rest
import (
"bytes"
"crypto/tls"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
"github.com/TIBCOSoftware/flogo-lib/core/activity"
"github.com/TIBCOSoftware/flogo-lib/logger"
)
// log is the default package logger
var log = logger.GetLogger("activity-tibco-rest")
const (
methodGET = "GET"
methodPOST = "POST"
methodPUT = "PUT"
methodPATCH = "PATCH"
methodDELETE = "DELETE"
ivMethod = "method"
ivURI = "uri"
ivPathParams = "pathParams"
ivQueryParams = "queryParams"
ivHeader = "header"
ivContent = "content"
ivParams = "params"
ivProxy = "proxy"
ivSkipSsl = "skipSsl"
ovResult = "result"
ovStatus = "status"
)
var validMethods = []string{methodGET, methodPOST, methodPUT, methodPATCH, methodDELETE}
// RESTActivity is an Activity that is used to invoke a REST Operation
// inputs : {method,uri,params}
// outputs: {result}
type RESTActivity struct {
metadata *activity.Metadata
}
// NewActivity creates a new RESTActivity
func NewActivity(metadata *activity.Metadata) activity.Activity {
return &RESTActivity{metadata: metadata}
}
// Metadata returns the activity's metadata
func (a *RESTActivity) Metadata() *activity.Metadata {
return a.metadata
}
// Eval implements api.Activity.Eval - Invokes a REST Operation
func (a *RESTActivity) Eval(context activity.Context) (done bool, err error) {
method := strings.ToUpper(context.GetInput(ivMethod).(string))
uri := context.GetInput(ivURI).(string)
containsParam := strings.Index(uri, "/:") > -1
if containsParam {
val := context.GetInput(ivPathParams)
if val == nil {
val = context.GetInput(ivParams)
if val == nil {
err := activity.NewError("Path Params not specified, required for URI: "+uri, "", nil)
return false, err
}
}
pathParams := val.(map[string]string)
uri = BuildURI(uri, pathParams)
}
if queryParams, ok := context.GetInput(ivQueryParams).(map[string]string); ok && len(queryParams) > 0 {
qp := url.Values{}
for key, value := range queryParams {
qp.Set(key, value)
}
uri = uri + "?" + qp.Encode()
}
log.Debugf("REST Call: [%s] %s\n", method, uri)
var reqBody io.Reader
contentType := "application/json; charset=UTF-8"
if method == methodPOST || method == methodPUT || method == methodPATCH {
content := context.GetInput(ivContent)
contentType = getContentType(content)
if content != nil {
if str, ok := content.(string); ok {
reqBody = bytes.NewBuffer([]byte(str))
} else {
b, _ := json.Marshal(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)
}
// Set headers
log.Debug("Setting HTTP request headers...")
if headers, ok := context.GetInput(ivHeader).(map[string]string); ok && len(headers) > 0 {
for key, value := range headers {
log.Debugf("%s: %s", key, value)
req.Header.Set(key, value)
}
}
httpTransportSettings := &http.Transport{}
// Set the proxy server to use, if supplied
proxy := context.GetInput(ivProxy)
var client *http.Client
var proxyValue, ok = proxy.(string)
if ok && len(proxyValue) > 0 {
proxyURL, urlErr := url.Parse(proxyValue)
if urlErr != nil {
log.Debug("Error parsing proxy url:", urlErr)
return false, urlErr
}
log.Debug("Setting proxy server:", proxyValue)
httpTransportSettings.Proxy = http.ProxyURL(proxyURL)
}
// Skip ssl validation
skipSsl, ok := context.GetInput(ivSkipSsl).(bool)
if ok && skipSsl {
httpTransportSettings.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
}
client = &http.Client{Transport: httpTransportSettings}
resp, err := client.Do(req)
defer resp.Body.Close()
if err != nil {
return false, err
}
log.Debug("response Status:", resp.Status)
respBody, _ := ioutil.ReadAll(resp.Body)
var result interface{}
if strings.Contains(resp.Header.Get("Content-Type"), "application/json") {
d := json.NewDecoder(bytes.NewReader(respBody))
d.UseNumber()
err = d.Decode(&result)
} else {
result = string(respBody)
}
//json.Unmarshal(respBody, &result)
log.Debug("response Body:", result)
context.SetOutput(ovResult, result)
context.SetOutput(ovStatus, resp.StatusCode)
return true, nil
}
////////////////////////////////////////////////////////////////////////////////////////
// Utils
//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
}
func methodIsValid(method string) bool {
if !stringInList(method, validMethods) {
return false
}
//validate path
return true
}
func stringInList(str string, list []string) bool {
for _, value := range list {
if value == str {
return true
}
}
return false
}
// 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()
}