Add swagger UI
[vfc/nfvo/wfengine.git] / wfenginemgrservice / src / main / resources / api-doc / lib / shred / content.js
1 /*\r
2  * Copyright 2016 ZTE Corporation.\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 // The purpose of the `Content` object is to abstract away the data conversions\r
17 // to and from raw content entities as strings. For example, you want to be able\r
18 // to pass in a Javascript object and have it be automatically converted into a\r
19 // JSON string if the `content-type` is set to a JSON-based media type.\r
20 // Conversely, you want to be able to transparently get back a Javascript object\r
21 // in the response if the `content-type` is a JSON-based media-type.\r
22 \r
23 // One limitation of the current implementation is that it [assumes the `charset` is UTF-8](https://github.com/spire-io/shred/issues/5).\r
24 \r
25 // The `Content` constructor takes an options object, which *must* have either a\r
26 // `body` or `data` property and *may* have a `type` property indicating the\r
27 // media type. If there is no `type` attribute, a default will be inferred.\r
28 var Content = function(options) {\r
29   this.body = options.body;\r
30   this.data = options.data;\r
31   this.type = options.type;\r
32 };\r
33 \r
34 Content.prototype = {\r
35   // Treat `toString()` as asking for the `content.body`. That is, the raw content entity.\r
36   //\r
37   //     toString: function() { return this.body; }\r
38   //\r
39   // Commented out, but I've forgotten why. :/\r
40 };\r
41 \r
42 \r
43 // `Content` objects have the following attributes:\r
44 Object.defineProperties(Content.prototype,{\r
45   \r
46 // - **type**. Typically accessed as `content.type`, reflects the `content-type`\r
47 //   header associated with the request or response. If not passed as an options\r
48 //   to the constructor or set explicitly, it will infer the type the `data`\r
49 //   attribute, if possible, and, failing that, will default to `text/plain`.\r
50   type: {\r
51     get: function() {\r
52       if (this._type) {\r
53         return this._type;\r
54       } else {\r
55         if (this._data) {\r
56           switch(typeof this._data) {\r
57             case "string": return "text/plain";\r
58             case "object": return "application/json";\r
59           }\r
60         }\r
61       }\r
62       return "text/plain";\r
63     },\r
64     set: function(value) {\r
65       this._type = value;\r
66       return this;\r
67     },\r
68     enumerable: true\r
69   },\r
70 \r
71 // - **data**. Typically accessed as `content.data`, reflects the content entity\r
72 //   converted into Javascript data. This can be a string, if the `type` is, say,\r
73 //   `text/plain`, but can also be a Javascript object. The conversion applied is\r
74 //   based on the `processor` attribute. The `data` attribute can also be set\r
75 //   directly, in which case the conversion will be done the other way, to infer\r
76 //   the `body` attribute.\r
77   data: {\r
78     get: function() {\r
79       if (this._body) {\r
80         return this.processor.parser(this._body);\r
81       } else {\r
82         return this._data;\r
83       }\r
84     },\r
85     set: function(data) {\r
86       if (this._body&&data) Errors.setDataWithBody(this);\r
87       this._data = data;\r
88       return this;\r
89     },\r
90     enumerable: true\r
91   },\r
92 \r
93 // - **body**. Typically accessed as `content.body`, reflects the content entity\r
94 //   as a UTF-8 string. It is the mirror of the `data` attribute. If you set the\r
95 //   `data` attribute, the `body` attribute will be inferred and vice-versa. If\r
96 //   you attempt to set both, an exception is raised.\r
97   body: {\r
98     get: function() {\r
99       if (this._data) {\r
100         return this.processor.stringify(this._data);\r
101       } else {\r
102         return this._body.toString();\r
103       }\r
104     },\r
105     set: function(body) {\r
106       if (this._data&&body) Errors.setBodyWithData(this);\r
107       this._body = body;\r
108       return this;\r
109     },\r
110     enumerable: true\r
111   },\r
112 \r
113 // - **processor**. The functions that will be used to convert to/from `data` and\r
114 //   `body` attributes. You can add processors. The two that are built-in are for\r
115 //   `text/plain`, which is basically an identity transformation and\r
116 //   `application/json` and other JSON-based media types (including custom media\r
117 //   types with `+json`). You can add your own processors. See below.\r
118   processor: {\r
119     get: function() {\r
120       var processor = Content.processors[this.type];\r
121       if (processor) {\r
122         return processor;\r
123       } else {\r
124         // Return the first processor that matches any part of the\r
125         // content type. ex: application/vnd.foobar.baz+json will match json.\r
126         var main = this.type.split(";")[0];\r
127         var parts = main.split(/\+|\//);\r
128         for (var i=0, l=parts.length; i < l; i++) {\r
129           processor = Content.processors[parts[i]]\r
130         }\r
131         return processor || {parser:identity,stringify:toString};\r
132       }\r
133     },\r
134     enumerable: true\r
135   },\r
136 \r
137 // - **length**. Typically accessed as `content.length`, returns the length in\r
138 //   bytes of the raw content entity.\r
139   length: {\r
140     get: function() {\r
141       if (typeof Buffer !== 'undefined') {\r
142         return Buffer.byteLength(this.body);\r
143       }\r
144       return this.body.length;\r
145     }\r
146   }\r
147 });\r
148 \r
149 Content.processors = {};\r
150 \r
151 // The `registerProcessor` function allows you to add your own processors to\r
152 // convert content entities. Each processor consists of a Javascript object with\r
153 // two properties:\r
154 // - **parser**. The function used to parse a raw content entity and convert it\r
155 //   into a Javascript data type.\r
156 // - **stringify**. The function used to convert a Javascript data type into a\r
157 //   raw content entity.\r
158 Content.registerProcessor = function(types,processor) {\r
159   \r
160 // You can pass an array of types that will trigger this processor, or just one.\r
161 // We determine the array via duck-typing here.\r
162   if (types.forEach) {\r
163     types.forEach(function(type) {\r
164       Content.processors[type] = processor;\r
165     });\r
166   } else {\r
167     // If you didn't pass an array, we just use what you pass in.\r
168     Content.processors[types] = processor;\r
169   }\r
170 };\r
171 \r
172 // Register the identity processor, which is used for text-based media types.\r
173 var identity = function(x) { return x; }\r
174   , toString = function(x) { return x.toString(); }\r
175 Content.registerProcessor(\r
176   ["text/html","text/plain","text"],\r
177   { parser: identity, stringify: toString });\r
178 \r
179 // Register the JSON processor, which is used for JSON-based media types.\r
180 Content.registerProcessor(\r
181   ["application/json; charset=utf-8","application/json","json"],\r
182   {\r
183     parser: function(string) {\r
184       return JSON.parse(string);\r
185     },\r
186     stringify: function(data) {\r
187       return JSON.stringify(data); }});\r
188 \r
189 var qs = require('querystring');\r
190 // Register the post processor, which is used for JSON-based media types.\r
191 Content.registerProcessor(\r
192   ["application/x-www-form-urlencoded"],\r
193   { parser : qs.parse, stringify : qs.stringify });\r
194 \r
195 // Error functions are defined separately here in an attempt to make the code\r
196 // easier to read.\r
197 var Errors = {\r
198   setDataWithBody: function(object) {\r
199     throw new Error("Attempt to set data attribute of a content object " +\r
200         "when the body attributes was already set.");\r
201   },\r
202   setBodyWithData: function(object) {\r
203     throw new Error("Attempt to set body attribute of a content object " +\r
204         "when the data attributes was already set.");\r
205   }\r
206 }\r
207 module.exports = Content;