Debian Codegraph @

res/ari/resource_applications.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2013, Digium, Inc.
5 *
6 * David M. Lee, II <[email protected]>
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 <[email protected]>
25 */
26
27 #include "asterisk.h"
28
29 #include "asterisk/stasis_app.h"
30 #include "resource_applications.h"
31
32 static int append_json(void *obj, void *arg, int flags)
33 {
34 const char *app = obj;
35 struct ast_json *array = arg;
36
37 ast_json_array_append(array, stasis_app_to_json(app));
38
39 return 0;
40 }
41
42 void ast_ari_applications_list(struct ast_variable *headers,
43 struct ast_ari_applications_list_args *args,
44 struct ast_ari_response *response)
45 {
46 RAII_VAR(struct ao2_container *, apps, NULL, ao2_cleanup);
47 RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
48 size_t count;
49
50 apps = stasis_app_get_all();
51 json = ast_json_array_create();
52 if (!apps || !json) {
53 ast_ari_response_error(response, 500, "Internal Server Error",
54 "Allocation failed");
55 return;
56 }
57
58 ao2_lock(apps);
59 count = ao2_container_count(apps);
60 ao2_callback(apps, OBJ_NOLOCK | OBJ_NODATA, append_json, json);
61 ao2_unlock(apps);
62
63 if (count != ast_json_array_size(json)) {
64 ast_ari_response_error(response, 500, "Internal Server Error",
65 "Allocation failed");
66 return;
67 }
68
69 ast_ari_response_ok(response, ast_json_ref(json));
70 }
71
72 void ast_ari_applications_get(struct ast_variable *headers,
73 struct ast_ari_applications_get_args *args,
74 struct ast_ari_response *response)
75 {
76 struct ast_json *json;
77
78 json = stasis_app_to_json(args->application_name);
79
80 if (!json) {
81 ast_ari_response_error(response, 404, "Not Found",
82 "Application not found");
83 return;
84 }
85
86 ast_ari_response_ok(response, json);
87 }
88
89 void ast_ari_applications_subscribe(struct ast_variable *headers,
90 struct ast_ari_applications_subscribe_args *args,
91 struct ast_ari_response *response)
92 {
93 RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
94 enum stasis_app_subscribe_res res;
95
96 if (args->event_source_count <= 0) {
97 ast_ari_response_error(response, 400, "Bad Request",
98 "Missing parameter eventSource");
99 return;
100 }
101
102 if (ast_strlen_zero(args->application_name)) {
103 ast_ari_response_error(response, 400, "Bad Request",
104 "Missing parameter applicationName");
105 return;
106 }
107
108 res = stasis_app_subscribe(args->application_name, args->event_source,
109 args->event_source_count, &json);
110
111 switch (res) {
112 case STASIS_ASR_OK:
113 ast_ari_response_ok(response, ast_json_ref(json));
114 break;
115 case STASIS_ASR_APP_NOT_FOUND:
116 ast_ari_response_error(response, 404, "Not Found",
117 "Application not found");
118 break;
119 case STASIS_ASR_EVENT_SOURCE_NOT_FOUND:
120 ast_ari_response_error(response, 422, "Unprocessable Entity",
121 "Event source does not exist");
122 break;
123 case STASIS_ASR_EVENT_SOURCE_BAD_SCHEME:
124 ast_ari_response_error(response, 400, "Bad Request",
125 "Invalid event source URI scheme");
126 break;
127 case STASIS_ASR_INTERNAL_ERROR:
128 ast_ari_response_error(response, 500, "Internal Server Error",
129 "Error processing request");
130 break;
131 }
132 }
133
134 void ast_ari_applications_unsubscribe(struct ast_variable *headers,
135 struct ast_ari_applications_unsubscribe_args *args,
136 struct ast_ari_response *response)
137 {
138 RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
139 enum stasis_app_subscribe_res res;
140
141 if (args->event_source_count == 0) {
142 ast_ari_response_error(response, 400, "Bad Request",
143 "Missing parameter eventSource");
144 return;
145 }
146
147 res = stasis_app_unsubscribe(args->application_name, args->event_source,
148 args->event_source_count, &json);
149
150 switch (res) {
151 case STASIS_ASR_OK:
152 ast_ari_response_ok(response, ast_json_ref(json));
153 break;
154 case STASIS_ASR_APP_NOT_FOUND:
155 ast_ari_response_error(response, 404, "Not Found",
156 "Application not found");
157 break;
158 case STASIS_ASR_EVENT_SOURCE_NOT_FOUND:
159 ast_ari_response_error(response, 422, "Unprocessable Entity",
160 "Event source was not subscribed to");
161 break;
162 case STASIS_ASR_EVENT_SOURCE_BAD_SCHEME:
163 ast_ari_response_error(response, 400, "Bad Request",
164 "Invalid event source URI scheme");
165 break;
166 case STASIS_ASR_INTERNAL_ERROR:
167 ast_ari_response_error(response, 500, "Internal Server Error",
168 "Error processing request");
169 }
170 }
171
172 void ast_ari_applications_filter(struct ast_variable *headers,
173 struct ast_ari_applications_filter_args *args,
174 struct ast_ari_response *response)
175 {
176 struct stasis_app *app = stasis_app_get_by_name(args->application_name);
177
178 if (!app) {
179 ast_ari_response_error(response, 404, "Not Found", "Application not found");
180 return;
181 }
182
183 if (stasis_app_event_filter_set(app, args->filter)) {
184 ast_ari_response_error(response, 400, "Bad Request", "Invalid format definition");
185 } else {
186 ast_ari_response_ok(response, stasis_app_object_to_json(app));
187 }
188
189 ao2_ref(app, -1);
190 }