Asterisk - The Open Source Telephony Project  GIT-master-a24979a
res_ari_endpoints.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 Endpoint resources
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  <support_level>core</support_level>
39  ***/
40 
41 #include "asterisk.h"
42 
43 #include "asterisk/app.h"
44 #include "asterisk/module.h"
45 #include "asterisk/stasis_app.h"
46 #include "ari/resource_endpoints.h"
47 #if defined(AST_DEVMODE)
49 #endif
50 
51 #define MAX_VALS 128
52 
53 /*!
54  * \brief Parameter parsing callback for /endpoints.
55  * \param ser TCP/TLS session object
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 body
60  * \param[out] response Response to the HTTP request.
61  */
63  struct ast_tcptls_session_instance *ser,
64  struct ast_variable *get_params, struct ast_variable *path_vars,
65  struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
66 {
68 #if defined(AST_DEVMODE)
69  int is_valid;
70  int code;
71 #endif /* AST_DEVMODE */
72 
73  ast_ari_endpoints_list(headers, &args, response);
74 #if defined(AST_DEVMODE)
75  code = response->response_code;
76 
77  switch (code) {
78  case 0: /* Implementation is still a stub, or the code wasn't set */
79  is_valid = response->message == NULL;
80  break;
81  case 500: /* Internal Server Error */
82  case 501: /* Not Implemented */
83  is_valid = 1;
84  break;
85  default:
86  if (200 <= code && code <= 299) {
87  is_valid = ast_ari_validate_list(response->message,
89  } else {
90  ast_log(LOG_ERROR, "Invalid error response %d for /endpoints\n", code);
91  is_valid = 0;
92  }
93  }
94 
95  if (!is_valid) {
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");
99  }
100 #endif /* AST_DEVMODE */
101 
102 fin: __attribute__((unused))
103  return;
104 }
106  struct ast_json *body,
108 {
109  struct ast_json *field;
110  /* Parse query parameters out of it */
111  field = ast_json_object_get(body, "to");
112  if (field) {
113  args->to = ast_json_string_get(field);
114  }
115  field = ast_json_object_get(body, "from");
116  if (field) {
117  args->from = ast_json_string_get(field);
118  }
119  field = ast_json_object_get(body, "body");
120  if (field) {
121  args->body = ast_json_string_get(field);
122  }
123  return 0;
124 }
125 
126 /*!
127  * \brief Parameter parsing callback for /endpoints/sendMessage.
128  * \param ser TCP/TLS session object
129  * \param get_params GET parameters in the HTTP request.
130  * \param path_vars Path variables extracted from the request.
131  * \param headers HTTP headers.
132  * \param body
133  * \param[out] response Response to the HTTP request.
134  */
136  struct ast_tcptls_session_instance *ser,
137  struct ast_variable *get_params, struct ast_variable *path_vars,
138  struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
139 {
141  struct ast_variable *i;
142 #if defined(AST_DEVMODE)
143  int is_valid;
144  int code;
145 #endif /* AST_DEVMODE */
146 
147  for (i = get_params; i; i = i->next) {
148  if (strcmp(i->name, "to") == 0) {
149  args.to = (i->value);
150  } else
151  if (strcmp(i->name, "from") == 0) {
152  args.from = (i->value);
153  } else
154  if (strcmp(i->name, "body") == 0) {
155  args.body = (i->value);
156  } else
157  {}
158  }
159  args.variables = body;
160  ast_ari_endpoints_send_message(headers, &args, response);
161 #if defined(AST_DEVMODE)
162  code = response->response_code;
163 
164  switch (code) {
165  case 0: /* Implementation is still a stub, or the code wasn't set */
166  is_valid = response->message == NULL;
167  break;
168  case 500: /* Internal Server Error */
169  case 501: /* Not Implemented */
170  case 400: /* Invalid parameters for sending a message. */
171  case 404: /* Endpoint not found */
172  is_valid = 1;
173  break;
174  default:
175  if (200 <= code && code <= 299) {
176  is_valid = ast_ari_validate_void(
177  response->message);
178  } else {
179  ast_log(LOG_ERROR, "Invalid error response %d for /endpoints/sendMessage\n", code);
180  is_valid = 0;
181  }
182  }
183 
184  if (!is_valid) {
185  ast_log(LOG_ERROR, "Response validation failed for /endpoints/sendMessage\n");
186  ast_ari_response_error(response, 500,
187  "Internal Server Error", "Response validation failed");
188  }
189 #endif /* AST_DEVMODE */
190 
191 fin: __attribute__((unused))
192  return;
193 }
194 /*!
195  * \brief Parameter parsing callback for /endpoints/{tech}.
196  * \param ser TCP/TLS session object
197  * \param get_params GET parameters in the HTTP request.
198  * \param path_vars Path variables extracted from the request.
199  * \param headers HTTP headers.
200  * \param body
201  * \param[out] response Response to the HTTP request.
202  */
204  struct ast_tcptls_session_instance *ser,
205  struct ast_variable *get_params, struct ast_variable *path_vars,
206  struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
207 {
209  struct ast_variable *i;
210 #if defined(AST_DEVMODE)
211  int is_valid;
212  int code;
213 #endif /* AST_DEVMODE */
214 
215  for (i = path_vars; i; i = i->next) {
216  if (strcmp(i->name, "tech") == 0) {
217  args.tech = (i->value);
218  } else
219  {}
220  }
221  ast_ari_endpoints_list_by_tech(headers, &args, response);
222 #if defined(AST_DEVMODE)
223  code = response->response_code;
224 
225  switch (code) {
226  case 0: /* Implementation is still a stub, or the code wasn't set */
227  is_valid = response->message == NULL;
228  break;
229  case 500: /* Internal Server Error */
230  case 501: /* Not Implemented */
231  case 404: /* Endpoints not found */
232  is_valid = 1;
233  break;
234  default:
235  if (200 <= code && code <= 299) {
236  is_valid = ast_ari_validate_list(response->message,
238  } else {
239  ast_log(LOG_ERROR, "Invalid error response %d for /endpoints/{tech}\n", code);
240  is_valid = 0;
241  }
242  }
243 
244  if (!is_valid) {
245  ast_log(LOG_ERROR, "Response validation failed for /endpoints/{tech}\n");
246  ast_ari_response_error(response, 500,
247  "Internal Server Error", "Response validation failed");
248  }
249 #endif /* AST_DEVMODE */
250 
251 fin: __attribute__((unused))
252  return;
253 }
254 /*!
255  * \brief Parameter parsing callback for /endpoints/{tech}/{resource}.
256  * \param ser TCP/TLS session object
257  * \param get_params GET parameters in the HTTP request.
258  * \param path_vars Path variables extracted from the request.
259  * \param headers HTTP headers.
260  * \param body
261  * \param[out] response Response to the HTTP request.
262  */
264  struct ast_tcptls_session_instance *ser,
265  struct ast_variable *get_params, struct ast_variable *path_vars,
266  struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
267 {
268  struct ast_ari_endpoints_get_args args = {};
269  struct ast_variable *i;
270 #if defined(AST_DEVMODE)
271  int is_valid;
272  int code;
273 #endif /* AST_DEVMODE */
274 
275  for (i = path_vars; i; i = i->next) {
276  if (strcmp(i->name, "tech") == 0) {
277  args.tech = (i->value);
278  } else
279  if (strcmp(i->name, "resource") == 0) {
280  args.resource = (i->value);
281  } else
282  {}
283  }
284  ast_ari_endpoints_get(headers, &args, response);
285 #if defined(AST_DEVMODE)
286  code = response->response_code;
287 
288  switch (code) {
289  case 0: /* Implementation is still a stub, or the code wasn't set */
290  is_valid = response->message == NULL;
291  break;
292  case 500: /* Internal Server Error */
293  case 501: /* Not Implemented */
294  case 400: /* Invalid parameters for sending a message. */
295  case 404: /* Endpoints not found */
296  is_valid = 1;
297  break;
298  default:
299  if (200 <= code && code <= 299) {
300  is_valid = ast_ari_validate_endpoint(
301  response->message);
302  } else {
303  ast_log(LOG_ERROR, "Invalid error response %d for /endpoints/{tech}/{resource}\n", code);
304  is_valid = 0;
305  }
306  }
307 
308  if (!is_valid) {
309  ast_log(LOG_ERROR, "Response validation failed for /endpoints/{tech}/{resource}\n");
310  ast_ari_response_error(response, 500,
311  "Internal Server Error", "Response validation failed");
312  }
313 #endif /* AST_DEVMODE */
314 
315 fin: __attribute__((unused))
316  return;
317 }
319  struct ast_json *body,
321 {
322  struct ast_json *field;
323  /* Parse query parameters out of it */
324  field = ast_json_object_get(body, "from");
325  if (field) {
326  args->from = ast_json_string_get(field);
327  }
328  field = ast_json_object_get(body, "body");
329  if (field) {
330  args->body = ast_json_string_get(field);
331  }
332  return 0;
333 }
334 
335 /*!
336  * \brief Parameter parsing callback for /endpoints/{tech}/{resource}/sendMessage.
337  * \param ser TCP/TLS session object
338  * \param get_params GET parameters in the HTTP request.
339  * \param path_vars Path variables extracted from the request.
340  * \param headers HTTP headers.
341  * \param body
342  * \param[out] response Response to the HTTP request.
343  */
345  struct ast_tcptls_session_instance *ser,
346  struct ast_variable *get_params, struct ast_variable *path_vars,
347  struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
348 {
350  struct ast_variable *i;
351 #if defined(AST_DEVMODE)
352  int is_valid;
353  int code;
354 #endif /* AST_DEVMODE */
355 
356  for (i = get_params; i; i = i->next) {
357  if (strcmp(i->name, "from") == 0) {
358  args.from = (i->value);
359  } else
360  if (strcmp(i->name, "body") == 0) {
361  args.body = (i->value);
362  } else
363  {}
364  }
365  for (i = path_vars; i; i = i->next) {
366  if (strcmp(i->name, "tech") == 0) {
367  args.tech = (i->value);
368  } else
369  if (strcmp(i->name, "resource") == 0) {
370  args.resource = (i->value);
371  } else
372  {}
373  }
374  args.variables = body;
376 #if defined(AST_DEVMODE)
377  code = response->response_code;
378 
379  switch (code) {
380  case 0: /* Implementation is still a stub, or the code wasn't set */
381  is_valid = response->message == NULL;
382  break;
383  case 500: /* Internal Server Error */
384  case 501: /* Not Implemented */
385  case 400: /* Invalid parameters for sending a message. */
386  case 404: /* Endpoint not found */
387  is_valid = 1;
388  break;
389  default:
390  if (200 <= code && code <= 299) {
391  is_valid = ast_ari_validate_void(
392  response->message);
393  } else {
394  ast_log(LOG_ERROR, "Invalid error response %d for /endpoints/{tech}/{resource}/sendMessage\n", code);
395  is_valid = 0;
396  }
397  }
398 
399  if (!is_valid) {
400  ast_log(LOG_ERROR, "Response validation failed for /endpoints/{tech}/{resource}/sendMessage\n");
401  ast_ari_response_error(response, 500,
402  "Internal Server Error", "Response validation failed");
403  }
404 #endif /* AST_DEVMODE */
405 
406 fin: __attribute__((unused))
407  return;
408 }
409 
410 /*! \brief REST handler for /api-docs/endpoints.json */
412  .path_segment = "sendMessage",
413  .callbacks = {
415  },
416  .num_children = 0,
417  .children = { }
418 };
419 /*! \brief REST handler for /api-docs/endpoints.json */
421  .path_segment = "sendMessage",
422  .callbacks = {
424  },
425  .num_children = 0,
426  .children = { }
427 };
428 /*! \brief REST handler for /api-docs/endpoints.json */
430  .path_segment = "resource",
431  .is_wildcard = 1,
432  .callbacks = {
434  },
435  .num_children = 1,
436  .children = { &endpoints_tech_resource_sendMessage, }
437 };
438 /*! \brief REST handler for /api-docs/endpoints.json */
439 static struct stasis_rest_handlers endpoints_tech = {
440  .path_segment = "tech",
441  .is_wildcard = 1,
442  .callbacks = {
444  },
445  .num_children = 1,
446  .children = { &endpoints_tech_resource, }
447 };
448 /*! \brief REST handler for /api-docs/endpoints.json */
449 static struct stasis_rest_handlers endpoints = {
450  .path_segment = "endpoints",
451  .callbacks = {
453  },
454  .num_children = 2,
455  .children = { &endpoints_sendMessage,&endpoints_tech, }
456 };
457 
458 static int unload_module(void)
459 {
461  return 0;
462 }
463 
464 static int load_module(void)
465 {
466  int res = 0;
467 
468 
470  if (res) {
471  unload_module();
473  }
474 
476 }
477 
478 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "RESTful API module - Endpoint resources",
479  .support_level = AST_MODULE_SUPPORT_CORE,
480  .load = load_module,
481  .unload = unload_module,
482  .requires = "res_ari,res_ari_model,res_stasis",
483 );
int ast_ari_remove_handler(struct stasis_rest_handlers *handler)
Definition: res_ari.c:202
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:259
int ast_ari_add_handler(struct stasis_rest_handlers *handler)
Definition: res_ari.c:179
int ast_ari_validate_endpoint(struct ast_json *json)
Validator for Endpoint.
ari_validator ast_ari_validate_endpoint_fn(void)
Function pointer to ast_ari_validate_endpoint().
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
int ast_ari_validate_list(struct ast_json *json, int(*fn)(struct ast_json *))
Validator for a Swagger List[]/JSON array.
Asterisk main include file. File version handling, generic pbx functions.
#define ast_log
Definition: astobj2.c:42
@ AST_HTTP_PUT
Definition: http.h:63
@ AST_HTTP_GET
Definition: http.h:60
Application convenience functions, designed to give consistent look and feel to Asterisk apps.
#define LOG_ERROR
struct ast_json * ast_json_object_get(struct ast_json *object, const char *key)
Get a field from a JSON object.
Definition: json.c:397
const char * ast_json_string_get(const struct ast_json *string)
Get the value of a JSON string.
Definition: json.c:273
Asterisk module definitions.
@ AST_MODFLAG_DEFAULT
Definition: module.h:315
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition: module.h:543
@ 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_endpoints_send_message_parse_body(struct ast_json *body, struct ast_ari_endpoints_send_message_args *args)
Body parsing function for /endpoints/sendMessage.
static void ast_ari_endpoints_list_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 /endpoints.
static struct stasis_rest_handlers endpoints_tech_resource_sendMessage
REST handler for /api-docs/endpoints.json.
int ast_ari_endpoints_send_message_to_endpoint_parse_body(struct ast_json *body, struct ast_ari_endpoints_send_message_to_endpoint_args *args)
Body parsing function for /endpoints/{tech}/{resource}/sendMessage.
static void ast_ari_endpoints_list_by_tech_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 /endpoints/{tech}.
static struct stasis_rest_handlers endpoints_sendMessage
REST handler for /api-docs/endpoints.json.
static struct stasis_rest_handlers endpoints
REST handler for /api-docs/endpoints.json.
static void ast_ari_endpoints_get_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 /endpoints/{tech}/{resource}.
static void ast_ari_endpoints_send_message_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 /endpoints/sendMessage.
static struct stasis_rest_handlers endpoints_tech
REST handler for /api-docs/endpoints.json.
static int load_module(void)
static int unload_module(void)
static void ast_ari_endpoints_send_message_to_endpoint_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 /endpoints/{tech}/{resource}/sendMessage.
static struct stasis_rest_handlers endpoints_tech_resource
REST handler for /api-docs/endpoints.json.
#define NULL
Definition: resample.c:96
void ast_ari_endpoints_send_message(struct ast_variable *headers, struct ast_ari_endpoints_send_message_args *args, struct ast_ari_response *response)
Send a message to some technology URI or endpoint.
void ast_ari_endpoints_list_by_tech(struct ast_variable *headers, struct ast_ari_endpoints_list_by_tech_args *args, struct ast_ari_response *response)
List available endoints for a given endpoint technology.
void ast_ari_endpoints_send_message_to_endpoint(struct ast_variable *headers, struct ast_ari_endpoints_send_message_to_endpoint_args *args, struct ast_ari_response *response)
Send a message to some endpoint in a technology.
void ast_ari_endpoints_get(struct ast_variable *headers, struct ast_ari_endpoints_get_args *args, struct ast_ari_response *response)
Details for an endpoint.
void ast_ari_endpoints_list(struct ast_variable *headers, struct ast_ari_endpoints_list_args *args, struct ast_ari_response *response)
List all endpoints.
Generated file - declares stubs to be implemented in res/ari/resource_endpoints.c.
Stasis Application API. See Stasis Application API for detailed documentation.
struct ast_json * message
Definition: ari.h:94
int response_code
Definition: ari.h:99
Abstract JSON element (object, array, string, int, ...).
describes a server instance
Definition: tcptls.h:150
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
const char * path_segment
Definition: ari.h:71
const char * args