Asterisk - The Open Source Telephony Project  GIT-master-a24979a
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 <jrose@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 Mailboxes resources
30  *
31  * \author Jonathan Rose <jrose@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  <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 ser TCP/TLS session object
57  * \param get_params GET parameters in the HTTP request.
58  * \param path_vars Path variables extracted from the request.
59  * \param headers HTTP headers.
60  * \param body
61  * \param[out] response Response to the HTTP request.
62  */
64  struct ast_tcptls_session_instance *ser,
65  struct ast_variable *get_params, struct ast_variable *path_vars,
66  struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
67 {
69 #if defined(AST_DEVMODE)
70  int is_valid;
71  int code;
72 #endif /* AST_DEVMODE */
73 
74  ast_ari_mailboxes_list(headers, &args, response);
75 #if defined(AST_DEVMODE)
76  code = response->response_code;
77 
78  switch (code) {
79  case 0: /* Implementation is still a stub, or the code wasn't set */
80  is_valid = response->message == NULL;
81  break;
82  case 500: /* Internal Server Error */
83  case 501: /* Not Implemented */
84  is_valid = 1;
85  break;
86  default:
87  if (200 <= code && code <= 299) {
88  is_valid = ast_ari_validate_list(response->message,
90  } else {
91  ast_log(LOG_ERROR, "Invalid error response %d for /mailboxes\n", code);
92  is_valid = 0;
93  }
94  }
95 
96  if (!is_valid) {
97  ast_log(LOG_ERROR, "Response validation failed for /mailboxes\n");
98  ast_ari_response_error(response, 500,
99  "Internal Server Error", "Response validation failed");
100  }
101 #endif /* AST_DEVMODE */
102 
103 fin: __attribute__((unused))
104  return;
105 }
106 /*!
107  * \brief Parameter parsing callback for /mailboxes/{mailboxName}.
108  * \param ser TCP/TLS session object
109  * \param get_params GET parameters in the HTTP request.
110  * \param path_vars Path variables extracted from the request.
111  * \param headers HTTP headers.
112  * \param body
113  * \param[out] response Response to the HTTP request.
114  */
116  struct ast_tcptls_session_instance *ser,
117  struct ast_variable *get_params, struct ast_variable *path_vars,
118  struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
119 {
120  struct ast_ari_mailboxes_get_args args = {};
121  struct ast_variable *i;
122 #if defined(AST_DEVMODE)
123  int is_valid;
124  int code;
125 #endif /* AST_DEVMODE */
126 
127  for (i = path_vars; i; i = i->next) {
128  if (strcmp(i->name, "mailboxName") == 0) {
129  args.mailbox_name = (i->value);
130  } else
131  {}
132  }
133  ast_ari_mailboxes_get(headers, &args, response);
134 #if defined(AST_DEVMODE)
135  code = response->response_code;
136 
137  switch (code) {
138  case 0: /* Implementation is still a stub, or the code wasn't set */
139  is_valid = response->message == NULL;
140  break;
141  case 500: /* Internal Server Error */
142  case 501: /* Not Implemented */
143  case 404: /* Mailbox not found */
144  is_valid = 1;
145  break;
146  default:
147  if (200 <= code && code <= 299) {
148  is_valid = ast_ari_validate_mailbox(
149  response->message);
150  } else {
151  ast_log(LOG_ERROR, "Invalid error response %d for /mailboxes/{mailboxName}\n", code);
152  is_valid = 0;
153  }
154  }
155 
156  if (!is_valid) {
157  ast_log(LOG_ERROR, "Response validation failed for /mailboxes/{mailboxName}\n");
158  ast_ari_response_error(response, 500,
159  "Internal Server Error", "Response validation failed");
160  }
161 #endif /* AST_DEVMODE */
162 
163 fin: __attribute__((unused))
164  return;
165 }
167  struct ast_json *body,
169 {
170  struct ast_json *field;
171  /* Parse query parameters out of it */
172  field = ast_json_object_get(body, "oldMessages");
173  if (field) {
174  args->old_messages = ast_json_integer_get(field);
175  }
176  field = ast_json_object_get(body, "newMessages");
177  if (field) {
178  args->new_messages = ast_json_integer_get(field);
179  }
180  return 0;
181 }
182 
183 /*!
184  * \brief Parameter parsing callback for /mailboxes/{mailboxName}.
185  * \param ser TCP/TLS session object
186  * \param get_params GET parameters in the HTTP request.
187  * \param path_vars Path variables extracted from the request.
188  * \param headers HTTP headers.
189  * \param body
190  * \param[out] response Response to the HTTP request.
191  */
193  struct ast_tcptls_session_instance *ser,
194  struct ast_variable *get_params, struct ast_variable *path_vars,
195  struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
196 {
198  struct ast_variable *i;
199 #if defined(AST_DEVMODE)
200  int is_valid;
201  int code;
202 #endif /* AST_DEVMODE */
203 
204  for (i = get_params; i; i = i->next) {
205  if (strcmp(i->name, "oldMessages") == 0) {
206  args.old_messages = atoi(i->value);
207  } else
208  if (strcmp(i->name, "newMessages") == 0) {
209  args.new_messages = atoi(i->value);
210  } else
211  {}
212  }
213  for (i = path_vars; i; i = i->next) {
214  if (strcmp(i->name, "mailboxName") == 0) {
215  args.mailbox_name = (i->value);
216  } else
217  {}
218  }
221  goto fin;
222  }
223  ast_ari_mailboxes_update(headers, &args, response);
224 #if defined(AST_DEVMODE)
225  code = response->response_code;
226 
227  switch (code) {
228  case 0: /* Implementation is still a stub, or the code wasn't set */
229  is_valid = response->message == NULL;
230  break;
231  case 500: /* Internal Server Error */
232  case 501: /* Not Implemented */
233  case 404: /* Mailbox not found */
234  is_valid = 1;
235  break;
236  default:
237  if (200 <= code && code <= 299) {
238  is_valid = ast_ari_validate_void(
239  response->message);
240  } else {
241  ast_log(LOG_ERROR, "Invalid error response %d for /mailboxes/{mailboxName}\n", code);
242  is_valid = 0;
243  }
244  }
245 
246  if (!is_valid) {
247  ast_log(LOG_ERROR, "Response validation failed for /mailboxes/{mailboxName}\n");
248  ast_ari_response_error(response, 500,
249  "Internal Server Error", "Response validation failed");
250  }
251 #endif /* AST_DEVMODE */
252 
253 fin: __attribute__((unused))
254  return;
255 }
256 /*!
257  * \brief Parameter parsing callback for /mailboxes/{mailboxName}.
258  * \param ser TCP/TLS session object
259  * \param get_params GET parameters in the HTTP request.
260  * \param path_vars Path variables extracted from the request.
261  * \param headers HTTP headers.
262  * \param body
263  * \param[out] response Response to the HTTP request.
264  */
266  struct ast_tcptls_session_instance *ser,
267  struct ast_variable *get_params, struct ast_variable *path_vars,
268  struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
269 {
271  struct ast_variable *i;
272 #if defined(AST_DEVMODE)
273  int is_valid;
274  int code;
275 #endif /* AST_DEVMODE */
276 
277  for (i = path_vars; i; i = i->next) {
278  if (strcmp(i->name, "mailboxName") == 0) {
279  args.mailbox_name = (i->value);
280  } else
281  {}
282  }
283  ast_ari_mailboxes_delete(headers, &args, response);
284 #if defined(AST_DEVMODE)
285  code = response->response_code;
286 
287  switch (code) {
288  case 0: /* Implementation is still a stub, or the code wasn't set */
289  is_valid = response->message == NULL;
290  break;
291  case 500: /* Internal Server Error */
292  case 501: /* Not Implemented */
293  case 404: /* Mailbox not found */
294  is_valid = 1;
295  break;
296  default:
297  if (200 <= code && code <= 299) {
298  is_valid = ast_ari_validate_void(
299  response->message);
300  } else {
301  ast_log(LOG_ERROR, "Invalid error response %d for /mailboxes/{mailboxName}\n", code);
302  is_valid = 0;
303  }
304  }
305 
306  if (!is_valid) {
307  ast_log(LOG_ERROR, "Response validation failed for /mailboxes/{mailboxName}\n");
308  ast_ari_response_error(response, 500,
309  "Internal Server Error", "Response validation failed");
310  }
311 #endif /* AST_DEVMODE */
312 
313 fin: __attribute__((unused))
314  return;
315 }
316 
317 /*! \brief REST handler for /api-docs/mailboxes.json */
319  .path_segment = "mailboxName",
320  .is_wildcard = 1,
321  .callbacks = {
325  },
326  .num_children = 0,
327  .children = { }
328 };
329 /*! \brief REST handler for /api-docs/mailboxes.json */
330 static struct stasis_rest_handlers mailboxes = {
331  .path_segment = "mailboxes",
332  .callbacks = {
334  },
335  .num_children = 1,
336  .children = { &mailboxes_mailboxName, }
337 };
338 
339 static int unload_module(void)
340 {
342  return 0;
343 }
344 
345 static int load_module(void)
346 {
347  int res = 0;
348 
349 
351  if (res) {
352  unload_module();
354  }
355 
357 }
358 
359 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "RESTful API module - Mailboxes resources",
360  .support_level = AST_MODULE_SUPPORT_CORE,
361  .load = load_module,
362  .unload = unload_module,
363  .requires = "res_ari,res_ari_model,res_stasis,res_stasis_mailbox",
364 );
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
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_add_handler(struct stasis_rest_handlers *handler)
Definition: res_ari.c:179
int ast_ari_validate_mailbox(struct ast_json *json)
Validator for Mailbox.
ari_validator ast_ari_validate_mailbox_fn(void)
Function pointer to ast_ari_validate_mailbox().
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_DELETE
Definition: http.h:64
@ 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
intmax_t ast_json_integer_get(const struct ast_json *integer)
Get the value from a JSON integer.
Definition: json.c:322
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
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 struct stasis_rest_handlers mailboxes
REST handler for /api-docs/mailboxes.json.
static struct stasis_rest_handlers mailboxes_mailboxName
REST handler for /api-docs/mailboxes.json.
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}.
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)
static int unload_module(void)
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_mailboxes_update_parse_body(struct ast_json *body, struct ast_ari_mailboxes_update_args *args)
Body parsing function for /mailboxes/{mailboxName}.
#define NULL
Definition: resample.c:96
void ast_ari_mailboxes_delete(struct ast_variable *headers, struct ast_ari_mailboxes_delete_args *args, struct ast_ari_response *response)
Destroy a 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.
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).
void ast_ari_mailboxes_list(struct ast_variable *headers, struct ast_ari_mailboxes_list_args *args, struct ast_ari_response *response)
List all mailboxes.
Generated file - declares stubs to be implemented in res/ari/resource_mailboxes.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