res_ari_playbacks.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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_stasis</depend>
  34. <support_level>core</support_level>
  35. ***/
  36. #include "asterisk.h"
  37. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  38. #include "asterisk/app.h"
  39. #include "asterisk/module.h"
  40. #include "asterisk/stasis_app.h"
  41. #include "ari/resource_playbacks.h"
  42. #if defined(AST_DEVMODE)
  43. #include "ari/ari_model_validators.h"
  44. #endif
  45. #define MAX_VALS 128
  46. /*!
  47. * \brief Parameter parsing callback for /playbacks/{playbackId}.
  48. * \param get_params GET parameters in the HTTP request.
  49. * \param path_vars Path variables extracted from the request.
  50. * \param headers HTTP headers.
  51. * \param[out] response Response to the HTTP request.
  52. */
  53. static void ast_ari_playbacks_get_cb(
  54. struct ast_tcptls_session_instance *ser,
  55. struct ast_variable *get_params, struct ast_variable *path_vars,
  56. struct ast_variable *headers, struct ast_ari_response *response)
  57. {
  58. struct ast_ari_playbacks_get_args args = {};
  59. struct ast_variable *i;
  60. RAII_VAR(struct ast_json *, body, NULL, ast_json_unref);
  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_ari_response *response)
  112. {
  113. struct ast_ari_playbacks_stop_args args = {};
  114. struct ast_variable *i;
  115. RAII_VAR(struct ast_json *, body, NULL, ast_json_unref);
  116. #if defined(AST_DEVMODE)
  117. int is_valid;
  118. int code;
  119. #endif /* AST_DEVMODE */
  120. for (i = path_vars; i; i = i->next) {
  121. if (strcmp(i->name, "playbackId") == 0) {
  122. args.playback_id = (i->value);
  123. } else
  124. {}
  125. }
  126. ast_ari_playbacks_stop(headers, &args, response);
  127. #if defined(AST_DEVMODE)
  128. code = response->response_code;
  129. switch (code) {
  130. case 0: /* Implementation is still a stub, or the code wasn't set */
  131. is_valid = response->message == NULL;
  132. break;
  133. case 500: /* Internal Server Error */
  134. case 501: /* Not Implemented */
  135. case 404: /* The playback cannot be found */
  136. is_valid = 1;
  137. break;
  138. default:
  139. if (200 <= code && code <= 299) {
  140. is_valid = ast_ari_validate_void(
  141. response->message);
  142. } else {
  143. ast_log(LOG_ERROR, "Invalid error response %d for /playbacks/{playbackId}\n", code);
  144. is_valid = 0;
  145. }
  146. }
  147. if (!is_valid) {
  148. ast_log(LOG_ERROR, "Response validation failed for /playbacks/{playbackId}\n");
  149. ast_ari_response_error(response, 500,
  150. "Internal Server Error", "Response validation failed");
  151. }
  152. #endif /* AST_DEVMODE */
  153. fin: __attribute__((unused))
  154. return;
  155. }
  156. int ast_ari_playbacks_control_parse_body(
  157. struct ast_json *body,
  158. struct ast_ari_playbacks_control_args *args)
  159. {
  160. struct ast_json *field;
  161. /* Parse query parameters out of it */
  162. field = ast_json_object_get(body, "operation");
  163. if (field) {
  164. args->operation = ast_json_string_get(field);
  165. }
  166. return 0;
  167. }
  168. /*!
  169. * \brief Parameter parsing callback for /playbacks/{playbackId}/control.
  170. * \param get_params GET parameters in the HTTP request.
  171. * \param path_vars Path variables extracted from the request.
  172. * \param headers HTTP headers.
  173. * \param[out] response Response to the HTTP request.
  174. */
  175. static void ast_ari_playbacks_control_cb(
  176. struct ast_tcptls_session_instance *ser,
  177. struct ast_variable *get_params, struct ast_variable *path_vars,
  178. struct ast_variable *headers, struct ast_ari_response *response)
  179. {
  180. struct ast_ari_playbacks_control_args args = {};
  181. struct ast_variable *i;
  182. RAII_VAR(struct ast_json *, body, NULL, ast_json_unref);
  183. #if defined(AST_DEVMODE)
  184. int is_valid;
  185. int code;
  186. #endif /* AST_DEVMODE */
  187. for (i = get_params; i; i = i->next) {
  188. if (strcmp(i->name, "operation") == 0) {
  189. args.operation = (i->value);
  190. } else
  191. {}
  192. }
  193. for (i = path_vars; i; i = i->next) {
  194. if (strcmp(i->name, "playbackId") == 0) {
  195. args.playback_id = (i->value);
  196. } else
  197. {}
  198. }
  199. /* Look for a JSON request entity */
  200. body = ast_http_get_json(ser, headers);
  201. if (!body) {
  202. switch (errno) {
  203. case EFBIG:
  204. ast_ari_response_error(response, 413, "Request Entity Too Large", "Request body too large");
  205. goto fin;
  206. case ENOMEM:
  207. ast_ari_response_error(response, 500, "Internal Server Error", "Error processing request");
  208. goto fin;
  209. case EIO:
  210. ast_ari_response_error(response, 400, "Bad Request", "Error parsing request body");
  211. goto fin;
  212. }
  213. }
  214. if (ast_ari_playbacks_control_parse_body(body, &args)) {
  215. ast_ari_response_alloc_failed(response);
  216. goto fin;
  217. }
  218. ast_ari_playbacks_control(headers, &args, response);
  219. #if defined(AST_DEVMODE)
  220. code = response->response_code;
  221. switch (code) {
  222. case 0: /* Implementation is still a stub, or the code wasn't set */
  223. is_valid = response->message == NULL;
  224. break;
  225. case 500: /* Internal Server Error */
  226. case 501: /* Not Implemented */
  227. case 400: /* The provided operation parameter was invalid */
  228. case 404: /* The playback cannot be found */
  229. case 409: /* The operation cannot be performed in the playback's current state */
  230. is_valid = 1;
  231. break;
  232. default:
  233. if (200 <= code && code <= 299) {
  234. is_valid = ast_ari_validate_void(
  235. response->message);
  236. } else {
  237. ast_log(LOG_ERROR, "Invalid error response %d for /playbacks/{playbackId}/control\n", code);
  238. is_valid = 0;
  239. }
  240. }
  241. if (!is_valid) {
  242. ast_log(LOG_ERROR, "Response validation failed for /playbacks/{playbackId}/control\n");
  243. ast_ari_response_error(response, 500,
  244. "Internal Server Error", "Response validation failed");
  245. }
  246. #endif /* AST_DEVMODE */
  247. fin: __attribute__((unused))
  248. return;
  249. }
  250. /*! \brief REST handler for /api-docs/playbacks.{format} */
  251. static struct stasis_rest_handlers playbacks_playbackId_control = {
  252. .path_segment = "control",
  253. .callbacks = {
  254. [AST_HTTP_POST] = ast_ari_playbacks_control_cb,
  255. },
  256. .num_children = 0,
  257. .children = { }
  258. };
  259. /*! \brief REST handler for /api-docs/playbacks.{format} */
  260. static struct stasis_rest_handlers playbacks_playbackId = {
  261. .path_segment = "playbackId",
  262. .is_wildcard = 1,
  263. .callbacks = {
  264. [AST_HTTP_GET] = ast_ari_playbacks_get_cb,
  265. [AST_HTTP_DELETE] = ast_ari_playbacks_stop_cb,
  266. },
  267. .num_children = 1,
  268. .children = { &playbacks_playbackId_control, }
  269. };
  270. /*! \brief REST handler for /api-docs/playbacks.{format} */
  271. static struct stasis_rest_handlers playbacks = {
  272. .path_segment = "playbacks",
  273. .callbacks = {
  274. },
  275. .num_children = 1,
  276. .children = { &playbacks_playbackId, }
  277. };
  278. static int load_module(void)
  279. {
  280. int res = 0;
  281. stasis_app_ref();
  282. res |= ast_ari_add_handler(&playbacks);
  283. return res;
  284. }
  285. static int unload_module(void)
  286. {
  287. ast_ari_remove_handler(&playbacks);
  288. stasis_app_unref();
  289. return 0;
  290. }
  291. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "RESTful API module - Playback control resources",
  292. .support_level = AST_MODULE_SUPPORT_CORE,
  293. .load = load_module,
  294. .unload = unload_module,
  295. .nonoptreq = "res_ari,res_stasis",
  296. );