2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2012 - 2013, Digium, Inc.
6 * David M. Lee, II <dlee@digium.com>
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.
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.
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
29 * \brief Endpoint resources
31 * \author David M. Lee, II <dlee@digium.com>
35 <depend type="module">res_ari</depend>
36 <depend type="module">res_stasis</depend>
37 <support_level>core</support_level>
42 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
44 #include "asterisk/app.h"
45 #include "asterisk/module.h"
46 #include "asterisk/stasis_app.h"
47 #include "ari/resource_endpoints.h"
48 #if defined(AST_DEVMODE)
49 #include "ari/ari_model_validators.h"
55 * \brief Parameter parsing callback for /endpoints.
56 * \param get_params GET parameters in the HTTP request.
57 * \param path_vars Path variables extracted from the request.
58 * \param headers HTTP headers.
59 * \param[out] response Response to the HTTP request.
61 static void ast_ari_endpoints_list_cb(
62 struct ast_tcptls_session_instance *ser,
63 struct ast_variable *get_params, struct ast_variable *path_vars,
64 struct ast_variable *headers, struct ast_ari_response *response)
66 struct ast_ari_endpoints_list_args args = {};
67 RAII_VAR(struct ast_json *, body, NULL, ast_json_unref);
68 #if defined(AST_DEVMODE)
71 #endif /* AST_DEVMODE */
73 ast_ari_endpoints_list(headers, &args, response);
74 #if defined(AST_DEVMODE)
75 code = response->response_code;
78 case 0: /* Implementation is still a stub, or the code wasn't set */
79 is_valid = response->message == NULL;
81 case 500: /* Internal Server Error */
82 case 501: /* Not Implemented */
86 if (200 <= code && code <= 299) {
87 is_valid = ast_ari_validate_list(response->message,
88 ast_ari_validate_endpoint_fn());
90 ast_log(LOG_ERROR, "Invalid error response %d for /endpoints\n", code);
96 ast_log(LOG_ERROR, "Response validation failed for /endpoints\n");
97 ast_ari_response_error(response, 500,
98 "Internal Server Error", "Response validation failed");
100 #endif /* AST_DEVMODE */
102 fin: __attribute__((unused))
105 int ast_ari_endpoints_send_message_parse_body(
106 struct ast_json *body,
107 struct ast_ari_endpoints_send_message_args *args)
109 struct ast_json *field;
110 /* Parse query parameters out of it */
111 field = ast_json_object_get(body, "to");
113 args->to = ast_json_string_get(field);
115 field = ast_json_object_get(body, "from");
117 args->from = ast_json_string_get(field);
119 field = ast_json_object_get(body, "body");
121 args->body = ast_json_string_get(field);
127 * \brief Parameter parsing callback for /endpoints/sendMessage.
128 * \param get_params GET parameters in the HTTP request.
129 * \param path_vars Path variables extracted from the request.
130 * \param headers HTTP headers.
131 * \param[out] response Response to the HTTP request.
133 static void ast_ari_endpoints_send_message_cb(
134 struct ast_tcptls_session_instance *ser,
135 struct ast_variable *get_params, struct ast_variable *path_vars,
136 struct ast_variable *headers, struct ast_ari_response *response)
138 struct ast_ari_endpoints_send_message_args args = {};
139 struct ast_variable *i;
140 RAII_VAR(struct ast_json *, body, NULL, ast_json_unref);
141 #if defined(AST_DEVMODE)
144 #endif /* AST_DEVMODE */
146 for (i = get_params; i; i = i->next) {
147 if (strcmp(i->name, "to") == 0) {
148 args.to = (i->value);
150 if (strcmp(i->name, "from") == 0) {
151 args.from = (i->value);
153 if (strcmp(i->name, "body") == 0) {
154 args.body = (i->value);
158 /* Look for a JSON request entity */
159 body = ast_http_get_json(ser, headers);
163 ast_ari_response_error(response, 413, "Request Entity Too Large", "Request body too large");
166 ast_ari_response_error(response, 500, "Internal Server Error", "Error processing request");
169 ast_ari_response_error(response, 400, "Bad Request", "Error parsing request body");
173 args.variables = body;
174 ast_ari_endpoints_send_message(headers, &args, response);
175 #if defined(AST_DEVMODE)
176 code = response->response_code;
179 case 0: /* Implementation is still a stub, or the code wasn't set */
180 is_valid = response->message == NULL;
182 case 500: /* Internal Server Error */
183 case 501: /* Not Implemented */
184 case 400: /* Invalid parameters for sending a message. */
185 case 404: /* Endpoint not found */
189 if (200 <= code && code <= 299) {
190 is_valid = ast_ari_validate_void(
193 ast_log(LOG_ERROR, "Invalid error response %d for /endpoints/sendMessage\n", code);
199 ast_log(LOG_ERROR, "Response validation failed for /endpoints/sendMessage\n");
200 ast_ari_response_error(response, 500,
201 "Internal Server Error", "Response validation failed");
203 #endif /* AST_DEVMODE */
205 fin: __attribute__((unused))
209 * \brief Parameter parsing callback for /endpoints/{tech}.
210 * \param get_params GET parameters in the HTTP request.
211 * \param path_vars Path variables extracted from the request.
212 * \param headers HTTP headers.
213 * \param[out] response Response to the HTTP request.
215 static void ast_ari_endpoints_list_by_tech_cb(
216 struct ast_tcptls_session_instance *ser,
217 struct ast_variable *get_params, struct ast_variable *path_vars,
218 struct ast_variable *headers, struct ast_ari_response *response)
220 struct ast_ari_endpoints_list_by_tech_args args = {};
221 struct ast_variable *i;
222 RAII_VAR(struct ast_json *, body, NULL, ast_json_unref);
223 #if defined(AST_DEVMODE)
226 #endif /* AST_DEVMODE */
228 for (i = path_vars; i; i = i->next) {
229 if (strcmp(i->name, "tech") == 0) {
230 args.tech = (i->value);
234 ast_ari_endpoints_list_by_tech(headers, &args, response);
235 #if defined(AST_DEVMODE)
236 code = response->response_code;
239 case 0: /* Implementation is still a stub, or the code wasn't set */
240 is_valid = response->message == NULL;
242 case 500: /* Internal Server Error */
243 case 501: /* Not Implemented */
244 case 404: /* Endpoints not found */
248 if (200 <= code && code <= 299) {
249 is_valid = ast_ari_validate_list(response->message,
250 ast_ari_validate_endpoint_fn());
252 ast_log(LOG_ERROR, "Invalid error response %d for /endpoints/{tech}\n", code);
258 ast_log(LOG_ERROR, "Response validation failed for /endpoints/{tech}\n");
259 ast_ari_response_error(response, 500,
260 "Internal Server Error", "Response validation failed");
262 #endif /* AST_DEVMODE */
264 fin: __attribute__((unused))
268 * \brief Parameter parsing callback for /endpoints/{tech}/{resource}.
269 * \param get_params GET parameters in the HTTP request.
270 * \param path_vars Path variables extracted from the request.
271 * \param headers HTTP headers.
272 * \param[out] response Response to the HTTP request.
274 static void ast_ari_endpoints_get_cb(
275 struct ast_tcptls_session_instance *ser,
276 struct ast_variable *get_params, struct ast_variable *path_vars,
277 struct ast_variable *headers, struct ast_ari_response *response)
279 struct ast_ari_endpoints_get_args args = {};
280 struct ast_variable *i;
281 RAII_VAR(struct ast_json *, body, NULL, ast_json_unref);
282 #if defined(AST_DEVMODE)
285 #endif /* AST_DEVMODE */
287 for (i = path_vars; i; i = i->next) {
288 if (strcmp(i->name, "tech") == 0) {
289 args.tech = (i->value);
291 if (strcmp(i->name, "resource") == 0) {
292 args.resource = (i->value);
296 ast_ari_endpoints_get(headers, &args, response);
297 #if defined(AST_DEVMODE)
298 code = response->response_code;
301 case 0: /* Implementation is still a stub, or the code wasn't set */
302 is_valid = response->message == NULL;
304 case 500: /* Internal Server Error */
305 case 501: /* Not Implemented */
306 case 400: /* Invalid parameters for sending a message. */
307 case 404: /* Endpoints not found */
311 if (200 <= code && code <= 299) {
312 is_valid = ast_ari_validate_endpoint(
315 ast_log(LOG_ERROR, "Invalid error response %d for /endpoints/{tech}/{resource}\n", code);
321 ast_log(LOG_ERROR, "Response validation failed for /endpoints/{tech}/{resource}\n");
322 ast_ari_response_error(response, 500,
323 "Internal Server Error", "Response validation failed");
325 #endif /* AST_DEVMODE */
327 fin: __attribute__((unused))
330 int ast_ari_endpoints_send_message_to_endpoint_parse_body(
331 struct ast_json *body,
332 struct ast_ari_endpoints_send_message_to_endpoint_args *args)
334 struct ast_json *field;
335 /* Parse query parameters out of it */
336 field = ast_json_object_get(body, "from");
338 args->from = ast_json_string_get(field);
340 field = ast_json_object_get(body, "body");
342 args->body = ast_json_string_get(field);
348 * \brief Parameter parsing callback for /endpoints/{tech}/{resource}/sendMessage.
349 * \param get_params GET parameters in the HTTP request.
350 * \param path_vars Path variables extracted from the request.
351 * \param headers HTTP headers.
352 * \param[out] response Response to the HTTP request.
354 static void ast_ari_endpoints_send_message_to_endpoint_cb(
355 struct ast_tcptls_session_instance *ser,
356 struct ast_variable *get_params, struct ast_variable *path_vars,
357 struct ast_variable *headers, struct ast_ari_response *response)
359 struct ast_ari_endpoints_send_message_to_endpoint_args args = {};
360 struct ast_variable *i;
361 RAII_VAR(struct ast_json *, body, NULL, ast_json_unref);
362 #if defined(AST_DEVMODE)
365 #endif /* AST_DEVMODE */
367 for (i = get_params; i; i = i->next) {
368 if (strcmp(i->name, "from") == 0) {
369 args.from = (i->value);
371 if (strcmp(i->name, "body") == 0) {
372 args.body = (i->value);
376 for (i = path_vars; i; i = i->next) {
377 if (strcmp(i->name, "tech") == 0) {
378 args.tech = (i->value);
380 if (strcmp(i->name, "resource") == 0) {
381 args.resource = (i->value);
385 /* Look for a JSON request entity */
386 body = ast_http_get_json(ser, headers);
390 ast_ari_response_error(response, 413, "Request Entity Too Large", "Request body too large");
393 ast_ari_response_error(response, 500, "Internal Server Error", "Error processing request");
396 ast_ari_response_error(response, 400, "Bad Request", "Error parsing request body");
400 args.variables = body;
401 ast_ari_endpoints_send_message_to_endpoint(headers, &args, response);
402 #if defined(AST_DEVMODE)
403 code = response->response_code;
406 case 0: /* Implementation is still a stub, or the code wasn't set */
407 is_valid = response->message == NULL;
409 case 500: /* Internal Server Error */
410 case 501: /* Not Implemented */
411 case 400: /* Invalid parameters for sending a message. */
412 case 404: /* Endpoint not found */
416 if (200 <= code && code <= 299) {
417 is_valid = ast_ari_validate_void(
420 ast_log(LOG_ERROR, "Invalid error response %d for /endpoints/{tech}/{resource}/sendMessage\n", code);
426 ast_log(LOG_ERROR, "Response validation failed for /endpoints/{tech}/{resource}/sendMessage\n");
427 ast_ari_response_error(response, 500,
428 "Internal Server Error", "Response validation failed");
430 #endif /* AST_DEVMODE */
432 fin: __attribute__((unused))
436 /*! \brief REST handler for /api-docs/endpoints.{format} */
437 static struct stasis_rest_handlers endpoints_sendMessage = {
438 .path_segment = "sendMessage",
440 [AST_HTTP_PUT] = ast_ari_endpoints_send_message_cb,
445 /*! \brief REST handler for /api-docs/endpoints.{format} */
446 static struct stasis_rest_handlers endpoints_tech_resource_sendMessage = {
447 .path_segment = "sendMessage",
449 [AST_HTTP_PUT] = ast_ari_endpoints_send_message_to_endpoint_cb,
454 /*! \brief REST handler for /api-docs/endpoints.{format} */
455 static struct stasis_rest_handlers endpoints_tech_resource = {
456 .path_segment = "resource",
459 [AST_HTTP_GET] = ast_ari_endpoints_get_cb,
462 .children = { &endpoints_tech_resource_sendMessage, }
464 /*! \brief REST handler for /api-docs/endpoints.{format} */
465 static struct stasis_rest_handlers endpoints_tech = {
466 .path_segment = "tech",
469 [AST_HTTP_GET] = ast_ari_endpoints_list_by_tech_cb,
472 .children = { &endpoints_tech_resource, }
474 /*! \brief REST handler for /api-docs/endpoints.{format} */
475 static struct stasis_rest_handlers endpoints = {
476 .path_segment = "endpoints",
478 [AST_HTTP_GET] = ast_ari_endpoints_list_cb,
481 .children = { &endpoints_sendMessage,&endpoints_tech, }
484 static int load_module(void)
488 res |= ast_ari_add_handler(&endpoints);
492 static int unload_module(void)
494 ast_ari_remove_handler(&endpoints);
499 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "RESTful API module - Endpoint resources",
500 .support_level = AST_MODULE_SUPPORT_CORE,
502 .unload = unload_module,
503 .nonoptreq = "res_ari,res_stasis",