res_ari_playbacks.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2012 - 2013, Digium, Inc.
  5. *
  6. * David M. Lee, II <dlee@digium.com>
  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. * !!!!! DO NOT EDIT !!!!!
  21. * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  22. * This file is generated by a mustache template. Please see the original
  23. * template in rest-api-templates/res_ari_resource.c.mustache
  24. */
  25. /*! \file
  26. *
  27. * \brief Playback control resources
  28. *
  29. * \author David M. Lee, II <dlee@digium.com>
  30. */
  31. /*** MODULEINFO
  32. <depend type="module">res_ari</depend>
  33. <depend type="module">res_ari_model</depend>
  34. <depend type="module">res_stasis</depend>
  35. <support_level>core</support_level>
  36. ***/
  37. #include "asterisk.h"
  38. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  39. #include "asterisk/app.h"
  40. #include "asterisk/module.h"
  41. #include "asterisk/stasis_app.h"
  42. #include "ari/resource_playbacks.h"
  43. #if defined(AST_DEVMODE)
  44. #include "ari/ari_model_validators.h"
  45. #endif
  46. #define MAX_VALS 128
  47. /*!
  48. * \brief Parameter parsing callback for /playbacks/{playbackId}.
  49. * \param get_params GET parameters in the HTTP request.
  50. * \param path_vars Path variables extracted from the request.
  51. * \param headers HTTP headers.
  52. * \param[out] response Response to the HTTP request.
  53. */
  54. static void ast_ari_playbacks_get_cb(
  55. struct ast_tcptls_session_instance *ser,
  56. struct ast_variable *get_params, struct ast_variable *path_vars,
  57. struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
  58. {
  59. struct ast_ari_playbacks_get_args args = {};
  60. struct ast_variable *i;
  61. #if defined(AST_DEVMODE)
  62. int is_valid;
  63. int code;
  64. #endif /* AST_DEVMODE */
  65. for (i = path_vars; i; i = i->next) {
  66. if (strcmp(i->name, "playbackId") == 0) {
  67. args.playback_id = (i->value);
  68. } else
  69. {}
  70. }
  71. ast_ari_playbacks_get(headers, &args, response);
  72. #if defined(AST_DEVMODE)
  73. code = response->response_code;
  74. switch (code) {
  75. case 0: /* Implementation is still a stub, or the code wasn't set */
  76. is_valid = response->message == NULL;
  77. break;
  78. case 500: /* Internal Server Error */
  79. case 501: /* Not Implemented */
  80. case 404: /* The playback cannot be found */
  81. is_valid = 1;
  82. break;
  83. default:
  84. if (200 <= code && code <= 299) {
  85. is_valid = ast_ari_validate_playback(
  86. response->message);
  87. } else {
  88. ast_log(LOG_ERROR, "Invalid error response %d for /playbacks/{playbackId}\n", code);
  89. is_valid = 0;
  90. }
  91. }
  92. if (!is_valid) {
  93. ast_log(LOG_ERROR, "Response validation failed for /playbacks/{playbackId}\n");
  94. ast_ari_response_error(response, 500,
  95. "Internal Server Error", "Response validation failed");
  96. }
  97. #endif /* AST_DEVMODE */
  98. fin: __attribute__((unused))
  99. return;
  100. }
  101. /*!
  102. * \brief Parameter parsing callback for /playbacks/{playbackId}.
  103. * \param get_params GET parameters in the HTTP request.
  104. * \param path_vars Path variables extracted from the request.
  105. * \param headers HTTP headers.
  106. * \param[out] response Response to the HTTP request.
  107. */
  108. static void ast_ari_playbacks_stop_cb(
  109. struct ast_tcptls_session_instance *ser,
  110. struct ast_variable *get_params, struct ast_variable *path_vars,
  111. struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
  112. {
  113. struct ast_ari_playbacks_stop_args args = {};
  114. struct ast_variable *i;
  115. #if defined(AST_DEVMODE)
  116. int is_valid;
  117. int code;
  118. #endif /* AST_DEVMODE */
  119. for (i = path_vars; i; i = i->next) {
  120. if (strcmp(i->name, "playbackId") == 0) {
  121. args.playback_id = (i->value);
  122. } else
  123. {}
  124. }
  125. ast_ari_playbacks_stop(headers, &args, response);
  126. #if defined(AST_DEVMODE)
  127. code = response->response_code;
  128. switch (code) {
  129. case 0: /* Implementation is still a stub, or the code wasn't set */
  130. is_valid = response->message == NULL;
  131. break;
  132. case 500: /* Internal Server Error */
  133. case 501: /* Not Implemented */
  134. case 404: /* The playback cannot be found */
  135. is_valid = 1;
  136. break;
  137. default:
  138. if (200 <= code && code <= 299) {
  139. is_valid = ast_ari_validate_void(
  140. response->message);
  141. } else {
  142. ast_log(LOG_ERROR, "Invalid error response %d for /playbacks/{playbackId}\n", code);
  143. is_valid = 0;
  144. }
  145. }
  146. if (!is_valid) {
  147. ast_log(LOG_ERROR, "Response validation failed for /playbacks/{playbackId}\n");
  148. ast_ari_response_error(response, 500,
  149. "Internal Server Error", "Response validation failed");
  150. }
  151. #endif /* AST_DEVMODE */
  152. fin: __attribute__((unused))
  153. return;
  154. }
  155. int ast_ari_playbacks_control_parse_body(
  156. struct ast_json *body,
  157. struct ast_ari_playbacks_control_args *args)
  158. {
  159. struct ast_json *field;
  160. /* Parse query parameters out of it */
  161. field = ast_json_object_get(body, "operation");
  162. if (field) {
  163. args->operation = ast_json_string_get(field);
  164. }
  165. return 0;
  166. }
  167. /*!
  168. * \brief Parameter parsing callback for /playbacks/{playbackId}/control.
  169. * \param get_params GET parameters in the HTTP request.
  170. * \param path_vars Path variables extracted from the request.
  171. * \param headers HTTP headers.
  172. * \param[out] response Response to the HTTP request.
  173. */
  174. static void ast_ari_playbacks_control_cb(
  175. struct ast_tcptls_session_instance *ser,
  176. struct ast_variable *get_params, struct ast_variable *path_vars,
  177. struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
  178. {
  179. struct ast_ari_playbacks_control_args args = {};
  180. struct ast_variable *i;
  181. #if defined(AST_DEVMODE)
  182. int is_valid;
  183. int code;
  184. #endif /* AST_DEVMODE */
  185. for (i = get_params; i; i = i->next) {
  186. if (strcmp(i->name, "operation") == 0) {
  187. args.operation = (i->value);
  188. } else
  189. {}
  190. }
  191. for (i = path_vars; i; i = i->next) {
  192. if (strcmp(i->name, "playbackId") == 0) {
  193. args.playback_id = (i->value);
  194. } else
  195. {}
  196. }
  197. if (ast_ari_playbacks_control_parse_body(body, &args)) {
  198. ast_ari_response_alloc_failed(response);
  199. goto fin;
  200. }
  201. ast_ari_playbacks_control(headers, &args, response);
  202. #if defined(AST_DEVMODE)
  203. code = response->response_code;
  204. switch (code) {
  205. case 0: /* Implementation is still a stub, or the code wasn't set */
  206. is_valid = response->message == NULL;
  207. break;
  208. case 500: /* Internal Server Error */
  209. case 501: /* Not Implemented */
  210. case 400: /* The provided operation parameter was invalid */
  211. case 404: /* The playback cannot be found */
  212. case 409: /* The operation cannot be performed in the playback's current state */
  213. is_valid = 1;
  214. break;
  215. default:
  216. if (200 <= code && code <= 299) {
  217. is_valid = ast_ari_validate_void(
  218. response->message);
  219. } else {
  220. ast_log(LOG_ERROR, "Invalid error response %d for /playbacks/{playbackId}/control\n", code);
  221. is_valid = 0;
  222. }
  223. }
  224. if (!is_valid) {
  225. ast_log(LOG_ERROR, "Response validation failed for /playbacks/{playbackId}/control\n");
  226. ast_ari_response_error(response, 500,
  227. "Internal Server Error", "Response validation failed");
  228. }
  229. #endif /* AST_DEVMODE */
  230. fin: __attribute__((unused))
  231. return;
  232. }
  233. /*! \brief REST handler for /api-docs/playbacks.json */
  234. static struct stasis_rest_handlers playbacks_playbackId_control = {
  235. .path_segment = "control",
  236. .callbacks = {
  237. [AST_HTTP_POST] = ast_ari_playbacks_control_cb,
  238. },
  239. .num_children = 0,
  240. .children = { }
  241. };
  242. /*! \brief REST handler for /api-docs/playbacks.json */
  243. static struct stasis_rest_handlers playbacks_playbackId = {
  244. .path_segment = "playbackId",
  245. .is_wildcard = 1,
  246. .callbacks = {
  247. [AST_HTTP_GET] = ast_ari_playbacks_get_cb,
  248. [AST_HTTP_DELETE] = ast_ari_playbacks_stop_cb,
  249. },
  250. .num_children = 1,
  251. .children = { &playbacks_playbackId_control, }
  252. };
  253. /*! \brief REST handler for /api-docs/playbacks.json */
  254. static struct stasis_rest_handlers playbacks = {
  255. .path_segment = "playbacks",
  256. .callbacks = {
  257. },
  258. .num_children = 1,
  259. .children = { &playbacks_playbackId, }
  260. };
  261. static int unload_module(void)
  262. {
  263. ast_ari_remove_handler(&playbacks);
  264. stasis_app_unref();
  265. return 0;
  266. }
  267. static int load_module(void)
  268. {
  269. int res = 0;
  270. CHECK_ARI_MODULE_LOADED();
  271. stasis_app_ref();
  272. res |= ast_ari_add_handler(&playbacks);
  273. if (res) {
  274. unload_module();
  275. return AST_MODULE_LOAD_DECLINE;
  276. }
  277. return AST_MODULE_LOAD_SUCCESS;
  278. }
  279. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "RESTful API module - Playback control resources",
  280. .support_level = AST_MODULE_SUPPORT_CORE,
  281. .load = load_module,
  282. .unload = unload_module,
  283. );