Asterisk - The Open Source Telephony Project  18.5.0
resource_applications.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  * David M. Lee, II <[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 /*! \file
20  *
21  * \brief /api-docs/applications.{format} implementation - Stasis application
22  * resources
23  *
24  * \author David M. Lee, II <[email protected]>
25  */
26 
27 #include "asterisk.h"
28 
29 #include "asterisk/stasis_app.h"
30 #include "resource_applications.h"
31 
32 static int append_json(void *obj, void *arg, int flags)
33 {
34  const char *app = obj;
35  struct ast_json *array = arg;
36 
38 
39  return 0;
40 }
41 
44  struct ast_ari_response *response)
45 {
47  RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
48  size_t count;
49 
51  json = ast_json_array_create();
52  if (!apps || !json) {
53  ast_ari_response_error(response, 500, "Internal Server Error",
54  "Allocation failed");
55  return;
56  }
57 
58  ao2_lock(apps);
59  count = ao2_container_count(apps);
62 
63  if (count != ast_json_array_size(json)) {
64  ast_ari_response_error(response, 500, "Internal Server Error",
65  "Allocation failed");
66  return;
67  }
68 
69  ast_ari_response_ok(response, ast_json_ref(json));
70 }
71 
74  struct ast_ari_response *response)
75 {
76  struct ast_json *json;
77 
79 
80  if (!json) {
81  ast_ari_response_error(response, 404, "Not Found",
82  "Application not found");
83  return;
84  }
85 
86  ast_ari_response_ok(response, json);
87 }
88 
91  struct ast_ari_response *response)
92 {
93  RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
94  enum stasis_app_subscribe_res res;
95 
96  if (args->event_source_count <= 0) {
97  ast_ari_response_error(response, 400, "Bad Request",
98  "Missing parameter eventSource");
99  return;
100  }
101 
102  if (ast_strlen_zero(args->application_name)) {
103  ast_ari_response_error(response, 400, "Bad Request",
104  "Missing parameter applicationName");
105  return;
106  }
107 
109  args->event_source_count, &json);
110 
111  switch (res) {
112  case STASIS_ASR_OK:
113  ast_ari_response_ok(response, ast_json_ref(json));
114  break;
116  ast_ari_response_error(response, 404, "Not Found",
117  "Application not found");
118  break;
120  ast_ari_response_error(response, 422, "Unprocessable Entity",
121  "Event source does not exist");
122  break;
124  ast_ari_response_error(response, 400, "Bad Request",
125  "Invalid event source URI scheme");
126  break;
128  ast_ari_response_error(response, 500, "Internal Server Error",
129  "Error processing request");
130  break;
131  }
132 }
133 
136  struct ast_ari_response *response)
137 {
138  RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
139  enum stasis_app_subscribe_res res;
140 
141  if (args->event_source_count == 0) {
142  ast_ari_response_error(response, 400, "Bad Request",
143  "Missing parameter eventSource");
144  return;
145  }
146 
148  args->event_source_count, &json);
149 
150  switch (res) {
151  case STASIS_ASR_OK:
152  ast_ari_response_ok(response, ast_json_ref(json));
153  break;
155  ast_ari_response_error(response, 404, "Not Found",
156  "Application not found");
157  break;
159  ast_ari_response_error(response, 422, "Unprocessable Entity",
160  "Event source was not subscribed to");
161  break;
163  ast_ari_response_error(response, 400, "Bad Request",
164  "Invalid event source URI scheme");
165  break;
167  ast_ari_response_error(response, 500, "Internal Server Error",
168  "Error processing request");
169  }
170 }
171 
174  struct ast_ari_response *response)
175 {
177 
178  if (!app) {
179  ast_ari_response_error(response, 404, "Not Found", "Application not found");
180  return;
181  }
182 
183  if (stasis_app_event_filter_set(app, args->filter)) {
184  ast_ari_response_error(response, 400, "Bad Request", "Invalid format definition");
185  } else {
187  }
188 
189  ao2_ref(app, -1);
190 }
struct ast_json * stasis_app_object_to_json(struct stasis_app *app)
Return the JSON representation of a Stasis application.
Definition: res_stasis.c:1866
enum stasis_app_subscribe_res stasis_app_unsubscribe(const char *app_name, const char **event_source_uris, int event_sources_count, struct ast_json **json)
Unsubscribes an application from a list of event sources.
Definition: res_stasis.c:2086
struct stasis_app * stasis_app_get_by_name(const char *name)
Retrieve a handle to a Stasis application by its name.
Definition: res_stasis.c:1694
int stasis_app_event_filter_set(struct stasis_app *app, struct ast_json *filter)
Set the application&#39;s event type filter.
struct ast_json * ast_json_ref(struct ast_json *value)
Increase refcount on value.
Definition: json.c:67
Asterisk main include file. File version handling, generic pbx functions.
int ao2_container_count(struct ao2_container *c)
Returns the number of elements in a container.
Registered applications container.
Definition: pbx_app.c:67
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
Definition: json.c:73
void ast_ari_applications_list(struct ast_variable *headers, struct ast_ari_applications_list_args *args, struct ast_ari_response *response)
List all applications.
#define ao2_callback(c, flags, cb_fn, arg)
Definition: astobj2.h:1716
Structure for variables, used for configurations and for channel variables.
Assume that the ao2_container is already locked.
Definition: astobj2.h:1067
void ast_ari_applications_unsubscribe(struct ast_variable *headers, struct ast_ari_applications_unsubscribe_args *args, struct ast_ari_response *response)
Unsubscribe an application from an event source.
#define ao2_unlock(a)
Definition: astobj2.h:730
const char * args
#define NULL
Definition: resample.c:96
static int append_json(void *obj, void *arg, int flags)
#define ast_strlen_zero(foo)
Definition: strings.h:52
void ast_ari_applications_get(struct ast_variable *headers, struct ast_ari_applications_get_args *args, struct ast_ari_response *response)
Get details of an application.
void ast_ari_applications_subscribe(struct ast_variable *headers, struct ast_ari_applications_subscribe_args *args, struct ast_ari_response *response)
Subscribe an application to a event source.
enum stasis_app_subscribe_res stasis_app_subscribe(const char *app_name, const char **event_source_uris, int event_sources_count, struct ast_json **json)
Subscribes an application to a list of event sources.
Definition: res_stasis.c:2045
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:911
void ast_ari_response_ok(struct ast_ari_response *response, struct ast_json *message)
Fill in an OK (200) ast_ari_response.
Definition: res_ari.c:276
#define ao2_ref(o, delta)
Definition: astobj2.h:464
#define ao2_lock(a)
Definition: astobj2.h:718
struct ast_json * ast_json_array_create(void)
Create a empty JSON array.
Definition: json.c:352
int ast_json_array_append(struct ast_json *array, struct ast_json *value)
Append to an array.
Definition: json.c:368
static int array(struct ast_channel *chan, const char *cmd, char *var, const char *value)
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 ao2_container * stasis_app_get_all(void)
Gets the names of all registered Stasis applications.
Definition: res_stasis.c:1708
stasis_app_subscribe_res
Return code for stasis_app_[un]subscribe.
Definition: stasis_app.h:282
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
size_t ast_json_array_size(const struct ast_json *array)
Get the size of a JSON array.
Definition: json.c:356
Abstract JSON element (object, array, string, int, ...).
Generated file - declares stubs to be implemented in res/ari/resource_applications.c.
Stasis Application API. See Stasis Application API for detailed documentation.
Generic container type.
static const char app[]
Definition: app_mysql.c:62
struct ast_json * stasis_app_to_json(const char *app_name)
Return the JSON representation of a Stasis application.
Definition: res_stasis.c:1876
void ast_ari_applications_filter(struct ast_variable *headers, struct ast_ari_applications_filter_args *args, struct ast_ari_response *response)
Filter application events types.