Asterisk - The Open Source Telephony Project  18.5.0
res_ari_mailboxes.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * Jonathan Rose <[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 /*
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 Mailboxes resources
30  *
31  * \author Jonathan Rose <[email protected]>
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_stasis_mailbox</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_mailboxes.h"
48 #if defined(AST_DEVMODE)
50 #endif
51 
52 #define MAX_VALS 128
53 
54 /*!
55  * \brief Parameter parsing callback for /mailboxes.
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.
60  */
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_json *body, struct ast_ari_response *response)
65 {
66  struct ast_ari_mailboxes_list_args args = {};
67 #if defined(AST_DEVMODE)
68  int is_valid;
69  int code;
70 #endif /* AST_DEVMODE */
71 
72  ast_ari_mailboxes_list(headers, &args, response);
73 #if defined(AST_DEVMODE)
74  code = response->response_code;
75 
76  switch (code) {
77  case 0: /* Implementation is still a stub, or the code wasn't set */
78  is_valid = response->message == NULL;
79  break;
80  case 500: /* Internal Server Error */
81  case 501: /* Not Implemented */
82  is_valid = 1;
83  break;
84  default:
85  if (200 <= code && code <= 299) {
86  is_valid = ast_ari_validate_list(response->message,
88  } else {
89  ast_log(LOG_ERROR, "Invalid error response %d for /mailboxes\n", code);
90  is_valid = 0;
91  }
92  }
93 
94  if (!is_valid) {
95  ast_log(LOG_ERROR, "Response validation failed for /mailboxes\n");
96  ast_ari_response_error(response, 500,
97  "Internal Server Error", "Response validation failed");
98  }
99 #endif /* AST_DEVMODE */
100 
101 fin: __attribute__((unused))
102  return;
103 }
104 /*!
105  * \brief Parameter parsing callback for /mailboxes/{mailboxName}.
106  * \param get_params GET parameters in the HTTP request.
107  * \param path_vars Path variables extracted from the request.
108  * \param headers HTTP headers.
109  * \param[out] response Response to the HTTP request.
110  */
112  struct ast_tcptls_session_instance *ser,
113  struct ast_variable *get_params, struct ast_variable *path_vars,
114  struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
115 {
116  struct ast_ari_mailboxes_get_args args = {};
117  struct ast_variable *i;
118 #if defined(AST_DEVMODE)
119  int is_valid;
120  int code;
121 #endif /* AST_DEVMODE */
122 
123  for (i = path_vars; i; i = i->next) {
124  if (strcmp(i->name, "mailboxName") == 0) {
125  args.mailbox_name = (i->value);
126  } else
127  {}
128  }
129  ast_ari_mailboxes_get(headers, &args, response);
130 #if defined(AST_DEVMODE)
131  code = response->response_code;
132 
133  switch (code) {
134  case 0: /* Implementation is still a stub, or the code wasn't set */
135  is_valid = response->message == NULL;
136  break;
137  case 500: /* Internal Server Error */
138  case 501: /* Not Implemented */
139  case 404: /* Mailbox not found */
140  is_valid = 1;
141  break;
142  default:
143  if (200 <= code && code <= 299) {
144  is_valid = ast_ari_validate_mailbox(
145  response->message);
146  } else {
147  ast_log(LOG_ERROR, "Invalid error response %d for /mailboxes/{mailboxName}\n", code);
148  is_valid = 0;
149  }
150  }
151 
152  if (!is_valid) {
153  ast_log(LOG_ERROR, "Response validation failed for /mailboxes/{mailboxName}\n");
154  ast_ari_response_error(response, 500,
155  "Internal Server Error", "Response validation failed");
156  }
157 #endif /* AST_DEVMODE */
158 
159 fin: __attribute__((unused))
160  return;
161 }
163  struct ast_json *body,
165 {
166  struct ast_json *field;
167  /* Parse query parameters out of it */
168  field = ast_json_object_get(body, "oldMessages");
169  if (field) {
170  args->old_messages = ast_json_integer_get(field);
171  }
172  field = ast_json_object_get(body, "newMessages");
173  if (field) {
174  args->new_messages = ast_json_integer_get(field);
175  }
176  return 0;
177 }
178 
179 /*!
180  * \brief Parameter parsing callback for /mailboxes/{mailboxName}.
181  * \param get_params GET parameters in the HTTP request.
182  * \param path_vars Path variables extracted from the request.
183  * \param headers HTTP headers.
184  * \param[out] response Response to the HTTP request.
185  */
187  struct ast_tcptls_session_instance *ser,
188  struct ast_variable *get_params, struct ast_variable *path_vars,
189  struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
190 {
191  struct ast_ari_mailboxes_update_args args = {};
192  struct ast_variable *i;
193 #if defined(AST_DEVMODE)
194  int is_valid;
195  int code;
196 #endif /* AST_DEVMODE */
197 
198  for (i = get_params; i; i = i->next) {
199  if (strcmp(i->name, "oldMessages") == 0) {
200  args.old_messages = atoi(i->value);
201  } else
202  if (strcmp(i->name, "newMessages") == 0) {
203  args.new_messages = atoi(i->value);
204  } else
205  {}
206  }
207  for (i = path_vars; i; i = i->next) {
208  if (strcmp(i->name, "mailboxName") == 0) {
209  args.mailbox_name = (i->value);
210  } else
211  {}
212  }
213  if (ast_ari_mailboxes_update_parse_body(body, &args)) {
215  goto fin;
216  }
217  ast_ari_mailboxes_update(headers, &args, response);
218 #if defined(AST_DEVMODE)
219  code = response->response_code;
220 
221  switch (code) {
222  case 0: /* Implementation is still a stub, or the code wasn't set */
223  is_valid = response->message == NULL;
224  break;
225  case 500: /* Internal Server Error */
226  case 501: /* Not Implemented */
227  case 404: /* Mailbox not found */
228  is_valid = 1;
229  break;
230  default:
231  if (200 <= code && code <= 299) {
232  is_valid = ast_ari_validate_void(
233  response->message);
234  } else {
235  ast_log(LOG_ERROR, "Invalid error response %d for /mailboxes/{mailboxName}\n", code);
236  is_valid = 0;
237  }
238  }
239 
240  if (!is_valid) {
241  ast_log(LOG_ERROR, "Response validation failed for /mailboxes/{mailboxName}\n");
242  ast_ari_response_error(response, 500,
243  "Internal Server Error", "Response validation failed");
244  }
245 #endif /* AST_DEVMODE */
246 
247 fin: __attribute__((unused))
248  return;
249 }
250 /*!
251  * \brief Parameter parsing callback for /mailboxes/{mailboxName}.
252  * \param get_params GET parameters in the HTTP request.
253  * \param path_vars Path variables extracted from the request.
254  * \param headers HTTP headers.
255  * \param[out] response Response to the HTTP request.
256  */
258  struct ast_tcptls_session_instance *ser,
259  struct ast_variable *get_params, struct ast_variable *path_vars,
260  struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
261 {
262  struct ast_ari_mailboxes_delete_args args = {};
263  struct ast_variable *i;
264 #if defined(AST_DEVMODE)
265  int is_valid;
266  int code;
267 #endif /* AST_DEVMODE */
268 
269  for (i = path_vars; i; i = i->next) {
270  if (strcmp(i->name, "mailboxName") == 0) {
271  args.mailbox_name = (i->value);
272  } else
273  {}
274  }
275  ast_ari_mailboxes_delete(headers, &args, response);
276 #if defined(AST_DEVMODE)
277  code = response->response_code;
278 
279  switch (code) {
280  case 0: /* Implementation is still a stub, or the code wasn't set */
281  is_valid = response->message == NULL;
282  break;
283  case 500: /* Internal Server Error */
284  case 501: /* Not Implemented */
285  case 404: /* Mailbox not found */
286  is_valid = 1;
287  break;
288  default:
289  if (200 <= code && code <= 299) {
290  is_valid = ast_ari_validate_void(
291  response->message);
292  } else {
293  ast_log(LOG_ERROR, "Invalid error response %d for /mailboxes/{mailboxName}\n", code);
294  is_valid = 0;
295  }
296  }
297 
298  if (!is_valid) {
299  ast_log(LOG_ERROR, "Response validation failed for /mailboxes/{mailboxName}\n");
300  ast_ari_response_error(response, 500,
301  "Internal Server Error", "Response validation failed");
302  }
303 #endif /* AST_DEVMODE */
304 
305 fin: __attribute__((unused))
306  return;
307 }
308 
309 /*! \brief REST handler for /api-docs/mailboxes.json */
311  .path_segment = "mailboxName",
312  .is_wildcard = 1,
313  .callbacks = {
317  },
318  .num_children = 0,
319  .children = { }
320 };
321 /*! \brief REST handler for /api-docs/mailboxes.json */
323  .path_segment = "mailboxes",
324  .callbacks = {
326  },
327  .num_children = 1,
328  .children = { &mailboxes_mailboxName, }
329 };
330 
331 static int unload_module(void)
332 {
333  ast_ari_remove_handler(&mailboxes);
334  return 0;
335 }
336 
337 static int load_module(void)
338 {
339  int res = 0;
340 
341 
342  res |= ast_ari_add_handler(&mailboxes);
343  if (res) {
344  unload_module();
346  }
347 
349 }
350 
351 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "RESTful API module - Mailboxes resources",
352  .support_level = AST_MODULE_SUPPORT_CORE,
353  .load = load_module,
354  .unload = unload_module,
355  .requires = "res_ari,res_ari_model,res_stasis,res_stasis_mailbox",
356 );
void ast_ari_mailboxes_update(struct ast_variable *headers, struct ast_ari_mailboxes_update_args *args, struct ast_ari_response *response)
Change the state of a mailbox. (Note - implicitly creates the mailbox).
struct ast_variable * next
ari_validator ast_ari_validate_mailbox_fn(void)
Function pointer to ast_ari_validate_mailbox().
int ast_ari_mailboxes_update_parse_body(struct ast_json *body, struct ast_ari_mailboxes_update_args *args)
Body parsing function for /mailboxes/{mailboxName}.
Asterisk main include file. File version handling, generic pbx functions.
static void ast_ari_mailboxes_delete_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 /mailboxes/{mailboxName}.
static int load_module(void)
void ast_ari_mailboxes_delete(struct ast_variable *headers, struct ast_ari_mailboxes_delete_args *args, struct ast_ari_response *response)
Destroy a mailbox.
static struct stasis_rest_handlers mailboxes
REST handler for /api-docs/mailboxes.json.
Structure for variables, used for configurations and for channel variables.
Generated file - Build validators for ARI model objects.
const char * args
#define NULL
Definition: resample.c:96
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:298
static void ast_ari_mailboxes_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 /mailboxes.
int ast_ari_validate_void(struct ast_json *json)
Validator for native Swagger void.
Definition: res_ari_model.c:91
int response_code
Definition: ari.h:98
#define ast_log
Definition: astobj2.c:42
static void ast_ari_mailboxes_update_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 /mailboxes/{mailboxName}.
static int unload_module(void)
describes a server instance
Definition: tcptls.h:149
#define LOG_ERROR
Definition: logger.h:285
int ast_ari_add_handler(struct stasis_rest_handlers *handler)
Definition: res_ari.c:179
Generated file - declares stubs to be implemented in res/ari/resource_mailboxes.c.
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
static void ast_ari_mailboxes_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 /mailboxes/{mailboxName}.
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
struct ast_json * message
Definition: ari.h:93
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS|AST_MODFLAG_LOAD_ORDER, "HTTP Phone Provisioning",.support_level=AST_MODULE_SUPPORT_EXTENDED,.load=load_module,.unload=unload_module,.reload=reload,.load_pri=AST_MODPRI_CHANNEL_DEPEND,.requires="http",)
int ast_ari_validate_mailbox(struct ast_json *json)
Validator for Mailbox.
const char * path_segment
Definition: ari.h:70
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
static struct stasis_rest_handlers mailboxes_mailboxName
REST handler for /api-docs/mailboxes.json.
Abstract JSON element (object, array, string, int, ...).
void ast_ari_mailboxes_get(struct ast_variable *headers, struct ast_ari_mailboxes_get_args *args, struct ast_ari_response *response)
Retrieve the current state of a mailbox.
Stasis Application API. See Stasis Application API for detailed documentation.
int ast_ari_validate_list(struct ast_json *json, int(*fn)(struct ast_json *))
Validator for a Swagger List[]/JSON array.
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Asterisk module definitions.
intmax_t ast_json_integer_get(const struct ast_json *integer)
Get the value from a JSON integer.
Definition: json.c:322
Application convenience functions, designed to give consistent look and feel to Asterisk apps...
Handler for a single RESTful path segment.
Definition: ari.h:68
void ast_ari_mailboxes_list(struct ast_variable *headers, struct ast_ari_mailboxes_list_args *args, struct ast_ari_response *response)
List all mailboxes.