func (p *JSONMediaTypeHandler) OutputTo(req wm.Request, cxt wm.Context, writer io.Writer, resp wm.ResponseWriter) {
	buf := bytes.NewBufferString("")
	obj := jsonhelper.NewJSONObject()
	enc := json.NewEncoder(buf)
	obj.Set("status", "success")
	obj.Set("result", p.obj)
	err := enc.Encode(obj)
	if err != nil {
		//resp.Header().Set("Content-Type", wm.MIME_TYPE_JSON)
		if !p.writtenStatusHeader {
			resp.WriteHeader(500)
			p.writtenStatusHeader = true
		}
		m := jsonhelper.NewJSONObject()
		w := json.NewEncoder(writer)
		m.Set("status", "error")
		m.Set("message", err.String())
		m.Set("result", nil)
		w.Encode(m)
		return
	}
	//resp.Header().Set("Content-Type", wm.MIME_TYPE_JSON)
	resp.Header().Set("Content-Length", strconv.Itoa(buf.Len()))
	if p.lastModified != nil {
		resp.Header().Set("Last-Modified", p.lastModified.Format(http.TimeFormat))
	}
	if len(p.etag) > 0 {
		resp.Header().Set("ETag", strconv.Quote(p.etag))
	}
	handler := wm.NewPassThroughMediaTypeHandler(wm.MIME_TYPE_JSON, ioutil.NopCloser(bytes.NewBuffer(buf.Bytes())), int64(buf.Len()), p.lastModified)
	handler.OutputTo(req, cxt, writer, resp)
}
Exemplo n.º 2
0
func (p *JSONMediaTypeHandler) MediaTypeHandleOutputTo(req wm.Request, cxt wm.Context, writer io.Writer, resp wm.ResponseWriter) {
	var responseHeaders http.Header
	var responseStatusCode int
	if p.obj == nil && p.responseGenerator != nil {
		p.obj, p.lastModified, p.etag, responseStatusCode, responseHeaders = p.responseGenerator()
	}
	buf := bytes.NewBufferString("")
	obj := jsonhelper.NewJSONObject()
	enc := json.NewEncoder(buf)
	obj.Set("status", "success")
	obj.Set("result", p.obj)
	err := enc.Encode(obj)
	if err != nil {
		//resp.Header().Set("Content-Type", wm.MIME_TYPE_JSON)
		if !p.writtenStatusHeader {
			resp.WriteHeader(500)
			p.writtenStatusHeader = true
		}
		m := jsonhelper.NewJSONObject()
		w := json.NewEncoder(writer)
		m.Set("status", "error")
		m.Set("message", err.Error())
		m.Set("result", nil)
		w.Encode(m)
		return
	}
	if responseHeaders != nil {
		for k, arr := range responseHeaders {
			if _, ok := resp.Header()[k]; ok {
				if len(arr) > 0 {
					resp.Header().Set(k, arr[len(arr)-1])
				}
			} else {
				for _, v := range arr {
					resp.Header().Add(k, v)
				}
			}
		}
	}
	//resp.Header().Set("Content-Type", wm.MIME_TYPE_JSON)
	resp.Header().Set("Content-Length", strconv.Itoa(buf.Len()))
	if !p.lastModified.IsZero() {
		resp.Header().Set("Last-Modified", p.lastModified.Format(http.TimeFormat))
	}
	if len(p.etag) > 0 {
		resp.Header().Set("ETag", strconv.Quote(p.etag))
	}
	handler := wm.NewPassThroughMediaTypeHandler(wm.MIME_TYPE_JSON, ioutil.NopCloser(bytes.NewBuffer(buf.Bytes())), int64(buf.Len()), p.lastModified)
	handler.SetStatusCode(responseStatusCode)
	handler.MediaTypeHandleOutputTo(req, cxt, writer, resp)
}