res_ari_endpoints.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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 Endpoint 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_endpoints.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 /endpoints.
  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_endpoints_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_endpoints_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_endpoints_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_endpoint_fn());
  79. } else {
  80. ast_log(LOG_ERROR, "Invalid error response %d for /endpoints\n", code);
  81. is_valid = 0;
  82. }
  83. }
  84. if (!is_valid) {
  85. ast_log(LOG_ERROR, "Response validation failed for /endpoints\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. int ast_ari_endpoints_send_message_parse_body(
  94. struct ast_json *body,
  95. struct ast_ari_endpoints_send_message_args *args)
  96. {
  97. struct ast_json *field;
  98. /* Parse query parameters out of it */
  99. field = ast_json_object_get(body, "to");
  100. if (field) {
  101. args->to = ast_json_string_get(field);
  102. }
  103. field = ast_json_object_get(body, "from");
  104. if (field) {
  105. args->from = ast_json_string_get(field);
  106. }
  107. field = ast_json_object_get(body, "body");
  108. if (field) {
  109. args->body = ast_json_string_get(field);
  110. }
  111. return 0;
  112. }
  113. /*!
  114. * \brief Parameter parsing callback for /endpoints/sendMessage.
  115. * \param get_params GET parameters in the HTTP request.
  116. * \param path_vars Path variables extracted from the request.
  117. * \param headers HTTP headers.
  118. * \param[out] response Response to the HTTP request.
  119. */
  120. static void ast_ari_endpoints_send_message_cb(
  121. struct ast_tcptls_session_instance *ser,
  122. struct ast_variable *get_params, struct ast_variable *path_vars,
  123. struct ast_variable *headers, struct ast_ari_response *response)
  124. {
  125. struct ast_ari_endpoints_send_message_args args = {};
  126. struct ast_variable *i;
  127. RAII_VAR(struct ast_json *, body, NULL, ast_json_unref);
  128. #if defined(AST_DEVMODE)
  129. int is_valid;
  130. int code;
  131. #endif /* AST_DEVMODE */
  132. for (i = get_params; i; i = i->next) {
  133. if (strcmp(i->name, "to") == 0) {
  134. args.to = (i->value);
  135. } else
  136. if (strcmp(i->name, "from") == 0) {
  137. args.from = (i->value);
  138. } else
  139. if (strcmp(i->name, "body") == 0) {
  140. args.body = (i->value);
  141. } else
  142. {}
  143. }
  144. /* Look for a JSON request entity */
  145. body = ast_http_get_json(ser, headers);
  146. if (!body) {
  147. switch (errno) {
  148. case EFBIG:
  149. ast_ari_response_error(response, 413, "Request Entity Too Large", "Request body too large");
  150. goto fin;
  151. case ENOMEM:
  152. ast_ari_response_error(response, 500, "Internal Server Error", "Error processing request");
  153. goto fin;
  154. case EIO:
  155. ast_ari_response_error(response, 400, "Bad Request", "Error parsing request body");
  156. goto fin;
  157. }
  158. }
  159. args.variables = ast_json_ref(body);
  160. ast_ari_endpoints_send_message(headers, &args, response);
  161. #if defined(AST_DEVMODE)
  162. code = response->response_code;
  163. switch (code) {
  164. case 0: /* Implementation is still a stub, or the code wasn't set */
  165. is_valid = response->message == NULL;
  166. break;
  167. case 500: /* Internal Server Error */
  168. case 501: /* Not Implemented */
  169. case 404: /* Endpoint not found */
  170. is_valid = 1;
  171. break;
  172. default:
  173. if (200 <= code && code <= 299) {
  174. is_valid = ast_ari_validate_void(
  175. response->message);
  176. } else {
  177. ast_log(LOG_ERROR, "Invalid error response %d for /endpoints/sendMessage\n", code);
  178. is_valid = 0;
  179. }
  180. }
  181. if (!is_valid) {
  182. ast_log(LOG_ERROR, "Response validation failed for /endpoints/sendMessage\n");
  183. ast_ari_response_error(response, 500,
  184. "Internal Server Error", "Response validation failed");
  185. }
  186. #endif /* AST_DEVMODE */
  187. fin: __attribute__((unused))
  188. return;
  189. }
  190. /*!
  191. * \brief Parameter parsing callback for /endpoints/{tech}.
  192. * \param get_params GET parameters in the HTTP request.
  193. * \param path_vars Path variables extracted from the request.
  194. * \param headers HTTP headers.
  195. * \param[out] response Response to the HTTP request.
  196. */
  197. static void ast_ari_endpoints_list_by_tech_cb(
  198. struct ast_tcptls_session_instance *ser,
  199. struct ast_variable *get_params, struct ast_variable *path_vars,
  200. struct ast_variable *headers, struct ast_ari_response *response)
  201. {
  202. struct ast_ari_endpoints_list_by_tech_args args = {};
  203. struct ast_variable *i;
  204. RAII_VAR(struct ast_json *, body, NULL, ast_json_unref);
  205. #if defined(AST_DEVMODE)
  206. int is_valid;
  207. int code;
  208. #endif /* AST_DEVMODE */
  209. for (i = path_vars; i; i = i->next) {
  210. if (strcmp(i->name, "tech") == 0) {
  211. args.tech = (i->value);
  212. } else
  213. {}
  214. }
  215. ast_ari_endpoints_list_by_tech(headers, &args, response);
  216. #if defined(AST_DEVMODE)
  217. code = response->response_code;
  218. switch (code) {
  219. case 0: /* Implementation is still a stub, or the code wasn't set */
  220. is_valid = response->message == NULL;
  221. break;
  222. case 500: /* Internal Server Error */
  223. case 501: /* Not Implemented */
  224. case 404: /* Endpoints not found */
  225. is_valid = 1;
  226. break;
  227. default:
  228. if (200 <= code && code <= 299) {
  229. is_valid = ast_ari_validate_list(response->message,
  230. ast_ari_validate_endpoint_fn());
  231. } else {
  232. ast_log(LOG_ERROR, "Invalid error response %d for /endpoints/{tech}\n", code);
  233. is_valid = 0;
  234. }
  235. }
  236. if (!is_valid) {
  237. ast_log(LOG_ERROR, "Response validation failed for /endpoints/{tech}\n");
  238. ast_ari_response_error(response, 500,
  239. "Internal Server Error", "Response validation failed");
  240. }
  241. #endif /* AST_DEVMODE */
  242. fin: __attribute__((unused))
  243. return;
  244. }
  245. /*!
  246. * \brief Parameter parsing callback for /endpoints/{tech}/{resource}.
  247. * \param get_params GET parameters in the HTTP request.
  248. * \param path_vars Path variables extracted from the request.
  249. * \param headers HTTP headers.
  250. * \param[out] response Response to the HTTP request.
  251. */
  252. static void ast_ari_endpoints_get_cb(
  253. struct ast_tcptls_session_instance *ser,
  254. struct ast_variable *get_params, struct ast_variable *path_vars,
  255. struct ast_variable *headers, struct ast_ari_response *response)
  256. {
  257. struct ast_ari_endpoints_get_args args = {};
  258. struct ast_variable *i;
  259. RAII_VAR(struct ast_json *, body, NULL, ast_json_unref);
  260. #if defined(AST_DEVMODE)
  261. int is_valid;
  262. int code;
  263. #endif /* AST_DEVMODE */
  264. for (i = path_vars; i; i = i->next) {
  265. if (strcmp(i->name, "tech") == 0) {
  266. args.tech = (i->value);
  267. } else
  268. if (strcmp(i->name, "resource") == 0) {
  269. args.resource = (i->value);
  270. } else
  271. {}
  272. }
  273. ast_ari_endpoints_get(headers, &args, response);
  274. #if defined(AST_DEVMODE)
  275. code = response->response_code;
  276. switch (code) {
  277. case 0: /* Implementation is still a stub, or the code wasn't set */
  278. is_valid = response->message == NULL;
  279. break;
  280. case 500: /* Internal Server Error */
  281. case 501: /* Not Implemented */
  282. case 400: /* Invalid parameters for sending a message. */
  283. case 404: /* Endpoints not found */
  284. is_valid = 1;
  285. break;
  286. default:
  287. if (200 <= code && code <= 299) {
  288. is_valid = ast_ari_validate_endpoint(
  289. response->message);
  290. } else {
  291. ast_log(LOG_ERROR, "Invalid error response %d for /endpoints/{tech}/{resource}\n", code);
  292. is_valid = 0;
  293. }
  294. }
  295. if (!is_valid) {
  296. ast_log(LOG_ERROR, "Response validation failed for /endpoints/{tech}/{resource}\n");
  297. ast_ari_response_error(response, 500,
  298. "Internal Server Error", "Response validation failed");
  299. }
  300. #endif /* AST_DEVMODE */
  301. fin: __attribute__((unused))
  302. return;
  303. }
  304. int ast_ari_endpoints_send_message_to_endpoint_parse_body(
  305. struct ast_json *body,
  306. struct ast_ari_endpoints_send_message_to_endpoint_args *args)
  307. {
  308. struct ast_json *field;
  309. /* Parse query parameters out of it */
  310. field = ast_json_object_get(body, "from");
  311. if (field) {
  312. args->from = ast_json_string_get(field);
  313. }
  314. field = ast_json_object_get(body, "body");
  315. if (field) {
  316. args->body = ast_json_string_get(field);
  317. }
  318. return 0;
  319. }
  320. /*!
  321. * \brief Parameter parsing callback for /endpoints/{tech}/{resource}/sendMessage.
  322. * \param get_params GET parameters in the HTTP request.
  323. * \param path_vars Path variables extracted from the request.
  324. * \param headers HTTP headers.
  325. * \param[out] response Response to the HTTP request.
  326. */
  327. static void ast_ari_endpoints_send_message_to_endpoint_cb(
  328. struct ast_tcptls_session_instance *ser,
  329. struct ast_variable *get_params, struct ast_variable *path_vars,
  330. struct ast_variable *headers, struct ast_ari_response *response)
  331. {
  332. struct ast_ari_endpoints_send_message_to_endpoint_args args = {};
  333. struct ast_variable *i;
  334. RAII_VAR(struct ast_json *, body, NULL, ast_json_unref);
  335. #if defined(AST_DEVMODE)
  336. int is_valid;
  337. int code;
  338. #endif /* AST_DEVMODE */
  339. for (i = get_params; i; i = i->next) {
  340. if (strcmp(i->name, "from") == 0) {
  341. args.from = (i->value);
  342. } else
  343. if (strcmp(i->name, "body") == 0) {
  344. args.body = (i->value);
  345. } else
  346. {}
  347. }
  348. for (i = path_vars; i; i = i->next) {
  349. if (strcmp(i->name, "tech") == 0) {
  350. args.tech = (i->value);
  351. } else
  352. if (strcmp(i->name, "resource") == 0) {
  353. args.resource = (i->value);
  354. } else
  355. {}
  356. }
  357. /* Look for a JSON request entity */
  358. body = ast_http_get_json(ser, headers);
  359. if (!body) {
  360. switch (errno) {
  361. case EFBIG:
  362. ast_ari_response_error(response, 413, "Request Entity Too Large", "Request body too large");
  363. goto fin;
  364. case ENOMEM:
  365. ast_ari_response_error(response, 500, "Internal Server Error", "Error processing request");
  366. goto fin;
  367. case EIO:
  368. ast_ari_response_error(response, 400, "Bad Request", "Error parsing request body");
  369. goto fin;
  370. }
  371. }
  372. args.variables = ast_json_ref(body);
  373. ast_ari_endpoints_send_message_to_endpoint(headers, &args, response);
  374. #if defined(AST_DEVMODE)
  375. code = response->response_code;
  376. switch (code) {
  377. case 0: /* Implementation is still a stub, or the code wasn't set */
  378. is_valid = response->message == NULL;
  379. break;
  380. case 500: /* Internal Server Error */
  381. case 501: /* Not Implemented */
  382. case 400: /* Invalid parameters for sending a message. */
  383. case 404: /* Endpoint not found */
  384. is_valid = 1;
  385. break;
  386. default:
  387. if (200 <= code && code <= 299) {
  388. is_valid = ast_ari_validate_void(
  389. response->message);
  390. } else {
  391. ast_log(LOG_ERROR, "Invalid error response %d for /endpoints/{tech}/{resource}/sendMessage\n", code);
  392. is_valid = 0;
  393. }
  394. }
  395. if (!is_valid) {
  396. ast_log(LOG_ERROR, "Response validation failed for /endpoints/{tech}/{resource}/sendMessage\n");
  397. ast_ari_response_error(response, 500,
  398. "Internal Server Error", "Response validation failed");
  399. }
  400. #endif /* AST_DEVMODE */
  401. fin: __attribute__((unused))
  402. return;
  403. }
  404. /*! \brief REST handler for /api-docs/endpoints.{format} */
  405. static struct stasis_rest_handlers endpoints_sendMessage = {
  406. .path_segment = "sendMessage",
  407. .callbacks = {
  408. [AST_HTTP_PUT] = ast_ari_endpoints_send_message_cb,
  409. },
  410. .num_children = 0,
  411. .children = { }
  412. };
  413. /*! \brief REST handler for /api-docs/endpoints.{format} */
  414. static struct stasis_rest_handlers endpoints_tech_resource_sendMessage = {
  415. .path_segment = "sendMessage",
  416. .callbacks = {
  417. [AST_HTTP_PUT] = ast_ari_endpoints_send_message_to_endpoint_cb,
  418. },
  419. .num_children = 0,
  420. .children = { }
  421. };
  422. /*! \brief REST handler for /api-docs/endpoints.{format} */
  423. static struct stasis_rest_handlers endpoints_tech_resource = {
  424. .path_segment = "resource",
  425. .is_wildcard = 1,
  426. .callbacks = {
  427. [AST_HTTP_GET] = ast_ari_endpoints_get_cb,
  428. },
  429. .num_children = 1,
  430. .children = { &endpoints_tech_resource_sendMessage, }
  431. };
  432. /*! \brief REST handler for /api-docs/endpoints.{format} */
  433. static struct stasis_rest_handlers endpoints_tech = {
  434. .path_segment = "tech",
  435. .is_wildcard = 1,
  436. .callbacks = {
  437. [AST_HTTP_GET] = ast_ari_endpoints_list_by_tech_cb,
  438. },
  439. .num_children = 1,
  440. .children = { &endpoints_tech_resource, }
  441. };
  442. /*! \brief REST handler for /api-docs/endpoints.{format} */
  443. static struct stasis_rest_handlers endpoints = {
  444. .path_segment = "endpoints",
  445. .callbacks = {
  446. [AST_HTTP_GET] = ast_ari_endpoints_list_cb,
  447. },
  448. .num_children = 2,
  449. .children = { &endpoints_sendMessage,&endpoints_tech, }
  450. };
  451. static int load_module(void)
  452. {
  453. int res = 0;
  454. stasis_app_ref();
  455. res |= ast_ari_add_handler(&endpoints);
  456. return res;
  457. }
  458. static int unload_module(void)
  459. {
  460. ast_ari_remove_handler(&endpoints);
  461. stasis_app_unref();
  462. return 0;
  463. }
  464. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "RESTful API module - Endpoint resources",
  465. .support_level = AST_MODULE_SUPPORT_CORE,
  466. .load = load_module,
  467. .unload = unload_module,
  468. .nonoptreq = "res_ari,res_stasis",
  469. );