Exemple #1
0
static int app_event_filter_set(struct stasis_app *app,	struct ast_json **member,
	struct ast_json *filter, const char *filter_type)
{
	if (filter && ast_json_typeof(filter) == AST_JSON_OBJECT) {
		if (!ast_json_object_size(filter)) {
			/* If no filters are specified then reset this filter type */
			filter = NULL;
		} else {
			/* Otherwise try to get the filter array for this type */
			filter = ast_json_object_get(filter, filter_type);
			if (!filter) {
				/* A filter type exists, but not this one, so don't update */
				return 0;
			}
		}
	}

	/* At this point the filter object should be an array */
	if (filter && ast_json_typeof(filter) != AST_JSON_ARRAY) {
		ast_log(LOG_ERROR, "Invalid json type event filter - app: %s, filter: %s\n",
				app->name, filter_type);
		return -1;
	}

	if (filter) {
		/* Confirm that at least the type names are specified */
		struct ast_json *obj;
		int i;

		for (i = 0; i < ast_json_array_size(filter) &&
				 (obj = ast_json_array_get(filter, i)); ++i) {

			if (ast_strlen_zero(ast_json_object_string_get(obj, "type"))) {
				ast_log(LOG_ERROR, "Filter event must have a type - app: %s, "
						"filter: %s\n",	app->name, filter_type);
				return -1;
			}
		}
	}

	ao2_lock(app);
	ast_json_unref(*member);
	*member = filter ? ast_json_ref(filter) : NULL;
	ao2_unlock(app);

	return 0;
}
int ast_ari_validate_boolean(struct ast_json *json)
{
	enum ast_json_type actual = ast_json_typeof(json);
	switch (actual) {
	case AST_JSON_TRUE:
	case AST_JSON_FALSE:
		return 1;
	default:
		ast_log(LOG_ERROR, "Expected type boolean, was %s\n",
			ast_json_typename(actual));
		return 0;
	}
}
static int check_type(struct ast_json *json, enum ast_json_type expected)
{
	enum ast_json_type actual;

	if (!json) {
		ast_log(LOG_ERROR, "Expected type %s, was NULL\n",
			ast_json_typename(expected));
		return 0;
	}

	actual = ast_json_typeof(json);
	if (expected != actual) {
		ast_log(LOG_ERROR, "Expected type %s, was %s\n",
			ast_json_typename(expected), ast_json_typename(actual));
		return 0;
	}
	return 1;
}
Exemple #4
0
enum ast_json_to_ast_vars_code ast_json_to_ast_variables(struct ast_json *json_variables, struct ast_variable **variables)
{
	struct ast_json_iter *it_json_var;

	*variables = NULL;

	for (it_json_var = ast_json_object_iter(json_variables); it_json_var;
		it_json_var = ast_json_object_iter_next(json_variables, it_json_var)) {
		struct ast_variable *new_var;
		const char *key = ast_json_object_iter_key(it_json_var);
		const char *value;
		struct ast_json *json_value;

		if (ast_strlen_zero(key)) {
			continue;
		}

		json_value = ast_json_object_iter_value(it_json_var);
		if (ast_json_typeof(json_value) != AST_JSON_STRING) {
			/* Error: Only strings allowed */
			ast_variables_destroy(*variables);
			*variables = NULL;
			return AST_JSON_TO_AST_VARS_CODE_INVALID_TYPE;
		}
		value = ast_json_string_get(json_value);
		/* Should never be NULL.  Otherwise, how could it be a string type? */
		ast_assert(value != NULL);
		if (!value) {
			/* To be safe. */
			continue;
		}
		new_var = ast_variable_new(key, value, "");
		if (!new_var) {
			/* Error: OOM */
			ast_variables_destroy(*variables);
			*variables = NULL;
			return AST_JSON_TO_AST_VARS_CODE_OOM;
		}

		ast_variable_list_append(variables, new_var);
	}

	return AST_JSON_TO_AST_VARS_CODE_SUCCESS;
}
Exemple #5
0
int ast_ari_bridges_add_channel_parse_body(
	struct ast_json *body,
	struct ast_ari_bridges_add_channel_args *args)
{
	struct ast_json *field;
	/* Parse query parameters out of it */
	field = ast_json_object_get(body, "channel");
	if (field) {
		/* If they were silly enough to both pass in a query param and a
		 * JSON body, free up the query value.
		 */
		ast_free(args->channel);
		if (ast_json_typeof(field) == AST_JSON_ARRAY) {
			/* Multiple param passed as array */
			size_t i;
			args->channel_count = ast_json_array_size(field);
			args->channel = ast_malloc(sizeof(*args->channel) * args->channel_count);

			if (!args->channel) {
				return -1;
			}

			for (i = 0; i < args->channel_count; ++i) {
				args->channel[i] = ast_json_string_get(ast_json_array_get(field, i));
			}
		} else {
			/* Multiple param passed as single value */
			args->channel_count = 1;
			args->channel = ast_malloc(sizeof(*args->channel) * args->channel_count);
			if (!args->channel) {
				return -1;
			}
			args->channel[0] = ast_json_string_get(field);
		}
	}
	field = ast_json_object_get(body, "role");
	if (field) {
		args->role = ast_json_string_get(field);
	}
	return 0;
}
Exemple #6
0
int ast_ari_asterisk_get_info_parse_body(
	struct ast_json *body,
	struct ast_ari_asterisk_get_info_args *args)
{
	struct ast_json *field;
	/* Parse query parameters out of it */
	field = ast_json_object_get(body, "only");
	if (field) {
		/* If they were silly enough to both pass in a query param and a
		 * JSON body, free up the query value.
		 */
		ast_free(args->only);
		if (ast_json_typeof(field) == AST_JSON_ARRAY) {
			/* Multiple param passed as array */
			size_t i;
			args->only_count = ast_json_array_size(field);
			args->only = ast_malloc(sizeof(*args->only) * args->only_count);

			if (!args->only) {
				return -1;
			}

			for (i = 0; i < args->only_count; ++i) {
				args->only[i] = ast_json_string_get(ast_json_array_get(field, i));
			}
		} else {
			/* Multiple param passed as single value */
			args->only_count = 1;
			args->only = ast_malloc(sizeof(*args->only) * args->only_count);
			if (!args->only) {
				return -1;
			}
			args->only[0] = ast_json_string_get(field);
		}
	}
	return 0;
}
int ast_ari_applications_unsubscribe_parse_body(
	struct ast_json *body,
	struct ast_ari_applications_unsubscribe_args *args)
{
	struct ast_json *field;
	/* Parse query parameters out of it */
	field = ast_json_object_get(body, "eventSource");
	if (field) {
		/* If they were silly enough to both pass in a query param and a
		 * JSON body, free up the query value.
		 */
		ast_free(args->event_source);
		if (ast_json_typeof(field) == AST_JSON_ARRAY) {
			/* Multiple param passed as array */
			size_t i;
			args->event_source_count = ast_json_array_size(field);
			args->event_source = ast_malloc(sizeof(*args->event_source) * args->event_source_count);

			if (!args->event_source) {
				return -1;
			}

			for (i = 0; i < args->event_source_count; ++i) {
				args->event_source[i] = ast_json_string_get(ast_json_array_get(field, i));
			}
		} else {
			/* Multiple param passed as single value */
			args->event_source_count = 1;
			args->event_source = ast_malloc(sizeof(*args->event_source) * args->event_source_count);
			if (!args->event_source) {
				return -1;
			}
			args->event_source[0] = ast_json_string_get(field);
		}
	}
	return 0;
}