Debian Codegraph @

res/ari/resource_events.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2012 - 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/events.{format} implementation- WebSocket resource
22 *
23 * \author David M. Lee, II <[email protected]>
24 */
25
26 /*** MODULEINFO
27 <support_level>core</support_level>
28 ***/
29
30 #include "asterisk.h"
31
32 #include "resource_events.h"
33 #include "internal.h"
34 #include "asterisk/stasis_app.h"
35 #include "asterisk/stasis_app_broadcast.h"
36
37 void ast_ari_events_user_event(struct ast_variable *headers,
38 struct ast_ari_events_user_event_args *args,
39 struct ast_ari_response *response)
40 {
41 enum stasis_app_user_event_res res;
42 struct ast_json *json_variables = NULL;
43
44 if (args->variables) {
45 ast_ari_events_user_event_parse_body(args->variables, args);
46 json_variables = ast_json_object_get(args->variables, "variables");
47 }
48
49 if (ast_strlen_zero(args->application)) {
50 ast_ari_response_error(response, 400, "Bad Request",
51 "Missing parameter application");
52 return;
53 }
54
55 res = stasis_app_user_event(args->application,
56 args->event_name,
57 args->source, args->source_count,
58 json_variables);
59
60 switch (res) {
61 case STASIS_APP_USER_OK:
62 ast_ari_response_no_content(response);
63 break;
64
65 case STASIS_APP_USER_APP_NOT_FOUND:
66 ast_ari_response_error(response, 404, "Not Found",
67 "Application not found");
68 break;
69
70 case STASIS_APP_USER_EVENT_SOURCE_NOT_FOUND:
71 ast_ari_response_error(response, 422, "Unprocessable Entity",
72 "Event source was not found");
73 break;
74
75 case STASIS_APP_USER_EVENT_SOURCE_BAD_SCHEME:
76 ast_ari_response_error(response, 400, "Bad Request",
77 "Invalid event source URI scheme");
78 break;
79
80 case STASIS_APP_USER_USEREVENT_INVALID:
81 ast_ari_response_error(response, 400, "Bad Request",
82 "Invalid userevent data");
83 break;
84
85 case STASIS_APP_USER_INTERNAL_ERROR:
86 default:
87 ast_ari_response_error(response, 500, "Internal Server Error",
88 "Error processing request");
89 }
90 }
91
92 void ast_ari_events_claim_channel(struct ast_variable *headers,
93 struct ast_ari_events_claim_channel_args *args,
94 struct ast_ari_response *response)
95 {
96 int res;
97
98 if (ast_strlen_zero(args->channel_id)) {
99 ast_ari_response_error(response, 400, "Bad Request",
100 "channelId parameter is required");
101 return;
102 }
103
104 if (ast_strlen_zero(args->application)) {
105 ast_ari_response_error(response, 400, "Bad Request",
106 "application parameter is required");
107 return;
108 }
109
110 res = stasis_app_claim_channel(args->channel_id, args->application);
111
112 switch (res) {
113 case 0:
114 /* Success */
115 ast_ari_response_no_content(response);
116 break;
117 case -1:
118 /* Channel not found */
119 ast_ari_response_error(response, 404, "Not Found",
120 "Channel not found or not in broadcast state");
121 break;
122 case -2:
123 /* Already claimed */
124 ast_ari_response_error(response, 409, "Conflict",
125 "Channel has already been claimed by another application");
126 break;
127 case AST_OPTIONAL_API_UNAVAILABLE:
128 /* Module not loaded */
129 ast_ari_response_error(response, 501, "Not Implemented",
130 "Broadcast functionality not available (res_stasis_broadcast not loaded)");
131 break;
132 default:
133 ast_ari_response_error(response, 500, "Internal Server Error",
134 "Failed to claim channel");
135 break;
136 }
137 }