...

Source file src/github.com/emicklei/go-restful/v3/entity_accessors.go

Documentation: github.com/emicklei/go-restful/v3

     1  package restful
     2  
     3  // Copyright 2015 Ernest Micklei. All rights reserved.
     4  // Use of this source code is governed by a license
     5  // that can be found in the LICENSE file.
     6  
     7  import (
     8  	"encoding/json"
     9  	"encoding/xml"
    10  	"strings"
    11  	"sync"
    12  )
    13  
    14  var (
    15  	MarshalIndent = json.MarshalIndent
    16  	NewDecoder    = json.NewDecoder
    17  	NewEncoder    = json.NewEncoder
    18  )
    19  
    20  // EntityReaderWriter can read and write values using an encoding such as JSON,XML.
    21  type EntityReaderWriter interface {
    22  	// Read a serialized version of the value from the request.
    23  	// The Request may have a decompressing reader. Depends on Content-Encoding.
    24  	Read(req *Request, v interface{}) error
    25  
    26  	// Write a serialized version of the value on the response.
    27  	// The Response may have a compressing writer. Depends on Accept-Encoding.
    28  	// status should be a valid Http Status code
    29  	Write(resp *Response, status int, v interface{}) error
    30  }
    31  
    32  // entityAccessRegistry is a singleton
    33  var entityAccessRegistry = &entityReaderWriters{
    34  	protection: new(sync.RWMutex),
    35  	accessors:  map[string]EntityReaderWriter{},
    36  }
    37  
    38  // entityReaderWriters associates MIME to an EntityReaderWriter
    39  type entityReaderWriters struct {
    40  	protection *sync.RWMutex
    41  	accessors  map[string]EntityReaderWriter
    42  }
    43  
    44  func init() {
    45  	RegisterEntityAccessor(MIME_JSON, NewEntityAccessorJSON(MIME_JSON))
    46  	RegisterEntityAccessor(MIME_XML, NewEntityAccessorXML(MIME_XML))
    47  }
    48  
    49  // RegisterEntityAccessor add/overrides the ReaderWriter for encoding content with this MIME type.
    50  func RegisterEntityAccessor(mime string, erw EntityReaderWriter) {
    51  	entityAccessRegistry.protection.Lock()
    52  	defer entityAccessRegistry.protection.Unlock()
    53  	entityAccessRegistry.accessors[mime] = erw
    54  }
    55  
    56  // NewEntityAccessorJSON returns a new EntityReaderWriter for accessing JSON content.
    57  // This package is already initialized with such an accessor using the MIME_JSON contentType.
    58  func NewEntityAccessorJSON(contentType string) EntityReaderWriter {
    59  	return entityJSONAccess{ContentType: contentType}
    60  }
    61  
    62  // NewEntityAccessorXML returns a new EntityReaderWriter for accessing XML content.
    63  // This package is already initialized with such an accessor using the MIME_XML contentType.
    64  func NewEntityAccessorXML(contentType string) EntityReaderWriter {
    65  	return entityXMLAccess{ContentType: contentType}
    66  }
    67  
    68  // accessorAt returns the registered ReaderWriter for this MIME type.
    69  func (r *entityReaderWriters) accessorAt(mime string) (EntityReaderWriter, bool) {
    70  	r.protection.RLock()
    71  	defer r.protection.RUnlock()
    72  	er, ok := r.accessors[mime]
    73  	if !ok {
    74  		// retry with reverse lookup
    75  		// more expensive but we are in an exceptional situation anyway
    76  		for k, v := range r.accessors {
    77  			if strings.Contains(mime, k) {
    78  				return v, true
    79  			}
    80  		}
    81  	}
    82  	return er, ok
    83  }
    84  
    85  // entityXMLAccess is a EntityReaderWriter for XML encoding
    86  type entityXMLAccess struct {
    87  	// This is used for setting the Content-Type header when writing
    88  	ContentType string
    89  }
    90  
    91  // Read unmarshalls the value from XML
    92  func (e entityXMLAccess) Read(req *Request, v interface{}) error {
    93  	return xml.NewDecoder(req.Request.Body).Decode(v)
    94  }
    95  
    96  // Write marshalls the value to JSON and set the Content-Type Header.
    97  func (e entityXMLAccess) Write(resp *Response, status int, v interface{}) error {
    98  	return writeXML(resp, status, e.ContentType, v)
    99  }
   100  
   101  // writeXML marshalls the value to JSON and set the Content-Type Header.
   102  func writeXML(resp *Response, status int, contentType string, v interface{}) error {
   103  	if v == nil {
   104  		resp.WriteHeader(status)
   105  		// do not write a nil representation
   106  		return nil
   107  	}
   108  	if resp.prettyPrint {
   109  		// pretty output must be created and written explicitly
   110  		output, err := xml.MarshalIndent(v, " ", " ")
   111  		if err != nil {
   112  			return err
   113  		}
   114  		resp.Header().Set(HEADER_ContentType, contentType)
   115  		resp.WriteHeader(status)
   116  		_, err = resp.Write([]byte(xml.Header))
   117  		if err != nil {
   118  			return err
   119  		}
   120  		_, err = resp.Write(output)
   121  		return err
   122  	}
   123  	// not-so-pretty
   124  	resp.Header().Set(HEADER_ContentType, contentType)
   125  	resp.WriteHeader(status)
   126  	return xml.NewEncoder(resp).Encode(v)
   127  }
   128  
   129  // entityJSONAccess is a EntityReaderWriter for JSON encoding
   130  type entityJSONAccess struct {
   131  	// This is used for setting the Content-Type header when writing
   132  	ContentType string
   133  }
   134  
   135  // Read unmarshalls the value from JSON
   136  func (e entityJSONAccess) Read(req *Request, v interface{}) error {
   137  	decoder := NewDecoder(req.Request.Body)
   138  	decoder.UseNumber()
   139  	return decoder.Decode(v)
   140  }
   141  
   142  // Write marshalls the value to JSON and set the Content-Type Header.
   143  func (e entityJSONAccess) Write(resp *Response, status int, v interface{}) error {
   144  	return writeJSON(resp, status, e.ContentType, v)
   145  }
   146  
   147  // write marshalls the value to JSON and set the Content-Type Header.
   148  func writeJSON(resp *Response, status int, contentType string, v interface{}) error {
   149  	if v == nil {
   150  		resp.WriteHeader(status)
   151  		// do not write a nil representation
   152  		return nil
   153  	}
   154  	if resp.prettyPrint {
   155  		// pretty output must be created and written explicitly
   156  		output, err := MarshalIndent(v, "", " ")
   157  		if err != nil {
   158  			return err
   159  		}
   160  		resp.Header().Set(HEADER_ContentType, contentType)
   161  		resp.WriteHeader(status)
   162  		_, err = resp.Write(output)
   163  		return err
   164  	}
   165  	// not-so-pretty
   166  	resp.Header().Set(HEADER_ContentType, contentType)
   167  	resp.WriteHeader(status)
   168  	return NewEncoder(resp).Encode(v)
   169  }
   170  

View as plain text