Asterisk - The Open Source Telephony Project GIT-master-754dea3
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Modules Pages
res_ari_events.c
Go to the documentation of this file.
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 * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
21 * !!!!! DO NOT EDIT !!!!!
22 * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
23 * This file is generated by a mustache template. Please see the original
24 * template in rest-api-templates/res_ari_resource.c.mustache
25 */
26
27/*! \file
28 *
29 * \brief WebSocket resource
30 *
31 * \author David M. Lee, II <dlee@digium.com>
32 */
33
34/*** MODULEINFO
35 <depend type="module">res_ari</depend>
36 <depend type="module">res_ari_model</depend>
37 <depend type="module">res_stasis</depend>
38 <depend type="module">res_http_websocket</depend>
39 <support_level>core</support_level>
40 ***/
41
42#include "asterisk.h"
43
44#include "asterisk/app.h"
45#include "asterisk/module.h"
46#include "asterisk/stasis_app.h"
47#include "ari/resource_events.h"
48#if defined(AST_DEVMODE)
50#endif
51
52#define MAX_VALS 128
53
55 struct ast_json *body,
57{
58 struct ast_json *field;
59 /* Parse query parameters out of it */
60 field = ast_json_object_get(body, "application");
61 if (field) {
62 args->application = ast_json_string_get(field);
63 }
64 field = ast_json_object_get(body, "source");
65 if (field) {
66 /* If they were silly enough to both pass in a query param and a
67 * JSON body, free up the query value.
68 */
69 ast_free(args->source);
70 if (ast_json_typeof(field) == AST_JSON_ARRAY) {
71 /* Multiple param passed as array */
72 size_t i;
73 args->source_count = ast_json_array_size(field);
74 args->source = ast_malloc(sizeof(*args->source) * args->source_count);
75
76 if (!args->source) {
77 return -1;
78 }
79
80 for (i = 0; i < args->source_count; ++i) {
81 args->source[i] = ast_json_string_get(ast_json_array_get(field, i));
82 }
83 } else {
84 /* Multiple param passed as single value */
85 args->source_count = 1;
86 args->source = ast_malloc(sizeof(*args->source) * args->source_count);
87 if (!args->source) {
88 return -1;
89 }
90 args->source[0] = ast_json_string_get(field);
91 }
92 }
93 return 0;
94}
95
96/*!
97 * \brief Parameter parsing callback for /events/user/{eventName}.
98 * \param ser TCP/TLS session object
99 * \param get_params GET parameters in the HTTP request.
100 * \param path_vars Path variables extracted from the request.
101 * \param headers HTTP headers.
102 * \param body
103 * \param[out] response Response to the HTTP request.
104 */
106 struct ast_tcptls_session_instance *ser,
107 struct ast_variable *get_params, struct ast_variable *path_vars,
108 struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
109{
111 struct ast_variable *i;
112#if defined(AST_DEVMODE)
113 int is_valid;
114 int code;
115#endif /* AST_DEVMODE */
116
117 for (i = get_params; i; i = i->next) {
118 if (strcmp(i->name, "application") == 0) {
119 args.application = (i->value);
120 } else
121 if (strcmp(i->name, "source") == 0) {
122 /* Parse comma separated list */
123 char *vals[MAX_VALS];
124 size_t j;
125
126 args.source_parse = ast_strdup(i->value);
127 if (!args.source_parse) {
129 goto fin;
130 }
131
132 if (strlen(args.source_parse) == 0) {
133 /* ast_app_separate_args can't handle "" */
134 args.source_count = 1;
135 vals[0] = args.source_parse;
136 } else {
137 args.source_count = ast_app_separate_args(
138 args.source_parse, ',', vals,
139 ARRAY_LEN(vals));
140 }
141
142 if (args.source_count == 0) {
144 goto fin;
145 }
146
147 if (args.source_count >= MAX_VALS) {
148 ast_ari_response_error(response, 400,
149 "Bad Request",
150 "Too many values for source");
151 goto fin;
152 }
153
154 args.source = ast_malloc(sizeof(*args.source) * args.source_count);
155 if (!args.source) {
157 goto fin;
158 }
159
160 for (j = 0; j < args.source_count; ++j) {
161 args.source[j] = (vals[j]);
162 }
163 } else
164 {}
165 }
166 for (i = path_vars; i; i = i->next) {
167 if (strcmp(i->name, "eventName") == 0) {
168 args.event_name = (i->value);
169 } else
170 {}
171 }
172 args.variables = body;
173 ast_ari_events_user_event(headers, &args, response);
174#if defined(AST_DEVMODE)
175 code = response->response_code;
176
177 switch (code) {
178 case 0: /* Implementation is still a stub, or the code wasn't set */
179 is_valid = response->message == NULL;
180 break;
181 case 500: /* Internal Server Error */
182 case 501: /* Not Implemented */
183 case 404: /* Application does not exist. */
184 case 422: /* Event source not found. */
185 case 400: /* Invalid even tsource URI or userevent data. */
186 is_valid = 1;
187 break;
188 default:
189 if (200 <= code && code <= 299) {
190 is_valid = ast_ari_validate_void(
191 response->message);
192 } else {
193 ast_log(LOG_ERROR, "Invalid error response %d for /events/user/{eventName}\n", code);
194 is_valid = 0;
195 }
196 }
197
198 if (!is_valid) {
199 ast_log(LOG_ERROR, "Response validation failed for /events/user/{eventName}\n");
200 ast_ari_response_error(response, 500,
201 "Internal Server Error", "Response validation failed");
202 }
203#endif /* AST_DEVMODE */
204
205fin: __attribute__((unused))
206 ast_free(args.source_parse);
207 ast_free(args.source);
208 return;
209}
210
211/*! \brief REST handler for /api-docs/events.json */
213 .path_segment = "eventName",
214 .is_wildcard = 1,
215 .callbacks = {
217 },
218 .num_children = 0,
219 .children = { }
220};
221/*! \brief REST handler for /api-docs/events.json */
223 .path_segment = "user",
224 .callbacks = {
225 },
226 .num_children = 1,
227 .children = { &events_user_eventName, }
228};
229/*! \brief REST handler for /api-docs/events.json */
231 .path_segment = "events",
232 .callbacks = {
233 },
234 .num_children = 1,
235 .children = { &events_user, }
236};
237
238static int unload_module(void)
239{
241 return 0;
242}
243
244static int load_module(void)
245{
246 int res = 0;
247
249
251 if (res) {
254 }
255
257}
258
259AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "RESTful API module - WebSocket resource",
260 .support_level = AST_MODULE_SUPPORT_CORE,
261 .load = load_module,
262 .unload = unload_module,
263 .requires = "res_ari,res_ari_model,res_stasis,res_http_websocket",
int ast_ari_remove_handler(struct stasis_rest_handlers *handler)
Definition: res_ari.c:262
void ast_ari_response_error(struct ast_ari_response *response, int response_code, const char *response_text, const char *message_fmt,...)
Fill in an error ast_ari_response.
Definition: res_ari.c:319
void ast_ari_response_alloc_failed(struct ast_ari_response *response)
Fill in response with a 500 message for allocation failures.
Definition: res_ari.c:358
int ast_ari_add_handler(struct stasis_rest_handlers *handler)
Definition: res_ari.c:239
Generated file - Build validators for ARI model objects.
int ast_ari_validate_void(struct ast_json *json)
Validator for native Swagger void.
Definition: res_ari_model.c:91
Asterisk main include file. File version handling, generic pbx functions.
#define ast_free(a)
Definition: astmm.h:180
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:241
#define ast_malloc(len)
A wrapper for malloc()
Definition: astmm.h:191
#define ast_log
Definition: astobj2.c:42
@ AST_HTTP_POST
Definition: http.h:61
Application convenience functions, designed to give consistent look and feel to Asterisk apps.
#define ast_app_separate_args(a, b, c, d)
#define LOG_ERROR
enum ast_json_type ast_json_typeof(const struct ast_json *value)
Get the type of value.
Definition: json.c:78
struct ast_json * ast_json_array_get(const struct ast_json *array, size_t index)
Get an element from an array.
Definition: json.c:370
@ AST_JSON_ARRAY
Definition: json.h:165
const char * ast_json_string_get(const struct ast_json *string)
Get the value of a JSON string.
Definition: json.c:283
struct ast_json * ast_json_object_get(struct ast_json *object, const char *key)
Get a field from a JSON object.
Definition: json.c:407
size_t ast_json_array_size(const struct ast_json *array)
Get the size of a JSON array.
Definition: json.c:366
Asterisk module definitions.
@ AST_MODFLAG_DEFAULT
Definition: module.h:329
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition: module.h:557
@ AST_MODULE_SUPPORT_CORE
Definition: module.h:121
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
@ AST_MODULE_LOAD_SUCCESS
Definition: module.h:70
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
int ast_ari_events_user_event_parse_body(struct ast_json *body, struct ast_ari_events_user_event_args *args)
Body parsing function for /events/user/{eventName}.
static struct stasis_rest_handlers events
REST handler for /api-docs/events.json.
static struct stasis_rest_handlers events_user_eventName
REST handler for /api-docs/events.json.
#define MAX_VALS
static struct stasis_rest_handlers events_user
REST handler for /api-docs/events.json.
static void ast_ari_events_user_event_cb(struct ast_tcptls_session_instance *ser, struct ast_variable *get_params, struct ast_variable *path_vars, struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
Parameter parsing callback for /events/user/{eventName}.
static int load_module(void)
static int unload_module(void)
#define NULL
Definition: resample.c:96
void ast_ari_events_user_event(struct ast_variable *headers, struct ast_ari_events_user_event_args *args, struct ast_ari_response *response)
Generate a user event.
Generated file - declares stubs to be implemented in res/ari/resource_events.c.
Stasis Application API. See Stasis Application API for detailed documentation.
struct ast_json * message
Definition: ari.h:103
int response_code
Definition: ari.h:108
Abstract JSON element (object, array, string, int, ...).
describes a server instance
Definition: tcptls.h:151
Structure for variables, used for configurations and for channel variables.
struct ast_variable * next
Handler for a single RESTful path segment.
Definition: ari.h:69
int is_websocket
Definition: ari.h:90
const char * path_segment
Definition: ari.h:71
const char * args
#define ARRAY_LEN(a)
Definition: utils.h:666