Asterisk - The Open Source Telephony Project  18.5.0
Macros | Functions | Variables
res_ari_mailboxes.c File Reference

Mailboxes resources. More...

#include "asterisk.h"
#include "asterisk/app.h"
#include "asterisk/module.h"
#include "asterisk/stasis_app.h"
#include "ari/resource_mailboxes.h"
#include "ari/ari_model_validators.h"
Include dependency graph for res_ari_mailboxes.c:

Go to the source code of this file.

Macros

#define MAX_VALS   128
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
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}. More...
 
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}. More...
 
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. More...
 
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}. More...
 
int ast_ari_mailboxes_update_parse_body (struct ast_json *body, struct ast_ari_mailboxes_update_args *args)
 Body parsing function for /mailboxes/{mailboxName}. More...
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
static int load_module (void)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "RESTful API module - Mailboxes resources" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "30ef0c93b36035ec78c9cfd712d36d9b" , .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .requires = "res_ari,res_ari_model,res_stasis,res_stasis_mailbox", }
 
static const struct ast_module_infoast_module_info = &__mod_info
 
static struct stasis_rest_handlers mailboxes
 REST handler for /api-docs/mailboxes.json. More...
 
static struct stasis_rest_handlers mailboxes_mailboxName
 REST handler for /api-docs/mailboxes.json. More...
 

Detailed Description

Mailboxes resources.

Author
Jonathan Rose jrose.nosp@m.@dig.nosp@m.ium.c.nosp@m.om

Definition in file res_ari_mailboxes.c.

Macro Definition Documentation

◆ MAX_VALS

#define MAX_VALS   128

Definition at line 52 of file res_ari_mailboxes.c.

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 356 of file res_ari_mailboxes.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 356 of file res_ari_mailboxes.c.

◆ ast_ari_mailboxes_delete_cb()

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 
)
static

Parameter parsing callback for /mailboxes/{mailboxName}.

Parameters
get_paramsGET parameters in the HTTP request.
path_varsPath variables extracted from the request.
headersHTTP headers.
[out]responseResponse to the HTTP request.

Definition at line 257 of file res_ari_mailboxes.c.

References ast_ari_mailboxes_delete(), ast_ari_response_error(), ast_ari_validate_void(), ast_log, LOG_ERROR, ast_ari_mailboxes_delete_args::mailbox_name, ast_ari_response::message, ast_variable::name, ast_variable::next, NULL, ast_ari_response::response_code, and ast_variable::value.

261 {
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 }
struct ast_variable * next
void ast_ari_mailboxes_delete(struct ast_variable *headers, struct ast_ari_mailboxes_delete_args *args, struct ast_ari_response *response)
Destroy a mailbox.
Structure for variables, used for configurations and for channel variables.
const char * args
#define NULL
Definition: resample.c:96
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
#define LOG_ERROR
Definition: logger.h:285
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
struct ast_json * message
Definition: ari.h:93

◆ ast_ari_mailboxes_get_cb()

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 
)
static

Parameter parsing callback for /mailboxes/{mailboxName}.

Parameters
get_paramsGET parameters in the HTTP request.
path_varsPath variables extracted from the request.
headersHTTP headers.
[out]responseResponse to the HTTP request.

Definition at line 111 of file res_ari_mailboxes.c.

References ast_ari_mailboxes_get(), ast_ari_response_error(), ast_ari_validate_mailbox(), ast_log, LOG_ERROR, ast_ari_mailboxes_get_args::mailbox_name, ast_ari_response::message, ast_variable::name, ast_variable::next, NULL, ast_ari_response::response_code, and ast_variable::value.

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 }
struct ast_variable * next
Structure for variables, used for configurations and for channel variables.
const char * args
#define NULL
Definition: resample.c:96
int response_code
Definition: ari.h:98
#define ast_log
Definition: astobj2.c:42
#define LOG_ERROR
Definition: logger.h:285
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
struct ast_json * message
Definition: ari.h:93
int ast_ari_validate_mailbox(struct ast_json *json)
Validator for Mailbox.
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.

◆ ast_ari_mailboxes_list_cb()

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 
)
static

Parameter parsing callback for /mailboxes.

Parameters
get_paramsGET parameters in the HTTP request.
path_varsPath variables extracted from the request.
headersHTTP headers.
[out]responseResponse to the HTTP request.

Definition at line 61 of file res_ari_mailboxes.c.

References ast_ari_mailboxes_list(), ast_ari_response_error(), ast_ari_validate_list(), ast_ari_validate_mailbox_fn(), ast_log, LOG_ERROR, ast_ari_response::message, NULL, and ast_ari_response::response_code.

65 {
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 }
ari_validator ast_ari_validate_mailbox_fn(void)
Function pointer to ast_ari_validate_mailbox().
const char * args
#define NULL
Definition: resample.c:96
int response_code
Definition: ari.h:98
#define ast_log
Definition: astobj2.c:42
#define LOG_ERROR
Definition: logger.h:285
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
struct ast_json * message
Definition: ari.h:93
int ast_ari_validate_list(struct ast_json *json, int(*fn)(struct ast_json *))
Validator for a Swagger List[]/JSON array.
void ast_ari_mailboxes_list(struct ast_variable *headers, struct ast_ari_mailboxes_list_args *args, struct ast_ari_response *response)
List all mailboxes.

◆ ast_ari_mailboxes_update_cb()

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 
)
static

Parameter parsing callback for /mailboxes/{mailboxName}.

Parameters
get_paramsGET parameters in the HTTP request.
path_varsPath variables extracted from the request.
headersHTTP headers.
[out]responseResponse to the HTTP request.

Definition at line 186 of file res_ari_mailboxes.c.

References ast_ari_mailboxes_update(), ast_ari_mailboxes_update_parse_body(), ast_ari_response_alloc_failed(), ast_ari_response_error(), ast_ari_validate_void(), ast_log, LOG_ERROR, ast_ari_mailboxes_update_args::mailbox_name, ast_ari_response::message, ast_variable::name, ast_ari_mailboxes_update_args::new_messages, ast_variable::next, NULL, ast_ari_mailboxes_update_args::old_messages, ast_ari_response::response_code, and ast_variable::value.

190 {
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 }
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
int ast_ari_mailboxes_update_parse_body(struct ast_json *body, struct ast_ari_mailboxes_update_args *args)
Body parsing function for /mailboxes/{mailboxName}.
Structure for variables, used for configurations and for channel variables.
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
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
#define LOG_ERROR
Definition: logger.h:285
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
struct ast_json * message
Definition: ari.h:93

◆ ast_ari_mailboxes_update_parse_body()

int ast_ari_mailboxes_update_parse_body ( struct ast_json body,
struct ast_ari_mailboxes_update_args args 
)

Body parsing function for /mailboxes/{mailboxName}.

Parameters
bodyThe JSON body from which to parse parameters.
[out]argsThe args structure to parse into.
Return values
zeroon success
non-zeroon failure

Definition at line 162 of file res_ari_mailboxes.c.

References ast_json_integer_get(), ast_json_object_get(), ast_ari_mailboxes_update_args::new_messages, and ast_ari_mailboxes_update_args::old_messages.

Referenced by ast_ari_mailboxes_update_cb().

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 }
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
Abstract JSON element (object, array, string, int, ...).
intmax_t ast_json_integer_get(const struct ast_json *integer)
Get the value from a JSON integer.
Definition: json.c:322

◆ AST_MODULE_SELF_SYM()

struct ast_module* AST_MODULE_SELF_SYM ( void  )

Definition at line 356 of file res_ari_mailboxes.c.

◆ load_module()

static int load_module ( void  )
static

Definition at line 337 of file res_ari_mailboxes.c.

References ast_ari_add_handler(), AST_MODFLAG_DEFAULT, AST_MODULE_INFO(), AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, AST_MODULE_SUPPORT_CORE, ASTERISK_GPL_KEY, and unload_module().

338 {
339  int res = 0;
340 
341 
343  if (res) {
344  unload_module();
346  }
347 
349 }
static struct stasis_rest_handlers mailboxes
REST handler for /api-docs/mailboxes.json.
static int unload_module(void)
int ast_ari_add_handler(struct stasis_rest_handlers *handler)
Definition: res_ari.c:179
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 331 of file res_ari_mailboxes.c.

References ast_ari_remove_handler().

Referenced by load_module().

332 {
334  return 0;
335 }
static struct stasis_rest_handlers mailboxes
REST handler for /api-docs/mailboxes.json.
int ast_ari_remove_handler(struct stasis_rest_handlers *handler)
Definition: res_ari.c:202

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "RESTful API module - Mailboxes resources" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "30ef0c93b36035ec78c9cfd712d36d9b" , .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .requires = "res_ari,res_ari_model,res_stasis,res_stasis_mailbox", }
static

Definition at line 356 of file res_ari_mailboxes.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 356 of file res_ari_mailboxes.c.

◆ mailboxes

struct stasis_rest_handlers mailboxes
static

◆ mailboxes_mailboxName

struct stasis_rest_handlers mailboxes_mailboxName
static

REST handler for /api-docs/mailboxes.json.

Definition at line 310 of file res_ari_mailboxes.c.