-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrigger.go
313 lines (259 loc) · 8.2 KB
/
trigger.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
package mht
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"github.com/TIBCOSoftware/flogo-contrib/trigger/rest/cors"
"github.com/TIBCOSoftware/flogo-lib/core/action"
"github.com/TIBCOSoftware/flogo-lib/core/trigger"
"github.com/TIBCOSoftware/flogo-lib/logger"
condition "github.com/TIBCOSoftware/mashling-lib/conditions"
"github.com/TIBCOSoftware/mashling-lib/util"
"github.com/julienschmidt/httprouter"
)
const (
REST_CORS_PREFIX = "REST_TRIGGER"
)
// log is the default package logger
var log = logger.GetLogger("trigger-tibco-rest")
//OptimizedHandler optimized handler
type OptimizedHandler struct {
defaultActionId string
settings map[string]interface{}
dispatches []*Dispatch
}
//Dispatch holds dispatch actionId and condition
type Dispatch struct {
actionId string
condition string
}
var validMethods = []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"}
// RestTrigger REST trigger struct
type RestTrigger struct {
metadata *trigger.Metadata
runner action.Runner
server *Server
config *trigger.Config
}
//NewFactory create a new Trigger factory
func NewFactory(md *trigger.Metadata) trigger.Factory {
return &RestFactory{metadata: md}
}
// RestFactory REST Trigger factory
type RestFactory struct {
metadata *trigger.Metadata
}
//New Creates a new trigger instance for a given id
func (t *RestFactory) New(config *trigger.Config) trigger.Trigger {
return &RestTrigger{metadata: t.metadata, config: config}
}
// Metadata implements trigger.Trigger.Metadata
func (t *RestTrigger) Metadata() *trigger.Metadata {
return t.metadata
}
//Init trigger initialization
func (t *RestTrigger) Init(runner action.Runner) {
log.SetLogLevel(logger.DebugLevel)
router := httprouter.New()
if t.config.Settings == nil {
panic(fmt.Sprintf("No Settings found for trigger '%s'", t.config.Id))
}
if _, ok := t.config.Settings["port"]; !ok {
panic(fmt.Sprintf("No Port found for trigger '%s' in settings", t.config.Id))
}
addr := ":" + t.config.GetSetting("port")
t.runner = runner
//optimize flog-handlers i.e merge handlers having same settings
optHandlers := []*OptimizedHandler{}
for _, handler := range t.config.Handlers {
//check if there is any handler already added with same settings
handlerAdded := false
for _, optHandler := range optHandlers {
//loop through all settings
settingsMatched := true
for k, v := range optHandler.settings {
if v != handler.Settings[k] {
settingsMatched = false
break
}
}
if settingsMatched {
//check for dispatch condition
if dispatchCondition := handler.Settings[util.Flogo_Trigger_Handler_Setting_Condition]; dispatchCondition != nil {
tmpDispatch := &Dispatch{
actionId: handler.ActionId,
condition: dispatchCondition.(string),
}
optHandler.dispatches = append(optHandler.dispatches, tmpDispatch)
} else {
//no dispatch condition, hence make it as default action
optHandler.defaultActionId = handler.ActionId
}
handlerAdded = true
break
}
}
if !handlerAdded {
tmpSettings := make(map[string]interface{})
for k, v := range handler.Settings {
if k != util.Flogo_Trigger_Handler_Setting_Condition {
tmpSettings[k] = v
}
}
var tmpDispatches []*Dispatch
//check for dispatch condition
if dispatchCondition := handler.Settings[util.Flogo_Trigger_Handler_Setting_Condition]; dispatchCondition != nil {
tmpDispatch := &Dispatch{
actionId: handler.ActionId,
condition: handler.Settings[util.Flogo_Trigger_Handler_Setting_Condition].(string),
}
tmpDispatches = append(tmpDispatches, tmpDispatch)
}
optHandler := OptimizedHandler{
defaultActionId: handler.ActionId,
settings: tmpSettings,
dispatches: tmpDispatches,
}
optHandlers = append(optHandlers, &optHandler)
}
}
// Init handlers
for _, optHandler := range optHandlers {
if handlerIsValid(optHandler) {
method := strings.ToUpper(optHandler.settings["method"].(string))
path := optHandler.settings["path"].(string)
log.Debugf("REST Trigger: Registering handler [%s: %s] with default Action Id: [%s]", method, path, optHandler.defaultActionId)
router.OPTIONS(path, handleCorsPreflight)
router.Handle(method, path, newActionHandler(t, optHandler))
}
}
log.Debugf("REST Trigger: Configured on port %s", t.config.Settings["port"])
t.server = NewServer(addr, router)
}
func (t *RestTrigger) Start() error {
return t.server.Start()
}
// Stop implements util.Managed.Stop
func (t *RestTrigger) Stop() error {
return t.server.Stop()
}
// Handles the cors preflight request
func handleCorsPreflight(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
log.Infof("Received [OPTIONS] request to CorsPreFlight: %+v", r)
c := cors.New(REST_CORS_PREFIX, log)
c.HandlePreflight(w, r)
}
// IDResponse id response object
type IDResponse struct {
ID string `json:"id"`
}
func newActionHandler(rt *RestTrigger, handler *OptimizedHandler) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
log.Infof("REST Trigger: Received request for id '%s'", rt.config.Id)
c := cors.New(REST_CORS_PREFIX, log)
c.WriteCorsActualRequestHeaders(w)
pathParams := make(map[string]string)
for _, param := range ps {
pathParams[param.Key] = param.Value
}
var content interface{}
err := json.NewDecoder(r.Body).Decode(&content)
if err != nil {
switch {
case err == io.EOF:
// empty body
//todo should handler say if content is expected?
case err != nil:
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
}
queryValues := r.URL.Query()
queryParams := make(map[string]string, len(queryValues))
for key, value := range queryValues {
queryParams[key] = strings.Join(value, ",")
}
data := map[string]interface{}{
"params": pathParams,
"pathParams": pathParams,
"queryParams": queryParams,
"content": content,
}
//pick action based on dispatch condition
contentBytes, err := json.Marshal(content)
contentStr := string(contentBytes)
actionId := ""
for _, dispatch := range handler.dispatches {
expressionStr := dispatch.condition
conditionOperation, err := condition.GetConditionOperation(expressionStr)
if err != nil {
log.Errorf("not able parse the condition '%v' mentioned for content based handler. skipping the handler.", expressionStr)
continue
}
//evaluate expression
exprResult, err := condition.EvaluateCondition(*conditionOperation, contentStr)
if err != nil {
log.Errorf("not able evaluate expression - %v with error - %v. skipping the handler.", expressionStr, err)
}
if exprResult {
actionId = dispatch.actionId
log.Debugf("dispatch resolved with the actionId - %v", actionId)
break
}
}
//If no dispatch is found, use default action
if actionId == "" {
actionId = handler.defaultActionId
log.Debugf("dispatch not resolved. Continue with default action - %v", actionId)
}
//todo handle error
startAttrs, _ := rt.metadata.OutputsToAttrs(data, false)
action := action.Get(actionId)
log.Debugf("Found action' %+x'", action)
context := trigger.NewContext(context.Background(), startAttrs)
replyCode, replyData, err := rt.runner.Run(context, action, actionId, nil)
if err != nil {
log.Debugf("REST Trigger Error: %s", err.Error())
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if replyData != nil {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(replyCode)
if err := json.NewEncoder(w).Encode(replyData); err != nil {
log.Error(err)
}
}
if replyCode > 0 {
w.WriteHeader(replyCode)
} else {
w.WriteHeader(http.StatusOK)
}
}
}
////////////////////////////////////////////////////////////////////////////////////////
// Utils
func handlerIsValid(handler *OptimizedHandler) bool {
if handler.settings == nil {
return false
}
if handler.settings["method"] == "" {
return false
}
if !stringInList(strings.ToUpper(handler.settings["method"].(string)), 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
}