2015-04-13 21:12:03 +00:00
|
|
|
package audit
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2016-09-21 14:29:42 +00:00
|
|
|
"fmt"
|
2015-04-13 21:12:03 +00:00
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
2016-09-21 14:29:42 +00:00
|
|
|
// JSONFormatWriter is an AuditFormatWriter implementation that structures data into
|
2015-04-13 21:12:03 +00:00
|
|
|
// a JSON format.
|
2017-02-11 00:56:28 +00:00
|
|
|
type JSONFormatWriter struct {
|
|
|
|
Prefix string
|
|
|
|
}
|
2015-06-19 01:30:18 +00:00
|
|
|
|
2016-09-21 14:29:42 +00:00
|
|
|
func (f *JSONFormatWriter) WriteRequest(w io.Writer, req *AuditRequestEntry) error {
|
|
|
|
if req == nil {
|
|
|
|
return fmt.Errorf("request entry was nil, cannot encode")
|
2015-06-19 01:30:18 +00:00
|
|
|
}
|
2015-04-13 21:12:03 +00:00
|
|
|
|
2017-02-11 00:56:28 +00:00
|
|
|
if len(f.Prefix) > 0 {
|
|
|
|
_, err := w.Write([]byte(f.Prefix))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-13 21:12:03 +00:00
|
|
|
enc := json.NewEncoder(w)
|
2016-09-21 14:29:42 +00:00
|
|
|
return enc.Encode(req)
|
2015-04-13 21:12:03 +00:00
|
|
|
}
|
|
|
|
|
2016-09-21 14:29:42 +00:00
|
|
|
func (f *JSONFormatWriter) WriteResponse(w io.Writer, resp *AuditResponseEntry) error {
|
2015-04-13 21:12:03 +00:00
|
|
|
if resp == nil {
|
2016-09-21 14:29:42 +00:00
|
|
|
return fmt.Errorf("response entry was nil, cannot encode")
|
2015-04-13 21:12:03 +00:00
|
|
|
}
|
|
|
|
|
2017-02-11 00:56:28 +00:00
|
|
|
if len(f.Prefix) > 0 {
|
|
|
|
_, err := w.Write([]byte(f.Prefix))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-13 21:12:03 +00:00
|
|
|
enc := json.NewEncoder(w)
|
2016-09-21 14:29:42 +00:00
|
|
|
return enc.Encode(resp)
|
2015-06-29 22:27:28 +00:00
|
|
|
}
|