Debian Codegraph @

res/res_ari_mailboxes.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2013, Digium, Inc.
5 *
6 * Jonathan Rose <[email protected]>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 */
18
19 /*
20 * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
21 * !!!!! DO NOT EDIT !!!!!
22 * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
23 * This file is generated by a mustache template. Please see the original
24 * template in rest-api-templates/res_ari_resource.c.mustache
25 */
26
27 /*! \file
28 *
29 * \brief Mailboxes resources
30 *
31 * \author Jonathan Rose <[email protected]>
32 */
33
34 /*** MODULEINFO
35 <depend type="module">res_ari</depend>
36 <depend type="module">res_ari_model</depend>
37 <depend type="module">res_stasis</depend>
38 <depend type="module">res_stasis_mailbox</depend>
39 <support_level>core</support_level>
40 ***/
41
42 #include "asterisk.h"
43
44 #include "asterisk/app.h"
45 #include "asterisk/module.h"
46 #include "asterisk/stasis_app.h"
47 #include "ari/resource_mailboxes.h"
48 #if defined(AST_DEVMODE)
49 #include "ari/ari_model_validators.h"
50 #endif
51
52 #define MAX_VALS 128
53
54 /*!
55 * \brief Parameter parsing callback for /mailboxes.
56 * \param ser TCP/TLS session object
57 * \param get_params GET parameters in the HTTP request.
58 * \param path_vars Path variables extracted from the request.
59 * \param headers HTTP headers.
60 * \param body
61 * \param[out] response Response to the HTTP request.
62 */
63 static void ast_ari_mailboxes_list_cb(
64 struct ast_tcptls_session_instance *ser,
65 struct ast_variable *get_params, struct ast_variable *path_vars,
66 struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
67 {
68 struct ast_ari_mailboxes_list_args args = {};
69 #if defined(AST_DEVMODE)
70 int is_valid;
71 int code;
72 #endif /* AST_DEVMODE */
73
74 ast_ari_mailboxes_list(headers, &args, response);
75 #if defined(AST_DEVMODE)
76 code = response->response_code;
77
78 switch (code) {
79 case 0: /* Implementation is still a stub, or the code wasn't set */
80 is_valid = response->message == NULL;
81 break;
82 case 500: /* Internal Server Error */
83 case 501: /* Not Implemented */
84 is_valid = 1;
85 break;
86 default:
87 if (200 <= code && code <= 299) {
88 is_valid = ast_ari_validate_list(response->message,
89 ast_ari_validate_mailbox_fn());
90 } else {
91 ast_log(LOG_ERROR, "Invalid error response %d for /mailboxes\n", code);
92 is_valid = 0;
93 }
94 }
95
96 if (!is_valid) {
97 ast_log(LOG_ERROR, "Response validation failed for /mailboxes\n");
98 ast_ari_response_error(response, 500,
99 "Internal Server Error", "Response validation failed");
100 }
101 #endif /* AST_DEVMODE */
102
103 fin: __attribute__((unused))
104 return;
105 }
106 /*!
107 * \brief Parameter parsing callback for /mailboxes/{mailboxName}.
108 * \param ser TCP/TLS session object
109 * \param get_params GET parameters in the HTTP request.
110 * \param path_vars Path variables extracted from the request.
111 * \param headers HTTP headers.
112 * \param body
113 * \param[out] response Response to the HTTP request.
114 */
115 static void ast_ari_mailboxes_get_cb(
116 struct ast_tcptls_session_instance *ser,
117 struct ast_variable *get_params, struct ast_variable *path_vars,
118 struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
119 {
120 struct ast_ari_mailboxes_get_args args = {};
121 struct ast_variable *i;
122 #if defined(AST_DEVMODE)
123 int is_valid;
124 int code;
125 #endif /* AST_DEVMODE */
126
127 for (i = path_vars; i; i = i->next) {
128 if (strcmp(i->name, "mailboxName") == 0) {
129 args.mailbox_name = (i->value);
130 } else
131 {}
132 }
133 ast_ari_mailboxes_get(headers, &args, response);
134 #if defined(AST_DEVMODE)
135 code = response->response_code;
136
137 switch (code) {
138 case 0: /* Implementation is still a stub, or the code wasn't set */
139 is_valid = response->message == NULL;
140 break;
141 case 500: /* Internal Server Error */
142 case 501: /* Not Implemented */
143 case 404: /* Mailbox not found */
144 is_valid = 1;
145 break;
146 default:
147 if (200 <= code && code <= 299) {
148 is_valid = ast_ari_validate_mailbox(
149 response->message);
150 } else {
151 ast_log(LOG_ERROR, "Invalid error response %d for /mailboxes/{mailboxName}\n", code);
152 is_valid = 0;
153 }
154 }
155
156 if (!is_valid) {
157 ast_log(LOG_ERROR, "Response validation failed for /mailboxes/{mailboxName}\n");
158 ast_ari_response_error(response, 500,
159 "Internal Server Error", "Response validation failed");
160 }
161 #endif /* AST_DEVMODE */
162
163 fin: __attribute__((unused))
164 return;
165 }
166 int ast_ari_mailboxes_update_parse_body(
167 struct ast_json *body,
168 struct ast_ari_mailboxes_update_args *args)
169 {
170 struct ast_json *field;
171 /* Parse query parameters out of it */
172 field = ast_json_object_get(body, "oldMessages");
173 if (field) {
174 args->old_messages = ast_json_integer_get(field);
175 }
176 field = ast_json_object_get(body, "newMessages");
177 if (field) {
178 args->new_messages = ast_json_integer_get(field);
179 }
180 return 0;
181 }
182
183 /*!
184 * \brief Parameter parsing callback for /mailboxes/{mailboxName}.
185 * \param ser TCP/TLS session object
186 * \param get_params GET parameters in the HTTP request.
187 * \param path_vars Path variables extracted from the request.
188 * \param headers HTTP headers.
189 * \param body
190 * \param[out] response Response to the HTTP request.
191 */
192 static void ast_ari_mailboxes_update_cb(
193 struct ast_tcptls_session_instance *ser,
194 struct ast_variable *get_params, struct ast_variable *path_vars,
195 struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
196 {
197 struct ast_ari_mailboxes_update_args args = {};
198 struct ast_variable *i;
199 #if defined(AST_DEVMODE)
200 int is_valid;
201 int code;
202 #endif /* AST_DEVMODE */
203
204 for (i = get_params; i; i = i->next) {
205 if (strcmp(i->name, "oldMessages") == 0) {
206 args.old_messages = atoi(i->value);
207 } else
208 if (strcmp(i->name, "newMessages") == 0) {
209 args.new_messages = atoi(i->value);
210 } else
211 {}
212 }
213 for (i = path_vars; i; i = i->next) {
214 if (strcmp(i->name, "mailboxName") == 0) {
215 args.mailbox_name = (i->value);
216 } else
217 {}
218 }
219 if (ast_ari_mailboxes_update_parse_body(body, &args)) {
220 ast_ari_response_alloc_failed(response);
221 goto fin;
222 }
223 ast_ari_mailboxes_update(headers, &args, response);
224 #if defined(AST_DEVMODE)
225 code = response->response_code;
226
227 switch (code) {
228 case 0: /* Implementation is still a stub, or the code wasn't set */
229 is_valid = response->message == NULL;
230 break;
231 case 500: /* Internal Server Error */
232 case 501: /* Not Implemented */
233 case 404: /* Mailbox not found */
234 is_valid = 1;
235 break;
236 default:
237 if (200 <= code && code <= 299) {
238 is_valid = ast_ari_validate_void(
239 response->message);
240 } else {
241 ast_log(LOG_ERROR, "Invalid error response %d for /mailboxes/{mailboxName}\n", code);
242 is_valid = 0;
243 }
244 }
245
246 if (!is_valid) {
247 ast_log(LOG_ERROR, "Response validation failed for /mailboxes/{mailboxName}\n");
248 ast_ari_response_error(response, 500,
249 "Internal Server Error", "Response validation failed");
250 }
251 #endif /* AST_DEVMODE */
252
253 fin: __attribute__((unused))
254 return;
255 }
256 /*!
257 * \brief Parameter parsing callback for /mailboxes/{mailboxName}.
258 * \param ser TCP/TLS session object
259 * \param get_params GET parameters in the HTTP request.
260 * \param path_vars Path variables extracted from the request.
261 * \param headers HTTP headers.
262 * \param body
263 * \param[out] response Response to the HTTP request.
264 */
265 static void ast_ari_mailboxes_delete_cb(
266 struct ast_tcptls_session_instance *ser,
267 struct ast_variable *get_params, struct ast_variable *path_vars,
268 struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
269 {
270 struct ast_ari_mailboxes_delete_args args = {};
271 struct ast_variable *i;
272 #if defined(AST_DEVMODE)
273 int is_valid;
274 int code;
275 #endif /* AST_DEVMODE */
276
277 for (i = path_vars; i; i = i->next) {
278 if (strcmp(i->name, "mailboxName") == 0) {
279 args.mailbox_name = (i->value);
280 } else
281 {}
282 }
283 ast_ari_mailboxes_delete(headers, &args, response);
284 #if defined(AST_DEVMODE)
285 code = response->response_code;
286
287 switch (code) {
288 case 0: /* Implementation is still a stub, or the code wasn't set */
289 is_valid = response->message == NULL;
290 break;
291 case 500: /* Internal Server Error */
292 case 501: /* Not Implemented */
293 case 404: /* Mailbox not found */
294 is_valid = 1;
295 break;
296 default:
297 if (200 <= code && code <= 299) {
298 is_valid = ast_ari_validate_void(
299 response->message);
300 } else {
301 ast_log(LOG_ERROR, "Invalid error response %d for /mailboxes/{mailboxName}\n", code);
302 is_valid = 0;
303 }
304 }
305
306 if (!is_valid) {
307 ast_log(LOG_ERROR, "Response validation failed for /mailboxes/{mailboxName}\n");
308 ast_ari_response_error(response, 500,
309 "Internal Server Error", "Response validation failed");
310 }
311 #endif /* AST_DEVMODE */
312
313 fin: __attribute__((unused))
314 return;
315 }
316
317 /*! \brief REST handler for /api-docs/mailboxes.json */
318 static struct stasis_rest_handlers mailboxes_mailboxName = {
319 .path_segment = "mailboxName",
320 .is_wildcard = 1,
321 .callbacks = {
322 [AST_HTTP_GET] = ast_ari_mailboxes_get_cb,
323 [AST_HTTP_PUT] = ast_ari_mailboxes_update_cb,
324 [AST_HTTP_DELETE] = ast_ari_mailboxes_delete_cb,
325 },
326 .num_children = 0,
327 .children = { }
328 };
329 /*! \brief REST handler for /api-docs/mailboxes.json */
330 static struct stasis_rest_handlers mailboxes = {
331 .path_segment = "mailboxes",
332 .callbacks = {
333 [AST_HTTP_GET] = ast_ari_mailboxes_list_cb,
334 },
335 .num_children = 1,
336 .children = { &mailboxes_mailboxName, }
337 };
338
339 static int unload_module(void)
340 {
341 ast_ari_remove_handler(&mailboxes);
342 return 0;
343 }
344
345 static int load_module(void)
346 {
347 int res = 0;
348
349
350 res |= ast_ari_add_handler(&mailboxes);
351 if (res) {
352 unload_module();
353 return AST_MODULE_LOAD_DECLINE;
354 }
355
356 return AST_MODULE_LOAD_SUCCESS;
357 }
358
359 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "RESTful API module - Mailboxes resources",
360 .support_level = AST_MODULE_SUPPORT_CORE,
361 .load = load_module,
362 .unload = unload_module,
363 .requires = "res_ari,res_ari_model,res_stasis,res_stasis_mailbox",
364 );