From: Jonathan Rose Date: Fri, 25 Oct 2013 21:20:42 +0000 (+0000) Subject: ARI recordings: Issue HTTP failures for recording requests with file conflicts X-Git-Tag: 12.0.0-beta2~84 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f503fc567316fd9f9ad6c6ab50bf7b4f5de2cf05;p=thirdparty%2Fasterisk.git ARI recordings: Issue HTTP failures for recording requests with file conflicts If a file already exists in the recordings directory with the same name as what we would record, issue a 422 instead of relying on the internal failure and issuing success. (closes issue ASTERISK-22623) Reported by: Joshua Colp Review: https://reviewboard.asterisk.org/r/2922/ git-svn-id: https://origsvn.digium.com/svn/asterisk/branches/12@401973 65c4cc65-6c06-0410-ace0-fbb531ad65f3 --- diff --git a/res/ari/ari_model_validators.c b/res/ari/ari_model_validators.c index aec2bb4710..9634a473cc 100644 --- a/res/ari/ari_model_validators.c +++ b/res/ari/ari_model_validators.c @@ -3288,6 +3288,136 @@ ari_validator ast_ari_validate_playback_started_fn(void) return ast_ari_validate_playback_started; } +int ast_ari_validate_recording_failed(struct ast_json *json) +{ + int res = 1; + struct ast_json_iter *iter; + int has_cause = 0; + int has_recording = 0; + + for (iter = ast_json_object_iter(json); iter; iter = ast_json_object_iter_next(json, iter)) { + if (strcmp("cause", ast_json_object_iter_key(iter)) == 0) { + int prop_is_valid; + has_cause = 1; + prop_is_valid = ast_ari_validate_string( + ast_json_object_iter_value(iter)); + if (!prop_is_valid) { + ast_log(LOG_ERROR, "ARI RecordingFailed field cause failed validation\n"); + res = 0; + } + } else + if (strcmp("recording", ast_json_object_iter_key(iter)) == 0) { + int prop_is_valid; + has_recording = 1; + prop_is_valid = ast_ari_validate_live_recording( + ast_json_object_iter_value(iter)); + if (!prop_is_valid) { + ast_log(LOG_ERROR, "ARI RecordingFailed field recording failed validation\n"); + res = 0; + } + } else + { + ast_log(LOG_ERROR, + "ARI RecordingFailed has undocumented field %s\n", + ast_json_object_iter_key(iter)); + res = 0; + } + } + + if (!has_cause) { + ast_log(LOG_ERROR, "ARI RecordingFailed missing required field cause\n"); + res = 0; + } + + if (!has_recording) { + ast_log(LOG_ERROR, "ARI RecordingFailed missing required field recording\n"); + res = 0; + } + + return res; +} + +ari_validator ast_ari_validate_recording_failed_fn(void) +{ + return ast_ari_validate_recording_failed; +} + +int ast_ari_validate_recording_finished(struct ast_json *json) +{ + int res = 1; + struct ast_json_iter *iter; + int has_recording = 0; + + for (iter = ast_json_object_iter(json); iter; iter = ast_json_object_iter_next(json, iter)) { + if (strcmp("recording", ast_json_object_iter_key(iter)) == 0) { + int prop_is_valid; + has_recording = 1; + prop_is_valid = ast_ari_validate_live_recording( + ast_json_object_iter_value(iter)); + if (!prop_is_valid) { + ast_log(LOG_ERROR, "ARI RecordingFinished field recording failed validation\n"); + res = 0; + } + } else + { + ast_log(LOG_ERROR, + "ARI RecordingFinished has undocumented field %s\n", + ast_json_object_iter_key(iter)); + res = 0; + } + } + + if (!has_recording) { + ast_log(LOG_ERROR, "ARI RecordingFinished missing required field recording\n"); + res = 0; + } + + return res; +} + +ari_validator ast_ari_validate_recording_finished_fn(void) +{ + return ast_ari_validate_recording_finished; +} + +int ast_ari_validate_recording_started(struct ast_json *json) +{ + int res = 1; + struct ast_json_iter *iter; + int has_recording = 0; + + for (iter = ast_json_object_iter(json); iter; iter = ast_json_object_iter_next(json, iter)) { + if (strcmp("recording", ast_json_object_iter_key(iter)) == 0) { + int prop_is_valid; + has_recording = 1; + prop_is_valid = ast_ari_validate_live_recording( + ast_json_object_iter_value(iter)); + if (!prop_is_valid) { + ast_log(LOG_ERROR, "ARI RecordingStarted field recording failed validation\n"); + res = 0; + } + } else + { + ast_log(LOG_ERROR, + "ARI RecordingStarted has undocumented field %s\n", + ast_json_object_iter_key(iter)); + res = 0; + } + } + + if (!has_recording) { + ast_log(LOG_ERROR, "ARI RecordingStarted missing required field recording\n"); + res = 0; + } + + return res; +} + +ari_validator ast_ari_validate_recording_started_fn(void) +{ + return ast_ari_validate_recording_started; +} + int ast_ari_validate_stasis_end(struct ast_json *json) { int res = 1; diff --git a/res/ari/ari_model_validators.h b/res/ari/ari_model_validators.h index 6aa6a2a14d..afa3961d65 100644 --- a/res/ari/ari_model_validators.h +++ b/res/ari/ari_model_validators.h @@ -862,6 +862,60 @@ int ast_ari_validate_playback_started(struct ast_json *json); */ ari_validator ast_ari_validate_playback_started_fn(void); +/*! + * \brief Validator for RecordingFailed. + * + * Event showing failure of a recording operation. + * + * \param json JSON object to validate. + * \returns True (non-zero) if valid. + * \returns False (zero) if invalid. + */ +int ast_ari_validate_recording_failed(struct ast_json *json); + +/*! + * \brief Function pointer to ast_ari_validate_recording_failed(). + * + * See \ref ast_ari_model_validators.h for more details. + */ +ari_validator ast_ari_validate_recording_failed_fn(void); + +/*! + * \brief Validator for RecordingFinished. + * + * Event showing the completion of a recording operation. + * + * \param json JSON object to validate. + * \returns True (non-zero) if valid. + * \returns False (zero) if invalid. + */ +int ast_ari_validate_recording_finished(struct ast_json *json); + +/*! + * \brief Function pointer to ast_ari_validate_recording_finished(). + * + * See \ref ast_ari_model_validators.h for more details. + */ +ari_validator ast_ari_validate_recording_finished_fn(void); + +/*! + * \brief Validator for RecordingStarted. + * + * Event showing the start of a recording operation. + * + * \param json JSON object to validate. + * \returns True (non-zero) if valid. + * \returns False (zero) if invalid. + */ +int ast_ari_validate_recording_started(struct ast_json *json); + +/*! + * \brief Function pointer to ast_ari_validate_recording_started(). + * + * See \ref ast_ari_model_validators.h for more details. + */ +ari_validator ast_ari_validate_recording_started_fn(void); + /*! * \brief Validator for StasisEnd. * @@ -1112,6 +1166,13 @@ ari_validator ast_ari_validate_application_fn(void); * - application: string (required) * - timestamp: Date * - playback: Playback (required) + * RecordingFailed + * - cause: string (required) + * - recording: LiveRecording (required) + * RecordingFinished + * - recording: LiveRecording (required) + * RecordingStarted + * - recording: LiveRecording (required) * StasisEnd * - type: string (required) * - application: string (required) diff --git a/res/ari/resource_bridges.c b/res/ari/resource_bridges.c index 77d5660ca7..a036ce0f3b 100644 --- a/res/ari/resource_bridges.c +++ b/res/ari/resource_bridges.c @@ -452,7 +452,7 @@ void ast_ari_record_bridge(struct ast_variable *headers, struct ast_record_bridg break; case EEXIST: ast_ari_response_error(response, 409, "Conflict", - "Recording '%s' already in progress", + "Recording '%s' already exists and can not be overwritten", args->name); break; case ENOMEM: diff --git a/res/ari/resource_channels.c b/res/ari/resource_channels.c index bdca21cdbf..b8d59d38b1 100644 --- a/res/ari/resource_channels.c +++ b/res/ari/resource_channels.c @@ -420,7 +420,7 @@ void ast_ari_record_channel(struct ast_variable *headers, break; case EEXIST: ast_ari_response_error(response, 409, "Conflict", - "Recording '%s' already in progress", + "Recording '%s' already exists and can not be overwritten", args->name); break; case ENOMEM: diff --git a/res/res_ari_bridges.c b/res/res_ari_bridges.c index 3908094a83..f6a3c1c1a7 100644 --- a/res/res_ari_bridges.c +++ b/res/res_ari_bridges.c @@ -742,9 +742,9 @@ static void ast_ari_record_bridge_cb( break; case 500: /* Internal Server Error */ case 501: /* Not Implemented */ - case 400: /* Recording name invalid */ + case 400: /* Invalid parameters */ case 404: /* Bridge not found */ - case 409: /* Bridge not in Stasis application; Recording already in progress */ + case 409: /* Bridge is not in a Stasis application; A recording with the same name already exists on the system and can not be overwritten because it is in progress or ifExists=fail */ is_valid = 1; break; default: diff --git a/res/res_ari_channels.c b/res/res_ari_channels.c index af7f17df21..8ff4c065c5 100644 --- a/res/res_ari_channels.c +++ b/res/res_ari_channels.c @@ -992,7 +992,7 @@ static void ast_ari_record_channel_cb( case 501: /* Not Implemented */ case 400: /* Invalid parameters */ case 404: /* Channel not found */ - case 409: /* Channel is not in a Stasis application; the channel is currently bridged with other channels; A recording with the same name is currently in progress. */ + case 409: /* Channel is not in a Stasis application; the channel is currently bridged with other hcannels; A recording with the same name already exists on the system and can not be overwritten because it is in progress or ifExists=fail */ is_valid = 1; break; default: diff --git a/res/res_stasis_recording.c b/res/res_stasis_recording.c index 49044c4433..bd2177a762 100644 --- a/res/res_stasis_recording.c +++ b/res/res_stasis_recording.c @@ -348,6 +348,14 @@ struct stasis_app_recording *stasis_app_control_record( recording->control = control; recording->state = STASIS_APP_RECORDING_STATE_QUEUED; + if ((recording->options->if_exists == AST_RECORD_IF_EXISTS_FAIL) && + (ast_fileexists(recording->absolute_name, NULL, NULL))) { + ast_log(LOG_WARNING, "Recording file '%s' already exists and ifExists option is failure.\n", + recording->absolute_name); + errno = EEXIST; + return NULL; + } + { RAII_VAR(struct stasis_app_recording *, old_recording, NULL, ao2_cleanup); diff --git a/rest-api/api-docs/bridges.json b/rest-api/api-docs/bridges.json index 819c777db4..187522826a 100644 --- a/rest-api/api-docs/bridges.json +++ b/rest-api/api-docs/bridges.json @@ -454,20 +454,20 @@ } } ], - "errorResponses": [ - { - "code": 400, - "reason": "Recording name invalid" - }, - { - "code": 404, - "reason": "Bridge not found" - }, - { - "code": 409, - "reason": "Bridge not in Stasis application; Recording already in progress" - } - ] + "errorResponses": [ + { + "code": 400, + "reason": "Invalid parameters" + }, + { + "code": 404, + "reason": "Bridge not found" + }, + { + "code": 409, + "reason": "Bridge is not in a Stasis application; A recording with the same name already exists on the system and can not be overwritten because it is in progress or ifExists=fail" + } + ] } ] } diff --git a/rest-api/api-docs/channels.json b/rest-api/api-docs/channels.json index 55997bb693..a9b55873a5 100644 --- a/rest-api/api-docs/channels.json +++ b/rest-api/api-docs/channels.json @@ -720,7 +720,7 @@ }, { "code": 409, - "reason": "Channel is not in a Stasis application; the channel is currently bridged with other channels; A recording with the same name is currently in progress." + "reason": "Channel is not in a Stasis application; the channel is currently bridged with other hcannels; A recording with the same name already exists on the system and can not be overwritten because it is in progress or ifExists=fail" } ] } diff --git a/rest-api/api-docs/events.json b/rest-api/api-docs/events.json index a9ea45c81c..8692400a43 100644 --- a/rest-api/api-docs/events.json +++ b/rest-api/api-docs/events.json @@ -120,6 +120,47 @@ } } }, + "RecordingStarted": { + "id": "RecordingStarted", + "extends": "Event", + "description": "Event showing the start of a recording operation.", + "properties": { + "recording": { + "type": "LiveRecording", + "description": "Recording control object", + "required": true + } + } + }, + "RecordingFinished": { + "id": "RecordingFinished", + "extends": "Event", + "description": "Event showing the completion of a recording operation.", + "properties": { + "recording": { + "type": "LiveRecording", + "description": "Recording control object", + "required": true + } + } + }, + "RecordingFailed": { + "id": "RecordingFailed", + "extends": "Event", + "description": "Event showing failure of a recording operation.", + "properties": { + "recording": { + "type": "LiveRecording", + "description": "Recording control object", + "required": true + }, + "cause": { + "type": "string", + "description": "Cause for the recording failure", + "required": true + } + } + }, "ApplicationReplaced": { "id": "ApplicationReplaced", "description": "Notification that another WebSocket has taken over for an application.\n\nAn application may only be subscribed to by a single WebSocket at a time. If multiple WebSockets attempt to subscribe to the same application, the newer WebSocket wins, and the older one receives this event.", diff --git a/rest-api/api-docs/recordings.json b/rest-api/api-docs/recordings.json index 5767ce479b..93340c10ee 100644 --- a/rest-api/api-docs/recordings.json +++ b/rest-api/api-docs/recordings.json @@ -309,9 +309,11 @@ "valueType": "LIST", "values": [ "queued", - "playing", + "recording", "paused", - "done" + "done", + "failed", + "canceled" ] } },