Statistics
| Branch: | Revision:

root / trunk / Pithos.Network / CloudFilesClient.cs @ d9b1cbeb

History | View | Annotate | Download (57.8 kB)

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 used to have to cleanup the content before returning it because it contains
1046
                            //error content after the list of hashes
1047
                            //
1048
                            //As of 30/1/2012, the result is a proper Json array so we don't need to read the content
1049
                            //line by line
1050
                            
1051
                            var serializer = new JsonSerializer();                            
1052
                            var hashes=(List<string>)serializer.Deserialize(reader, typeof (List<string>));
1053

    
1054
                            return hashes;
1055
                        }                        
1056
                    }                    
1057
                    //Any other status code is unexpected and the exception should be rethrown
1058
                    throw ex;
1059
                    
1060
                }
1061
                //Any other status code is unexpected but there was no exception. We can probably continue processing
1062
                Log.WarnFormat("Unexcpected status code when putting map: {0} - {1}",client.StatusCode,client.StatusDescription);                    
1063
                
1064
                return empty;
1065
            });
1066

    
1067
        }
1068

    
1069
        public Task<byte[]> GetBlock(string account, string container, Uri relativeUrl, long start, long? end)
1070
        {
1071
            if (String.IsNullOrWhiteSpace(Token))
1072
                throw new InvalidOperationException("Invalid Token");
1073
            if (StorageUrl == null)
1074
                throw new InvalidOperationException("Invalid Storage Url");
1075
            if (String.IsNullOrWhiteSpace(container))
1076
                throw new ArgumentNullException("container");
1077
            if (relativeUrl== null)
1078
                throw new ArgumentNullException("relativeUrl");
1079
            if (end.HasValue && end<0)
1080
                throw new ArgumentOutOfRangeException("end");
1081
            if (start<0)
1082
                throw new ArgumentOutOfRangeException("start");
1083
            Contract.EndContractBlock();
1084

    
1085

    
1086
            //Don't use a timeout because putting the hashmap may be a long process
1087
            var client = new RestClient(_baseClient) {Timeout = 0, RangeFrom = start, RangeTo = end};
1088
            if (!String.IsNullOrWhiteSpace(account))
1089
                client.BaseAddress = GetAccountUrl(account);
1090

    
1091
            var builder = client.GetAddressBuilder(container, relativeUrl.ToString());
1092
            var uri = builder.Uri;
1093

    
1094
            return client.DownloadDataTask(uri)
1095
                .ContinueWith(t=>
1096
                                  {
1097
                                      client.Dispose();
1098
                                      return t.Result;
1099
                                  });
1100
        }
1101

    
1102

    
1103
        public async Task PostBlock(string account, string container, byte[] block, int offset, int count)
1104
        {
1105
            if (String.IsNullOrWhiteSpace(container))
1106
                throw new ArgumentNullException("container");
1107
            if (block == null)
1108
                throw new ArgumentNullException("block");
1109
            if (offset < 0 || offset >= block.Length)
1110
                throw new ArgumentOutOfRangeException("offset");
1111
            if (count < 0 || count > block.Length)
1112
                throw new ArgumentOutOfRangeException("count");
1113
            if (String.IsNullOrWhiteSpace(Token))
1114
                throw new InvalidOperationException("Invalid Token");
1115
            if (StorageUrl == null)
1116
                throw new InvalidOperationException("Invalid Storage Url");                        
1117
            Contract.EndContractBlock();
1118

    
1119

    
1120
            try
1121
            {
1122

    
1123
            //Don't use a timeout because putting the hashmap may be a long process
1124
                using (var client = new RestClient(_baseClient) { Timeout = 0 })
1125
                {
1126
                    if (!String.IsNullOrWhiteSpace(account))
1127
                        client.BaseAddress = GetAccountUrl(account);
1128

    
1129
                    var builder = client.GetAddressBuilder(container, "");
1130
                    //We are doing an update
1131
                    builder.Query = "update";
1132
                    var uri = builder.Uri;
1133

    
1134
                    client.Headers[HttpRequestHeader.ContentType] = "application/octet-stream";
1135

    
1136
                    Log.InfoFormat("[BLOCK POST] START");
1137

    
1138
                    client.UploadProgressChanged += (sender, args) =>
1139
                                                    Log.InfoFormat("[BLOCK POST PROGRESS] {0}% {1} of {2}",
1140
                                                                   args.ProgressPercentage, args.BytesSent,
1141
                                                                   args.TotalBytesToSend);
1142
                    client.UploadFileCompleted += (sender, args) =>
1143
                                                  Log.InfoFormat("[BLOCK POST PROGRESS] Completed ");
1144

    
1145
                    var buffer = new byte[count];
1146
                    Buffer.BlockCopy(block, offset, buffer, 0, count);
1147
                    //Send the block
1148
                    await client.UploadDataTask(uri, "POST", buffer);
1149
                    Log.InfoFormat("[BLOCK POST] END");
1150
                }
1151
            }
1152
            catch (Exception exc)
1153
            {
1154
                Log.ErrorFormat("[BLOCK POST] FAIL with \r{0}", exc);                                        
1155
                throw;
1156
            }
1157
        }
1158

    
1159

    
1160
        public async Task<TreeHash> GetHashMap(string account, string container, string objectName)
1161
        {
1162
            if (String.IsNullOrWhiteSpace(container))
1163
                throw new ArgumentNullException("container");
1164
            if (String.IsNullOrWhiteSpace(objectName))
1165
                throw new ArgumentNullException("objectName");
1166
            if (String.IsNullOrWhiteSpace(Token))
1167
                throw new InvalidOperationException("Invalid Token");
1168
            if (StorageUrl == null)
1169
                throw new InvalidOperationException("Invalid Storage Url");
1170
            Contract.EndContractBlock();
1171

    
1172
            try
1173
            {
1174
                //WebClient, and by extension RestClient, are not thread-safe. Create a new RestClient
1175
                //object to avoid concurrency errors.
1176
                //
1177
                //Download operations take a long time therefore they have no timeout.
1178
                //TODO: Do they really? this is a hashmap operation, not a download
1179
                
1180
                //Start downloading the object asynchronously
1181
                using (var client = new RestClient(_baseClient) { Timeout = 0 })
1182
                {
1183
                    if (!String.IsNullOrWhiteSpace(account))
1184
                        client.BaseAddress = GetAccountUrl(account);
1185

    
1186
                    //The container and objectName are relative names. They are joined with the client's
1187
                    //BaseAddress to create the object's absolute address
1188
                    var builder = client.GetAddressBuilder(container, objectName);
1189
                    builder.Query = "format=json&hashmap";
1190
                    var uri = builder.Uri;
1191

    
1192

    
1193
                    var json = await client.DownloadStringTaskAsync(uri);
1194
                    var treeHash = TreeHash.Parse(json);
1195
                    Log.InfoFormat("[GET HASH] END {0}", objectName);
1196
                    return treeHash;
1197
                }
1198
            }
1199
            catch (Exception exc)
1200
            {
1201
                Log.ErrorFormat("[GET HASH] END {0} with {1}", objectName, exc);
1202
                throw;
1203
            }
1204

    
1205
        }
1206

    
1207

    
1208
        /// <summary>
1209
        /// 
1210
        /// </summary>
1211
        /// <param name="account"></param>
1212
        /// <param name="container"></param>
1213
        /// <param name="objectName"></param>
1214
        /// <param name="fileName"></param>
1215
        /// <param name="hash">Optional hash value for the file. If no hash is provided, the method calculates a new hash</param>
1216
        /// <remarks>>This method should have no timeout or a very long one</remarks>
1217
        public async Task PutObject(string account, string container, string objectName, string fileName, string hash = null, string contentType = "application/octet-stream")
1218
        {
1219
            if (String.IsNullOrWhiteSpace(container))
1220
                throw new ArgumentNullException("container", "The container property can't be empty");
1221
            if (String.IsNullOrWhiteSpace(objectName))
1222
                throw new ArgumentNullException("objectName", "The objectName property can't be empty");
1223
            if (String.IsNullOrWhiteSpace(fileName))
1224
                throw new ArgumentNullException("fileName", "The fileName property can't be empty");
1225
/*
1226
            if (!File.Exists(fileName) && !Directory.Exists(fileName))
1227
                throw new FileNotFoundException("The file or directory does not exist",fileName);
1228
*/
1229
            Contract.EndContractBlock();
1230
            
1231
            try
1232
            {
1233

    
1234
                using (var client = new RestClient(_baseClient) { Timeout = 0 })
1235
                {
1236
                    if (!String.IsNullOrWhiteSpace(account))
1237
                        client.BaseAddress = GetAccountUrl(account);
1238

    
1239
                    var builder = client.GetAddressBuilder(container, objectName);
1240
                    var uri = builder.Uri;
1241

    
1242
                    string etag = hash ?? CalculateHash(fileName);
1243

    
1244
                    client.Headers.Add("Content-Type", contentType);
1245
                    client.Headers.Add("ETag", etag);
1246

    
1247

    
1248
                    Log.InfoFormat("[PUT] START {0}", objectName);
1249
                    client.UploadProgressChanged += (sender, args) =>
1250
                                                        {
1251
                                                            using (ThreadContext.Stacks["PUT"].Push("Progress"))
1252
                                                            {
1253
                                                                Log.InfoFormat("{0} {1}% {2} of {3}", fileName,
1254
                                                                               args.ProgressPercentage,
1255
                                                                               args.BytesSent, args.TotalBytesToSend);
1256
                                                            }
1257
                                                        };
1258

    
1259
                    client.UploadFileCompleted += (sender, args) =>
1260
                                                      {
1261
                                                          using (ThreadContext.Stacks["PUT"].Push("Progress"))
1262
                                                          {
1263
                                                              Log.InfoFormat("Completed {0}", fileName);
1264
                                                          }
1265
                                                      };
1266
                    if (contentType=="application/directory")
1267
                        await client.UploadDataTaskAsync(uri, "PUT", new byte[0]);
1268
                    else
1269
                        await client.UploadFileTaskAsync(uri, "PUT", fileName);
1270
                }
1271

    
1272
                Log.InfoFormat("[PUT] END {0}", objectName);
1273
            }
1274
            catch (Exception exc)
1275
            {
1276
                Log.ErrorFormat("[PUT] END {0} with {1}", objectName, exc);
1277
                throw;
1278
            }                
1279

    
1280
        }
1281
       
1282
        
1283
        private static string CalculateHash(string fileName)
1284
        {
1285
            Contract.Requires(!String.IsNullOrWhiteSpace(fileName));
1286
            Contract.EndContractBlock();
1287

    
1288
            string hash;
1289
            using (var hasher = MD5.Create())
1290
            using(var stream=File.OpenRead(fileName))
1291
            {
1292
                var hashBuilder=new StringBuilder();
1293
                foreach (byte b in hasher.ComputeHash(stream))
1294
                    hashBuilder.Append(b.ToString("x2").ToLower());
1295
                hash = hashBuilder.ToString();                
1296
            }
1297
            return hash;
1298
        }
1299
        
1300
        public void MoveObject(string account, string sourceContainer, string oldObjectName, string targetContainer, string newObjectName)
1301
        {
1302
            if (String.IsNullOrWhiteSpace(sourceContainer))
1303
                throw new ArgumentNullException("sourceContainer", "The container property can't be empty");
1304
            if (String.IsNullOrWhiteSpace(oldObjectName))
1305
                throw new ArgumentNullException("oldObjectName", "The oldObjectName property can't be empty");
1306
            if (String.IsNullOrWhiteSpace(targetContainer))
1307
                throw new ArgumentNullException("targetContainer", "The container property can't be empty");
1308
            if (String.IsNullOrWhiteSpace(newObjectName))
1309
                throw new ArgumentNullException("newObjectName", "The newObjectName property can't be empty");
1310
            Contract.EndContractBlock();
1311

    
1312
            var targetUrl = targetContainer + "/" + newObjectName;
1313
            var sourceUrl = String.Format("/{0}/{1}", sourceContainer, oldObjectName);
1314

    
1315
            using (var client = new RestClient(_baseClient))
1316
            {
1317
                if (!String.IsNullOrWhiteSpace(account))
1318
                    client.BaseAddress = GetAccountUrl(account);
1319

    
1320
                client.Headers.Add("X-Move-From", sourceUrl);
1321
                client.PutWithRetry(targetUrl, 3);
1322

    
1323
                var expectedCodes = new[] {HttpStatusCode.OK, HttpStatusCode.NoContent, HttpStatusCode.Created};
1324
                if (!expectedCodes.Contains(client.StatusCode))
1325
                    throw CreateWebException("MoveObject", client.StatusCode);
1326
            }
1327
        }
1328

    
1329
        public void DeleteObject(string account, string sourceContainer, string objectName)
1330
        {            
1331
            if (String.IsNullOrWhiteSpace(sourceContainer))
1332
                throw new ArgumentNullException("sourceContainer", "The container property can't be empty");
1333
            if (String.IsNullOrWhiteSpace(objectName))
1334
                throw new ArgumentNullException("objectName", "The oldObjectName property can't be empty");
1335
            Contract.EndContractBlock();
1336

    
1337
            var targetUrl = FolderConstants.TrashContainer + "/" + objectName;
1338
            var sourceUrl = String.Format("/{0}/{1}", sourceContainer, objectName);
1339

    
1340
            using (var client = new RestClient(_baseClient))
1341
            {
1342
                if (!String.IsNullOrWhiteSpace(account))
1343
                    client.BaseAddress = GetAccountUrl(account);
1344

    
1345
                client.Headers.Add("X-Move-From", sourceUrl);
1346
                client.AllowedStatusCodes.Add(HttpStatusCode.NotFound);
1347
                client.PutWithRetry(targetUrl, 3);
1348

    
1349
                var expectedCodes = new[] {HttpStatusCode.OK, HttpStatusCode.NoContent, HttpStatusCode.Created,HttpStatusCode.NotFound};
1350
                if (!expectedCodes.Contains(client.StatusCode))
1351
                    throw CreateWebException("DeleteObject", client.StatusCode);
1352
            }
1353
        }
1354

    
1355
      
1356
        private static WebException CreateWebException(string operation, HttpStatusCode statusCode)
1357
        {
1358
            return new WebException(String.Format("{0} failed with unexpected status code {1}", operation, statusCode));
1359
        }
1360

    
1361
        
1362
    }
1363

    
1364
    public class ShareAccountInfo
1365
    {
1366
        public DateTime? last_modified { get; set; }
1367
        public string name { get; set; }
1368
    }
1369
}