/// <summary>
        /// Deserialize a request to a JsonDocument
        /// </summary>
        public static JsonDocumentMetadata DeserializeJsonDocumentMetadata(string key,
                                                                           NameValueCollection headers,
                                                                           HttpStatusCode statusCode)
        {
            RavenJObject meta = null;

            try
            {
                meta = headers.FilterHeadersToObject();
            }
            catch (JsonReaderException jre)
            {
                throw new JsonReaderException("Invalid Json Response", jre);
            }
            var    etag             = headers[Constants.MetadataEtagField];
            string lastModified     = headers[Constants.RavenLastModified] ?? headers[Constants.LastModified];
            var    dateTime         = DateTime.ParseExact(lastModified, new[] { "o", "r" }, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind);
            var    lastModifiedDate = DateTime.SpecifyKind(dateTime, DateTimeKind.Utc);

            return(new JsonDocumentMetadata
            {
                NonAuthoritativeInformation = statusCode == HttpStatusCode.NonAuthoritativeInformation,
                Key = key,
                Etag = HttpExtensions.EtagHeaderToEtag(etag),
                LastModified = lastModifiedDate,
                Metadata = meta
            });
        }
Exemple #2
0
        public static JsonDocument RavenJObjectToJsonDocument(RavenJObject doc)
        {
            var metadata = (RavenJObject)doc["@metadata"];

            doc.Remove("@metadata");
            var key = Extract(metadata, "@id", string.Empty);

            if (string.IsNullOrEmpty(key))
            {
                // projection, it seems.
                return(new JsonDocument
                {
                    Key = string.Empty,
                    DataAsJson = doc,
                    LastModified = SystemTime.UtcNow,
                });
            }

            var lastModified = GetLastModified(metadata);

            metadata[Constants.Headers.LastModified] = lastModified;
            var etag = Extract(metadata, "@etag", 0, (string g) => HttpExtensions.EtagHeaderToEtag(g));

            var jsonDocument = new JsonDocument
            {
                Key            = key,
                LastModified   = lastModified,
                Etag           = etag,
                TempIndexScore = metadata == null ? null : metadata.Value <float?>(Constants.Indexing.Fields.IndexFieldScoreName),
                Metadata       = metadata.FilterHeadersToObject(),
                DataAsJson     = doc
            };

            return(jsonDocument);
        }
Exemple #3
0
        /// <summary>
        /// Deserialize a request to a JsonDocument
        /// </summary>
        public static JsonDocumentMetadata DeserializeJsonDocumentMetadata(string key,
                                                                           HttpResponseHeaders headers,
                                                                           HttpStatusCode statusCode)
        {
            RavenJObject metadata = null;

            try
            {
                metadata = headers.FilterHeadersToObject();
            }
            catch (JsonReaderException jre)
            {
                throw new JsonReaderException("Invalid Json Response", jre);
            }
            var    etag             = headers.ETag.Tag;
            string lastModified     = headers.GetFirstValue(Constants.Headers.RavenLastModified) ?? headers.GetFirstValue(Constants.Headers.LastModified);
            var    dateTime         = DateTime.ParseExact(lastModified, new[] { "o", "r" }, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind);
            var    lastModifiedDate = DateTime.SpecifyKind(dateTime, DateTimeKind.Utc);

            metadata[Constants.Headers.LastModified] = lastModified;

            return(new JsonDocumentMetadata
            {
                Key = key,
                Etag = HttpExtensions.EtagHeaderToEtag(etag),
                LastModified = lastModifiedDate,
                Metadata = metadata
            });
        }
Exemple #4
0
        public static JsonDocument RavenJObjectToJsonDocument(RavenJObject doc)
        {
            var metadata = (RavenJObject)doc["@metadata"];

            doc.Remove("@metadata");
            var key = Extract(metadata, "@id", string.Empty);

            if (string.IsNullOrEmpty(key))
            {
                // projection, it seems.
                return(new JsonDocument
                {
                    DataAsJson = doc,
                    LastModified = SystemTime.UtcNow,
                });
            }

            var lastModified = GetLastModified(metadata);

            var etag = Extract(metadata, "@etag", Etag.Empty, (string g) => HttpExtensions.EtagHeaderToEtag(g));
            var nai  = Extract(metadata, "Non-Authoritative-Information", false, (string b) => Convert.ToBoolean(b));

            var jsonDocument = new JsonDocument
            {
                Key            = key,
                LastModified   = lastModified,
                Etag           = etag,
                TempIndexScore = metadata == null ? null : metadata.Value <float?>(Constants.TemporaryScoreValue),
                NonAuthoritativeInformation = nai,
                Metadata   = metadata.FilterHeaders(),
                DataAsJson = doc,
            };

            return(jsonDocument);
        }
        public static JsonDocument DeserializeJsonDocument(string key, RavenJToken requestJson,
                                                           NameValueCollection headers,
                                                           HttpStatusCode statusCode)
        {
            var jsonData = (RavenJObject)requestJson;
            var meta     = headers.FilterHeadersToObject();

            var etag = headers[Constants.MetadataEtagField];

            return(new JsonDocument
            {
                DataAsJson = jsonData,
                NonAuthoritativeInformation = statusCode == HttpStatusCode.NonAuthoritativeInformation,
                Key = key,
                Etag = HttpExtensions.EtagHeaderToEtag(etag),
                LastModified = GetLastModifiedDate(headers),
                Metadata = meta
            });
        }
Exemple #6
0
        /// <summary>
        /// Deserialize a request to a JsonDocument
        /// </summary>
        public static JsonDocument DeserializeJsonDocument(string key, RavenJToken requestJson,
                                                           HttpResponseHeaders headers,
                                                           HttpStatusCode statusCode)
        {
            var jsonData = (RavenJObject)requestJson;
            var metadata = headers.FilterHeadersToObject();

            var etag         = headers.ETag.Tag;
            var lastModified = GetLastModifiedDate(headers);

            metadata[Constants.Headers.LastModified] = lastModified;

            return(new JsonDocument
            {
                DataAsJson = jsonData,
                Key = key,
                Etag = HttpExtensions.EtagHeaderToEtag(etag),
                LastModified = lastModified,
                Metadata = metadata
            });
        }
Exemple #7
0
        public async Task TryResolveConflictOrCreateConcurrencyException(GetResponse[] responses, Func <string, RavenJObject, Etag, Task <ConflictException> > tryResolveConflictOrCreateConcurrencyException)
        {
            // ReSharper disable once ForCanBeConvertedToForeach
            // see: http://ayende.com/blog/169377/the-bug-that-ruined-my-weekend
            for (int index = 0; index < responses.Length; index++)
            {
                var response = responses[index];
                if (response == null)
                {
                    continue;
                }
                if (response.RequestHasErrors() && response.Status != 409)
                {
                    continue;
                }

                var result = response.Result as RavenJObject;
                if (result == null)
                {
                    continue;
                }

                if (result.ContainsKey("Results"))
                {
                    var results = result["Results"] as RavenJArray;
                    if (results == null)
                    {
                        continue;
                    }

                    foreach (RavenJToken value in results)
                    {
                        var docResult = value as RavenJObject;
                        if (docResult == null)
                        {
                            return;
                        }

                        var metadata = docResult[Constants.Metadata];
                        if (metadata == null)
                        {
                            return;
                        }

                        if (metadata.Value <int>("@Http-Status-Code") != 409)
                        {
                            return;
                        }

                        var id   = metadata.Value <string>("@id");
                        var etag = HttpExtensions.EtagHeaderToEtag(metadata.Value <string>("@etag"));

                        await TryResolveConflictOrCreateConcurrencyExceptionForSingleDocument(
                            tryResolveConflictOrCreateConcurrencyException,
                            id,
                            etag,
                            docResult,
                            response);
                    }

                    continue;
                }
                if (result.ContainsKey("Conflicts"))
                {
                    var id   = response.Headers[Constants.DocumentIdFieldName];
                    var etag = response.GetEtagHeader();

                    await TryResolveConflictOrCreateConcurrencyExceptionForSingleDocument(
                        tryResolveConflictOrCreateConcurrencyException,
                        id,
                        etag,
                        result,
                        response);
                }
            }
        }
        public async Task TryResolveConflictOrCreateConcurrencyException(GetResponse[] responses, Func <string, RavenJObject, Etag, Task <ConflictException> > tryResolveConflictOrCreateConcurrencyException)
        {
            foreach (var response in responses)
            {
                if (response == null)
                {
                    continue;
                }
                if (response.RequestHasErrors() && response.Status != 409)
                {
                    continue;
                }

                var result = response.Result as RavenJObject;
                if (result == null)
                {
                    continue;
                }

                if (result.ContainsKey("Results"))
                {
                    var results = result["Results"] as RavenJArray;
                    if (results == null)
                    {
                        continue;
                    }

                    foreach (RavenJToken value in results)
                    {
                        var docResult = value as RavenJObject;
                        if (docResult == null)
                        {
                            return;
                        }

                        var metadata = docResult[Constants.Metadata];
                        if (metadata == null)
                        {
                            return;
                        }

                        if (metadata.Value <int>("@Http-Status-Code") != 409)
                        {
                            return;
                        }

                        var id   = metadata.Value <string>("@id");
                        var etag = HttpExtensions.EtagHeaderToEtag(metadata.Value <string>("@etag"));

                        await TryResolveConflictOrCreateConcurrencyExceptionForSingleDocument(
                            tryResolveConflictOrCreateConcurrencyException,
                            id,
                            etag,
                            docResult,
                            response);
                    }

                    continue;
                }
                if (result.ContainsKey("Conflicts"))
                {
                    var id   = response.Headers[Constants.DocumentIdFieldName];
                    var etag = response.GetEtagHeader();

                    await TryResolveConflictOrCreateConcurrencyExceptionForSingleDocument(
                        tryResolveConflictOrCreateConcurrencyException,
                        id,
                        etag,
                        result,
                        response);
                }
            }
        }