forked from forj-oss/forjj-jenkins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
171 lines (140 loc) · 3.99 KB
/
handlers.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
// This file has been created by "go generate" as initial code. go generate will never update it, EXCEPT if you remove it.
// So, update it for your need.
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"strings"
"github.com/forj-oss/goforjj"
)
// RESTReaderLimit is the maximun supported REST API request size.
const RESTReaderLimit = 65535
// PluginData response object creator
func newPluginData() *goforjj.PluginData {
r := goforjj.PluginData{
Repos: make(map[string]goforjj.PluginRepo),
Options: make(map[string]goforjj.PluginOption),
Services: goforjj.PluginService{
Urls: make(map[string]string),
},
}
return &r
}
// Function to detect header content-type matching
// return true if match
func contentTypeMatch(header http.Header, match string) (string, bool) {
contentType, found := header["Content-Type"]
if !found {
return "Header 'Content-Type' missing.", false
}
for _, v := range contentType {
if v == match {
return "", true
}
}
return strings.Join(contentType, ", "), false
}
func panicIfError(w http.ResponseWriter, err error, message string, pars ...interface{}) {
if err != nil {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(422) // unprocessable entity
if message != "" {
err = fmt.Errorf("%s %s", fmt.Errorf(message, pars...), err)
}
if err := json.NewEncoder(w).Encode(err); err != nil {
panic(err)
}
}
}
func requestResponse(w http.ResponseWriter, data *goforjj.PluginData, code int) {
if data.ErrorMessage != "" {
if code == 0 {
code = 422 // unprocessable entity
}
log.Print("HTTP ERROR: ", code, " - ", data.ErrorMessage)
} else {
code = 200
}
w.WriteHeader(code)
if err := json.NewEncoder(w).Encode(data); err != nil {
panic(err)
}
}
func doPluginAction(w http.ResponseWriter, r *http.Request,
requestUnmarshal func([]byte) error,
requestDo func(*goforjj.PluginData) int) {
data := newPluginData()
var errCode int
// Respond to the request in json format except if fatal
defer requestResponse(w, data, errCode)
body, err := ioutil.ReadAll(io.LimitReader(r.Body, RESTReaderLimit))
if err != nil {
data.Errorf("Unable to read request buffer. %s", err)
return
}
if contentType, found := contentTypeMatch(r.Header, "application/json"); !found {
data.Errorf("Invalid request payload format. Must be 'application/json'. Got %s", contentType)
return
}
err = requestUnmarshal(body)
if err != nil {
if err.Error() == "unexpected end of JSON input" && len(body) == RESTReaderLimit {
err = fmt.Errorf("%s. The plugin reach REST API request buffer limit (%d)", err, RESTReaderLimit)
}
data.Errorf("Unable to decode json. %s", err)
return
}
errCode = requestDo(data)
// defer will respond, with data and errCode
}
// request Handlers
// Create handler
func Create(w http.ResponseWriter, r *http.Request) {
var reqData CreateReq
doPluginAction(w, r,
func(body []byte) error {
return json.Unmarshal(body, &reqData)
},
func(data *goforjj.PluginData) (errCode int) {
errCode = DoCreate(r, &reqData, data)
reqData.Objects.SaveMaintainOptions(data)
return
})
}
// Update handler
func Update(w http.ResponseWriter, r *http.Request) {
var reqData UpdateReq
doPluginAction(w, r,
func(body []byte) error {
return json.Unmarshal(body, &reqData)
},
func(data *goforjj.PluginData) (errCode int) {
errCode = DoUpdate(r, &reqData, data)
reqData.Objects.SaveMaintainOptions(data)
return
})
}
// Maintain handler
func Maintain(w http.ResponseWriter, r *http.Request) {
var reqData MaintainReq
doPluginAction(w, r,
func(body []byte) error {
return json.Unmarshal(body, &reqData)
},
func(data *goforjj.PluginData) int {
return DoMaintain(r, &reqData, data)
})
}
// Index Handler
//
func Index(w http.ResponseWriter, _ *http.Request) {
fmt.Fprintf(w, "FORJJ - jenkins driver for FORJJ. It is Implemented as a REST API.")
}
// Quit handler
func Quit(w http.ResponseWriter, _ *http.Request) {
goforjj.DefaultQuit(w, "")
}