Пример #1
0
        //*** Request handlers ***//

        public async Task <object> HandleFullSyncRequest(IDictionary <string, object> parameters, C8oResponseListener listener)
        {
            parameters = new Dictionary <string, object>(parameters);

            // Gets the project and the sequence parameter in order to know which database and which fullSyncrequestable to use
            string projectParameterValue = C8oUtils.PeekParameterStringValue(parameters, C8o.ENGINE_PARAMETER_PROJECT, true);

            if (!projectParameterValue.StartsWith(FULL_SYNC_PROJECT))
            {
                throw new ArgumentException(C8oExceptionMessage.InvalidParameterValue(projectParameterValue, "its don't start with " + FULL_SYNC_PROJECT));
            }

            string fullSyncRequestableValue = C8oUtils.PeekParameterStringValue(parameters, C8o.ENGINE_PARAMETER_SEQUENCE, true);
            // Gets the fullSync requestable and gets the response from this requestable
            FullSyncRequestable fullSyncRequestable = FullSyncRequestable.GetFullSyncRequestable(fullSyncRequestableValue);

            if (fullSyncRequestable == null)
            {
                throw new ArgumentException(C8oExceptionMessage.InvalidParameterValue(C8o.ENGINE_PARAMETER_PROJECT, C8oExceptionMessage.UnknownValue("fullSync requestable", fullSyncRequestableValue)));
            }

            // Gets the database name if this is not specified then if takes the default database name
            string databaseName = projectParameterValue.Substring(C8oFullSync.FULL_SYNC_PROJECT.Length);

            if (databaseName.Length < 1)
            {
                databaseName = c8o.DefaultDatabaseName;
                if (databaseName == null)
                {
                    throw new ArgumentException(C8oExceptionMessage.InvalidParameterValue(C8o.ENGINE_PARAMETER_PROJECT, C8oExceptionMessage.MissingValue("fullSync database name")));
                }
            }

            Object response;

            try
            {
                response = await fullSyncRequestable.HandleFullSyncRequest(this, databaseName, parameters, listener);
            }
            catch (C8oException e)
            {
                throw e;
            }
            catch (Exception e)
            {
                throw new C8oException(C8oExceptionMessage.FullSyncRequestFail(), e);
            }

            if (response == null)
            {
                throw new C8oException(C8oExceptionMessage.couchNullResult());
            }

            response = HandleFullSyncResponse(response, listener);
            return(response);
        }
Пример #2
0
        public async override Task <object> HandlePostDocumentRequest(string fullSyncDatatbaseName, FullSyncPolicy fullSyncPolicy, IDictionary <string, object> parameters)
        {
            await CheckDatabase(fullSyncDatatbaseName);

            var options = new Dictionary <string, object>();

            foreach (var parameter in parameters)
            {
                var isUse = RE_FS_USE.Match(parameter.Key);
                if (isUse.Success)
                {
                    if (isUse.Groups[1].Success)
                    {
                        options[isUse.Groups[1].Value] = parameter.Value;
                    }
                    parameters.Remove(parameter.Key);
                }
            }

            string uri = HandleQuery(GetDatabaseUrl(fullSyncDatatbaseName), options);

            var request = HttpWebRequest.CreateHttp(uri);

            request.Method = "POST";

            // Gets the subkey separator parameter
            string subkeySeparatorParameterValue = C8oUtils.PeekParameterStringValue(parameters, FullSyncPostDocumentParameter.SUBKEY_SEPARATOR.name, false);

            if (subkeySeparatorParameterValue == null)
            {
                subkeySeparatorParameterValue = ".";
            }

            var postData = new JObject();

            foreach (KeyValuePair <string, object> kvp in parameters)
            {
                var      obj   = postData;
                string   key   = kvp.Key;
                String[] paths = key.Split(subkeySeparatorParameterValue.ToCharArray());

                if (paths.Length > 1)
                {
                    for (int i = 0; i < paths.Length - 1; i++)
                    {
                        string path = paths[i];
                        if (obj[path] is JObject)
                        {
                            obj = obj[path] as JObject;
                        }
                        else
                        {
                            obj = (obj[path] = new JObject()) as JObject;
                        }
                    }

                    key = paths[paths.Length - 1];
                }
                obj[key] = JToken.FromObject(kvp.Value);
            }

            postData = await ApplyPolicy(fullSyncDatatbaseName, postData, fullSyncPolicy);

            return(await Execute(request, postData));
        }