Changed proxy settings to request update if the proxy requires authentication
[pithos-ms-client] / trunk / Pithos.Network / CloudFilesClient.cs
1 // -----------------------------------------------------------------------
2 // <copyright file="CloudFilesClient.cs" company="GRNET">
3 // Copyright 2011 GRNET S.A. All rights reserved.
4 // 
5 // Redistribution and use in source and binary forms, with or
6 // without modification, are permitted provided that the following
7 // conditions are met:
8 // 
9 //   1. Redistributions of source code must retain the above
10 //      copyright notice, this list of conditions and the following
11 //      disclaimer.
12 // 
13 //   2. Redistributions in binary form must reproduce the above
14 //      copyright notice, this list of conditions and the following
15 //      disclaimer in the documentation and/or other materials
16 //      provided with the distribution.
17 // 
18 // THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
19 // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
22 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
25 // USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 // AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 // POSSIBILITY OF SUCH DAMAGE.
30 // 
31 // The views and conclusions contained in the software and
32 // documentation are those of the authors and should not be
33 // interpreted as representing official policies, either expressed
34 // or implied, of GRNET S.A.
35 // </copyright>
36 // -----------------------------------------------------------------------
37
38
39 // **CloudFilesClient** provides a simple client interface to CloudFiles and Pithos
40 //
41 // The class provides methods to upload/download files, delete files, manage containers
42
43
44 using System;
45 using System.Collections.Generic;
46 using System.Collections.Specialized;
47 using System.ComponentModel.Composition;
48 using System.Diagnostics;
49 using System.Diagnostics.Contracts;
50 using System.IO;
51 using System.Linq;
52 using System.Net;
53 using System.Security.Cryptography;
54 using System.Text;
55 using System.Threading.Tasks;
56 using Newtonsoft.Json;
57 using Pithos.Interfaces;
58 using log4net;
59
60 namespace Pithos.Network
61 {
62     [Export(typeof(ICloudClient))]
63     public class CloudFilesClient:ICloudClient
64     {
65         //CloudFilesClient uses *_baseClient* internally to communicate with the server
66         //RestClient provides a REST-friendly interface over the standard WebClient.
67         private RestClient _baseClient;
68         
69
70         //During authentication the client provides a UserName 
71         public string UserName { get; set; }
72         
73         //and and ApiKey to the server
74         public string ApiKey { get; set; }
75         
76         //And receives an authentication Token. This token must be provided in ALL other operations,
77         //in the X-Auth-Token header
78         private string _token;
79         public string Token
80         {
81             get { return _token; }
82             set
83             {
84                 _token = value;
85                 _baseClient.Headers["X-Auth-Token"] = value;
86             }
87         }
88
89         //The client also receives a StorageUrl after authentication. All subsequent operations must
90         //use this url
91         public Uri StorageUrl { get; set; }
92
93
94         protected Uri RootAddressUri { get; set; }
95
96         private WebProxy _proxy;
97         public WebProxy Proxy
98         {
99             get { return _proxy; }
100             set
101             {
102                 _proxy = value;
103                 if (_baseClient != null)
104                     _baseClient.Proxy = value;                
105             }
106         }
107
108
109         /* private Uri _proxy;
110         public Uri Proxy
111         {
112             get { return _proxy; }
113             set
114             {
115                 _proxy = value;
116                 if (_baseClient != null)
117                     _baseClient.Proxy = new WebProxy(value);                
118             }
119         }*/
120
121         public double DownloadPercentLimit { get; set; }
122         public double UploadPercentLimit { get; set; }
123
124         public string AuthenticationUrl { get; set; }
125
126  
127         public string VersionPath
128         {
129             get { return UsePithos ? "v1" : "v1.0"; }
130         }
131
132         public bool UsePithos { get; set; }
133
134
135         private static readonly ILog Log = LogManager.GetLogger("CloudFilesClient");
136
137         public CloudFilesClient(string userName, string apiKey)
138         {
139             UserName = userName;
140             ApiKey = apiKey;
141         }
142
143         public CloudFilesClient(AccountInfo accountInfo)
144         {
145             if (accountInfo==null)
146                 throw new ArgumentNullException("accountInfo");
147             Contract.Ensures(!String.IsNullOrWhiteSpace(Token));
148             Contract.Ensures(StorageUrl != null);
149             Contract.Ensures(_baseClient != null);
150             Contract.Ensures(RootAddressUri != null);
151             Contract.EndContractBlock();
152
153             _baseClient = new RestClient
154             {
155                 BaseAddress = accountInfo.StorageUri.ToString(),
156                 Timeout = 10000,
157                 Retries = 3,
158                 Proxy=this.Proxy
159             };
160             StorageUrl = accountInfo.StorageUri;
161             Token = accountInfo.Token;
162             UserName = accountInfo.UserName;
163
164             //Get the root address (StorageUrl without the account)
165             var storageUrl = StorageUrl.AbsoluteUri;
166             var usernameIndex = storageUrl.LastIndexOf(UserName);
167             var rootUrl = storageUrl.Substring(0, usernameIndex);
168             RootAddressUri = new Uri(rootUrl);
169         }
170
171
172         public AccountInfo Authenticate()
173         {
174             if (String.IsNullOrWhiteSpace(UserName))
175                 throw new InvalidOperationException("UserName is empty");
176             if (String.IsNullOrWhiteSpace(ApiKey))
177                 throw new InvalidOperationException("ApiKey is empty");
178             if (String.IsNullOrWhiteSpace(AuthenticationUrl))
179                 throw new InvalidOperationException("AuthenticationUrl is empty");
180             Contract.Ensures(!String.IsNullOrWhiteSpace(Token));
181             Contract.Ensures(StorageUrl != null);
182             Contract.Ensures(_baseClient != null);
183             Contract.Ensures(RootAddressUri != null);
184             Contract.EndContractBlock();
185
186
187             Log.InfoFormat("[AUTHENTICATE] Start for {0}", UserName);
188
189             var groups = new List<Group>();
190
191             using (var authClient = new RestClient{BaseAddress=AuthenticationUrl})
192             {                
193                 if (Proxy != null)
194                     authClient.Proxy = Proxy;
195
196                 Contract.Assume(authClient.Headers!=null);
197
198                 authClient.Headers.Add("X-Auth-User", UserName);
199                 authClient.Headers.Add("X-Auth-Key", ApiKey);
200
201                 authClient.DownloadStringWithRetry(VersionPath, 3);
202
203                 authClient.AssertStatusOK("Authentication failed");
204
205                 var storageUrl = authClient.GetHeaderValue("X-Storage-Url");
206                 if (String.IsNullOrWhiteSpace(storageUrl))
207                     throw new InvalidOperationException("Failed to obtain storage url");
208                 
209                 _baseClient = new RestClient
210                 {
211                     BaseAddress = storageUrl,
212                     Timeout = 10000,
213                     Retries = 3,
214                     Proxy=Proxy
215                 };
216
217                 StorageUrl = new Uri(storageUrl);
218                 
219                 //Get the root address (StorageUrl without the account)
220                 var usernameIndex=storageUrl.LastIndexOf(UserName);
221                 var rootUrl = storageUrl.Substring(0, usernameIndex);
222                 RootAddressUri = new Uri(rootUrl);
223                 
224                 var token = authClient.GetHeaderValue("X-Auth-Token");
225                 if (String.IsNullOrWhiteSpace(token))
226                     throw new InvalidOperationException("Failed to obtain token url");
227                 Token = token;
228
229                /* var keys = authClient.ResponseHeaders.AllKeys.AsQueryable();
230                 groups = (from key in keys
231                             where key.StartsWith("X-Account-Group-")
232                             let name = key.Substring(16)
233                             select new Group(name, authClient.ResponseHeaders[key]))
234                         .ToList();
235                     
236 */
237             }
238
239             Log.InfoFormat("[AUTHENTICATE] End for {0}", UserName);
240             
241
242             return new AccountInfo {StorageUri = StorageUrl, Token = Token, UserName = UserName,Groups=groups};            
243
244         }
245
246
247
248         public IList<ContainerInfo> ListContainers(string account)
249         {
250             using (var client = new RestClient(_baseClient))
251             {
252                 if (!String.IsNullOrWhiteSpace(account))
253                     client.BaseAddress = GetAccountUrl(account);
254                 
255                 client.Parameters.Clear();
256                 client.Parameters.Add("format", "json");
257                 var content = client.DownloadStringWithRetry("", 3);
258                 client.AssertStatusOK("List Containers failed");
259
260                 if (client.StatusCode == HttpStatusCode.NoContent)
261                     return new List<ContainerInfo>();
262                 var infos = JsonConvert.DeserializeObject<IList<ContainerInfo>>(content);
263                 
264                 foreach (var info in infos)
265                 {
266                     info.Account = account;
267                 }
268                 return infos;
269             }
270
271         }
272
273         private string GetAccountUrl(string account)
274         {
275             return new Uri(RootAddressUri, new Uri(account,UriKind.Relative)).AbsoluteUri;
276         }
277
278         public IList<ShareAccountInfo> ListSharingAccounts(DateTime? since=null)
279         {
280             using (ThreadContext.Stacks["Share"].Push("List Accounts"))
281             {
282                 if (Log.IsDebugEnabled) Log.DebugFormat("START");
283
284                 using (var client = new RestClient(_baseClient))
285                 {
286                     client.Parameters.Clear();
287                     client.Parameters.Add("format", "json");
288                     client.IfModifiedSince = since;
289
290                     //Extract the username from the base address
291                     client.BaseAddress = RootAddressUri.AbsoluteUri;
292
293                     var content = client.DownloadStringWithRetry(@"", 3);
294
295                     client.AssertStatusOK("ListSharingAccounts failed");
296
297                     //If the result is empty, return an empty list,
298                     var infos = String.IsNullOrWhiteSpace(content)
299                                     ? new List<ShareAccountInfo>()
300                                 //Otherwise deserialize the account list into a list of ShareAccountInfos
301                                     : JsonConvert.DeserializeObject<IList<ShareAccountInfo>>(content);
302
303                     Log.DebugFormat("END");
304                     return infos;
305                 }
306             }
307         }
308
309         //Request listing of all objects in a container modified since a specific time.
310         //If the *since* value is missing, return all objects
311         public IList<ObjectInfo> ListSharedObjects(DateTime? since = null)
312         {
313
314             using (ThreadContext.Stacks["Share"].Push("List Objects"))
315             {
316                 if (Log.IsDebugEnabled) Log.DebugFormat("START");
317
318                 var objects = new List<ObjectInfo>();
319                 var accounts = ListSharingAccounts(since);
320                 foreach (var account in accounts)
321                 {
322                     var containers = ListContainers(account.name);
323                     foreach (var container in containers)
324                     {
325                         var containerObjects = ListObjects(account.name, container.Name);
326                         objects.AddRange(containerObjects);
327                     }
328                 }
329                 if (Log.IsDebugEnabled) Log.DebugFormat("END");
330                 return objects;
331             }
332         }
333
334         public void SetTags(ObjectInfo target,IDictionary<string,string> tags)
335         {
336             if (String.IsNullOrWhiteSpace(Token))
337                 throw new InvalidOperationException("The Token is not set");
338             if (StorageUrl == null)
339                 throw new InvalidOperationException("The StorageUrl is not set");
340             if (target == null)
341                 throw new ArgumentNullException("target");
342             Contract.EndContractBlock();
343
344             using (ThreadContext.Stacks["Share"].Push("Share Object"))
345             {
346                 if (Log.IsDebugEnabled) Log.DebugFormat("START");
347
348                 using (var client = new RestClient(_baseClient))
349                 {
350
351                     client.BaseAddress = GetAccountUrl(target.Account);
352
353                     client.Parameters.Clear();
354                     client.Parameters.Add("update", "");
355
356                     foreach (var tag in tags)
357                     {
358                         var headerTag = String.Format("X-Object-Meta-{0}", tag.Key);
359                         client.Headers.Add(headerTag, tag.Value);
360                     }
361                     
362                     client.DownloadStringWithRetry(target.Container, 3);
363
364                     
365                     client.AssertStatusOK("SetTags failed");
366                     //If the status is NOT ACCEPTED we have a problem
367                     if (client.StatusCode != HttpStatusCode.Accepted)
368                     {
369                         Log.Error("Failed to set tags");
370                         throw new Exception("Failed to set tags");
371                     }
372
373                     if (Log.IsDebugEnabled) Log.DebugFormat("END");
374                 }
375             }
376
377
378         }
379
380         public void ShareObject(string account, string container, string objectName, string shareTo, bool read, bool write)
381         {
382             if (String.IsNullOrWhiteSpace(Token))
383                 throw new InvalidOperationException("The Token is not set");
384             if (StorageUrl==null)
385                 throw new InvalidOperationException("The StorageUrl is not set");
386             if (String.IsNullOrWhiteSpace(container))
387                 throw new ArgumentNullException("container");
388             if (String.IsNullOrWhiteSpace(objectName))
389                 throw new ArgumentNullException("objectName");
390             if (String.IsNullOrWhiteSpace(account))
391                 throw new ArgumentNullException("account");
392             if (String.IsNullOrWhiteSpace(shareTo))
393                 throw new ArgumentNullException("shareTo");
394             Contract.EndContractBlock();
395
396             using (ThreadContext.Stacks["Share"].Push("Share Object"))
397             {
398                 if (Log.IsDebugEnabled) Log.DebugFormat("START");
399                 
400                 using (var client = new RestClient(_baseClient))
401                 {
402
403                     client.BaseAddress = GetAccountUrl(account);
404
405                     client.Parameters.Clear();
406                     client.Parameters.Add("format", "json");
407
408                     string permission = "";
409                     if (write)
410                         permission = String.Format("write={0}", shareTo);
411                     else if (read)
412                         permission = String.Format("read={0}", shareTo);
413                     client.Headers.Add("X-Object-Sharing", permission);
414
415                     var content = client.DownloadStringWithRetry(container, 3);
416
417                     client.AssertStatusOK("ShareObject failed");
418
419                     //If the result is empty, return an empty list,
420                     var infos = String.IsNullOrWhiteSpace(content)
421                                     ? new List<ObjectInfo>()
422                                 //Otherwise deserialize the object list into a list of ObjectInfos
423                                     : JsonConvert.DeserializeObject<IList<ObjectInfo>>(content);
424
425                     if (Log.IsDebugEnabled) Log.DebugFormat("END");
426                 }
427             }
428
429
430         }
431
432         public AccountInfo GetAccountPolicies(AccountInfo accountInfo)
433         {
434             if (accountInfo==null)
435                 throw new ArgumentNullException("accountInfo");
436             Contract.EndContractBlock();
437
438             using (ThreadContext.Stacks["Account"].Push("GetPolicies"))
439             {
440                 if (Log.IsDebugEnabled) Log.DebugFormat("START");
441
442                 using (var client = new RestClient(_baseClient))
443                 {
444                     if (!String.IsNullOrWhiteSpace(accountInfo.UserName))
445                         client.BaseAddress = GetAccountUrl(accountInfo.UserName);
446
447                     client.Parameters.Clear();
448                     client.Parameters.Add("format", "json");                    
449                     client.Head(String.Empty, 3);
450
451                     var quotaValue=client.ResponseHeaders["X-Account-Policy-Quota"];
452                     var bytesValue= client.ResponseHeaders["X-Account-Bytes-Used"];
453
454                     long quota, bytes;
455                     if (long.TryParse(quotaValue, out quota))
456                         accountInfo.Quota = quota;
457                     if (long.TryParse(bytesValue, out bytes))
458                         accountInfo.BytesUsed = bytes;
459                     
460                     return accountInfo;
461
462                 }
463
464             }
465         }
466
467         public void UpdateMetadata(ObjectInfo objectInfo)
468         {
469             if (objectInfo == null)
470                 throw new ArgumentNullException("objectInfo");
471             Contract.EndContractBlock();
472
473             using (ThreadContext.Stacks["Objects"].Push("UpdateMetadata"))
474             {
475                 if (Log.IsDebugEnabled) Log.DebugFormat("START");
476
477
478                 using(var client=new RestClient(_baseClient))
479                 {
480
481                     client.BaseAddress = GetAccountUrl(objectInfo.Account);
482                     
483                     client.Parameters.Clear();
484                     
485
486                     //Set Tags
487                     foreach (var tag in objectInfo.Tags)
488                     {
489                         var headerTag = String.Format("X-Object-Meta-{0}", tag.Key);
490                         client.Headers.Add(headerTag, tag.Value);
491                     }
492
493                     //Set Permissions
494
495                     var permissions=objectInfo.GetPermissionString();
496                     client.SetNonEmptyHeaderValue("X-Object-Sharing",permissions);
497
498                     client.SetNonEmptyHeaderValue("Content-Disposition",objectInfo.ContendDisposition);
499                     client.SetNonEmptyHeaderValue("Content-Encoding",objectInfo.ContentEncoding);
500                     client.SetNonEmptyHeaderValue("X-Object-Manifest",objectInfo.Manifest);
501                     var isPublic = objectInfo.IsPublic.ToString().ToLower();
502                     client.Headers.Add("X-Object-Public", isPublic);
503
504
505                     var uriBuilder = client.GetAddressBuilder(objectInfo.Container, objectInfo.Name);
506                     var uri = uriBuilder.Uri;
507
508                     client.UploadValues(uri,new NameValueCollection());
509
510
511                     client.AssertStatusOK("UpdateMetadata failed");
512                     //If the status is NOT ACCEPTED or OK we have a problem
513                     if (!(client.StatusCode == HttpStatusCode.Accepted || client.StatusCode == HttpStatusCode.OK))
514                     {
515                         Log.Error("Failed to update metadata");
516                         throw new Exception("Failed to update metadata");
517                     }
518
519                     if (Log.IsDebugEnabled) Log.DebugFormat("END");
520                 }
521             }
522
523         }
524
525         public void UpdateMetadata(ContainerInfo containerInfo)
526         {
527             if (containerInfo == null)
528                 throw new ArgumentNullException("containerInfo");
529             Contract.EndContractBlock();
530
531             using (ThreadContext.Stacks["Containers"].Push("UpdateMetadata"))
532             {
533                 if (Log.IsDebugEnabled) Log.DebugFormat("START");
534
535
536                 using(var client=new RestClient(_baseClient))
537                 {
538
539                     client.BaseAddress = GetAccountUrl(containerInfo.Account);
540                     
541                     client.Parameters.Clear();
542                     
543
544                     //Set Tags
545                     foreach (var tag in containerInfo.Tags)
546                     {
547                         var headerTag = String.Format("X-Container-Meta-{0}", tag.Key);
548                         client.Headers.Add(headerTag, tag.Value);
549                     }
550
551                     
552                     //Set Policies
553                     foreach (var policy in containerInfo.Policies)
554                     {
555                         var headerPolicy = String.Format("X-Container-Policy-{0}", policy.Key);
556                         client.Headers.Add(headerPolicy, policy.Value);
557                     }
558
559
560                     var uriBuilder = client.GetAddressBuilder(containerInfo.Name,"");
561                     var uri = uriBuilder.Uri;
562
563                     client.UploadValues(uri,new NameValueCollection());
564
565
566                     client.AssertStatusOK("UpdateMetadata failed");
567                     //If the status is NOT ACCEPTED or OK we have a problem
568                     if (!(client.StatusCode == HttpStatusCode.Accepted || client.StatusCode == HttpStatusCode.OK))
569                     {
570                         Log.Error("Failed to update metadata");
571                         throw new Exception("Failed to update metadata");
572                     }
573
574                     if (Log.IsDebugEnabled) Log.DebugFormat("END");
575                 }
576             }
577
578         }
579
580
581         public IList<ObjectInfo> ListObjects(string account, string container, DateTime? since = null)
582         {
583             if (String.IsNullOrWhiteSpace(container))
584                 throw new ArgumentNullException("container");
585             Contract.EndContractBlock();
586
587             using (ThreadContext.Stacks["Objects"].Push("List"))
588             {
589                 if (Log.IsDebugEnabled) Log.DebugFormat("START");
590
591                 using (var client = new RestClient(_baseClient))
592                 {
593                     if (!String.IsNullOrWhiteSpace(account))
594                         client.BaseAddress = GetAccountUrl(account);
595
596                     client.Parameters.Clear();
597                     client.Parameters.Add("format", "json");
598                     client.IfModifiedSince = since;
599                     var content = client.DownloadStringWithRetry(container, 3);
600
601                     client.AssertStatusOK("ListObjects failed");
602
603                     //If the result is empty, return an empty list,
604                     var infos = String.IsNullOrWhiteSpace(content)
605                                     ? new List<ObjectInfo>()
606                                 //Otherwise deserialize the object list into a list of ObjectInfos
607                                     : JsonConvert.DeserializeObject<IList<ObjectInfo>>(content);
608
609                     foreach (var info in infos)
610                     {
611                         info.Container = container;
612                         info.Account = account;
613                     }
614                     if (Log.IsDebugEnabled) Log.DebugFormat("START");
615                     return infos;
616                 }
617             }
618         }
619
620
621
622         public IList<ObjectInfo> ListObjects(string account, string container, string folder, DateTime? since = null)
623         {
624             if (String.IsNullOrWhiteSpace(container))
625                 throw new ArgumentNullException("container");
626             if (String.IsNullOrWhiteSpace(folder))
627                 throw new ArgumentNullException("folder");
628             Contract.EndContractBlock();
629
630             using (ThreadContext.Stacks["Objects"].Push("List"))
631             {
632                 if (Log.IsDebugEnabled) Log.DebugFormat("START");
633
634                 using (var client = new RestClient(_baseClient))
635                 {
636                     if (!String.IsNullOrWhiteSpace(account))
637                         client.BaseAddress = GetAccountUrl(account);
638
639                     client.Parameters.Clear();
640                     client.Parameters.Add("format", "json");
641                     client.Parameters.Add("path", folder);
642                     client.IfModifiedSince = since;
643                     var content = client.DownloadStringWithRetry(container, 3);
644                     client.AssertStatusOK("ListObjects failed");
645
646                     var infos = JsonConvert.DeserializeObject<IList<ObjectInfo>>(content);
647
648                     if (Log.IsDebugEnabled) Log.DebugFormat("END");
649                     return infos;
650                 }
651             }
652         }
653
654  
655         public bool ContainerExists(string account, string container)
656         {
657             if (String.IsNullOrWhiteSpace(container))
658                 throw new ArgumentNullException("container", "The container property can't be empty");
659             Contract.EndContractBlock();
660
661             using (ThreadContext.Stacks["Containters"].Push("Exists"))
662             {
663                 if (Log.IsDebugEnabled) Log.DebugFormat("START");
664
665                 using (var client = new RestClient(_baseClient))
666                 {
667                     if (!String.IsNullOrWhiteSpace(account))
668                         client.BaseAddress = GetAccountUrl(account);
669
670                     client.Parameters.Clear();
671                     client.Head(container, 3);
672                                         
673                     bool result;
674                     switch (client.StatusCode)
675                     {
676                         case HttpStatusCode.OK:
677                         case HttpStatusCode.NoContent:
678                             result=true;
679                             break;
680                         case HttpStatusCode.NotFound:
681                             result=false;
682                             break;
683                         default:
684                             throw CreateWebException("ContainerExists", client.StatusCode);
685                     }
686                     if (Log.IsDebugEnabled) Log.DebugFormat("END");
687
688                     return result;
689                 }
690                 
691             }
692         }
693
694         public bool ObjectExists(string account, string container, string objectName)
695         {
696             if (String.IsNullOrWhiteSpace(container))
697                 throw new ArgumentNullException("container", "The container property can't be empty");
698             if (String.IsNullOrWhiteSpace(objectName))
699                 throw new ArgumentNullException("objectName", "The objectName property can't be empty");
700             Contract.EndContractBlock();
701
702             using (var client = new RestClient(_baseClient))
703             {
704                 if (!String.IsNullOrWhiteSpace(account))
705                     client.BaseAddress = GetAccountUrl(account);
706
707                 client.Parameters.Clear();
708                 client.Head(container + "/" + objectName, 3);
709
710                 switch (client.StatusCode)
711                 {
712                     case HttpStatusCode.OK:
713                     case HttpStatusCode.NoContent:
714                         return true;
715                     case HttpStatusCode.NotFound:
716                         return false;
717                     default:
718                         throw CreateWebException("ObjectExists", client.StatusCode);
719                 }
720             }
721
722         }
723
724         public ObjectInfo GetObjectInfo(string account, string container, string objectName)
725         {
726             if (String.IsNullOrWhiteSpace(container))
727                 throw new ArgumentNullException("container", "The container property can't be empty");
728             if (String.IsNullOrWhiteSpace(objectName))
729                 throw new ArgumentNullException("objectName", "The objectName property can't be empty");
730             Contract.EndContractBlock();
731
732             using (ThreadContext.Stacks["Objects"].Push("GetObjectInfo"))
733             {                
734
735                 using (var client = new RestClient(_baseClient))
736                 {
737                     if (!String.IsNullOrWhiteSpace(account))
738                         client.BaseAddress = GetAccountUrl(account);
739                     try
740                     {
741                         client.Parameters.Clear();
742
743                         client.Head(container + "/" + objectName, 3);
744
745                         if (client.TimedOut)
746                             return ObjectInfo.Empty;
747
748                         switch (client.StatusCode)
749                         {
750                             case HttpStatusCode.OK:
751                             case HttpStatusCode.NoContent:
752                                 var keys = client.ResponseHeaders.AllKeys.AsQueryable();
753                                 var tags = client.GetMeta("X-Object-Meta-");
754                                 var extensions = (from key in keys
755                                                   where key.StartsWith("X-Object-") && !key.StartsWith("X-Object-Meta-")
756                                                   select new {Name = key, Value = client.ResponseHeaders[key]})
757                                     .ToDictionary(t => t.Name, t => t.Value);
758
759                                 var permissions=client.GetHeaderValue("X-Object-Sharing", true);
760                                 
761                                 
762                                 var info = new ObjectInfo
763                                                {
764                                                    Account = account,
765                                                    Container = container,
766                                                    Name = objectName,
767                                                    Hash = client.GetHeaderValue("ETag"),
768                                                    Content_Type = client.GetHeaderValue("Content-Type"),
769                                                    Bytes = Convert.ToInt64(client.GetHeaderValue("Content-Length",true)),
770                                                    Tags = tags,
771                                                    Last_Modified = client.LastModified,
772                                                    Extensions = extensions,
773                                                    ContentEncoding=client.GetHeaderValue("Content-Encoding",true),
774                                                    ContendDisposition = client.GetHeaderValue("Content-Disposition",true),
775                                                    Manifest=client.GetHeaderValue("X-Object-Manifest",true),
776                                                    PublicUrl=client.GetHeaderValue("X-Object-Public",true),                                                   
777                                                };
778                                 info.SetPermissions(permissions);
779                                 return info;
780                             case HttpStatusCode.NotFound:
781                                 return ObjectInfo.Empty;
782                             default:
783                                 throw new WebException(
784                                     String.Format("[FAIL] GetObjectInfo for {0} failed with unexpected status code {1}",
785                                                   objectName, client.StatusCode));
786                         }
787
788                     }
789                     catch (RetryException)
790                     {
791                         Log.WarnFormat("[RETRY FAIL] GetObjectInfo for {0} failed.",objectName);
792                         return ObjectInfo.Empty;
793                     }
794                     catch (WebException e)
795                     {
796                         Log.Error(
797                             String.Format("[FAIL] GetObjectInfo for {0} failed with unexpected status code {1}",
798                                           objectName, client.StatusCode), e);
799                         throw;
800                     }
801                 }                
802             }
803
804         }
805
806         public void CreateFolder(string account, string container, string folder)
807         {
808             if (String.IsNullOrWhiteSpace(container))
809                 throw new ArgumentNullException("container", "The container property can't be empty");
810             if (String.IsNullOrWhiteSpace(folder))
811                 throw new ArgumentNullException("folder", "The folder property can't be empty");
812             Contract.EndContractBlock();
813
814             var folderUrl=String.Format("{0}/{1}",container,folder);
815             using (var client = new RestClient(_baseClient))
816             {
817                 if (!String.IsNullOrWhiteSpace(account))
818                     client.BaseAddress = GetAccountUrl(account);
819
820                 client.Parameters.Clear();
821                 client.Headers.Add("Content-Type", @"application/directory");
822                 client.Headers.Add("Content-Length", "0");
823                 client.PutWithRetry(folderUrl, 3);
824
825                 if (client.StatusCode != HttpStatusCode.Created && client.StatusCode != HttpStatusCode.Accepted)
826                     throw CreateWebException("CreateFolder", client.StatusCode);
827             }
828         }
829
830      
831
832         public ContainerInfo GetContainerInfo(string account, string container)
833         {
834             if (String.IsNullOrWhiteSpace(container))
835                 throw new ArgumentNullException("container", "The container property can't be empty");
836             Contract.EndContractBlock();
837
838             using (var client = new RestClient(_baseClient))
839             {
840                 if (!String.IsNullOrWhiteSpace(account))
841                     client.BaseAddress = GetAccountUrl(account);                
842
843                 client.Head(container);
844                 switch (client.StatusCode)
845                 {
846                     case HttpStatusCode.OK:
847                     case HttpStatusCode.NoContent:
848                         var tags = client.GetMeta("X-Container-Meta-");
849                         var policies = client.GetMeta("X-Container-Policy-");
850
851                         var containerInfo = new ContainerInfo
852                                                 {
853                                                     Account=account,
854                                                     Name = container,
855                                                     Count =
856                                                         long.Parse(client.GetHeaderValue("X-Container-Object-Count")),
857                                                     Bytes = long.Parse(client.GetHeaderValue("X-Container-Bytes-Used")),
858                                                     BlockHash = client.GetHeaderValue("X-Container-Block-Hash"),
859                                                     BlockSize=int.Parse(client.GetHeaderValue("X-Container-Block-Size")),
860                                                     Last_Modified=client.LastModified,
861                                                     Tags=tags,
862                                                     Policies=policies
863                                                 };
864                         
865
866                         return containerInfo;
867                     case HttpStatusCode.NotFound:
868                         return ContainerInfo.Empty;
869                     default:
870                         throw CreateWebException("GetContainerInfo", client.StatusCode);
871                 }
872             }
873         }
874
875         public void CreateContainer(string account, string container)
876         {
877             if (String.IsNullOrWhiteSpace(account))
878                 throw new ArgumentNullException("account");
879             if (String.IsNullOrWhiteSpace(container))
880                 throw new ArgumentNullException("container");
881             Contract.EndContractBlock();
882
883             using (var client = new RestClient(_baseClient))
884             {
885                 if (!String.IsNullOrWhiteSpace(account))
886                     client.BaseAddress = GetAccountUrl(account);
887
888                 client.PutWithRetry(container, 3);
889                 var expectedCodes = new[] {HttpStatusCode.Created, HttpStatusCode.Accepted, HttpStatusCode.OK};
890                 if (!expectedCodes.Contains(client.StatusCode))
891                     throw CreateWebException("CreateContainer", client.StatusCode);
892             }
893         }
894
895         public void DeleteContainer(string account, string container)
896         {
897             if (String.IsNullOrWhiteSpace(container))
898                 throw new ArgumentNullException("container", "The container property can't be empty");
899             Contract.EndContractBlock();
900
901             using (var client = new RestClient(_baseClient))
902             {
903                 if (!String.IsNullOrWhiteSpace(account))
904                     client.BaseAddress = GetAccountUrl(account);
905
906                 client.DeleteWithRetry(container, 3);
907                 var expectedCodes = new[] {HttpStatusCode.NotFound, HttpStatusCode.NoContent};
908                 if (!expectedCodes.Contains(client.StatusCode))
909                     throw CreateWebException("DeleteContainer", client.StatusCode);
910             }
911
912         }
913
914         /// <summary>
915         /// 
916         /// </summary>
917         /// <param name="account"></param>
918         /// <param name="container"></param>
919         /// <param name="objectName"></param>
920         /// <param name="fileName"></param>
921         /// <returns></returns>
922         /// <remarks>This method should have no timeout or a very long one</remarks>
923         //Asynchronously download the object specified by *objectName* in a specific *container* to 
924         // a local file
925         public Task GetObject(string account, string container, string objectName, string fileName)
926         {
927             if (String.IsNullOrWhiteSpace(container))
928                 throw new ArgumentNullException("container", "The container property can't be empty");
929             if (String.IsNullOrWhiteSpace(objectName))
930                 throw new ArgumentNullException("objectName", "The objectName property can't be empty");            
931             Contract.EndContractBlock();
932
933             try
934             {
935                 //WebClient, and by extension RestClient, are not thread-safe. Create a new RestClient
936                 //object to avoid concurrency errors.
937                 //
938                 //Download operations take a long time therefore they have no timeout.
939                 var client = new RestClient(_baseClient) { Timeout = 0 };
940                 if (!String.IsNullOrWhiteSpace(account))
941                     client.BaseAddress = GetAccountUrl(account);
942
943                 //The container and objectName are relative names. They are joined with the client's
944                 //BaseAddress to create the object's absolute address
945                 var builder = client.GetAddressBuilder(container, objectName);
946                 var uri = builder.Uri;
947
948                 //Download progress is reported to the Trace log
949                 Log.InfoFormat("[GET] START {0}", objectName);
950                 client.DownloadProgressChanged += (sender, args) => 
951                     Log.InfoFormat("[GET PROGRESS] {0} {1}% {2} of {3}",
952                                     fileName, args.ProgressPercentage,
953                                     args.BytesReceived,
954                                     args.TotalBytesToReceive);                                
955
956
957                 //Start downloading the object asynchronously
958                 var downloadTask = client.DownloadFileTask(uri, fileName);
959                 
960                 //Once the download completes
961                 return downloadTask.ContinueWith(download =>
962                                       {
963                                           //Delete the local client object
964                                           client.Dispose();
965                                           //And report failure or completion
966                                           if (download.IsFaulted)
967                                           {
968                                               Log.ErrorFormat("[GET] FAIL for {0} with \r{1}", objectName,
969                                                                download.Exception);
970                                           }
971                                           else
972                                           {
973                                               Log.InfoFormat("[GET] END {0}", objectName);                                             
974                                           }
975                                       });
976             }
977             catch (Exception exc)
978             {
979                 Log.ErrorFormat("[GET] END {0} with {1}", objectName, exc);
980                 throw;
981             }
982
983
984
985         }
986
987         public Task<IList<string>> PutHashMap(string account, string container, string objectName, TreeHash hash)
988         {
989             if (String.IsNullOrWhiteSpace(container))
990                 throw new ArgumentNullException("container");
991             if (String.IsNullOrWhiteSpace(objectName))
992                 throw new ArgumentNullException("objectName");
993             if (hash==null)
994                 throw new ArgumentNullException("hash");
995             if (String.IsNullOrWhiteSpace(Token))
996                 throw new InvalidOperationException("Invalid Token");
997             if (StorageUrl == null)
998                 throw new InvalidOperationException("Invalid Storage Url");
999             Contract.EndContractBlock();
1000
1001
1002             //Don't use a timeout because putting the hashmap may be a long process
1003             var client = new RestClient(_baseClient) { Timeout = 0 };           
1004             if (!String.IsNullOrWhiteSpace(account))
1005                 client.BaseAddress = GetAccountUrl(account);
1006
1007             //The container and objectName are relative names. They are joined with the client's
1008             //BaseAddress to create the object's absolute address
1009             var builder = client.GetAddressBuilder(container, objectName);
1010             builder.Query = "format=json&hashmap";
1011             var uri = builder.Uri;
1012
1013
1014             //Send the tree hash as Json to the server            
1015             client.Headers[HttpRequestHeader.ContentType] = "application/octet-stream";
1016             var jsonHash = hash.ToJson();
1017             var uploadTask=client.UploadStringTask(uri, "PUT", jsonHash);
1018             
1019             return uploadTask.ContinueWith(t =>
1020             {
1021
1022                 var empty = (IList<string>)new List<string>();
1023                 
1024
1025                 //The server will respond either with 201-created if all blocks were already on the server
1026                 if (client.StatusCode == HttpStatusCode.Created)                    
1027                 {
1028                     //in which case we return an empty hash list
1029                     return empty;
1030                 }
1031                 //or with a 409-conflict and return the list of missing parts
1032                 //A 409 will cause an exception so we need to check t.IsFaulted to avoid propagating the exception                
1033                 if (t.IsFaulted)
1034                 {
1035                     var ex = t.Exception.InnerException;
1036                     var we = ex as WebException;
1037                     var response = we.Response as HttpWebResponse;
1038                     if (response!=null && response.StatusCode==HttpStatusCode.Conflict)
1039                     {
1040                         //In case of 409 the missing parts will be in the response content                        
1041                         using (var stream = response.GetResponseStream())
1042                         using(var reader=new StreamReader(stream))
1043                         {
1044                             Debug.Assert(stream.Position == 0);
1045                             //We need to cleanup the content before returning it because it contains
1046                             //error content after the list of hashes
1047                             var hashes = new List<string>();
1048                             string line=null;
1049                             //All lines up to the first empty line are hashes
1050                             while(!String.IsNullOrWhiteSpace(line=reader.ReadLine()))
1051                             {
1052                                 hashes.Add(line);
1053                             }
1054
1055                             return hashes;
1056                         }                        
1057                     }                    
1058                     //Any other status code is unexpected and the exception should be rethrown
1059                     throw ex;
1060                     
1061                 }
1062                 //Any other status code is unexpected but there was no exception. We can probably continue processing
1063                 Log.WarnFormat("Unexcpected status code when putting map: {0} - {1}",client.StatusCode,client.StatusDescription);                    
1064                 
1065                 return empty;
1066             });
1067
1068         }
1069
1070         public Task<byte[]> GetBlock(string account, string container, Uri relativeUrl, long start, long? end)
1071         {
1072             if (String.IsNullOrWhiteSpace(Token))
1073                 throw new InvalidOperationException("Invalid Token");
1074             if (StorageUrl == null)
1075                 throw new InvalidOperationException("Invalid Storage Url");
1076             if (String.IsNullOrWhiteSpace(container))
1077                 throw new ArgumentNullException("container");
1078             if (relativeUrl== null)
1079                 throw new ArgumentNullException("relativeUrl");
1080             if (end.HasValue && end<0)
1081                 throw new ArgumentOutOfRangeException("end");
1082             if (start<0)
1083                 throw new ArgumentOutOfRangeException("start");
1084             Contract.EndContractBlock();
1085
1086
1087             //Don't use a timeout because putting the hashmap may be a long process
1088             var client = new RestClient(_baseClient) {Timeout = 0, RangeFrom = start, RangeTo = end};
1089             if (!String.IsNullOrWhiteSpace(account))
1090                 client.BaseAddress = GetAccountUrl(account);
1091
1092             var builder = client.GetAddressBuilder(container, relativeUrl.ToString());
1093             var uri = builder.Uri;
1094
1095             return client.DownloadDataTask(uri)
1096                 .ContinueWith(t=>
1097                                   {
1098                                       client.Dispose();
1099                                       return t.Result;
1100                                   });
1101         }
1102
1103
1104         public async Task PostBlock(string account, string container, byte[] block, int offset, int count)
1105         {
1106             if (String.IsNullOrWhiteSpace(container))
1107                 throw new ArgumentNullException("container");
1108             if (block == null)
1109                 throw new ArgumentNullException("block");
1110             if (offset < 0 || offset >= block.Length)
1111                 throw new ArgumentOutOfRangeException("offset");
1112             if (count < 0 || count > block.Length)
1113                 throw new ArgumentOutOfRangeException("count");
1114             if (String.IsNullOrWhiteSpace(Token))
1115                 throw new InvalidOperationException("Invalid Token");
1116             if (StorageUrl == null)
1117                 throw new InvalidOperationException("Invalid Storage Url");                        
1118             Contract.EndContractBlock();
1119
1120
1121             try
1122             {
1123
1124             //Don't use a timeout because putting the hashmap may be a long process
1125                 using (var client = new RestClient(_baseClient) { Timeout = 0 })
1126                 {
1127                     if (!String.IsNullOrWhiteSpace(account))
1128                         client.BaseAddress = GetAccountUrl(account);
1129
1130                     var builder = client.GetAddressBuilder(container, "");
1131                     //We are doing an update
1132                     builder.Query = "update";
1133                     var uri = builder.Uri;
1134
1135                     client.Headers[HttpRequestHeader.ContentType] = "application/octet-stream";
1136
1137                     Log.InfoFormat("[BLOCK POST] START");
1138
1139                     client.UploadProgressChanged += (sender, args) =>
1140                                                     Log.InfoFormat("[BLOCK POST PROGRESS] {0}% {1} of {2}",
1141                                                                    args.ProgressPercentage, args.BytesSent,
1142                                                                    args.TotalBytesToSend);
1143                     client.UploadFileCompleted += (sender, args) =>
1144                                                   Log.InfoFormat("[BLOCK POST PROGRESS] Completed ");
1145
1146                     var buffer = new byte[count];
1147                     Buffer.BlockCopy(block, offset, buffer, 0, count);
1148                     //Send the block
1149                     await client.UploadDataTask(uri, "POST", buffer);
1150                     Log.InfoFormat("[BLOCK POST] END");
1151                 }
1152             }
1153             catch (Exception exc)
1154             {
1155                 Log.ErrorFormat("[BLOCK POST] FAIL with \r{0}", exc);                                        
1156                 throw;
1157             }
1158         }
1159
1160
1161         public async Task<TreeHash> GetHashMap(string account, string container, string objectName)
1162         {
1163             if (String.IsNullOrWhiteSpace(container))
1164                 throw new ArgumentNullException("container");
1165             if (String.IsNullOrWhiteSpace(objectName))
1166                 throw new ArgumentNullException("objectName");
1167             if (String.IsNullOrWhiteSpace(Token))
1168                 throw new InvalidOperationException("Invalid Token");
1169             if (StorageUrl == null)
1170                 throw new InvalidOperationException("Invalid Storage Url");
1171             Contract.EndContractBlock();
1172
1173             try
1174             {
1175                 //WebClient, and by extension RestClient, are not thread-safe. Create a new RestClient
1176                 //object to avoid concurrency errors.
1177                 //
1178                 //Download operations take a long time therefore they have no timeout.
1179                 //TODO: Do they really? this is a hashmap operation, not a download
1180                 
1181                 //Start downloading the object asynchronously
1182                 using (var client = new RestClient(_baseClient) { Timeout = 0 })
1183                 {
1184                     if (!String.IsNullOrWhiteSpace(account))
1185                         client.BaseAddress = GetAccountUrl(account);
1186
1187                     //The container and objectName are relative names. They are joined with the client's
1188                     //BaseAddress to create the object's absolute address
1189                     var builder = client.GetAddressBuilder(container, objectName);
1190                     builder.Query = "format=json&hashmap";
1191                     var uri = builder.Uri;
1192
1193
1194                     var json = await client.DownloadStringTaskAsync(uri);
1195                     var treeHash = TreeHash.Parse(json);
1196                     Log.InfoFormat("[GET HASH] END {0}", objectName);
1197                     return treeHash;
1198                 }
1199             }
1200             catch (Exception exc)
1201             {
1202                 Log.ErrorFormat("[GET HASH] END {0} with {1}", objectName, exc);
1203                 throw;
1204             }
1205
1206         }
1207
1208
1209         /// <summary>
1210         /// 
1211         /// </summary>
1212         /// <param name="account"></param>
1213         /// <param name="container"></param>
1214         /// <param name="objectName"></param>
1215         /// <param name="fileName"></param>
1216         /// <param name="hash">Optional hash value for the file. If no hash is provided, the method calculates a new hash</param>
1217         /// <remarks>>This method should have no timeout or a very long one</remarks>
1218         public async Task PutObject(string account, string container, string objectName, string fileName, string hash = null, string contentType = "application/octet-stream")
1219         {
1220             if (String.IsNullOrWhiteSpace(container))
1221                 throw new ArgumentNullException("container", "The container property can't be empty");
1222             if (String.IsNullOrWhiteSpace(objectName))
1223                 throw new ArgumentNullException("objectName", "The objectName property can't be empty");
1224             if (String.IsNullOrWhiteSpace(fileName))
1225                 throw new ArgumentNullException("fileName", "The fileName property can't be empty");
1226 /*
1227             if (!File.Exists(fileName) && !Directory.Exists(fileName))
1228                 throw new FileNotFoundException("The file or directory does not exist",fileName);
1229 */
1230             Contract.EndContractBlock();
1231             
1232             try
1233             {
1234
1235                 using (var client = new RestClient(_baseClient) { Timeout = 0 })
1236                 {
1237                     if (!String.IsNullOrWhiteSpace(account))
1238                         client.BaseAddress = GetAccountUrl(account);
1239
1240                     var builder = client.GetAddressBuilder(container, objectName);
1241                     var uri = builder.Uri;
1242
1243                     string etag = hash ?? CalculateHash(fileName);
1244
1245                     client.Headers.Add("Content-Type", contentType);
1246                     client.Headers.Add("ETag", etag);
1247
1248
1249                     Log.InfoFormat("[PUT] START {0}", objectName);
1250                     client.UploadProgressChanged += (sender, args) =>
1251                                                         {
1252                                                             using (ThreadContext.Stacks["PUT"].Push("Progress"))
1253                                                             {
1254                                                                 Log.InfoFormat("{0} {1}% {2} of {3}", fileName,
1255                                                                                args.ProgressPercentage,
1256                                                                                args.BytesSent, args.TotalBytesToSend);
1257                                                             }
1258                                                         };
1259
1260                     client.UploadFileCompleted += (sender, args) =>
1261                                                       {
1262                                                           using (ThreadContext.Stacks["PUT"].Push("Progress"))
1263                                                           {
1264                                                               Log.InfoFormat("Completed {0}", fileName);
1265                                                           }
1266                                                       };
1267                     if (contentType=="application/directory")
1268                         await client.UploadDataTaskAsync(uri, "PUT", new byte[0]);
1269                     else
1270                         await client.UploadFileTaskAsync(uri, "PUT", fileName);
1271                 }
1272
1273                 Log.InfoFormat("[PUT] END {0}", objectName);
1274             }
1275             catch (Exception exc)
1276             {
1277                 Log.ErrorFormat("[PUT] END {0} with {1}", objectName, exc);
1278                 throw;
1279             }                
1280
1281         }
1282        
1283         
1284         private static string CalculateHash(string fileName)
1285         {
1286             Contract.Requires(!String.IsNullOrWhiteSpace(fileName));
1287             Contract.EndContractBlock();
1288
1289             string hash;
1290             using (var hasher = MD5.Create())
1291             using(var stream=File.OpenRead(fileName))
1292             {
1293                 var hashBuilder=new StringBuilder();
1294                 foreach (byte b in hasher.ComputeHash(stream))
1295                     hashBuilder.Append(b.ToString("x2").ToLower());
1296                 hash = hashBuilder.ToString();                
1297             }
1298             return hash;
1299         }
1300         
1301         public void MoveObject(string account, string sourceContainer, string oldObjectName, string targetContainer, string newObjectName)
1302         {
1303             if (String.IsNullOrWhiteSpace(sourceContainer))
1304                 throw new ArgumentNullException("sourceContainer", "The container property can't be empty");
1305             if (String.IsNullOrWhiteSpace(oldObjectName))
1306                 throw new ArgumentNullException("oldObjectName", "The oldObjectName property can't be empty");
1307             if (String.IsNullOrWhiteSpace(targetContainer))
1308                 throw new ArgumentNullException("targetContainer", "The container property can't be empty");
1309             if (String.IsNullOrWhiteSpace(newObjectName))
1310                 throw new ArgumentNullException("newObjectName", "The newObjectName property can't be empty");
1311             Contract.EndContractBlock();
1312
1313             var targetUrl = targetContainer + "/" + newObjectName;
1314             var sourceUrl = String.Format("/{0}/{1}", sourceContainer, oldObjectName);
1315
1316             using (var client = new RestClient(_baseClient))
1317             {
1318                 if (!String.IsNullOrWhiteSpace(account))
1319                     client.BaseAddress = GetAccountUrl(account);
1320
1321                 client.Headers.Add("X-Move-From", sourceUrl);
1322                 client.PutWithRetry(targetUrl, 3);
1323
1324                 var expectedCodes = new[] {HttpStatusCode.OK, HttpStatusCode.NoContent, HttpStatusCode.Created};
1325                 if (!expectedCodes.Contains(client.StatusCode))
1326                     throw CreateWebException("MoveObject", client.StatusCode);
1327             }
1328         }
1329
1330         public void DeleteObject(string account, string sourceContainer, string objectName)
1331         {            
1332             if (String.IsNullOrWhiteSpace(sourceContainer))
1333                 throw new ArgumentNullException("sourceContainer", "The container property can't be empty");
1334             if (String.IsNullOrWhiteSpace(objectName))
1335                 throw new ArgumentNullException("objectName", "The oldObjectName property can't be empty");
1336             Contract.EndContractBlock();
1337
1338             var targetUrl = FolderConstants.TrashContainer + "/" + objectName;
1339             var sourceUrl = String.Format("/{0}/{1}", sourceContainer, objectName);
1340
1341             using (var client = new RestClient(_baseClient))
1342             {
1343                 if (!String.IsNullOrWhiteSpace(account))
1344                     client.BaseAddress = GetAccountUrl(account);
1345
1346                 client.Headers.Add("X-Move-From", sourceUrl);
1347                 client.AllowedStatusCodes.Add(HttpStatusCode.NotFound);
1348                 client.PutWithRetry(targetUrl, 3);
1349
1350                 var expectedCodes = new[] {HttpStatusCode.OK, HttpStatusCode.NoContent, HttpStatusCode.Created,HttpStatusCode.NotFound};
1351                 if (!expectedCodes.Contains(client.StatusCode))
1352                     throw CreateWebException("DeleteObject", client.StatusCode);
1353             }
1354         }
1355
1356       
1357         private static WebException CreateWebException(string operation, HttpStatusCode statusCode)
1358         {
1359             return new WebException(String.Format("{0} failed with unexpected status code {1}", operation, statusCode));
1360         }
1361
1362         
1363     }
1364
1365     public class ShareAccountInfo
1366     {
1367         public DateTime? last_modified { get; set; }
1368         public string name { get; set; }
1369     }
1370 }