ARI recordings: Issue HTTP failures for recording requests with file conflicts
authorJonathan Rose <jrose@digium.com>
Fri, 25 Oct 2013 21:28:32 +0000 (21:28 +0000)
committerJonathan Rose <jrose@digium.com>
Fri, 25 Oct 2013 21:28:32 +0000 (21:28 +0000)
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/
........

Merged revisions 401973 from http://svn.asterisk.org/svn/asterisk/branches/12

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@401999 65c4cc65-6c06-0410-ace0-fbb531ad65f3

res/ari/ari_model_validators.c
res/ari/ari_model_validators.h
res/ari/resource_bridges.c
res/ari/resource_channels.c
res/res_ari_bridges.c
res/res_ari_channels.c
res/res_stasis_recording.c
rest-api/api-docs/bridges.json
rest-api/api-docs/channels.json
rest-api/api-docs/events.json
rest-api/api-docs/recordings.json

index aec2bb4..9634a47 100644 (file)
@@ -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;
index 6aa6a2a..afa3961 100644 (file)
@@ -863,6 +863,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.
  *
  * Notification that a channel has left a Stasis appliction.
@@ -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)
index 77d5660..a036ce0 100644 (file)
@@ -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:
index bdca21c..b8d59d3 100644 (file)
@@ -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:
index 3908094..f6a3c1c 100644 (file)
@@ -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:
index af7f17d..8ff4c06 100644 (file)
@@ -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:
index 49044c4..bd2177a 100644 (file)
@@ -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);
index 819c777..1875228 100644 (file)
                                                        }
                                                }
                                        ],
-                                       "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"
+                        }
+                    ]
                                }
                        ]
                }
index 55997bb..a9b5587 100644 (file)
                                                },
                                                {
                                                        "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"
                                                }
                                        ]
                                }
index a9ea45c..8692400 100644 (file)
                                }
                        }
                },
+               "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.",
index 5767ce4..93340c1 100644 (file)
                                                "valueType": "LIST",
                                                "values": [
                                                        "queued",
-                                                       "playing",
+                                                       "recording",
                                                        "paused",
-                                                       "done"
+                                                       "done",
+                                                       "failed",
+                                                       "canceled"
                                                ]
                                        }
                                },