summaryrefslogtreecommitdiff
path: root/res/ari/resource_applications.c
blob: fc93b28e228c98db416837e17cfdc7fdb2493b46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 2013, Digium, Inc.
 *
 * David M. Lee, II <dlee@digium.com>
 *
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2. See the LICENSE file
 * at the top of the source tree.
 */

/*! \file
 *
 * \brief /api-docs/applications.{format} implementation - Stasis application
 * resources
 *
 * \author David M. Lee, II <dlee@digium.com>
 */

#include "asterisk.h"

#include "asterisk/stasis_app.h"
#include "resource_applications.h"

static int append_json(void *obj, void *arg, int flags)
{
	const char *app = obj;
	struct ast_json *array = arg;

	ast_json_array_append(array, stasis_app_to_json(app));

	return 0;
}

void ast_ari_applications_list(struct ast_variable *headers,
	struct ast_ari_applications_list_args *args,
	struct ast_ari_response *response)
{
	RAII_VAR(struct ao2_container *, apps, NULL, ao2_cleanup);
	RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
	size_t count;

	apps = stasis_app_get_all();
	json = ast_json_array_create();
	if (!apps || !json) {
		ast_ari_response_error(response, 500, "Internal Server Error",
			"Allocation failed");
		return;
	}

	ao2_lock(apps);
	count = ao2_container_count(apps);
	ao2_callback(apps, OBJ_NOLOCK | OBJ_NODATA, append_json, json);
	ao2_lock(apps);

	if (count != ast_json_array_size(json)) {
		ast_ari_response_error(response, 500, "Internal Server Error",
			"Allocation failed");
		return;
	}

	ast_ari_response_ok(response, ast_json_ref(json));
}

void ast_ari_applications_get(struct ast_variable *headers,
	struct ast_ari_applications_get_args *args,
	struct ast_ari_response *response)
{
	struct ast_json *json;

	json = stasis_app_to_json(args->application_name);

	if (!json) {
		ast_ari_response_error(response, 404, "Not Found",
			"Application not found");
		return;
	}

	ast_ari_response_ok(response, json);
}

void ast_ari_applications_subscribe(struct ast_variable *headers,
	struct ast_ari_applications_subscribe_args *args,
	struct ast_ari_response *response)
{
	RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
	enum stasis_app_subscribe_res res;

	if (args->event_source_count <= 0) {
		ast_ari_response_error(response, 400, "Bad Request",
			"Missing parameter eventSource");
		return;
	}

	if (ast_strlen_zero(args->application_name)) {
		ast_ari_response_error(response, 400, "Bad Request",
			"Missing parameter applicationName");
		return;
	}

	res = stasis_app_subscribe(args->application_name, args->event_source,
		args->event_source_count, &json);

	switch (res) {
	case STASIS_ASR_OK:
		ast_ari_response_ok(response, ast_json_ref(json));
		break;
	case STASIS_ASR_APP_NOT_FOUND:
		ast_ari_response_error(response, 404, "Not Found",
			"Application not found");
		break;
	case STASIS_ASR_EVENT_SOURCE_NOT_FOUND:
		ast_ari_response_error(response, 422, "Unprocessable Entity",
			"Event source does not exist");
		break;
	case STASIS_ASR_EVENT_SOURCE_BAD_SCHEME:
		ast_ari_response_error(response, 400, "Bad Request",
			"Invalid event source URI scheme");
		break;
	case STASIS_ASR_INTERNAL_ERROR:
		ast_ari_response_error(response, 500, "Internal Server Error",
			"Error processing request");
		break;
	}
}

void ast_ari_applications_unsubscribe(struct ast_variable *headers,
	struct ast_ari_applications_unsubscribe_args *args,
	struct ast_ari_response *response)
{
	RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
	enum stasis_app_subscribe_res res;

	if (args->event_source_count == 0) {
		ast_ari_response_error(response, 400, "Bad Request",
			"Missing parameter eventSource");
		return;
	}

	res = stasis_app_unsubscribe(args->application_name, args->event_source,
		args->event_source_count, &json);

	switch (res) {
	case STASIS_ASR_OK:
		ast_ari_response_ok(response, ast_json_ref(json));
		break;
	case STASIS_ASR_APP_NOT_FOUND:
		ast_ari_response_error(response, 404, "Not Found",
			"Application not found");
		break;
	case STASIS_ASR_EVENT_SOURCE_NOT_FOUND:
		ast_ari_response_error(response, 422, "Unprocessable Entity",
			"Event source was not subscribed to");
		break;
	case STASIS_ASR_EVENT_SOURCE_BAD_SCHEME:
		ast_ari_response_error(response, 400, "Bad Request",
			"Invalid event source URI scheme");
		break;
	case STASIS_ASR_INTERNAL_ERROR:
		ast_ari_response_error(response, 500, "Internal Server Error",
			"Error processing request");
	}
}