res_ari_mailboxes.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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_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_mailboxes.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 /mailboxes.
  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_mailboxes_list_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_mailboxes_list_args args = {};
  59. RAII_VAR(struct ast_json *, body, NULL, ast_json_unref);
  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_ari_response *response)
  104. {
  105. struct ast_ari_mailboxes_get_args args = {};
  106. struct ast_variable *i;
  107. RAII_VAR(struct ast_json *, body, NULL, ast_json_unref);
  108. #if defined(AST_DEVMODE)
  109. int is_valid;
  110. int code;
  111. #endif /* AST_DEVMODE */
  112. for (i = path_vars; i; i = i->next) {
  113. if (strcmp(i->name, "mailboxName") == 0) {
  114. args.mailbox_name = (i->value);
  115. } else
  116. {}
  117. }
  118. ast_ari_mailboxes_get(headers, &args, response);
  119. #if defined(AST_DEVMODE)
  120. code = response->response_code;
  121. switch (code) {
  122. case 0: /* Implementation is still a stub, or the code wasn't set */
  123. is_valid = response->message == NULL;
  124. break;
  125. case 500: /* Internal Server Error */
  126. case 501: /* Not Implemented */
  127. case 404: /* Mailbox not found */
  128. is_valid = 1;
  129. break;
  130. default:
  131. if (200 <= code && code <= 299) {
  132. is_valid = ast_ari_validate_mailbox(
  133. response->message);
  134. } else {
  135. ast_log(LOG_ERROR, "Invalid error response %d for /mailboxes/{mailboxName}\n", code);
  136. is_valid = 0;
  137. }
  138. }
  139. if (!is_valid) {
  140. ast_log(LOG_ERROR, "Response validation failed for /mailboxes/{mailboxName}\n");
  141. ast_ari_response_error(response, 500,
  142. "Internal Server Error", "Response validation failed");
  143. }
  144. #endif /* AST_DEVMODE */
  145. fin: __attribute__((unused))
  146. return;
  147. }
  148. int ast_ari_mailboxes_update_parse_body(
  149. struct ast_json *body,
  150. struct ast_ari_mailboxes_update_args *args)
  151. {
  152. struct ast_json *field;
  153. /* Parse query parameters out of it */
  154. field = ast_json_object_get(body, "oldMessages");
  155. if (field) {
  156. args->old_messages = ast_json_integer_get(field);
  157. }
  158. field = ast_json_object_get(body, "newMessages");
  159. if (field) {
  160. args->new_messages = ast_json_integer_get(field);
  161. }
  162. return 0;
  163. }
  164. /*!
  165. * \brief Parameter parsing callback for /mailboxes/{mailboxName}.
  166. * \param get_params GET parameters in the HTTP request.
  167. * \param path_vars Path variables extracted from the request.
  168. * \param headers HTTP headers.
  169. * \param[out] response Response to the HTTP request.
  170. */
  171. static void ast_ari_mailboxes_update_cb(
  172. struct ast_tcptls_session_instance *ser,
  173. struct ast_variable *get_params, struct ast_variable *path_vars,
  174. struct ast_variable *headers, struct ast_ari_response *response)
  175. {
  176. struct ast_ari_mailboxes_update_args args = {};
  177. struct ast_variable *i;
  178. RAII_VAR(struct ast_json *, body, NULL, ast_json_unref);
  179. #if defined(AST_DEVMODE)
  180. int is_valid;
  181. int code;
  182. #endif /* AST_DEVMODE */
  183. for (i = get_params; i; i = i->next) {
  184. if (strcmp(i->name, "oldMessages") == 0) {
  185. args.old_messages = atoi(i->value);
  186. } else
  187. if (strcmp(i->name, "newMessages") == 0) {
  188. args.new_messages = atoi(i->value);
  189. } else
  190. {}
  191. }
  192. for (i = path_vars; i; i = i->next) {
  193. if (strcmp(i->name, "mailboxName") == 0) {
  194. args.mailbox_name = (i->value);
  195. } else
  196. {}
  197. }
  198. /* Look for a JSON request entity */
  199. body = ast_http_get_json(ser, headers);
  200. if (!body) {
  201. switch (errno) {
  202. case EFBIG:
  203. ast_ari_response_error(response, 413, "Request Entity Too Large", "Request body too large");
  204. goto fin;
  205. case ENOMEM:
  206. ast_ari_response_error(response, 500, "Internal Server Error", "Error processing request");
  207. goto fin;
  208. case EIO:
  209. ast_ari_response_error(response, 400, "Bad Request", "Error parsing request body");
  210. goto fin;
  211. }
  212. }
  213. if (ast_ari_mailboxes_update_parse_body(body, &args)) {
  214. ast_ari_response_alloc_failed(response);
  215. goto fin;
  216. }
  217. ast_ari_mailboxes_update(headers, &args, response);
  218. #if defined(AST_DEVMODE)
  219. code = response->response_code;
  220. switch (code) {
  221. case 0: /* Implementation is still a stub, or the code wasn't set */
  222. is_valid = response->message == NULL;
  223. break;
  224. case 500: /* Internal Server Error */
  225. case 501: /* Not Implemented */
  226. case 404: /* Mailbox not found */
  227. is_valid = 1;
  228. break;
  229. default:
  230. if (200 <= code && code <= 299) {
  231. is_valid = ast_ari_validate_void(
  232. response->message);
  233. } else {
  234. ast_log(LOG_ERROR, "Invalid error response %d for /mailboxes/{mailboxName}\n", code);
  235. is_valid = 0;
  236. }
  237. }
  238. if (!is_valid) {
  239. ast_log(LOG_ERROR, "Response validation failed for /mailboxes/{mailboxName}\n");
  240. ast_ari_response_error(response, 500,
  241. "Internal Server Error", "Response validation failed");
  242. }
  243. #endif /* AST_DEVMODE */
  244. fin: __attribute__((unused))
  245. return;
  246. }
  247. /*!
  248. * \brief Parameter parsing callback for /mailboxes/{mailboxName}.
  249. * \param get_params GET parameters in the HTTP request.
  250. * \param path_vars Path variables extracted from the request.
  251. * \param headers HTTP headers.
  252. * \param[out] response Response to the HTTP request.
  253. */
  254. static void ast_ari_mailboxes_delete_cb(
  255. struct ast_tcptls_session_instance *ser,
  256. struct ast_variable *get_params, struct ast_variable *path_vars,
  257. struct ast_variable *headers, struct ast_ari_response *response)
  258. {
  259. struct ast_ari_mailboxes_delete_args args = {};
  260. struct ast_variable *i;
  261. RAII_VAR(struct ast_json *, body, NULL, ast_json_unref);
  262. #if defined(AST_DEVMODE)
  263. int is_valid;
  264. int code;
  265. #endif /* AST_DEVMODE */
  266. for (i = path_vars; i; i = i->next) {
  267. if (strcmp(i->name, "mailboxName") == 0) {
  268. args.mailbox_name = (i->value);
  269. } else
  270. {}
  271. }
  272. ast_ari_mailboxes_delete(headers, &args, response);
  273. #if defined(AST_DEVMODE)
  274. code = response->response_code;
  275. switch (code) {
  276. case 0: /* Implementation is still a stub, or the code wasn't set */
  277. is_valid = response->message == NULL;
  278. break;
  279. case 500: /* Internal Server Error */
  280. case 501: /* Not Implemented */
  281. case 404: /* Mailbox not found */
  282. is_valid = 1;
  283. break;
  284. default:
  285. if (200 <= code && code <= 299) {
  286. is_valid = ast_ari_validate_void(
  287. response->message);
  288. } else {
  289. ast_log(LOG_ERROR, "Invalid error response %d for /mailboxes/{mailboxName}\n", code);
  290. is_valid = 0;
  291. }
  292. }
  293. if (!is_valid) {
  294. ast_log(LOG_ERROR, "Response validation failed for /mailboxes/{mailboxName}\n");
  295. ast_ari_response_error(response, 500,
  296. "Internal Server Error", "Response validation failed");
  297. }
  298. #endif /* AST_DEVMODE */
  299. fin: __attribute__((unused))
  300. return;
  301. }
  302. /*! \brief REST handler for /api-docs/mailboxes.{format} */
  303. static struct stasis_rest_handlers mailboxes_mailboxName = {
  304. .path_segment = "mailboxName",
  305. .is_wildcard = 1,
  306. .callbacks = {
  307. [AST_HTTP_GET] = ast_ari_mailboxes_get_cb,
  308. [AST_HTTP_PUT] = ast_ari_mailboxes_update_cb,
  309. [AST_HTTP_DELETE] = ast_ari_mailboxes_delete_cb,
  310. },
  311. .num_children = 0,
  312. .children = { }
  313. };
  314. /*! \brief REST handler for /api-docs/mailboxes.{format} */
  315. static struct stasis_rest_handlers mailboxes = {
  316. .path_segment = "mailboxes",
  317. .callbacks = {
  318. [AST_HTTP_GET] = ast_ari_mailboxes_list_cb,
  319. },
  320. .num_children = 1,
  321. .children = { &mailboxes_mailboxName, }
  322. };
  323. static int load_module(void)
  324. {
  325. int res = 0;
  326. stasis_app_ref();
  327. res |= ast_ari_add_handler(&mailboxes);
  328. return res;
  329. }
  330. static int unload_module(void)
  331. {
  332. ast_ari_remove_handler(&mailboxes);
  333. stasis_app_unref();
  334. return 0;
  335. }
  336. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "RESTful API module - Mailboxes resources",
  337. .support_level = AST_MODULE_SUPPORT_CORE,
  338. .load = load_module,
  339. .unload = unload_module,
  340. .nonoptreq = "res_ari,res_stasis",
  341. );