Ejemplo n.º 1
0
        public void GetBlobAsync(string entityId, BlobOperationComplete onComplete)
        {
            SsdsBlobEntity   blob      = null;
            Exception        exception = null;
            BackgroundWorker worker    = new BackgroundWorker();

            worker.DoWork += new DoWorkEventHandler(delegate(object sender, DoWorkEventArgs e)
            {
                try
                {
                    blob = this.GetBlob(entityId);
                }
                catch (Exception ex)
                {
                    exception = ex;
                }
            });

            worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(delegate(object sender, RunWorkerCompletedEventArgs e)
            {
                if (onComplete != null)
                {
                    onComplete(blob, exception);
                }
            });

            worker.RunWorkerAsync();
        }
Ejemplo n.º 2
0
        public void InsertAsync(SsdsBlobEntity blob, BlobOperationComplete onComplete)
        {
            Exception        exception = null;
            BackgroundWorker worker    = new BackgroundWorker();

            worker.DoWork += new DoWorkEventHandler(delegate(object sender, DoWorkEventArgs e)
            {
                try
                {
                    this.Insert(blob);
                }
                catch (Exception ex)
                {
                    exception = ex;
                }
            });

            worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(delegate(object sender, RunWorkerCompletedEventArgs e)
            {
                if (onComplete != null)
                {
                    onComplete(blob, exception);
                }
            });

            worker.RunWorkerAsync();
        }
Ejemplo n.º 3
0
        public SsdsBlobEntity GetBlob(string entityId)
        {
            SsdsBlobEntity blob    = null;
            WebRequest     request = this.CreateRequest(new Uri(String.Format(CultureInfo.InvariantCulture, "{0}/{1}", this.scope, entityId)), HttpVerbs.Get);

            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                blob = new SsdsBlobEntity();
                blob.Append(response.GetResponseStream());
                blob.MimeType           = response.Headers[HttpResponseHeader.ContentType];
                blob.ContentDisposition = response.Headers["Content-Disposition"];
                blob.Version            = response.Headers[HttpResponseHeader.ETag];
                blob.Id = entityId;
            }

            return(blob);
        }
Ejemplo n.º 4
0
        private HttpWebRequest CreateStreamingRequest(Uri containerUri, string verb, SsdsBlobEntity entity)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(containerUri);

            request.Method        = verb;
            request.ContentLength = entity.Length;
            request.ContentType   = entity.MimeType;
            request.Timeout       = 30000;

            System.Net.Mime.ContentDisposition contentDisposition = new System.Net.Mime.ContentDisposition();
            contentDisposition.FileName            = entity.ContentDisposition;
            request.Headers["Content-Disposition"] = contentDisposition.ToString();

            // Disable buffering by the client so that we get true streaming.
            request.AllowWriteStreamBuffering = false;

            // Construct the Authorization header for this request manually since there is a known
            // issue on HttpWebRequest with streaming authenticated requests which don't use the GET verb.
            byte[] credentialBuffer = new UTF8Encoding().GetBytes(String.Format(CultureInfo.InvariantCulture, "{0}:{1}", this.userName, this.password));
            request.Headers.Add(HttpRequestHeader.Authorization, "Basic " + Convert.ToBase64String(credentialBuffer));
            return(request);
        }
Ejemplo n.º 5
0
        private string InsertOrUpdate(SsdsBlobEntity blob, string verb, ConcurrencyPattern concurrencyPattern)
        {
            string       locationResult  = String.Empty;
            IAsyncResult asyncResult     = null;
            Exception    backgroundError = null;

            blob.InitRead();

            Uri containerScope = this.scope;

            if (verb == HttpVerbs.Put)
            {
                containerScope = new Uri(String.Format(CultureInfo.InvariantCulture, "{0}/{1}", this.scope, blob.Id));
            }

            HttpWebRequest uploadRequest = CreateStreamingRequest(containerScope, verb, blob);

            if (verb == HttpVerbs.Post)
            {
                uploadRequest.Headers["Slug"] = blob.Id;
            }

            if (concurrencyPattern == ConcurrencyPattern.IfMatch)
            {
                uploadRequest.Headers.Add("if-match", blob.Version);
            }
            else if (concurrencyPattern == ConcurrencyPattern.IfNoneMatch)
            {
                uploadRequest.Headers.Add("if-none-match", blob.Version);
            }

            try
            {
                using (Stream requestStream = uploadRequest.GetRequestStream())
                {
                    while (blob.ReadBuffer() > 0)
                    {
                        requestStream.Write(blob.Buffer, 0, blob.ReadSize);
                        if (asyncResult == null)
                        {
                            asyncResult = uploadRequest.BeginGetResponse((IAsyncResult res) =>
                            {
                                HttpWebRequest req = (HttpWebRequest)res.AsyncState;
                                try
                                {
                                    using (HttpWebResponse response = (HttpWebResponse)req.EndGetResponse(res))
                                    {
                                        locationResult = response.Headers[HttpResponseHeader.Location];
                                    }
                                }
                                catch (Exception ex)
                                {
                                    backgroundError = ex;
                                }
                            }, uploadRequest);
                        }
                    }
                }

                if (asyncResult != null)
                {
                    asyncResult.AsyncWaitHandle.WaitOne();
                    if (backgroundError != null)
                    {
                        throw backgroundError;
                    }
                }
            }
            catch (WebException ex)
            {
                string errorMsg = ex.Message;

                if (ex.Response != null)
                {
                    using (HttpWebResponse errorResponse = (HttpWebResponse)ex.Response)
                    {
                        errorMsg = ReadResponse(errorResponse);
                    }
                }

                throw new ApplicationException(errorMsg);
            }

            return(locationResult);
        }
Ejemplo n.º 6
0
 public string Insert(SsdsBlobEntity blob)
 {
     return(this.InsertOrUpdate(blob, HttpVerbs.Post, ConcurrencyPattern.Always));
 }
Ejemplo n.º 7
0
 public void Update(SsdsBlobEntity blob, ConcurrencyPattern concurrencyPattern)
 {
     this.InsertOrUpdate(blob, HttpVerbs.Put, concurrencyPattern);
 }
Ejemplo n.º 8
0
        public string Insert(SsdsBlobEntity blob)
        {
            SsdsRestFacade facade = this.CreateFacade();

            return(facade.Insert(blob));
        }
Ejemplo n.º 9
0
 public void UpdateAsync(SsdsBlobEntity blob, BlobOperationComplete onComplete)
 {
     this.UpdateAsync(blob, ConcurrencyPattern.Always, onComplete);
 }
Ejemplo n.º 10
0
        public void Update(SsdsBlobEntity blob, ConcurrencyPattern concurrencyPattern)
        {
            SsdsRestFacade facade = this.CreateFacade();

            facade.Update(blob, concurrencyPattern);
        }
Ejemplo n.º 11
0
 public void Update(SsdsBlobEntity blob)
 {
     Update(blob, ConcurrencyPattern.Always);
 }
Ejemplo n.º 12
0
 public void Delete(SsdsBlobEntity entity, ConcurrencyPattern concurrencyPattern)
 {
     this.Delete(entity.Id, concurrencyPattern, entity.Version);
 }