3 * Asterisk -- An open source telephony toolkit.
9 * Copyright (C) 2013, Digium, Inc.
11 * David M. Lee, II <dlee@digium.com>
14 * See http://www.asterisk.org for more information about
15 * the Asterisk project. Please do not directly contact
16 * any of the maintainers of this project for assistance;
17 * the project provides a web site, mailing lists and IRC
18 * channels for your use.
20 * This program is free software, distributed under the terms of
21 * the GNU General Public License Version 2. See the LICENSE file
22 * at the top of the source tree.
25 {{! Template for rendering the res_ module for an HTTP resource. }}
28 * This file is generated by a mustache template. Please see the original
29 * template in rest-api-templates/res_ari_resource.c.mustache
34 * \brief {{{description}}}
36 * \author {{{author}}}
40 <depend type="module">res_ari</depend>
41 <depend type="module">res_stasis</depend>
42 <support_level>core</support_level>
47 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
49 #include "asterisk/app.h"
50 #include "asterisk/module.h"
51 #include "asterisk/stasis_app.h"
52 #include "ari/resource_{{c_name}}.h"
53 #if defined(AST_DEVMODE)
54 #include "ari/ari_model_validators.h"
57 {{! Only include http_websocket if necessary. Otherwise we'll do a lot of
58 * unnecessary optional_api intialization, which makes optional_api harder
61 #include "asterisk/http_websocket.h"
71 * \brief Parameter parsing callback for {{path}}.
72 * \param get_params GET parameters in the HTTP request.
73 * \param path_vars Path variables extracted from the request.
74 * \param headers HTTP headers.
75 * \param[out] response Response to the HTTP request.
77 static void ast_ari_{{c_name}}_{{c_nickname}}_cb(
78 struct ast_tcptls_session_instance *ser,
79 struct ast_variable *get_params, struct ast_variable *path_vars,
80 struct ast_variable *headers, struct ast_ari_response *response)
82 struct ast_ari_{{c_name}}_{{c_nickname}}_args args = {};
84 struct ast_variable *i;
86 RAII_VAR(struct ast_json *, body, NULL, ast_json_unref);
87 #if defined(AST_DEVMODE)
90 #endif /* AST_DEVMODE */
93 ast_ari_{{c_name}}_{{c_nickname}}(headers, &args, response);
94 #if defined(AST_DEVMODE)
95 code = response->response_code;
98 case 0: /* Implementation is still a stub, or the code wasn't set */
99 is_valid = response->message == NULL;
101 case 500: /* Internal Server Error */
102 case 501: /* Not Implemented */
104 case {{code}}: /* {{{reason}}} */
109 if (200 <= code && code <= 299) {
112 is_valid = ast_ari_validate_list(response->message,
113 ast_ari_validate_{{c_singular_name}}_fn());
116 is_valid = ast_ari_validate_{{c_name}}(
121 ast_log(LOG_ERROR, "Invalid error response %d for {{path}}\n", code);
127 ast_log(LOG_ERROR, "Response validation failed for {{path}}\n");
128 ast_ari_response_error(response, 500,
129 "Internal Server Error", "Response validation failed");
131 #endif /* AST_DEVMODE */
133 fin: __attribute__((unused))
139 static void ast_ari_{{c_name}}_{{c_nickname}}_ws_cb(struct ast_websocket *ws_session,
140 struct ast_variable *get_params, struct ast_variable *headers)
142 struct ast_ari_{{c_name}}_{{c_nickname}}_args args = {};
144 RAII_VAR(struct ast_ari_response *, response, NULL, ast_free);
145 struct ast_variable *i;
147 RAII_VAR(struct ast_websocket *, s, ws_session, ast_websocket_unref);
148 RAII_VAR(struct ast_ari_websocket_session *, session, NULL, ao2_cleanup);
149 {{#has_path_parameters}}
150 /* TODO: It's not immediately obvious how to pass path params through
151 * the websocket code to this callback. Not needed right now, so we'll
153 struct ast_variable *path_vars = NULL;
154 {{/has_path_parameters}}
157 response = ast_calloc(1, sizeof(*response));
159 ast_log(LOG_ERROR, "Failed to create response.\n");
164 #if defined(AST_DEVMODE)
165 session = ast_ari_websocket_session_create(ws_session,
166 ast_ari_validate_{{response_class.c_name}}_fn());
168 session = ast_ari_websocket_session_create(ws_session, NULL);
171 ast_log(LOG_ERROR, "Failed to create ARI session\n");
177 ast_ari_websocket_{{c_name}}_{{c_nickname}}(session, headers, &args);
179 fin: __attribute__((unused))
180 if (response && response->response_code != 0) {
181 /* Param parsing failure */
182 /* TODO - ideally, this would return the error code to the
183 * HTTP client; but we've already done the WebSocket
184 * negotiation. Param parsing should happen earlier, but we
185 * need a way to pass it through the WebSocket code to the
187 RAII_VAR(char *, msg, NULL, ast_json_free);
188 if (response->message) {
189 msg = ast_json_dump_string(response->message);
191 ast_log(LOG_ERROR, "Missing response message\n");
194 ast_websocket_write(ws_session,
195 AST_WEBSOCKET_OPCODE_TEXT, msg, strlen(msg));
204 {{! The rest_handler partial expands to the tree of stasis_rest_handlers }}
209 static int load_module(void)
214 {{full_name}}.ws_server = ast_websocket_server_create();
215 if (!{{full_name}}.ws_server) {
216 return AST_MODULE_LOAD_FAILURE;
221 res |= ast_websocket_server_add_protocol({{full_name}}.ws_server,
222 "{{websocket_protocol}}", ast_ari_{{c_name}}_{{c_nickname}}_ws_cb);
227 res |= ast_ari_add_handler(&{{root_full_name}});
231 static int unload_module(void)
233 ast_ari_remove_handler(&{{root_full_name}});
236 ao2_cleanup({{full_name}}.ws_server);
237 {{full_name}}.ws_server = NULL;
244 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "RESTful API module - {{{description}}}",
245 .support_level = AST_MODULE_SUPPORT_CORE,
247 .unload = unload_module,
248 .nonoptreq = "res_ari,res_stasis",