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