Debian Codegraph @

res/res_ari_endpoints.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2012 - 2013, Digium, Inc.
5 *
6 * David M. Lee, II <[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 Endpoint resources
30 *
31 * \author David M. Lee, II <[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 <support_level>core</support_level>
39 ***/
40
41 #include "asterisk.h"
42
43 #include "asterisk/app.h"
44 #include "asterisk/module.h"
45 #include "asterisk/stasis_app.h"
46 #include "ari/resource_endpoints.h"
47 #if defined(AST_DEVMODE)
48 #include "ari/ari_model_validators.h"
49 #endif
50
51 #define MAX_VALS 128
52
53 /*!
54 * \brief Parameter parsing callback for /endpoints.
55 * \param ser TCP/TLS session object
56 * \param get_params GET parameters in the HTTP request.
57 * \param path_vars Path variables extracted from the request.
58 * \param headers HTTP headers.
59 * \param body
60 * \param[out] response Response to the HTTP request.
61 */
62 static void ast_ari_endpoints_list_cb(
63 struct ast_tcptls_session_instance *ser,
64 struct ast_variable *get_params, struct ast_variable *path_vars,
65 struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
66 {
67 struct ast_ari_endpoints_list_args args = {};
68 #if defined(AST_DEVMODE)
69 int is_valid;
70 int code;
71 #endif /* AST_DEVMODE */
72
73 ast_ari_endpoints_list(headers, &args, response);
74 #if defined(AST_DEVMODE)
75 code = response->response_code;
76
77 switch (code) {
78 case 0: /* Implementation is still a stub, or the code wasn't set */
79 is_valid = response->message == NULL;
80 break;
81 case 500: /* Internal Server Error */
82 case 501: /* Not Implemented */
83 is_valid = 1;
84 break;
85 default:
86 if (200 <= code && code <= 299) {
87 is_valid = ast_ari_validate_list(response->message,
88 ast_ari_validate_endpoint_fn());
89 } else {
90 ast_log(LOG_ERROR, "Invalid error response %d for /endpoints\n", code);
91 is_valid = 0;
92 }
93 }
94
95 if (!is_valid) {
96 ast_log(LOG_ERROR, "Response validation failed for /endpoints\n");
97 ast_ari_response_error(response, 500,
98 "Internal Server Error", "Response validation failed");
99 }
100 #endif /* AST_DEVMODE */
101
102 fin: __attribute__((unused))
103 return;
104 }
105 int ast_ari_endpoints_send_message_parse_body(
106 struct ast_json *body,
107 struct ast_ari_endpoints_send_message_args *args)
108 {
109 struct ast_json *field;
110 /* Parse query parameters out of it */
111 field = ast_json_object_get(body, "to");
112 if (field) {
113 args->to = ast_json_string_get(field);
114 }
115 field = ast_json_object_get(body, "from");
116 if (field) {
117 args->from = ast_json_string_get(field);
118 }
119 field = ast_json_object_get(body, "body");
120 if (field) {
121 args->body = ast_json_string_get(field);
122 }
123 return 0;
124 }
125
126 /*!
127 * \brief Parameter parsing callback for /endpoints/sendMessage.
128 * \param ser TCP/TLS session object
129 * \param get_params GET parameters in the HTTP request.
130 * \param path_vars Path variables extracted from the request.
131 * \param headers HTTP headers.
132 * \param body
133 * \param[out] response Response to the HTTP request.
134 */
135 static void ast_ari_endpoints_send_message_cb(
136 struct ast_tcptls_session_instance *ser,
137 struct ast_variable *get_params, struct ast_variable *path_vars,
138 struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
139 {
140 struct ast_ari_endpoints_send_message_args args = {};
141 struct ast_variable *i;
142 #if defined(AST_DEVMODE)
143 int is_valid;
144 int code;
145 #endif /* AST_DEVMODE */
146
147 for (i = get_params; i; i = i->next) {
148 if (strcmp(i->name, "to") == 0) {
149 args.to = (i->value);
150 } else
151 if (strcmp(i->name, "from") == 0) {
152 args.from = (i->value);
153 } else
154 if (strcmp(i->name, "body") == 0) {
155 args.body = (i->value);
156 } else
157 {}
158 }
159 args.variables = body;
160 ast_ari_endpoints_send_message(headers, &args, response);
161 #if defined(AST_DEVMODE)
162 code = response->response_code;
163
164 switch (code) {
165 case 0: /* Implementation is still a stub, or the code wasn't set */
166 is_valid = response->message == NULL;
167 break;
168 case 500: /* Internal Server Error */
169 case 501: /* Not Implemented */
170 case 400: /* Invalid parameters for sending a message. */
171 case 404: /* Endpoint not found */
172 is_valid = 1;
173 break;
174 default:
175 if (200 <= code && code <= 299) {
176 is_valid = ast_ari_validate_void(
177 response->message);
178 } else {
179 ast_log(LOG_ERROR, "Invalid error response %d for /endpoints/sendMessage\n", code);
180 is_valid = 0;
181 }
182 }
183
184 if (!is_valid) {
185 ast_log(LOG_ERROR, "Response validation failed for /endpoints/sendMessage\n");
186 ast_ari_response_error(response, 500,
187 "Internal Server Error", "Response validation failed");
188 }
189 #endif /* AST_DEVMODE */
190
191 fin: __attribute__((unused))
192 return;
193 }
194 int ast_ari_endpoints_refer_parse_body(
195 struct ast_json *body,
196 struct ast_ari_endpoints_refer_args *args)
197 {
198 struct ast_json *field;
199 /* Parse query parameters out of it */
200 field = ast_json_object_get(body, "to");
201 if (field) {
202 args->to = ast_json_string_get(field);
203 }
204 field = ast_json_object_get(body, "from");
205 if (field) {
206 args->from = ast_json_string_get(field);
207 }
208 field = ast_json_object_get(body, "refer_to");
209 if (field) {
210 args->refer_to = ast_json_string_get(field);
211 }
212 field = ast_json_object_get(body, "to_self");
213 if (field) {
214 args->to_self = ast_json_is_true(field);
215 }
216 return 0;
217 }
218
219 /*!
220 * \brief Parameter parsing callback for /endpoints/refer.
221 * \param ser TCP/TLS session object
222 * \param get_params GET parameters in the HTTP request.
223 * \param path_vars Path variables extracted from the request.
224 * \param headers HTTP headers.
225 * \param body
226 * \param[out] response Response to the HTTP request.
227 */
228 static void ast_ari_endpoints_refer_cb(
229 struct ast_tcptls_session_instance *ser,
230 struct ast_variable *get_params, struct ast_variable *path_vars,
231 struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
232 {
233 struct ast_ari_endpoints_refer_args args = {};
234 struct ast_variable *i;
235 #if defined(AST_DEVMODE)
236 int is_valid;
237 int code;
238 #endif /* AST_DEVMODE */
239
240 for (i = get_params; i; i = i->next) {
241 if (strcmp(i->name, "to") == 0) {
242 args.to = (i->value);
243 } else
244 if (strcmp(i->name, "from") == 0) {
245 args.from = (i->value);
246 } else
247 if (strcmp(i->name, "refer_to") == 0) {
248 args.refer_to = (i->value);
249 } else
250 if (strcmp(i->name, "to_self") == 0) {
251 args.to_self = ast_true(i->value);
252 } else
253 {}
254 }
255 args.variables = body;
256 ast_ari_endpoints_refer(headers, &args, response);
257 #if defined(AST_DEVMODE)
258 code = response->response_code;
259
260 switch (code) {
261 case 0: /* Implementation is still a stub, or the code wasn't set */
262 is_valid = response->message == NULL;
263 break;
264 case 500: /* Internal Server Error */
265 case 501: /* Not Implemented */
266 case 400: /* Invalid parameters for referring. */
267 case 404: /* Endpoint not found */
268 is_valid = 1;
269 break;
270 default:
271 if (200 <= code && code <= 299) {
272 is_valid = ast_ari_validate_void(
273 response->message);
274 } else {
275 ast_log(LOG_ERROR, "Invalid error response %d for /endpoints/refer\n", code);
276 is_valid = 0;
277 }
278 }
279
280 if (!is_valid) {
281 ast_log(LOG_ERROR, "Response validation failed for /endpoints/refer\n");
282 ast_ari_response_error(response, 500,
283 "Internal Server Error", "Response validation failed");
284 }
285 #endif /* AST_DEVMODE */
286
287 fin: __attribute__((unused))
288 return;
289 }
290 /*!
291 * \brief Parameter parsing callback for /endpoints/{tech}.
292 * \param ser TCP/TLS session object
293 * \param get_params GET parameters in the HTTP request.
294 * \param path_vars Path variables extracted from the request.
295 * \param headers HTTP headers.
296 * \param body
297 * \param[out] response Response to the HTTP request.
298 */
299 static void ast_ari_endpoints_list_by_tech_cb(
300 struct ast_tcptls_session_instance *ser,
301 struct ast_variable *get_params, struct ast_variable *path_vars,
302 struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
303 {
304 struct ast_ari_endpoints_list_by_tech_args args = {};
305 struct ast_variable *i;
306 #if defined(AST_DEVMODE)
307 int is_valid;
308 int code;
309 #endif /* AST_DEVMODE */
310
311 for (i = path_vars; i; i = i->next) {
312 if (strcmp(i->name, "tech") == 0) {
313 args.tech = (i->value);
314 } else
315 {}
316 }
317 ast_ari_endpoints_list_by_tech(headers, &args, response);
318 #if defined(AST_DEVMODE)
319 code = response->response_code;
320
321 switch (code) {
322 case 0: /* Implementation is still a stub, or the code wasn't set */
323 is_valid = response->message == NULL;
324 break;
325 case 500: /* Internal Server Error */
326 case 501: /* Not Implemented */
327 case 404: /* Endpoints not found */
328 is_valid = 1;
329 break;
330 default:
331 if (200 <= code && code <= 299) {
332 is_valid = ast_ari_validate_list(response->message,
333 ast_ari_validate_endpoint_fn());
334 } else {
335 ast_log(LOG_ERROR, "Invalid error response %d for /endpoints/{tech}\n", code);
336 is_valid = 0;
337 }
338 }
339
340 if (!is_valid) {
341 ast_log(LOG_ERROR, "Response validation failed for /endpoints/{tech}\n");
342 ast_ari_response_error(response, 500,
343 "Internal Server Error", "Response validation failed");
344 }
345 #endif /* AST_DEVMODE */
346
347 fin: __attribute__((unused))
348 return;
349 }
350 /*!
351 * \brief Parameter parsing callback for /endpoints/{tech}/{resource}.
352 * \param ser TCP/TLS session object
353 * \param get_params GET parameters in the HTTP request.
354 * \param path_vars Path variables extracted from the request.
355 * \param headers HTTP headers.
356 * \param body
357 * \param[out] response Response to the HTTP request.
358 */
359 static void ast_ari_endpoints_get_cb(
360 struct ast_tcptls_session_instance *ser,
361 struct ast_variable *get_params, struct ast_variable *path_vars,
362 struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
363 {
364 struct ast_ari_endpoints_get_args args = {};
365 struct ast_variable *i;
366 #if defined(AST_DEVMODE)
367 int is_valid;
368 int code;
369 #endif /* AST_DEVMODE */
370
371 for (i = path_vars; i; i = i->next) {
372 if (strcmp(i->name, "tech") == 0) {
373 args.tech = (i->value);
374 } else
375 if (strcmp(i->name, "resource") == 0) {
376 args.resource = (i->value);
377 } else
378 {}
379 }
380 ast_ari_endpoints_get(headers, &args, response);
381 #if defined(AST_DEVMODE)
382 code = response->response_code;
383
384 switch (code) {
385 case 0: /* Implementation is still a stub, or the code wasn't set */
386 is_valid = response->message == NULL;
387 break;
388 case 500: /* Internal Server Error */
389 case 501: /* Not Implemented */
390 case 400: /* Invalid parameters for sending a message. */
391 case 404: /* Endpoints not found */
392 is_valid = 1;
393 break;
394 default:
395 if (200 <= code && code <= 299) {
396 is_valid = ast_ari_validate_endpoint(
397 response->message);
398 } else {
399 ast_log(LOG_ERROR, "Invalid error response %d for /endpoints/{tech}/{resource}\n", code);
400 is_valid = 0;
401 }
402 }
403
404 if (!is_valid) {
405 ast_log(LOG_ERROR, "Response validation failed for /endpoints/{tech}/{resource}\n");
406 ast_ari_response_error(response, 500,
407 "Internal Server Error", "Response validation failed");
408 }
409 #endif /* AST_DEVMODE */
410
411 fin: __attribute__((unused))
412 return;
413 }
414 int ast_ari_endpoints_send_message_to_endpoint_parse_body(
415 struct ast_json *body,
416 struct ast_ari_endpoints_send_message_to_endpoint_args *args)
417 {
418 struct ast_json *field;
419 /* Parse query parameters out of it */
420 field = ast_json_object_get(body, "from");
421 if (field) {
422 args->from = ast_json_string_get(field);
423 }
424 field = ast_json_object_get(body, "body");
425 if (field) {
426 args->body = ast_json_string_get(field);
427 }
428 return 0;
429 }
430
431 /*!
432 * \brief Parameter parsing callback for /endpoints/{tech}/{resource}/sendMessage.
433 * \param ser TCP/TLS session object
434 * \param get_params GET parameters in the HTTP request.
435 * \param path_vars Path variables extracted from the request.
436 * \param headers HTTP headers.
437 * \param body
438 * \param[out] response Response to the HTTP request.
439 */
440 static void ast_ari_endpoints_send_message_to_endpoint_cb(
441 struct ast_tcptls_session_instance *ser,
442 struct ast_variable *get_params, struct ast_variable *path_vars,
443 struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
444 {
445 struct ast_ari_endpoints_send_message_to_endpoint_args args = {};
446 struct ast_variable *i;
447 #if defined(AST_DEVMODE)
448 int is_valid;
449 int code;
450 #endif /* AST_DEVMODE */
451
452 for (i = get_params; i; i = i->next) {
453 if (strcmp(i->name, "from") == 0) {
454 args.from = (i->value);
455 } else
456 if (strcmp(i->name, "body") == 0) {
457 args.body = (i->value);
458 } else
459 {}
460 }
461 for (i = path_vars; i; i = i->next) {
462 if (strcmp(i->name, "tech") == 0) {
463 args.tech = (i->value);
464 } else
465 if (strcmp(i->name, "resource") == 0) {
466 args.resource = (i->value);
467 } else
468 {}
469 }
470 args.variables = body;
471 ast_ari_endpoints_send_message_to_endpoint(headers, &args, response);
472 #if defined(AST_DEVMODE)
473 code = response->response_code;
474
475 switch (code) {
476 case 0: /* Implementation is still a stub, or the code wasn't set */
477 is_valid = response->message == NULL;
478 break;
479 case 500: /* Internal Server Error */
480 case 501: /* Not Implemented */
481 case 400: /* Invalid parameters for sending a message. */
482 case 404: /* Endpoint not found */
483 is_valid = 1;
484 break;
485 default:
486 if (200 <= code && code <= 299) {
487 is_valid = ast_ari_validate_void(
488 response->message);
489 } else {
490 ast_log(LOG_ERROR, "Invalid error response %d for /endpoints/{tech}/{resource}/sendMessage\n", code);
491 is_valid = 0;
492 }
493 }
494
495 if (!is_valid) {
496 ast_log(LOG_ERROR, "Response validation failed for /endpoints/{tech}/{resource}/sendMessage\n");
497 ast_ari_response_error(response, 500,
498 "Internal Server Error", "Response validation failed");
499 }
500 #endif /* AST_DEVMODE */
501
502 fin: __attribute__((unused))
503 return;
504 }
505 int ast_ari_endpoints_refer_to_endpoint_parse_body(
506 struct ast_json *body,
507 struct ast_ari_endpoints_refer_to_endpoint_args *args)
508 {
509 struct ast_json *field;
510 /* Parse query parameters out of it */
511 field = ast_json_object_get(body, "from");
512 if (field) {
513 args->from = ast_json_string_get(field);
514 }
515 field = ast_json_object_get(body, "refer_to");
516 if (field) {
517 args->refer_to = ast_json_string_get(field);
518 }
519 field = ast_json_object_get(body, "to_self");
520 if (field) {
521 args->to_self = ast_json_is_true(field);
522 }
523 return 0;
524 }
525
526 /*!
527 * \brief Parameter parsing callback for /endpoints/{tech}/{resource}/refer.
528 * \param ser TCP/TLS session object
529 * \param get_params GET parameters in the HTTP request.
530 * \param path_vars Path variables extracted from the request.
531 * \param headers HTTP headers.
532 * \param body
533 * \param[out] response Response to the HTTP request.
534 */
535 static void ast_ari_endpoints_refer_to_endpoint_cb(
536 struct ast_tcptls_session_instance *ser,
537 struct ast_variable *get_params, struct ast_variable *path_vars,
538 struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
539 {
540 struct ast_ari_endpoints_refer_to_endpoint_args args = {};
541 struct ast_variable *i;
542 #if defined(AST_DEVMODE)
543 int is_valid;
544 int code;
545 #endif /* AST_DEVMODE */
546
547 for (i = get_params; i; i = i->next) {
548 if (strcmp(i->name, "from") == 0) {
549 args.from = (i->value);
550 } else
551 if (strcmp(i->name, "refer_to") == 0) {
552 args.refer_to = (i->value);
553 } else
554 if (strcmp(i->name, "to_self") == 0) {
555 args.to_self = ast_true(i->value);
556 } else
557 {}
558 }
559 for (i = path_vars; i; i = i->next) {
560 if (strcmp(i->name, "tech") == 0) {
561 args.tech = (i->value);
562 } else
563 if (strcmp(i->name, "resource") == 0) {
564 args.resource = (i->value);
565 } else
566 {}
567 }
568 args.variables = body;
569 ast_ari_endpoints_refer_to_endpoint(headers, &args, response);
570 #if defined(AST_DEVMODE)
571 code = response->response_code;
572
573 switch (code) {
574 case 0: /* Implementation is still a stub, or the code wasn't set */
575 is_valid = response->message == NULL;
576 break;
577 case 500: /* Internal Server Error */
578 case 501: /* Not Implemented */
579 case 400: /* Invalid parameters for referring. */
580 case 404: /* Endpoint not found */
581 is_valid = 1;
582 break;
583 default:
584 if (200 <= code && code <= 299) {
585 is_valid = ast_ari_validate_void(
586 response->message);
587 } else {
588 ast_log(LOG_ERROR, "Invalid error response %d for /endpoints/{tech}/{resource}/refer\n", code);
589 is_valid = 0;
590 }
591 }
592
593 if (!is_valid) {
594 ast_log(LOG_ERROR, "Response validation failed for /endpoints/{tech}/{resource}/refer\n");
595 ast_ari_response_error(response, 500,
596 "Internal Server Error", "Response validation failed");
597 }
598 #endif /* AST_DEVMODE */
599
600 fin: __attribute__((unused))
601 return;
602 }
603
604 /*! \brief REST handler for /api-docs/endpoints.json */
605 static struct stasis_rest_handlers endpoints_sendMessage = {
606 .path_segment = "sendMessage",
607 .callbacks = {
608 [AST_HTTP_PUT] = ast_ari_endpoints_send_message_cb,
609 },
610 .num_children = 0,
611 .children = { }
612 };
613 /*! \brief REST handler for /api-docs/endpoints.json */
614 static struct stasis_rest_handlers endpoints_refer = {
615 .path_segment = "refer",
616 .callbacks = {
617 [AST_HTTP_POST] = ast_ari_endpoints_refer_cb,
618 },
619 .num_children = 0,
620 .children = { }
621 };
622 /*! \brief REST handler for /api-docs/endpoints.json */
623 static struct stasis_rest_handlers endpoints_tech_resource_sendMessage = {
624 .path_segment = "sendMessage",
625 .callbacks = {
626 [AST_HTTP_PUT] = ast_ari_endpoints_send_message_to_endpoint_cb,
627 },
628 .num_children = 0,
629 .children = { }
630 };
631 /*! \brief REST handler for /api-docs/endpoints.json */
632 static struct stasis_rest_handlers endpoints_tech_resource_refer = {
633 .path_segment = "refer",
634 .callbacks = {
635 [AST_HTTP_POST] = ast_ari_endpoints_refer_to_endpoint_cb,
636 },
637 .num_children = 0,
638 .children = { }
639 };
640 /*! \brief REST handler for /api-docs/endpoints.json */
641 static struct stasis_rest_handlers endpoints_tech_resource = {
642 .path_segment = "resource",
643 .is_wildcard = 1,
644 .callbacks = {
645 [AST_HTTP_GET] = ast_ari_endpoints_get_cb,
646 },
647 .num_children = 2,
648 .children = { &endpoints_tech_resource_sendMessage,&endpoints_tech_resource_refer, }
649 };
650 /*! \brief REST handler for /api-docs/endpoints.json */
651 static struct stasis_rest_handlers endpoints_tech = {
652 .path_segment = "tech",
653 .is_wildcard = 1,
654 .callbacks = {
655 [AST_HTTP_GET] = ast_ari_endpoints_list_by_tech_cb,
656 },
657 .num_children = 1,
658 .children = { &endpoints_tech_resource, }
659 };
660 /*! \brief REST handler for /api-docs/endpoints.json */
661 static struct stasis_rest_handlers endpoints = {
662 .path_segment = "endpoints",
663 .callbacks = {
664 [AST_HTTP_GET] = ast_ari_endpoints_list_cb,
665 },
666 .num_children = 3,
667 .children = { &endpoints_sendMessage,&endpoints_refer,&endpoints_tech, }
668 };
669
670 static int unload_module(void)
671 {
672 ast_ari_remove_handler(&endpoints);
673 return 0;
674 }
675
676 static int load_module(void)
677 {
678 int res = 0;
679
680
681 res |= ast_ari_add_handler(&endpoints);
682 if (res) {
683 unload_module();
684 return AST_MODULE_LOAD_DECLINE;
685 }
686
687 return AST_MODULE_LOAD_SUCCESS;
688 }
689
690 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "RESTful API module - Endpoint resources",
691 .support_level = AST_MODULE_SUPPORT_CORE,
692 .load = load_module,
693 .unload = unload_module,
694 .requires = "res_ari,res_ari_model,res_stasis",
695 );