/
managerold.go
184 lines (162 loc) · 4.76 KB
/
managerold.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
package support
import (
//"bytes"
//"compress/gzip"
//"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"sync"
"github.com/TIBCOSoftware/flogo-contrib/action/flow/definition"
"github.com/TIBCOSoftware/flogo-lib/logger"
"github.com/TIBCOSoftware/flogo-lib/util"
)
//const (
// uriSchemeFile = "file://"
// uriSchemeHttp = "http://"
// uriSchemeRes = "res://"
//)
// FlowManagerOld is a simple manager for flows
type FlowManagerOld struct {
mu *sync.Mutex // protects the flow maps
flows map[string]*FlowEntry
}
// FlowEntry will contain either a compressed flow, an uncompressed flow or a flow uri
type FlowEntry struct {
compressed string
uncompressed []byte
uri string
}
// NewFlowManager creates a new FlowManagerOld
func NewFlowManagerOld() *FlowManagerOld {
return &FlowManagerOld{mu: &sync.Mutex{}, flows: make(map[string]*FlowEntry)}
}
// AddCompressed adds a compressed flow to the map of flow entries
func (mgr *FlowManagerOld) AddCompressed(id string, newFlow string) error {
if len(newFlow) < 3 {
return fmt.Errorf("Empty Flow with id '%s'", id)
}
mgr.mu.Lock()
defer mgr.mu.Unlock()
_, ok := mgr.flows[id]
if ok {
return fmt.Errorf("Flow with id '%s' already exists", id)
}
// Add the flow
mgr.flows[id] = &FlowEntry{compressed: newFlow}
logger.Debugf("Compressed flow with id '%s' added", id)
return nil
}
// TODO add schema validation for flow
// AddUncompressed adds an uncompressed flow to the map of flow entries
func (mgr *FlowManagerOld) AddUncompressed(id string, newFlow []byte) error {
if len(newFlow) < 3 {
return fmt.Errorf("Empty Flow with id '%s'", id)
}
mgr.mu.Lock()
defer mgr.mu.Unlock()
_, ok := mgr.flows[id]
if ok {
return fmt.Errorf("Flow with id '%s' already exists", id)
}
// Add the flow
mgr.flows[id] = &FlowEntry{uncompressed: newFlow}
logger.Debugf("Uncompressed flow with id '%s' added", id)
return nil
}
// AddURI adds a uri flow to the map of flow entries
func (mgr *FlowManagerOld) AddURI(id string, newUri string) error {
if len(newUri) < 3 {
return fmt.Errorf("Empty Flow URI with id '%s'", id)
}
mgr.mu.Lock()
defer mgr.mu.Unlock()
_, ok := mgr.flows[id]
if ok {
return fmt.Errorf("Flow with id '%s' already exists", id)
}
// Add the flow
mgr.flows[id] = &FlowEntry{uri: newUri}
logger.Debugf("URI flow with id '%s' added", id)
return nil
}
// GetFlow gets the specified embedded flow
func (mgr *FlowManagerOld) GetFlow(id string) (*definition.DefinitionRep, error) {
entry, ok := mgr.flows[id]
if !ok {
//temporary fix for tester (dynamic uri)
if strings.HasPrefix(id, uriSchemeHttp) {
entry = &FlowEntry{uri: id}
mgr.flows[id] = entry
} else {
err := fmt.Errorf("No flow found for id '%s'", id)
logger.Errorf(err.Error())
return nil, err
}
}
var flowDefBytes []byte
// Uncompressed Flow condition
if len(entry.uncompressed) > 0 {
// Uncompressed flow
flowDefBytes = entry.uncompressed
}
// Compressed Flow condition
if len(entry.compressed) > 0 {
decodedBytes, err := decodeAndUnzip(entry.compressed)
if err != nil {
decodeErr := fmt.Errorf("Error decoding compressed flow with id '%s', %s", id, err.Error())
logger.Errorf(decodeErr.Error())
return nil, decodeErr
}
flowDefBytes = decodedBytes
}
// URI Flow condition
if len(entry.uri) > 0 {
if strings.HasPrefix(entry.uri, uriSchemeFile) {
// File URI
logger.Infof("Loading Local Flow: %s\n", entry.uri)
flowFilePath, _ := util.URLStringToFilePath(entry.uri)
readBytes, err := ioutil.ReadFile(flowFilePath)
if err != nil {
readErr := fmt.Errorf("Error reading flow file with id '%s', %s", id, err.Error())
logger.Errorf(readErr.Error())
return nil, readErr
}
flowDefBytes = readBytes
} else {
// URI
req, err := http.NewRequest("GET", entry.uri, nil)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
getErr := fmt.Errorf("Error getting flow uri with id '%s', %s", id, err.Error())
logger.Errorf(getErr.Error())
return nil, getErr
}
defer resp.Body.Close()
logger.Infof("response Status:", resp.Status)
if resp.StatusCode >= 300 {
//not found
getErr := fmt.Errorf("Error getting flow uri with id '%s', status code %d", id, resp.StatusCode)
logger.Errorf(getErr.Error())
return nil, getErr
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
readErr := fmt.Errorf("Error reading flow uri response body with id '%s', %s", id, err.Error())
logger.Errorf(readErr.Error())
return nil, readErr
}
flowDefBytes = body
}
}
var flow *definition.DefinitionRep
err := json.Unmarshal(flowDefBytes, &flow)
if err != nil {
logger.Errorf(err.Error())
return nil, fmt.Errorf("Error marshalling flow with id '%s', %s", id, err.Error())
}
return flow, nil
}