-
Notifications
You must be signed in to change notification settings - Fork 6
/
formatter.go
312 lines (267 loc) · 9.24 KB
/
formatter.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
// Tideland Go REST Server Library - REST - Formatter
//
// Copyright (C) 2009-2016 Frank Mueller / Tideland / Oldenburg / Germany
//
// All rights reserved. Use of this source code is governed
// by the new BSD license.
package rest
//--------------------
// IMPORTS
//--------------------
import (
"bytes"
"encoding/gob"
"encoding/json"
"encoding/xml"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"time"
"github.com/tideland/golib/errors"
"github.com/tideland/golib/logger"
"github.com/tideland/golib/stringex"
)
//--------------------
// CONST
//--------------------
const (
// Standard REST status codes.
StatusOK = http.StatusOK
StatusCreated = http.StatusCreated
StatusNoContent = http.StatusNoContent
StatusBadRequest = http.StatusBadRequest
StatusUnauthorized = http.StatusUnauthorized
StatusNotFound = http.StatusNotFound
StatusConflict = http.StatusConflict
StatusInternalServerError = http.StatusInternalServerError
// Standard REST content types.
ContentTypePlain = "text/plain"
ContentTypeHTML = "text/html"
ContentTypeXML = "application/xml"
ContentTypeJSON = "application/json"
ContentTypeGOB = "application/vnd.tideland.gob"
ContentTypeURLEncoded = "application/x-www-form-urlencoded"
)
//--------------------
// GLOBAL
//--------------------
var (
defaulter = stringex.NewDefaulter("job", false)
)
//--------------------
// ENVELOPE
//--------------------
// envelope is a helper to give a qualified feedback in RESTful requests.
// It contains wether the request has been successful, in case of an
// error an additional message and the payload.
type envelope struct {
Status string `json:"status" xml:"status"`
Message string `json:"message,omitempty" xml:"message,omitempty"`
Payload interface{} `json:"payload,omitempty" xml:"payload,omitempty"`
}
//--------------------
// FORMATTER
//--------------------
type Formatter interface {
// Write encodes the passed data to implementers format and writes
// it with the passed status code and possible header values to the
// response writer.
Write(status int, data interface{}, headers ...KeyValue) error
// Read checks if the request content type matches the implementers
// format, reads its body and decodes it to the value pointed to by
// data.
Read(data interface{}) error
}
// PositiveFeedback writes a positive feedback envelope to the formatter.
func PositiveFeedback(f Formatter, payload interface{}, msg string, args ...interface{}) error {
fmsg := fmt.Sprintf(msg, args...)
return f.Write(StatusOK, envelope{"success", fmsg, payload})
}
// NegativeFeedback writes a negative feedback envelope to the formatter.
func NegativeFeedback(f Formatter, status int, msg string, args ...interface{}) error {
fmsg := fmt.Sprintf(msg, args...)
logger.Warningf(fmsg)
return f.Write(status, envelope{"fail", fmsg, nil})
}
//--------------------
// GOB FORMATTER
//--------------------
// gobFormatter implements Formatter for the GOB encoding.
type gobFormatter struct {
job Job
}
// Write is specified on the Formatter interface.
func (gf *gobFormatter) Write(status int, data interface{}, headers ...KeyValue) error {
enc := gob.NewEncoder(gf.job.ResponseWriter())
for _, header := range headers {
gf.job.ResponseWriter().Header().Add(header.Key, fmt.Sprintf("%v", header.Value))
}
gf.job.ResponseWriter().Header().Set("Content-Type", ContentTypeGOB)
gf.job.ResponseWriter().Header().Set("Version", gf.job.Version().String())
gf.job.ResponseWriter().WriteHeader(status)
err := enc.Encode(data)
if err != nil {
http.Error(gf.job.ResponseWriter(), err.Error(), http.StatusInternalServerError)
}
return err
}
// Read is specified on the Formatter interface.
func (gf *gobFormatter) Read(data interface{}) error {
if !gf.job.HasContentType(ContentTypeGOB) {
return errors.New(ErrInvalidContentType, errorMessages, ContentTypeGOB)
}
dec := gob.NewDecoder(gf.job.Request().Body)
err := dec.Decode(data)
gf.job.Request().Body.Close()
return err
}
//--------------------
// JSON FORMATTER
//--------------------
// jsonFormatter implements Formatter for the JSON encoding. Writing
// also can be done with HTML escaping.
type jsonFormatter struct {
job Job
html bool
}
// Write is specified on the Formatter interface.
func (jf *jsonFormatter) Write(status int, data interface{}, headers ...KeyValue) error {
body, err := json.Marshal(data)
if err != nil {
http.Error(jf.job.ResponseWriter(), err.Error(), http.StatusInternalServerError)
return err
}
if jf.html {
var buf bytes.Buffer
json.HTMLEscape(&buf, body)
body = buf.Bytes()
}
for _, header := range headers {
jf.job.ResponseWriter().Header().Add(header.Key, fmt.Sprintf("%v", header.Value))
}
jf.job.ResponseWriter().Header().Set("Content-Type", ContentTypeJSON)
jf.job.ResponseWriter().Header().Set("Version", jf.job.Version().String())
jf.job.ResponseWriter().WriteHeader(status)
_, err = jf.job.ResponseWriter().Write(body)
return err
}
// Read is specified on the Formatter interface.
func (jf *jsonFormatter) Read(data interface{}) error {
if !jf.job.HasContentType(ContentTypeJSON) {
return errors.New(ErrInvalidContentType, errorMessages, ContentTypeJSON)
}
body, err := ioutil.ReadAll(jf.job.Request().Body)
jf.job.Request().Body.Close()
if err != nil {
return err
}
return json.Unmarshal(body, &data)
}
//--------------------
// XML FORMATTER
//--------------------
// xmlFormatter implements Formatter for the XML encoding.
type xmlFormatter struct {
job Job
}
// Write is specified on the Formatter interface.
func (xf *xmlFormatter) Write(status int, data interface{}, headers ...KeyValue) error {
body, err := xml.Marshal(data)
if err != nil {
http.Error(xf.job.ResponseWriter(), err.Error(), http.StatusInternalServerError)
return err
}
for _, header := range headers {
xf.job.ResponseWriter().Header().Add(header.Key, fmt.Sprintf("%v", header.Value))
}
xf.job.ResponseWriter().Header().Set("Content-Type", ContentTypeXML)
xf.job.ResponseWriter().Header().Set("Version", xf.job.Version().String())
xf.job.ResponseWriter().WriteHeader(status)
_, err = xf.job.ResponseWriter().Write(body)
return err
}
// Read is specified on the Formatter interface.
func (xf *xmlFormatter) Read(data interface{}) error {
if !xf.job.HasContentType(ContentTypeXML) {
return errors.New(ErrInvalidContentType, errorMessages, ContentTypeXML)
}
body, err := ioutil.ReadAll(xf.job.Request().Body)
xf.job.Request().Body.Close()
if err != nil {
return err
}
return xml.Unmarshal(body, &data)
}
//--------------------
// QUERY
//--------------------
// Query allows typed access with default values to a jobs
// request values passed as query.
type Query interface {
// ValueAsString retrieves the string value of a given key. If it
// doesn't exist the default value dv is returned.
ValueAsString(key, dv string) string
// ValueAsBool retrieves the bool value of a given key. If it
// doesn't exist the default value dv is returned.
ValueAsBool(key string, dv bool) bool
// ValueAsInt retrieves the int value of a given key. If it
// doesn't exist the default value dv is returned.
ValueAsInt(key string, dv int) int
// ValueAsFloat64 retrieves the float64 value of a given key. If it
// doesn't exist the default value dv is returned.
ValueAsFloat64(key string, dv float64) float64
// ValueAsTime retrieves the string value of a given key and
// interprets it as time with the passed format. If it
// doesn't exist the default value dv is returned.
ValueAsTime(key, layout string, dv time.Time) time.Time
// ValueAsDuration retrieves the duration value of a given key.
// If it doesn't exist the default value dv is returned.
ValueAsDuration(key string, dv time.Duration) time.Duration
}
// query implements Query.
type query struct {
values url.Values
}
// ValueAsString implements the Query interface.
func (q *query) ValueAsString(key, dv string) string {
value := queryValuer(q.values.Get(key))
return defaulter.AsString(value, dv)
}
// ValueAsBool implements the Query interface.
func (q *query) ValueAsBool(key string, dv bool) bool {
value := queryValuer(q.values.Get(key))
return defaulter.AsBool(value, dv)
}
// ValueAsInt implements the Query interface.
func (q *query) ValueAsInt(key string, dv int) int {
value := queryValuer(q.values.Get(key))
return defaulter.AsInt(value, dv)
}
// ValueAsFloat64 implements the Query interface.
func (q *query) ValueAsFloat64(key string, dv float64) float64 {
value := queryValuer(q.values.Get(key))
return defaulter.AsFloat64(value, dv)
}
// ValueAsTime implements the Query interface.
func (q *query) ValueAsTime(key, format string, dv time.Time) time.Time {
value := queryValuer(q.values.Get(key))
return defaulter.AsTime(value, format, dv)
}
// ValueAsDuration implements the Query interface.
func (q *query) ValueAsDuration(key string, dv time.Duration) time.Duration {
value := queryValuer(q.values.Get(key))
return defaulter.AsDuration(value, dv)
}
// queryValues implements the stringex.Valuer interface for
// the usage inside of query.
type queryValuer string
// Value implements the Valuer interface.
func (qv queryValuer) Value() (string, error) {
v := string(qv)
if len(v) == 0 {
return "", errors.New(ErrQueryValueNotFound, errorMessages)
}
return v, nil
}
// EOF