resource_applications.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. /*! \file
  19. *
  20. * \brief /api-docs/applications.{format} implementation - Stasis application
  21. * resources
  22. *
  23. * \author David M. Lee, II <dlee@digium.com>
  24. */
  25. #include "asterisk.h"
  26. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  27. #include "asterisk/stasis_app.h"
  28. #include "resource_applications.h"
  29. static int append_json(void *obj, void *arg, int flags)
  30. {
  31. const char *app = obj;
  32. struct ast_json *array = arg;
  33. ast_json_array_append(array, stasis_app_to_json(app));
  34. return 0;
  35. }
  36. void ast_ari_applications_list(struct ast_variable *headers,
  37. struct ast_ari_applications_list_args *args,
  38. struct ast_ari_response *response)
  39. {
  40. RAII_VAR(struct ao2_container *, apps, NULL, ao2_cleanup);
  41. RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
  42. size_t count;
  43. apps = stasis_app_get_all();
  44. json = ast_json_array_create();
  45. if (!apps || !json) {
  46. ast_ari_response_error(response, 500, "Internal Server Error",
  47. "Allocation failed");
  48. return;
  49. }
  50. ao2_lock(apps);
  51. count = ao2_container_count(apps);
  52. ao2_callback(apps, OBJ_NOLOCK | OBJ_NODATA, append_json, json);
  53. ao2_lock(apps);
  54. if (count != ast_json_array_size(json)) {
  55. ast_ari_response_error(response, 500, "Internal Server Error",
  56. "Allocation failed");
  57. return;
  58. }
  59. ast_ari_response_ok(response, ast_json_ref(json));
  60. }
  61. void ast_ari_applications_get(struct ast_variable *headers,
  62. struct ast_ari_applications_get_args *args,
  63. struct ast_ari_response *response)
  64. {
  65. struct ast_json *json;
  66. json = stasis_app_to_json(args->application_name);
  67. if (!json) {
  68. ast_ari_response_error(response, 404, "Not Found",
  69. "Application not found");
  70. return;
  71. }
  72. ast_ari_response_ok(response, json);
  73. }
  74. void ast_ari_applications_subscribe(struct ast_variable *headers,
  75. struct ast_ari_applications_subscribe_args *args,
  76. struct ast_ari_response *response)
  77. {
  78. RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
  79. enum stasis_app_subscribe_res res;
  80. if (args->event_source_count <= 0) {
  81. ast_ari_response_error(response, 400, "Bad Request",
  82. "Missing parameter eventSource");
  83. return;
  84. }
  85. if (ast_strlen_zero(args->application_name)) {
  86. ast_ari_response_error(response, 400, "Bad Request",
  87. "Missing parameter applicationName");
  88. return;
  89. }
  90. res = stasis_app_subscribe(args->application_name, args->event_source,
  91. args->event_source_count, &json);
  92. switch (res) {
  93. case STASIS_ASR_OK:
  94. ast_ari_response_ok(response, ast_json_ref(json));
  95. break;
  96. case STASIS_ASR_APP_NOT_FOUND:
  97. ast_ari_response_error(response, 404, "Not Found",
  98. "Application not found");
  99. break;
  100. case STASIS_ASR_EVENT_SOURCE_NOT_FOUND:
  101. ast_ari_response_error(response, 422, "Unprocessable Entity",
  102. "Event source does not exist");
  103. break;
  104. case STASIS_ASR_EVENT_SOURCE_BAD_SCHEME:
  105. ast_ari_response_error(response, 400, "Bad Request",
  106. "Invalid event source URI scheme");
  107. break;
  108. case STASIS_ASR_INTERNAL_ERROR:
  109. ast_ari_response_error(response, 500, "Internal Server Error",
  110. "Error processing request");
  111. break;
  112. }
  113. }
  114. void ast_ari_applications_unsubscribe(struct ast_variable *headers,
  115. struct ast_ari_applications_unsubscribe_args *args,
  116. struct ast_ari_response *response)
  117. {
  118. RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
  119. enum stasis_app_subscribe_res res;
  120. if (args->event_source_count == 0) {
  121. ast_ari_response_error(response, 400, "Bad Request",
  122. "Missing parameter eventSource");
  123. return;
  124. }
  125. res = stasis_app_unsubscribe(args->application_name, args->event_source,
  126. args->event_source_count, &json);
  127. switch (res) {
  128. case STASIS_ASR_OK:
  129. ast_ari_response_ok(response, ast_json_ref(json));
  130. break;
  131. case STASIS_ASR_APP_NOT_FOUND:
  132. ast_ari_response_error(response, 404, "Not Found",
  133. "Application not found");
  134. break;
  135. case STASIS_ASR_EVENT_SOURCE_NOT_FOUND:
  136. ast_ari_response_error(response, 422, "Unprocessable Entity",
  137. "Event source was not subscribed to");
  138. break;
  139. case STASIS_ASR_EVENT_SOURCE_BAD_SCHEME:
  140. ast_ari_response_error(response, 400, "Bad Request",
  141. "Invalid event source URI scheme");
  142. break;
  143. case STASIS_ASR_INTERNAL_ERROR:
  144. ast_ari_response_error(response, 500, "Internal Server Error",
  145. "Error processing request");
  146. }
  147. }