res_ari_sounds.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 Sound 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_sounds.h"
  42. #if defined(AST_DEVMODE)
  43. #include "ari/ari_model_validators.h"
  44. #endif
  45. #define MAX_VALS 128
  46. int ast_ari_sounds_list_parse_body(
  47. struct ast_json *body,
  48. struct ast_ari_sounds_list_args *args)
  49. {
  50. struct ast_json *field;
  51. /* Parse query parameters out of it */
  52. field = ast_json_object_get(body, "lang");
  53. if (field) {
  54. args->lang = ast_json_string_get(field);
  55. }
  56. field = ast_json_object_get(body, "format");
  57. if (field) {
  58. args->format = ast_json_string_get(field);
  59. }
  60. return 0;
  61. }
  62. /*!
  63. * \brief Parameter parsing callback for /sounds.
  64. * \param get_params GET parameters in the HTTP request.
  65. * \param path_vars Path variables extracted from the request.
  66. * \param headers HTTP headers.
  67. * \param[out] response Response to the HTTP request.
  68. */
  69. static void ast_ari_sounds_list_cb(
  70. struct ast_tcptls_session_instance *ser,
  71. struct ast_variable *get_params, struct ast_variable *path_vars,
  72. struct ast_variable *headers, struct ast_ari_response *response)
  73. {
  74. struct ast_ari_sounds_list_args args = {};
  75. struct ast_variable *i;
  76. RAII_VAR(struct ast_json *, body, NULL, ast_json_unref);
  77. #if defined(AST_DEVMODE)
  78. int is_valid;
  79. int code;
  80. #endif /* AST_DEVMODE */
  81. for (i = get_params; i; i = i->next) {
  82. if (strcmp(i->name, "lang") == 0) {
  83. args.lang = (i->value);
  84. } else
  85. if (strcmp(i->name, "format") == 0) {
  86. args.format = (i->value);
  87. } else
  88. {}
  89. }
  90. /* Look for a JSON request entity */
  91. body = ast_http_get_json(ser, headers);
  92. if (!body) {
  93. switch (errno) {
  94. case EFBIG:
  95. ast_ari_response_error(response, 413, "Request Entity Too Large", "Request body too large");
  96. goto fin;
  97. case ENOMEM:
  98. ast_ari_response_error(response, 500, "Internal Server Error", "Error processing request");
  99. goto fin;
  100. case EIO:
  101. ast_ari_response_error(response, 400, "Bad Request", "Error parsing request body");
  102. goto fin;
  103. }
  104. }
  105. if (ast_ari_sounds_list_parse_body(body, &args)) {
  106. ast_ari_response_alloc_failed(response);
  107. goto fin;
  108. }
  109. ast_ari_sounds_list(headers, &args, response);
  110. #if defined(AST_DEVMODE)
  111. code = response->response_code;
  112. switch (code) {
  113. case 0: /* Implementation is still a stub, or the code wasn't set */
  114. is_valid = response->message == NULL;
  115. break;
  116. case 500: /* Internal Server Error */
  117. case 501: /* Not Implemented */
  118. is_valid = 1;
  119. break;
  120. default:
  121. if (200 <= code && code <= 299) {
  122. is_valid = ast_ari_validate_list(response->message,
  123. ast_ari_validate_sound_fn());
  124. } else {
  125. ast_log(LOG_ERROR, "Invalid error response %d for /sounds\n", code);
  126. is_valid = 0;
  127. }
  128. }
  129. if (!is_valid) {
  130. ast_log(LOG_ERROR, "Response validation failed for /sounds\n");
  131. ast_ari_response_error(response, 500,
  132. "Internal Server Error", "Response validation failed");
  133. }
  134. #endif /* AST_DEVMODE */
  135. fin: __attribute__((unused))
  136. return;
  137. }
  138. /*!
  139. * \brief Parameter parsing callback for /sounds/{soundId}.
  140. * \param get_params GET parameters in the HTTP request.
  141. * \param path_vars Path variables extracted from the request.
  142. * \param headers HTTP headers.
  143. * \param[out] response Response to the HTTP request.
  144. */
  145. static void ast_ari_sounds_get_cb(
  146. struct ast_tcptls_session_instance *ser,
  147. struct ast_variable *get_params, struct ast_variable *path_vars,
  148. struct ast_variable *headers, struct ast_ari_response *response)
  149. {
  150. struct ast_ari_sounds_get_args args = {};
  151. struct ast_variable *i;
  152. RAII_VAR(struct ast_json *, body, NULL, ast_json_unref);
  153. #if defined(AST_DEVMODE)
  154. int is_valid;
  155. int code;
  156. #endif /* AST_DEVMODE */
  157. for (i = path_vars; i; i = i->next) {
  158. if (strcmp(i->name, "soundId") == 0) {
  159. args.sound_id = (i->value);
  160. } else
  161. {}
  162. }
  163. ast_ari_sounds_get(headers, &args, response);
  164. #if defined(AST_DEVMODE)
  165. code = response->response_code;
  166. switch (code) {
  167. case 0: /* Implementation is still a stub, or the code wasn't set */
  168. is_valid = response->message == NULL;
  169. break;
  170. case 500: /* Internal Server Error */
  171. case 501: /* Not Implemented */
  172. is_valid = 1;
  173. break;
  174. default:
  175. if (200 <= code && code <= 299) {
  176. is_valid = ast_ari_validate_sound(
  177. response->message);
  178. } else {
  179. ast_log(LOG_ERROR, "Invalid error response %d for /sounds/{soundId}\n", code);
  180. is_valid = 0;
  181. }
  182. }
  183. if (!is_valid) {
  184. ast_log(LOG_ERROR, "Response validation failed for /sounds/{soundId}\n");
  185. ast_ari_response_error(response, 500,
  186. "Internal Server Error", "Response validation failed");
  187. }
  188. #endif /* AST_DEVMODE */
  189. fin: __attribute__((unused))
  190. return;
  191. }
  192. /*! \brief REST handler for /api-docs/sounds.{format} */
  193. static struct stasis_rest_handlers sounds_soundId = {
  194. .path_segment = "soundId",
  195. .is_wildcard = 1,
  196. .callbacks = {
  197. [AST_HTTP_GET] = ast_ari_sounds_get_cb,
  198. },
  199. .num_children = 0,
  200. .children = { }
  201. };
  202. /*! \brief REST handler for /api-docs/sounds.{format} */
  203. static struct stasis_rest_handlers sounds = {
  204. .path_segment = "sounds",
  205. .callbacks = {
  206. [AST_HTTP_GET] = ast_ari_sounds_list_cb,
  207. },
  208. .num_children = 1,
  209. .children = { &sounds_soundId, }
  210. };
  211. static int load_module(void)
  212. {
  213. int res = 0;
  214. stasis_app_ref();
  215. res |= ast_ari_add_handler(&sounds);
  216. return res;
  217. }
  218. static int unload_module(void)
  219. {
  220. ast_ari_remove_handler(&sounds);
  221. stasis_app_unref();
  222. return 0;
  223. }
  224. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "RESTful API module - Sound resources",
  225. .support_level = AST_MODULE_SUPPORT_CORE,
  226. .load = load_module,
  227. .unload = unload_module,
  228. .nonoptreq = "res_ari,res_stasis",
  229. );