res_ari_applications.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 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 Stasis application 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_applications.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 /applications.
  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_applications_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_applications_list_args args = {};
  60. #if defined(AST_DEVMODE)
  61. int is_valid;
  62. int code;
  63. #endif /* AST_DEVMODE */
  64. ast_ari_applications_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_application_fn());
  79. } else {
  80. ast_log(LOG_ERROR, "Invalid error response %d for /applications\n", code);
  81. is_valid = 0;
  82. }
  83. }
  84. if (!is_valid) {
  85. ast_log(LOG_ERROR, "Response validation failed for /applications\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 /applications/{applicationName}.
  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_applications_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_applications_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, "applicationName") == 0) {
  113. args.application_name = (i->value);
  114. } else
  115. {}
  116. }
  117. ast_ari_applications_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: /* Application does not exist. */
  127. is_valid = 1;
  128. break;
  129. default:
  130. if (200 <= code && code <= 299) {
  131. is_valid = ast_ari_validate_application(
  132. response->message);
  133. } else {
  134. ast_log(LOG_ERROR, "Invalid error response %d for /applications/{applicationName}\n", code);
  135. is_valid = 0;
  136. }
  137. }
  138. if (!is_valid) {
  139. ast_log(LOG_ERROR, "Response validation failed for /applications/{applicationName}\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_applications_subscribe_parse_body(
  148. struct ast_json *body,
  149. struct ast_ari_applications_subscribe_args *args)
  150. {
  151. struct ast_json *field;
  152. /* Parse query parameters out of it */
  153. field = ast_json_object_get(body, "eventSource");
  154. if (field) {
  155. /* If they were silly enough to both pass in a query param and a
  156. * JSON body, free up the query value.
  157. */
  158. ast_free(args->event_source);
  159. if (ast_json_typeof(field) == AST_JSON_ARRAY) {
  160. /* Multiple param passed as array */
  161. size_t i;
  162. args->event_source_count = ast_json_array_size(field);
  163. args->event_source = ast_malloc(sizeof(*args->event_source) * args->event_source_count);
  164. if (!args->event_source) {
  165. return -1;
  166. }
  167. for (i = 0; i < args->event_source_count; ++i) {
  168. args->event_source[i] = ast_json_string_get(ast_json_array_get(field, i));
  169. }
  170. } else {
  171. /* Multiple param passed as single value */
  172. args->event_source_count = 1;
  173. args->event_source = ast_malloc(sizeof(*args->event_source) * args->event_source_count);
  174. if (!args->event_source) {
  175. return -1;
  176. }
  177. args->event_source[0] = ast_json_string_get(field);
  178. }
  179. }
  180. return 0;
  181. }
  182. /*!
  183. * \brief Parameter parsing callback for /applications/{applicationName}/subscription.
  184. * \param get_params GET parameters in the HTTP request.
  185. * \param path_vars Path variables extracted from the request.
  186. * \param headers HTTP headers.
  187. * \param[out] response Response to the HTTP request.
  188. */
  189. static void ast_ari_applications_subscribe_cb(
  190. struct ast_tcptls_session_instance *ser,
  191. struct ast_variable *get_params, struct ast_variable *path_vars,
  192. struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
  193. {
  194. struct ast_ari_applications_subscribe_args args = {};
  195. struct ast_variable *i;
  196. #if defined(AST_DEVMODE)
  197. int is_valid;
  198. int code;
  199. #endif /* AST_DEVMODE */
  200. for (i = get_params; i; i = i->next) {
  201. if (strcmp(i->name, "eventSource") == 0) {
  202. /* Parse comma separated list */
  203. char *vals[MAX_VALS];
  204. size_t j;
  205. args.event_source_parse = ast_strdup(i->value);
  206. if (!args.event_source_parse) {
  207. ast_ari_response_alloc_failed(response);
  208. goto fin;
  209. }
  210. if (strlen(args.event_source_parse) == 0) {
  211. /* ast_app_separate_args can't handle "" */
  212. args.event_source_count = 1;
  213. vals[0] = args.event_source_parse;
  214. } else {
  215. args.event_source_count = ast_app_separate_args(
  216. args.event_source_parse, ',', vals,
  217. ARRAY_LEN(vals));
  218. }
  219. if (args.event_source_count == 0) {
  220. ast_ari_response_alloc_failed(response);
  221. goto fin;
  222. }
  223. if (args.event_source_count >= MAX_VALS) {
  224. ast_ari_response_error(response, 400,
  225. "Bad Request",
  226. "Too many values for event_source");
  227. goto fin;
  228. }
  229. args.event_source = ast_malloc(sizeof(*args.event_source) * args.event_source_count);
  230. if (!args.event_source) {
  231. ast_ari_response_alloc_failed(response);
  232. goto fin;
  233. }
  234. for (j = 0; j < args.event_source_count; ++j) {
  235. args.event_source[j] = (vals[j]);
  236. }
  237. } else
  238. {}
  239. }
  240. for (i = path_vars; i; i = i->next) {
  241. if (strcmp(i->name, "applicationName") == 0) {
  242. args.application_name = (i->value);
  243. } else
  244. {}
  245. }
  246. if (ast_ari_applications_subscribe_parse_body(body, &args)) {
  247. ast_ari_response_alloc_failed(response);
  248. goto fin;
  249. }
  250. ast_ari_applications_subscribe(headers, &args, response);
  251. #if defined(AST_DEVMODE)
  252. code = response->response_code;
  253. switch (code) {
  254. case 0: /* Implementation is still a stub, or the code wasn't set */
  255. is_valid = response->message == NULL;
  256. break;
  257. case 500: /* Internal Server Error */
  258. case 501: /* Not Implemented */
  259. case 400: /* Missing parameter. */
  260. case 404: /* Application does not exist. */
  261. case 422: /* Event source does not exist. */
  262. is_valid = 1;
  263. break;
  264. default:
  265. if (200 <= code && code <= 299) {
  266. is_valid = ast_ari_validate_application(
  267. response->message);
  268. } else {
  269. ast_log(LOG_ERROR, "Invalid error response %d for /applications/{applicationName}/subscription\n", code);
  270. is_valid = 0;
  271. }
  272. }
  273. if (!is_valid) {
  274. ast_log(LOG_ERROR, "Response validation failed for /applications/{applicationName}/subscription\n");
  275. ast_ari_response_error(response, 500,
  276. "Internal Server Error", "Response validation failed");
  277. }
  278. #endif /* AST_DEVMODE */
  279. fin: __attribute__((unused))
  280. ast_free(args.event_source_parse);
  281. ast_free(args.event_source);
  282. return;
  283. }
  284. int ast_ari_applications_unsubscribe_parse_body(
  285. struct ast_json *body,
  286. struct ast_ari_applications_unsubscribe_args *args)
  287. {
  288. struct ast_json *field;
  289. /* Parse query parameters out of it */
  290. field = ast_json_object_get(body, "eventSource");
  291. if (field) {
  292. /* If they were silly enough to both pass in a query param and a
  293. * JSON body, free up the query value.
  294. */
  295. ast_free(args->event_source);
  296. if (ast_json_typeof(field) == AST_JSON_ARRAY) {
  297. /* Multiple param passed as array */
  298. size_t i;
  299. args->event_source_count = ast_json_array_size(field);
  300. args->event_source = ast_malloc(sizeof(*args->event_source) * args->event_source_count);
  301. if (!args->event_source) {
  302. return -1;
  303. }
  304. for (i = 0; i < args->event_source_count; ++i) {
  305. args->event_source[i] = ast_json_string_get(ast_json_array_get(field, i));
  306. }
  307. } else {
  308. /* Multiple param passed as single value */
  309. args->event_source_count = 1;
  310. args->event_source = ast_malloc(sizeof(*args->event_source) * args->event_source_count);
  311. if (!args->event_source) {
  312. return -1;
  313. }
  314. args->event_source[0] = ast_json_string_get(field);
  315. }
  316. }
  317. return 0;
  318. }
  319. /*!
  320. * \brief Parameter parsing callback for /applications/{applicationName}/subscription.
  321. * \param get_params GET parameters in the HTTP request.
  322. * \param path_vars Path variables extracted from the request.
  323. * \param headers HTTP headers.
  324. * \param[out] response Response to the HTTP request.
  325. */
  326. static void ast_ari_applications_unsubscribe_cb(
  327. struct ast_tcptls_session_instance *ser,
  328. struct ast_variable *get_params, struct ast_variable *path_vars,
  329. struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
  330. {
  331. struct ast_ari_applications_unsubscribe_args args = {};
  332. struct ast_variable *i;
  333. #if defined(AST_DEVMODE)
  334. int is_valid;
  335. int code;
  336. #endif /* AST_DEVMODE */
  337. for (i = get_params; i; i = i->next) {
  338. if (strcmp(i->name, "eventSource") == 0) {
  339. /* Parse comma separated list */
  340. char *vals[MAX_VALS];
  341. size_t j;
  342. args.event_source_parse = ast_strdup(i->value);
  343. if (!args.event_source_parse) {
  344. ast_ari_response_alloc_failed(response);
  345. goto fin;
  346. }
  347. if (strlen(args.event_source_parse) == 0) {
  348. /* ast_app_separate_args can't handle "" */
  349. args.event_source_count = 1;
  350. vals[0] = args.event_source_parse;
  351. } else {
  352. args.event_source_count = ast_app_separate_args(
  353. args.event_source_parse, ',', vals,
  354. ARRAY_LEN(vals));
  355. }
  356. if (args.event_source_count == 0) {
  357. ast_ari_response_alloc_failed(response);
  358. goto fin;
  359. }
  360. if (args.event_source_count >= MAX_VALS) {
  361. ast_ari_response_error(response, 400,
  362. "Bad Request",
  363. "Too many values for event_source");
  364. goto fin;
  365. }
  366. args.event_source = ast_malloc(sizeof(*args.event_source) * args.event_source_count);
  367. if (!args.event_source) {
  368. ast_ari_response_alloc_failed(response);
  369. goto fin;
  370. }
  371. for (j = 0; j < args.event_source_count; ++j) {
  372. args.event_source[j] = (vals[j]);
  373. }
  374. } else
  375. {}
  376. }
  377. for (i = path_vars; i; i = i->next) {
  378. if (strcmp(i->name, "applicationName") == 0) {
  379. args.application_name = (i->value);
  380. } else
  381. {}
  382. }
  383. if (ast_ari_applications_unsubscribe_parse_body(body, &args)) {
  384. ast_ari_response_alloc_failed(response);
  385. goto fin;
  386. }
  387. ast_ari_applications_unsubscribe(headers, &args, response);
  388. #if defined(AST_DEVMODE)
  389. code = response->response_code;
  390. switch (code) {
  391. case 0: /* Implementation is still a stub, or the code wasn't set */
  392. is_valid = response->message == NULL;
  393. break;
  394. case 500: /* Internal Server Error */
  395. case 501: /* Not Implemented */
  396. case 400: /* Missing parameter; event source scheme not recognized. */
  397. case 404: /* Application does not exist. */
  398. case 409: /* Application not subscribed to event source. */
  399. case 422: /* Event source does not exist. */
  400. is_valid = 1;
  401. break;
  402. default:
  403. if (200 <= code && code <= 299) {
  404. is_valid = ast_ari_validate_application(
  405. response->message);
  406. } else {
  407. ast_log(LOG_ERROR, "Invalid error response %d for /applications/{applicationName}/subscription\n", code);
  408. is_valid = 0;
  409. }
  410. }
  411. if (!is_valid) {
  412. ast_log(LOG_ERROR, "Response validation failed for /applications/{applicationName}/subscription\n");
  413. ast_ari_response_error(response, 500,
  414. "Internal Server Error", "Response validation failed");
  415. }
  416. #endif /* AST_DEVMODE */
  417. fin: __attribute__((unused))
  418. ast_free(args.event_source_parse);
  419. ast_free(args.event_source);
  420. return;
  421. }
  422. /*! \brief REST handler for /api-docs/applications.json */
  423. static struct stasis_rest_handlers applications_applicationName_subscription = {
  424. .path_segment = "subscription",
  425. .callbacks = {
  426. [AST_HTTP_POST] = ast_ari_applications_subscribe_cb,
  427. [AST_HTTP_DELETE] = ast_ari_applications_unsubscribe_cb,
  428. },
  429. .num_children = 0,
  430. .children = { }
  431. };
  432. /*! \brief REST handler for /api-docs/applications.json */
  433. static struct stasis_rest_handlers applications_applicationName = {
  434. .path_segment = "applicationName",
  435. .is_wildcard = 1,
  436. .callbacks = {
  437. [AST_HTTP_GET] = ast_ari_applications_get_cb,
  438. },
  439. .num_children = 1,
  440. .children = { &applications_applicationName_subscription, }
  441. };
  442. /*! \brief REST handler for /api-docs/applications.json */
  443. static struct stasis_rest_handlers applications = {
  444. .path_segment = "applications",
  445. .callbacks = {
  446. [AST_HTTP_GET] = ast_ari_applications_list_cb,
  447. },
  448. .num_children = 1,
  449. .children = { &applications_applicationName, }
  450. };
  451. static int unload_module(void)
  452. {
  453. ast_ari_remove_handler(&applications);
  454. stasis_app_unref();
  455. return 0;
  456. }
  457. static int load_module(void)
  458. {
  459. int res = 0;
  460. CHECK_ARI_MODULE_LOADED();
  461. stasis_app_ref();
  462. res |= ast_ari_add_handler(&applications);
  463. if (res) {
  464. unload_module();
  465. return AST_MODULE_LOAD_DECLINE;
  466. }
  467. return AST_MODULE_LOAD_SUCCESS;
  468. }
  469. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "RESTful API module - Stasis application resources",
  470. .support_level = AST_MODULE_SUPPORT_CORE,
  471. .load = load_module,
  472. .unload = unload_module,
  473. );