res_ari_mailboxes.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013, Digium, Inc.
  5. *
  6. * Jonathan Rose <jrose@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 Mailboxes resources
  28. *
  29. * \author Jonathan Rose <jrose@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_mailboxes.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 /mailboxes.
  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_mailboxes_list_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_mailboxes_list_args args = {};
  60. #if defined(AST_DEVMODE)
  61. int is_valid;
  62. int code;
  63. #endif /* AST_DEVMODE */
  64. ast_ari_mailboxes_list(headers, &args, response);
  65. #if defined(AST_DEVMODE)
  66. code = response->response_code;
  67. switch (code) {
  68. case 0: /* Implementation is still a stub, or the code wasn't set */
  69. is_valid = response->message == NULL;
  70. break;
  71. case 500: /* Internal Server Error */
  72. case 501: /* Not Implemented */
  73. is_valid = 1;
  74. break;
  75. default:
  76. if (200 <= code && code <= 299) {
  77. is_valid = ast_ari_validate_list(response->message,
  78. ast_ari_validate_mailbox_fn());
  79. } else {
  80. ast_log(LOG_ERROR, "Invalid error response %d for /mailboxes\n", code);
  81. is_valid = 0;
  82. }
  83. }
  84. if (!is_valid) {
  85. ast_log(LOG_ERROR, "Response validation failed for /mailboxes\n");
  86. ast_ari_response_error(response, 500,
  87. "Internal Server Error", "Response validation failed");
  88. }
  89. #endif /* AST_DEVMODE */
  90. fin: __attribute__((unused))
  91. return;
  92. }
  93. /*!
  94. * \brief Parameter parsing callback for /mailboxes/{mailboxName}.
  95. * \param get_params GET parameters in the HTTP request.
  96. * \param path_vars Path variables extracted from the request.
  97. * \param headers HTTP headers.
  98. * \param[out] response Response to the HTTP request.
  99. */
  100. static void ast_ari_mailboxes_get_cb(
  101. struct ast_tcptls_session_instance *ser,
  102. struct ast_variable *get_params, struct ast_variable *path_vars,
  103. struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
  104. {
  105. struct ast_ari_mailboxes_get_args args = {};
  106. struct ast_variable *i;
  107. #if defined(AST_DEVMODE)
  108. int is_valid;
  109. int code;
  110. #endif /* AST_DEVMODE */
  111. for (i = path_vars; i; i = i->next) {
  112. if (strcmp(i->name, "mailboxName") == 0) {
  113. args.mailbox_name = (i->value);
  114. } else
  115. {}
  116. }
  117. ast_ari_mailboxes_get(headers, &args, response);
  118. #if defined(AST_DEVMODE)
  119. code = response->response_code;
  120. switch (code) {
  121. case 0: /* Implementation is still a stub, or the code wasn't set */
  122. is_valid = response->message == NULL;
  123. break;
  124. case 500: /* Internal Server Error */
  125. case 501: /* Not Implemented */
  126. case 404: /* Mailbox not found */
  127. is_valid = 1;
  128. break;
  129. default:
  130. if (200 <= code && code <= 299) {
  131. is_valid = ast_ari_validate_mailbox(
  132. response->message);
  133. } else {
  134. ast_log(LOG_ERROR, "Invalid error response %d for /mailboxes/{mailboxName}\n", code);
  135. is_valid = 0;
  136. }
  137. }
  138. if (!is_valid) {
  139. ast_log(LOG_ERROR, "Response validation failed for /mailboxes/{mailboxName}\n");
  140. ast_ari_response_error(response, 500,
  141. "Internal Server Error", "Response validation failed");
  142. }
  143. #endif /* AST_DEVMODE */
  144. fin: __attribute__((unused))
  145. return;
  146. }
  147. int ast_ari_mailboxes_update_parse_body(
  148. struct ast_json *body,
  149. struct ast_ari_mailboxes_update_args *args)
  150. {
  151. struct ast_json *field;
  152. /* Parse query parameters out of it */
  153. field = ast_json_object_get(body, "oldMessages");
  154. if (field) {
  155. args->old_messages = ast_json_integer_get(field);
  156. }
  157. field = ast_json_object_get(body, "newMessages");
  158. if (field) {
  159. args->new_messages = ast_json_integer_get(field);
  160. }
  161. return 0;
  162. }
  163. /*!
  164. * \brief Parameter parsing callback for /mailboxes/{mailboxName}.
  165. * \param get_params GET parameters in the HTTP request.
  166. * \param path_vars Path variables extracted from the request.
  167. * \param headers HTTP headers.
  168. * \param[out] response Response to the HTTP request.
  169. */
  170. static void ast_ari_mailboxes_update_cb(
  171. struct ast_tcptls_session_instance *ser,
  172. struct ast_variable *get_params, struct ast_variable *path_vars,
  173. struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
  174. {
  175. struct ast_ari_mailboxes_update_args args = {};
  176. struct ast_variable *i;
  177. #if defined(AST_DEVMODE)
  178. int is_valid;
  179. int code;
  180. #endif /* AST_DEVMODE */
  181. for (i = get_params; i; i = i->next) {
  182. if (strcmp(i->name, "oldMessages") == 0) {
  183. args.old_messages = atoi(i->value);
  184. } else
  185. if (strcmp(i->name, "newMessages") == 0) {
  186. args.new_messages = atoi(i->value);
  187. } else
  188. {}
  189. }
  190. for (i = path_vars; i; i = i->next) {
  191. if (strcmp(i->name, "mailboxName") == 0) {
  192. args.mailbox_name = (i->value);
  193. } else
  194. {}
  195. }
  196. if (ast_ari_mailboxes_update_parse_body(body, &args)) {
  197. ast_ari_response_alloc_failed(response);
  198. goto fin;
  199. }
  200. ast_ari_mailboxes_update(headers, &args, response);
  201. #if defined(AST_DEVMODE)
  202. code = response->response_code;
  203. switch (code) {
  204. case 0: /* Implementation is still a stub, or the code wasn't set */
  205. is_valid = response->message == NULL;
  206. break;
  207. case 500: /* Internal Server Error */
  208. case 501: /* Not Implemented */
  209. case 404: /* Mailbox not found */
  210. is_valid = 1;
  211. break;
  212. default:
  213. if (200 <= code && code <= 299) {
  214. is_valid = ast_ari_validate_void(
  215. response->message);
  216. } else {
  217. ast_log(LOG_ERROR, "Invalid error response %d for /mailboxes/{mailboxName}\n", code);
  218. is_valid = 0;
  219. }
  220. }
  221. if (!is_valid) {
  222. ast_log(LOG_ERROR, "Response validation failed for /mailboxes/{mailboxName}\n");
  223. ast_ari_response_error(response, 500,
  224. "Internal Server Error", "Response validation failed");
  225. }
  226. #endif /* AST_DEVMODE */
  227. fin: __attribute__((unused))
  228. return;
  229. }
  230. /*!
  231. * \brief Parameter parsing callback for /mailboxes/{mailboxName}.
  232. * \param get_params GET parameters in the HTTP request.
  233. * \param path_vars Path variables extracted from the request.
  234. * \param headers HTTP headers.
  235. * \param[out] response Response to the HTTP request.
  236. */
  237. static void ast_ari_mailboxes_delete_cb(
  238. struct ast_tcptls_session_instance *ser,
  239. struct ast_variable *get_params, struct ast_variable *path_vars,
  240. struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
  241. {
  242. struct ast_ari_mailboxes_delete_args args = {};
  243. struct ast_variable *i;
  244. #if defined(AST_DEVMODE)
  245. int is_valid;
  246. int code;
  247. #endif /* AST_DEVMODE */
  248. for (i = path_vars; i; i = i->next) {
  249. if (strcmp(i->name, "mailboxName") == 0) {
  250. args.mailbox_name = (i->value);
  251. } else
  252. {}
  253. }
  254. ast_ari_mailboxes_delete(headers, &args, response);
  255. #if defined(AST_DEVMODE)
  256. code = response->response_code;
  257. switch (code) {
  258. case 0: /* Implementation is still a stub, or the code wasn't set */
  259. is_valid = response->message == NULL;
  260. break;
  261. case 500: /* Internal Server Error */
  262. case 501: /* Not Implemented */
  263. case 404: /* Mailbox not found */
  264. is_valid = 1;
  265. break;
  266. default:
  267. if (200 <= code && code <= 299) {
  268. is_valid = ast_ari_validate_void(
  269. response->message);
  270. } else {
  271. ast_log(LOG_ERROR, "Invalid error response %d for /mailboxes/{mailboxName}\n", code);
  272. is_valid = 0;
  273. }
  274. }
  275. if (!is_valid) {
  276. ast_log(LOG_ERROR, "Response validation failed for /mailboxes/{mailboxName}\n");
  277. ast_ari_response_error(response, 500,
  278. "Internal Server Error", "Response validation failed");
  279. }
  280. #endif /* AST_DEVMODE */
  281. fin: __attribute__((unused))
  282. return;
  283. }
  284. /*! \brief REST handler for /api-docs/mailboxes.json */
  285. static struct stasis_rest_handlers mailboxes_mailboxName = {
  286. .path_segment = "mailboxName",
  287. .is_wildcard = 1,
  288. .callbacks = {
  289. [AST_HTTP_GET] = ast_ari_mailboxes_get_cb,
  290. [AST_HTTP_PUT] = ast_ari_mailboxes_update_cb,
  291. [AST_HTTP_DELETE] = ast_ari_mailboxes_delete_cb,
  292. },
  293. .num_children = 0,
  294. .children = { }
  295. };
  296. /*! \brief REST handler for /api-docs/mailboxes.json */
  297. static struct stasis_rest_handlers mailboxes = {
  298. .path_segment = "mailboxes",
  299. .callbacks = {
  300. [AST_HTTP_GET] = ast_ari_mailboxes_list_cb,
  301. },
  302. .num_children = 1,
  303. .children = { &mailboxes_mailboxName, }
  304. };
  305. static int unload_module(void)
  306. {
  307. ast_ari_remove_handler(&mailboxes);
  308. stasis_app_unref();
  309. return 0;
  310. }
  311. static int load_module(void)
  312. {
  313. int res = 0;
  314. CHECK_ARI_MODULE_LOADED();
  315. stasis_app_ref();
  316. res |= ast_ari_add_handler(&mailboxes);
  317. if (res) {
  318. unload_module();
  319. return AST_MODULE_LOAD_DECLINE;
  320. }
  321. return AST_MODULE_LOAD_SUCCESS;
  322. }
  323. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "RESTful API module - Mailboxes resources",
  324. .support_level = AST_MODULE_SUPPORT_CORE,
  325. .load = load_module,
  326. .unload = unload_module,
  327. );