Beispiel #1
0
        public static RiakObject Load(Bucket bucket, RiakHttpResponse response)
        {
            RiakObject ro = new RiakObject
                                {
                                    Bucket = bucket
                                };

            ro.LoadFromResponse(response);

            return ro;
        }
Beispiel #2
0
        public static Document Load(RiakHttpResponse response)
        {
            Dictionary<string, string> headers = new Dictionary<string, string>
                                                     {
                                                         { HttpWellKnownHeader.ContentLength, response.ContentLength.ToString() },
                                                         { HttpWellKnownHeader.ContentType, response.ContentType },
                                                     };

            foreach(string headerKey in response.Headers.AllKeys)
            {
                headers[headerKey] = response.Headers[headerKey];
            }

            return Load(response.GetResponseStream(), headers, null);
        }
Beispiel #3
0
 public RiakServerException(RiakHttpResponse response, String messageFormat, params object[] args)
     : base(string.Format(messageFormat, args))
 {
     _statusCode = response.StatusCode;
 }
Beispiel #4
0
 public RiakServerException(RiakHttpResponse response, String message)
     : base(message)
 {
     _statusCode = response.StatusCode;
 }
Beispiel #5
0
 protected virtual void LoadCachedData(RiakHttpResponse response)
 {
     _cachedData = new byte[response.ContentLength];
     Util.CopyStream(response.GetResponseStream(), _cachedData);
 }
Beispiel #6
0
        protected virtual void LoadHeaders(RiakHttpResponse response)
        {
            HasSiblings = response.StatusCode == HttpStatusCode.Ambiguous;

            VClock = response.Headers[HttpWellKnownHeader.RiakVClock];
            ContentType = response.ContentType;
            ETag = response.Headers[HttpWellKnownHeader.ETag];
            LastModified = response.LastModified;
            ContentLength = response.ContentLength;
            Links = LinkCollection.Create(response.Headers[HttpWellKnownHeader.Link]);

            string location = response.Headers[HttpWellKnownHeader.Location];
            if(!string.IsNullOrEmpty(location))
            {
                KeyName = ParseLocation(location);
            }
        }
Beispiel #7
0
        protected virtual void LoadFromResponse(RiakHttpResponse response)
        {
            Exists = response.StatusCode != HttpStatusCode.NotFound;

            if (Exists)
            {
                LoadHeaders(response);
                if (!HasSiblings)
                {
                    LoadCachedData(response);
                }
            }
            else
            {
                InitializeNew();
            }
        }
Beispiel #8
0
        private ICollection<RiakObject> LoadSiblingObjects(RiakHttpResponse response, string keyName)
        {
            if (response.IsMultiPart)
            {
                List<RiakObject> siblings = new List<RiakObject>();

                Document doc = Document.Load(response);
                MultiPartDocument mpDoc = doc as MultiPartDocument;
                if (mpDoc != null)
                {
                    foreach (Document part in mpDoc.Parts)
                    {
                        Trace.WriteLine("Loaded document part:");
                        Trace.WriteLine(part.Dump());
                        siblings.Add(RiakObject.Load(this, keyName, part));
                    }
                }
                else
                {
                    siblings.Add(RiakObject.Load(this, keyName, doc));
                }

                return siblings;
            }

            return LoadConflictSiblings(response, keyName);
        }
Beispiel #9
0
        private void LoadFromJson(RiakHttpResponse response)
        {
            using (StreamReader sr = new StreamReader(response.GetResponseStream()))
            {
                string responseText = sr.ReadToEnd();
                Trace.WriteLine(responseText);
                JsonObject bucket = (JsonObject)JsonConvert.Import(responseText);

                if (ContainsKeys)
                {
                    JsonArray keys = (JsonArray) bucket["keys"];
                    foreach (string key in keys)
                    {
                        Keys.Add(Uri.UnescapeDataString(key));
                    }
                }

                JsonObject properties = (JsonObject)bucket["props"];
                Name = Uri.UnescapeDataString((string)properties["name"]);
                AllowMulti = (bool)properties["allow_mult"];
            }
        }
Beispiel #10
0
        private ICollection<RiakObject> LoadConflictSiblings(RiakHttpResponse response, string keyName)
        {
            List<string> siblingIds = new List<string>();

            using (StreamReader sr = new StreamReader(response.GetResponseStream()))
            {
                string siblingHeader = sr.ReadLine();

                if(siblingHeader != "Siblings:")
                {
                    throw new RiakServerException("Expected a sibling header (\"Sibling:\") but was actually: \"{0}\"", siblingHeader);
                }

                while (!sr.EndOfStream)
                {
                    siblingIds.Add(sr.ReadLine());
                }
            }

            List<RiakObject> siblingObjects = new List<RiakObject>(siblingIds.Count);

            siblingObjects.AddRange(siblingIds.Select(siblingId => RiakObject.Load(this, keyName, siblingId)));

            return siblingObjects;
        }