Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (61.2 kB)

1
#region
2
/* -----------------------------------------------------------------------
3
 * <copyright file="CloudFilesClient.cs" company="GRNet">
4
 * 
5
 * Copyright 2011-2012 GRNET S.A. All rights reserved.
6
 *
7
 * Redistribution and use in source and binary forms, with or
8
 * without modification, are permitted provided that the following
9
 * conditions are met:
10
 *
11
 *   1. Redistributions of source code must retain the above
12
 *      copyright notice, this list of conditions and the following
13
 *      disclaimer.
14
 *
15
 *   2. Redistributions in binary form must reproduce the above
16
 *      copyright notice, this list of conditions and the following
17
 *      disclaimer in the documentation and/or other materials
18
 *      provided with the distribution.
19
 *
20
 *
21
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
22
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
25
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
 * POSSIBILITY OF SUCH DAMAGE.
33
 *
34
 * The views and conclusions contained in the software and
35
 * documentation are those of the authors and should not be
36
 * interpreted as representing official policies, either expressed
37
 * or implied, of GRNET S.A.
38
 * </copyright>
39
 * -----------------------------------------------------------------------
40
 */
41
#endregion
42

    
43
// **CloudFilesClient** provides a simple client interface to CloudFiles and Pithos
44
//
45
// The class provides methods to upload/download files, delete files, manage containers
46

    
47

    
48
using System;
49
using System.Collections.Generic;
50
using System.Collections.Specialized;
51
using System.ComponentModel.Composition;
52
using System.Diagnostics;
53
using System.Diagnostics.Contracts;
54
using System.IO;
55
using System.Linq;
56
using System.Net;
57
using System.Reflection;
58
using System.Security.Cryptography;
59
using System.Text;
60
using System.Threading;
61
using System.Threading.Tasks;
62
using Newtonsoft.Json;
63
using Pithos.Interfaces;
64
using log4net;
65

    
66
namespace Pithos.Network
67
{
68
    [Export(typeof(ICloudClient))]
69
    public class CloudFilesClient:ICloudClient
70
    {
71
        private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
72

    
73
        //CloudFilesClient uses *_baseClient* internally to communicate with the server
74
        //RestClient provides a REST-friendly interface over the standard WebClient.
75
        private RestClient _baseClient;
76
        
77

    
78
        //During authentication the client provides a UserName 
79
        public string UserName { get; set; }
80
        
81
        //and and ApiKey to the server
82
        public string ApiKey { get; set; }
83
        
84
        //And receives an authentication Token. This token must be provided in ALL other operations,
85
        //in the X-Auth-Token header
86
        private string _token;
87
        public string Token
88
        {
89
            get { return _token; }
90
            set
91
            {
92
                _token = value;
93
                _baseClient.Headers["X-Auth-Token"] = value;
94
            }
95
        }
96

    
97
        //The client also receives a StorageUrl after authentication. All subsequent operations must
98
        //use this url
99
        public Uri StorageUrl { get; set; }
100

    
101

    
102
        public Uri RootAddressUri { get; set; }
103

    
104
       /* private WebProxy _proxy;
105
        public WebProxy Proxy
106
        {
107
            get { return _proxy; }
108
            set
109
            {
110
                _proxy = value;
111
                if (_baseClient != null)
112
                    _baseClient.Proxy = value;                
113
            }
114
        }
115
*/
116

    
117
        /* private Uri _proxy;
118
        public Uri Proxy
119
        {
120
            get { return _proxy; }
121
            set
122
            {
123
                _proxy = value;
124
                if (_baseClient != null)
125
                    _baseClient.Proxy = new WebProxy(value);                
126
            }
127
        }*/
128

    
129
        public double DownloadPercentLimit { get; set; }
130
        public double UploadPercentLimit { get; set; }
131

    
132
        public string AuthenticationUrl { get; set; }
133

    
134
 
135
        public string VersionPath
136
        {
137
            get { return UsePithos ? "v1" : "v1.0"; }
138
        }
139

    
140
        public bool UsePithos { get; set; }
141

    
142

    
143

    
144
        public CloudFilesClient(string userName, string apiKey)
145
        {
146
            UserName = userName;
147
            ApiKey = apiKey;
148
        }
149

    
150
        public CloudFilesClient(AccountInfo accountInfo)
151
        {
152
            if (accountInfo==null)
153
                throw new ArgumentNullException("accountInfo");
154
            Contract.Ensures(!String.IsNullOrWhiteSpace(Token));
155
            Contract.Ensures(StorageUrl != null);
156
            Contract.Ensures(_baseClient != null);
157
            Contract.Ensures(RootAddressUri != null);
158
            Contract.EndContractBlock();          
159

    
160
            _baseClient = new RestClient
161
            {
162
                BaseAddress = accountInfo.StorageUri.ToString(),
163
                Timeout = 10000,
164
                Retries = 3,
165
            };
166
            StorageUrl = accountInfo.StorageUri;
167
            Token = accountInfo.Token;
168
            UserName = accountInfo.UserName;
169

    
170
            //Get the root address (StorageUrl without the account)
171
            var storageUrl = StorageUrl.AbsoluteUri;
172
            var usernameIndex = storageUrl.LastIndexOf(UserName);
173
            var rootUrl = storageUrl.Substring(0, usernameIndex);
174
            RootAddressUri = new Uri(rootUrl);
175
        }
176

    
177

    
178
        public AccountInfo Authenticate()
179
        {
180
            if (String.IsNullOrWhiteSpace(UserName))
181
                throw new InvalidOperationException("UserName is empty");
182
            if (String.IsNullOrWhiteSpace(ApiKey))
183
                throw new InvalidOperationException("ApiKey is empty");
184
            if (String.IsNullOrWhiteSpace(AuthenticationUrl))
185
                throw new InvalidOperationException("AuthenticationUrl is empty");
186
            Contract.Ensures(!String.IsNullOrWhiteSpace(Token));
187
            Contract.Ensures(StorageUrl != null);
188
            Contract.Ensures(_baseClient != null);
189
            Contract.Ensures(RootAddressUri != null);
190
            Contract.EndContractBlock();
191

    
192

    
193
            Log.InfoFormat("[AUTHENTICATE] Start for {0}", UserName);
194

    
195
            var groups = new List<Group>();
196

    
197
            using (var authClient = new RestClient{BaseAddress=AuthenticationUrl})
198
            {                
199
               /* if (Proxy != null)
200
                    authClient.Proxy = Proxy;*/
201

    
202
                Contract.Assume(authClient.Headers!=null);
203

    
204
                authClient.Headers.Add("X-Auth-User", UserName);
205
                authClient.Headers.Add("X-Auth-Key", ApiKey);
206

    
207
                authClient.DownloadStringWithRetry(VersionPath, 3);
208

    
209
                authClient.AssertStatusOK("Authentication failed");
210

    
211
                var storageUrl = authClient.GetHeaderValue("X-Storage-Url");
212
                if (String.IsNullOrWhiteSpace(storageUrl))
213
                    throw new InvalidOperationException("Failed to obtain storage url");
214
                
215
                _baseClient = new RestClient
216
                {
217
                    BaseAddress = storageUrl,
218
                    Timeout = 10000,
219
                    Retries = 3,
220
                    //Proxy=Proxy
221
                };
222

    
223
                StorageUrl = new Uri(storageUrl);
224
                
225
                //Get the root address (StorageUrl without the account)
226
                var usernameIndex=storageUrl.LastIndexOf(UserName);
227
                var rootUrl = storageUrl.Substring(0, usernameIndex);
228
                RootAddressUri = new Uri(rootUrl);
229
                
230
                var token = authClient.GetHeaderValue("X-Auth-Token");
231
                if (String.IsNullOrWhiteSpace(token))
232
                    throw new InvalidOperationException("Failed to obtain token url");
233
                Token = token;
234

    
235
               /* var keys = authClient.ResponseHeaders.AllKeys.AsQueryable();
236
                groups = (from key in keys
237
                            where key.StartsWith("X-Account-Group-")
238
                            let name = key.Substring(16)
239
                            select new Group(name, authClient.ResponseHeaders[key]))
240
                        .ToList();
241
                    
242
*/
243
            }
244

    
245
            Log.InfoFormat("[AUTHENTICATE] End for {0}", UserName);
246
            Debug.Assert(_baseClient!=null);
247

    
248
            return new AccountInfo {StorageUri = StorageUrl, Token = Token, UserName = UserName,Groups=groups};            
249

    
250
        }
251

    
252

    
253

    
254
        public IList<ContainerInfo> ListContainers(string account)
255
        {
256
            using (var client = new RestClient(_baseClient))
257
            {
258
                if (!String.IsNullOrWhiteSpace(account))
259
                    client.BaseAddress = GetAccountUrl(account);
260
                
261
                client.Parameters.Clear();
262
                client.Parameters.Add("format", "json");
263
                var content = client.DownloadStringWithRetry("", 3);
264
                client.AssertStatusOK("List Containers failed");
265

    
266
                if (client.StatusCode == HttpStatusCode.NoContent)
267
                    return new List<ContainerInfo>();
268
                var infos = JsonConvert.DeserializeObject<IList<ContainerInfo>>(content);
269
                
270
                foreach (var info in infos)
271
                {
272
                    info.Account = account;
273
                }
274
                return infos;
275
            }
276

    
277
        }
278

    
279
        private string GetAccountUrl(string account)
280
        {
281
            return new Uri(RootAddressUri, new Uri(account,UriKind.Relative)).AbsoluteUri;
282
        }
283

    
284
        public IList<ShareAccountInfo> ListSharingAccounts(DateTime? since=null)
285
        {
286
            using (ThreadContext.Stacks["Share"].Push("List Accounts"))
287
            {
288
                if (Log.IsDebugEnabled) Log.DebugFormat("START");
289

    
290
                using (var client = new RestClient(_baseClient))
291
                {
292
                    client.Parameters.Clear();
293
                    client.Parameters.Add("format", "json");
294
                    client.IfModifiedSince = since;
295

    
296
                    //Extract the username from the base address
297
                    client.BaseAddress = RootAddressUri.AbsoluteUri;
298

    
299
                    var content = client.DownloadStringWithRetry(@"", 3);
300

    
301
                    client.AssertStatusOK("ListSharingAccounts failed");
302

    
303
                    //If the result is empty, return an empty list,
304
                    var infos = String.IsNullOrWhiteSpace(content)
305
                                    ? new List<ShareAccountInfo>()
306
                                //Otherwise deserialize the account list into a list of ShareAccountInfos
307
                                    : JsonConvert.DeserializeObject<IList<ShareAccountInfo>>(content);
308

    
309
                    Log.DebugFormat("END");
310
                    return infos;
311
                }
312
            }
313
        }
314

    
315
        //Request listing of all objects in a container modified since a specific time.
316
        //If the *since* value is missing, return all objects
317
        public IList<ObjectInfo> ListSharedObjects(DateTime? since = null)
318
        {
319

    
320
            using (ThreadContext.Stacks["Share"].Push("List Objects"))
321
            {
322
                if (Log.IsDebugEnabled) Log.DebugFormat("START");
323
                //'since' is not used here because we need to have ListObjects return a NoChange result
324
                //for all shared accounts,containers
325
                var accounts = ListSharingAccounts();
326
                var items = from account in accounts 
327
                            let containers = ListContainers(account.name) 
328
                            from container in containers 
329
                            select ListObjects(account.name, container.Name,since);
330
                var objects=items.SelectMany(r=> r).ToList();
331
/*
332
                var objects = new List<ObjectInfo>();
333
                foreach (var containerObjects in items)
334
                {
335
                    objects.AddRange(containerObjects);
336
                }
337
*/
338
                if (Log.IsDebugEnabled) Log.DebugFormat("END");
339
                return objects;
340
            }
341
        }
342

    
343
        public void SetTags(ObjectInfo target,IDictionary<string,string> tags)
344
        {
345
            if (String.IsNullOrWhiteSpace(Token))
346
                throw new InvalidOperationException("The Token is not set");
347
            if (StorageUrl == null)
348
                throw new InvalidOperationException("The StorageUrl is not set");
349
            if (target == null)
350
                throw new ArgumentNullException("target");
351
            Contract.EndContractBlock();
352

    
353
            using (ThreadContext.Stacks["Share"].Push("Share Object"))
354
            {
355
                if (Log.IsDebugEnabled) Log.DebugFormat("START");
356

    
357
                using (var client = new RestClient(_baseClient))
358
                {
359

    
360
                    client.BaseAddress = GetAccountUrl(target.Account);
361

    
362
                    client.Parameters.Clear();
363
                    client.Parameters.Add("update", "");
364

    
365
                    foreach (var tag in tags)
366
                    {
367
                        var headerTag = String.Format("X-Object-Meta-{0}", tag.Key);
368
                        client.Headers.Add(headerTag, tag.Value);
369
                    }
370
                    
371
                    client.DownloadStringWithRetry(target.Container, 3);
372

    
373
                    
374
                    client.AssertStatusOK("SetTags failed");
375
                    //If the status is NOT ACCEPTED we have a problem
376
                    if (client.StatusCode != HttpStatusCode.Accepted)
377
                    {
378
                        Log.Error("Failed to set tags");
379
                        throw new Exception("Failed to set tags");
380
                    }
381

    
382
                    if (Log.IsDebugEnabled) Log.DebugFormat("END");
383
                }
384
            }
385

    
386

    
387
        }
388

    
389
        public void ShareObject(string account, string container, string objectName, string shareTo, bool read, bool write)
390
        {
391
            if (String.IsNullOrWhiteSpace(Token))
392
                throw new InvalidOperationException("The Token is not set");
393
            if (StorageUrl==null)
394
                throw new InvalidOperationException("The StorageUrl is not set");
395
            if (String.IsNullOrWhiteSpace(container))
396
                throw new ArgumentNullException("container");
397
            if (String.IsNullOrWhiteSpace(objectName))
398
                throw new ArgumentNullException("objectName");
399
            if (String.IsNullOrWhiteSpace(account))
400
                throw new ArgumentNullException("account");
401
            if (String.IsNullOrWhiteSpace(shareTo))
402
                throw new ArgumentNullException("shareTo");
403
            Contract.EndContractBlock();
404

    
405
            using (ThreadContext.Stacks["Share"].Push("Share Object"))
406
            {
407
                if (Log.IsDebugEnabled) Log.DebugFormat("START");
408
                
409
                using (var client = new RestClient(_baseClient))
410
                {
411

    
412
                    client.BaseAddress = GetAccountUrl(account);
413

    
414
                    client.Parameters.Clear();
415
                    client.Parameters.Add("format", "json");
416

    
417
                    string permission = "";
418
                    if (write)
419
                        permission = String.Format("write={0}", shareTo);
420
                    else if (read)
421
                        permission = String.Format("read={0}", shareTo);
422
                    client.Headers.Add("X-Object-Sharing", permission);
423

    
424
                    var content = client.DownloadStringWithRetry(container, 3);
425

    
426
                    client.AssertStatusOK("ShareObject failed");
427

    
428
                    //If the result is empty, return an empty list,
429
                    var infos = String.IsNullOrWhiteSpace(content)
430
                                    ? new List<ObjectInfo>()
431
                                //Otherwise deserialize the object list into a list of ObjectInfos
432
                                    : JsonConvert.DeserializeObject<IList<ObjectInfo>>(content);
433

    
434
                    if (Log.IsDebugEnabled) Log.DebugFormat("END");
435
                }
436
            }
437

    
438

    
439
        }
440

    
441
        public AccountInfo GetAccountPolicies(AccountInfo accountInfo)
442
        {
443
            if (accountInfo==null)
444
                throw new ArgumentNullException("accountInfo");
445
            Contract.EndContractBlock();
446

    
447
            using (ThreadContext.Stacks["Account"].Push("GetPolicies"))
448
            {
449
                if (Log.IsDebugEnabled) Log.DebugFormat("START");
450

    
451
                using (var client = new RestClient(_baseClient))
452
                {
453
                    if (!String.IsNullOrWhiteSpace(accountInfo.UserName))
454
                        client.BaseAddress = GetAccountUrl(accountInfo.UserName);
455

    
456
                    client.Parameters.Clear();
457
                    client.Parameters.Add("format", "json");                    
458
                    client.Head(String.Empty, 3);
459

    
460
                    var quotaValue=client.ResponseHeaders["X-Account-Policy-Quota"];
461
                    var bytesValue= client.ResponseHeaders["X-Account-Bytes-Used"];
462

    
463
                    long quota, bytes;
464
                    if (long.TryParse(quotaValue, out quota))
465
                        accountInfo.Quota = quota;
466
                    if (long.TryParse(bytesValue, out bytes))
467
                        accountInfo.BytesUsed = bytes;
468
                    
469
                    return accountInfo;
470

    
471
                }
472

    
473
            }
474
        }
475

    
476
        public void UpdateMetadata(ObjectInfo objectInfo)
477
        {
478
            if (objectInfo == null)
479
                throw new ArgumentNullException("objectInfo");
480
            Contract.EndContractBlock();
481

    
482
            using (ThreadContext.Stacks["Objects"].Push("UpdateMetadata"))
483
            {
484
                if (Log.IsDebugEnabled) Log.DebugFormat("START");
485

    
486

    
487
                using(var client=new RestClient(_baseClient))
488
                {
489

    
490
                    client.BaseAddress = GetAccountUrl(objectInfo.Account);
491
                    
492
                    client.Parameters.Clear();
493
                    
494

    
495
                    //Set Tags
496
                    foreach (var tag in objectInfo.Tags)
497
                    {
498
                        var headerTag = String.Format("X-Object-Meta-{0}", tag.Key);
499
                        client.Headers.Add(headerTag, tag.Value);
500
                    }
501

    
502
                    //Set Permissions
503

    
504
                    var permissions=objectInfo.GetPermissionString();
505
                    client.SetNonEmptyHeaderValue("X-Object-Sharing",permissions);
506

    
507
                    client.SetNonEmptyHeaderValue("Content-Disposition",objectInfo.ContendDisposition);
508
                    client.SetNonEmptyHeaderValue("Content-Encoding",objectInfo.ContentEncoding);
509
                    client.SetNonEmptyHeaderValue("X-Object-Manifest",objectInfo.Manifest);
510
                    var isPublic = objectInfo.IsPublic.ToString().ToLower();
511
                    client.Headers.Add("X-Object-Public", isPublic);
512

    
513

    
514
                    /*var uriBuilder = client.GetAddressBuilder(objectInfo.Container, objectInfo.Name);
515
                    uriBuilder.Query = "update=";
516
                    var uri = uriBuilder.Uri.MakeRelativeUri(this.RootAddressUri);*/
517
                    var address = String.Format("{0}/{1}?update=",objectInfo.Container, objectInfo.Name);
518
                    client.PostWithRetry(address,"application/xml");
519
                    
520
                    //client.UploadValues(uri,new NameValueCollection());
521

    
522

    
523
                    client.AssertStatusOK("UpdateMetadata failed");
524
                    //If the status is NOT ACCEPTED or OK we have a problem
525
                    if (!(client.StatusCode == HttpStatusCode.Accepted || client.StatusCode == HttpStatusCode.OK))
526
                    {
527
                        Log.Error("Failed to update metadata");
528
                        throw new Exception("Failed to update metadata");
529
                    }
530

    
531
                    if (Log.IsDebugEnabled) Log.DebugFormat("END");
532
                }
533
            }
534

    
535
        }
536

    
537
        public void UpdateMetadata(ContainerInfo containerInfo)
538
        {
539
            if (containerInfo == null)
540
                throw new ArgumentNullException("containerInfo");
541
            Contract.EndContractBlock();
542

    
543
            using (ThreadContext.Stacks["Containers"].Push("UpdateMetadata"))
544
            {
545
                if (Log.IsDebugEnabled) Log.DebugFormat("START");
546

    
547

    
548
                using(var client=new RestClient(_baseClient))
549
                {
550

    
551
                    client.BaseAddress = GetAccountUrl(containerInfo.Account);
552
                    
553
                    client.Parameters.Clear();
554
                    
555

    
556
                    //Set Tags
557
                    foreach (var tag in containerInfo.Tags)
558
                    {
559
                        var headerTag = String.Format("X-Container-Meta-{0}", tag.Key);
560
                        client.Headers.Add(headerTag, tag.Value);
561
                    }
562

    
563
                    
564
                    //Set Policies
565
                    foreach (var policy in containerInfo.Policies)
566
                    {
567
                        var headerPolicy = String.Format("X-Container-Policy-{0}", policy.Key);
568
                        client.Headers.Add(headerPolicy, policy.Value);
569
                    }
570

    
571

    
572
                    var uriBuilder = client.GetAddressBuilder(containerInfo.Name,"");
573
                    var uri = uriBuilder.Uri;
574

    
575
                    client.UploadValues(uri,new NameValueCollection());
576

    
577

    
578
                    client.AssertStatusOK("UpdateMetadata failed");
579
                    //If the status is NOT ACCEPTED or OK we have a problem
580
                    if (!(client.StatusCode == HttpStatusCode.Accepted || client.StatusCode == HttpStatusCode.OK))
581
                    {
582
                        Log.Error("Failed to update metadata");
583
                        throw new Exception("Failed to update metadata");
584
                    }
585

    
586
                    if (Log.IsDebugEnabled) Log.DebugFormat("END");
587
                }
588
            }
589

    
590
        }
591

    
592

    
593
        public IList<ObjectInfo> ListObjects(string account, string container, DateTime? since = null)
594
        {
595
            if (String.IsNullOrWhiteSpace(container))
596
                throw new ArgumentNullException("container");
597
            Contract.EndContractBlock();
598

    
599
            using (ThreadContext.Stacks["Objects"].Push("List"))
600
            {
601
                if (Log.IsDebugEnabled) Log.DebugFormat("START");
602

    
603
                using (var client = new RestClient(_baseClient))
604
                {
605
                    if (!String.IsNullOrWhiteSpace(account))
606
                        client.BaseAddress = GetAccountUrl(account);
607

    
608
                    client.Parameters.Clear();
609
                    client.Parameters.Add("format", "json");
610
                    client.IfModifiedSince = since;
611
                    var content = client.DownloadStringWithRetry(container, 3);
612

    
613
                    client.AssertStatusOK("ListObjects failed");
614

    
615
                    if (client.StatusCode==HttpStatusCode.NotModified)
616
                        return new[]{new NoModificationInfo(account,container)};
617
                    //If the result is empty, return an empty list,
618
                    var infos = String.IsNullOrWhiteSpace(content)
619
                                    ? new List<ObjectInfo>()
620
                                //Otherwise deserialize the object list into a list of ObjectInfos
621
                                    : JsonConvert.DeserializeObject<IList<ObjectInfo>>(content);
622

    
623
                    foreach (var info in infos)
624
                    {
625
                        info.Container = container;
626
                        info.Account = account;
627
                        info.StorageUri = this.StorageUrl;
628
                    }
629
                    if (Log.IsDebugEnabled) Log.DebugFormat("END");
630
                    return infos;
631
                }
632
            }
633
        }
634

    
635
        public IList<ObjectInfo> ListObjects(string account, string container, string folder, DateTime? since = null)
636
        {
637
            if (String.IsNullOrWhiteSpace(container))
638
                throw new ArgumentNullException("container");
639
/*
640
            if (String.IsNullOrWhiteSpace(folder))
641
                throw new ArgumentNullException("folder");
642
*/
643
            Contract.EndContractBlock();
644

    
645
            using (ThreadContext.Stacks["Objects"].Push("List"))
646
            {
647
                if (Log.IsDebugEnabled) Log.DebugFormat("START");
648

    
649
                using (var client = new RestClient(_baseClient))
650
                {
651
                    if (!String.IsNullOrWhiteSpace(account))
652
                        client.BaseAddress = GetAccountUrl(account);
653

    
654
                    client.Parameters.Clear();
655
                    client.Parameters.Add("format", "json");
656
                    client.Parameters.Add("path", folder);
657
                    client.IfModifiedSince = since;
658
                    var content = client.DownloadStringWithRetry(container, 3);
659
                    client.AssertStatusOK("ListObjects failed");
660

    
661
                    if (client.StatusCode==HttpStatusCode.NotModified)
662
                        return new[]{new NoModificationInfo(account,container,folder)};
663

    
664
                    var infos = JsonConvert.DeserializeObject<IList<ObjectInfo>>(content);
665
                    foreach (var info in infos)
666
                    {
667
                        info.Account = account;
668
                        if (info.Container == null)
669
                            info.Container = container;
670
                        info.StorageUri = this.StorageUrl;
671
                    }
672
                    if (Log.IsDebugEnabled) Log.DebugFormat("END");
673
                    return infos;
674
                }
675
            }
676
        }
677

    
678
 
679
        public bool ContainerExists(string account, string container)
680
        {
681
            if (String.IsNullOrWhiteSpace(container))
682
                throw new ArgumentNullException("container", "The container property can't be empty");
683
            Contract.EndContractBlock();
684

    
685
            using (ThreadContext.Stacks["Containters"].Push("Exists"))
686
            {
687
                if (Log.IsDebugEnabled) Log.DebugFormat("START");
688

    
689
                using (var client = new RestClient(_baseClient))
690
                {
691
                    if (!String.IsNullOrWhiteSpace(account))
692
                        client.BaseAddress = GetAccountUrl(account);
693

    
694
                    client.Parameters.Clear();
695
                    client.Head(container, 3);
696
                                        
697
                    bool result;
698
                    switch (client.StatusCode)
699
                    {
700
                        case HttpStatusCode.OK:
701
                        case HttpStatusCode.NoContent:
702
                            result=true;
703
                            break;
704
                        case HttpStatusCode.NotFound:
705
                            result=false;
706
                            break;
707
                        default:
708
                            throw CreateWebException("ContainerExists", client.StatusCode);
709
                    }
710
                    if (Log.IsDebugEnabled) Log.DebugFormat("END");
711

    
712
                    return result;
713
                }
714
                
715
            }
716
        }
717

    
718
        public bool ObjectExists(string account, string container, string objectName)
719
        {
720
            if (String.IsNullOrWhiteSpace(container))
721
                throw new ArgumentNullException("container", "The container property can't be empty");
722
            if (String.IsNullOrWhiteSpace(objectName))
723
                throw new ArgumentNullException("objectName", "The objectName property can't be empty");
724
            Contract.EndContractBlock();
725

    
726
            using (var client = new RestClient(_baseClient))
727
            {
728
                if (!String.IsNullOrWhiteSpace(account))
729
                    client.BaseAddress = GetAccountUrl(account);
730

    
731
                client.Parameters.Clear();
732
                client.Head(container + "/" + objectName, 3);
733

    
734
                switch (client.StatusCode)
735
                {
736
                    case HttpStatusCode.OK:
737
                    case HttpStatusCode.NoContent:
738
                        return true;
739
                    case HttpStatusCode.NotFound:
740
                        return false;
741
                    default:
742
                        throw CreateWebException("ObjectExists", client.StatusCode);
743
                }
744
            }
745

    
746
        }
747

    
748
        public ObjectInfo GetObjectInfo(string account, string container, string objectName)
749
        {
750
            if (String.IsNullOrWhiteSpace(container))
751
                throw new ArgumentNullException("container", "The container property can't be empty");
752
            if (String.IsNullOrWhiteSpace(objectName))
753
                throw new ArgumentNullException("objectName", "The objectName property can't be empty");
754
            Contract.EndContractBlock();
755

    
756
            using (ThreadContext.Stacks["Objects"].Push("GetObjectInfo"))
757
            {                
758

    
759
                using (var client = new RestClient(_baseClient))
760
                {
761
                    if (!String.IsNullOrWhiteSpace(account))
762
                        client.BaseAddress = GetAccountUrl(account);
763
                    try
764
                    {
765
                        client.Parameters.Clear();
766

    
767
                        client.Head(container + "/" + objectName, 3);
768

    
769
                        if (client.TimedOut)
770
                            return ObjectInfo.Empty;
771

    
772
                        switch (client.StatusCode)
773
                        {
774
                            case HttpStatusCode.OK:
775
                            case HttpStatusCode.NoContent:
776
                                var keys = client.ResponseHeaders.AllKeys.AsQueryable();
777
                                var tags = client.GetMeta("X-Object-Meta-");
778
                                var extensions = (from key in keys
779
                                                  where key.StartsWith("X-Object-") && !key.StartsWith("X-Object-Meta-")
780
                                                  select new {Name = key, Value = client.ResponseHeaders[key]})
781
                                    .ToDictionary(t => t.Name, t => t.Value);
782

    
783
                                var permissions=client.GetHeaderValue("X-Object-Sharing", true);
784
                                
785
                                
786
                                var info = new ObjectInfo
787
                                               {
788
                                                   Account = account,
789
                                                   Container = container,
790
                                                   Name = objectName,
791
                                                   ETag = client.GetHeaderValue("ETag"),
792
                                                   UUID=client.GetHeaderValue("X-Object-UUID"),
793
                                                   X_Object_Hash = client.GetHeaderValue("X-Object-Hash"),
794
                                                   Content_Type = client.GetHeaderValue("Content-Type"),
795
                                                   Bytes = Convert.ToInt64(client.GetHeaderValue("Content-Length",true)),
796
                                                   Tags = tags,
797
                                                   Last_Modified = client.LastModified,
798
                                                   Extensions = extensions,
799
                                                   ContentEncoding=client.GetHeaderValue("Content-Encoding",true),
800
                                                   ContendDisposition = client.GetHeaderValue("Content-Disposition",true),
801
                                                   Manifest=client.GetHeaderValue("X-Object-Manifest",true),
802
                                                   PublicUrl=client.GetHeaderValue("X-Object-Public",true),  
803
                                                   StorageUri=this.StorageUrl,
804
                                               };
805
                                info.SetPermissions(permissions);
806
                                return info;
807
                            case HttpStatusCode.NotFound:
808
                                return ObjectInfo.Empty;
809
                            default:
810
                                throw new WebException(
811
                                    String.Format("[FAIL] GetObjectInfo for {0} failed with unexpected status code {1}",
812
                                                  objectName, client.StatusCode));
813
                        }
814

    
815
                    }
816
                    catch (RetryException)
817
                    {
818
                        Log.WarnFormat("[RETRY FAIL] GetObjectInfo for {0} failed.",objectName);
819
                        return ObjectInfo.Empty;
820
                    }
821
                    catch (WebException e)
822
                    {
823
                        Log.Error(
824
                            String.Format("[FAIL] GetObjectInfo for {0} failed with unexpected status code {1}",
825
                                          objectName, client.StatusCode), e);
826
                        throw;
827
                    }
828
                }                
829
            }
830

    
831
        }
832

    
833
        public void CreateFolder(string account, string container, string folder)
834
        {
835
            if (String.IsNullOrWhiteSpace(container))
836
                throw new ArgumentNullException("container", "The container property can't be empty");
837
            if (String.IsNullOrWhiteSpace(folder))
838
                throw new ArgumentNullException("folder", "The folder property can't be empty");
839
            Contract.EndContractBlock();
840

    
841
            var folderUrl=String.Format("{0}/{1}",container,folder);
842
            using (var client = new RestClient(_baseClient))
843
            {
844
                if (!String.IsNullOrWhiteSpace(account))
845
                    client.BaseAddress = GetAccountUrl(account);
846

    
847
                client.Parameters.Clear();
848
                client.Headers.Add("Content-Type", @"application/directory");
849
                client.Headers.Add("Content-Length", "0");
850
                client.PutWithRetry(folderUrl, 3);
851

    
852
                if (client.StatusCode != HttpStatusCode.Created && client.StatusCode != HttpStatusCode.Accepted)
853
                    throw CreateWebException("CreateFolder", client.StatusCode);
854
            }
855
        }
856

    
857
     
858

    
859
        public ContainerInfo GetContainerInfo(string account, string container)
860
        {
861
            if (String.IsNullOrWhiteSpace(container))
862
                throw new ArgumentNullException("container", "The container property can't be empty");
863
            Contract.EndContractBlock();
864

    
865
            using (var client = new RestClient(_baseClient))
866
            {
867
                if (!String.IsNullOrWhiteSpace(account))
868
                    client.BaseAddress = GetAccountUrl(account);                
869

    
870
                client.Head(container);
871
                switch (client.StatusCode)
872
                {
873
                    case HttpStatusCode.OK:
874
                    case HttpStatusCode.NoContent:
875
                        var tags = client.GetMeta("X-Container-Meta-");
876
                        var policies = client.GetMeta("X-Container-Policy-");
877

    
878
                        var containerInfo = new ContainerInfo
879
                                                {
880
                                                    Account=account,
881
                                                    Name = container,
882
                                                    StorageUrl=this.StorageUrl.ToString(),
883
                                                    Count =
884
                                                        long.Parse(client.GetHeaderValue("X-Container-Object-Count")),
885
                                                    Bytes = long.Parse(client.GetHeaderValue("X-Container-Bytes-Used")),
886
                                                    BlockHash = client.GetHeaderValue("X-Container-Block-Hash"),
887
                                                    BlockSize=int.Parse(client.GetHeaderValue("X-Container-Block-Size")),
888
                                                    Last_Modified=client.LastModified,
889
                                                    Tags=tags,
890
                                                    Policies=policies
891
                                                };
892
                        
893

    
894
                        return containerInfo;
895
                    case HttpStatusCode.NotFound:
896
                        return ContainerInfo.Empty;
897
                    default:
898
                        throw CreateWebException("GetContainerInfo", client.StatusCode);
899
                }
900
            }
901
        }
902

    
903
        public void CreateContainer(string account, string container)
904
        {
905
            if (String.IsNullOrWhiteSpace(account))
906
                throw new ArgumentNullException("account");
907
            if (String.IsNullOrWhiteSpace(container))
908
                throw new ArgumentNullException("container");
909
            Contract.EndContractBlock();
910

    
911
            using (var client = new RestClient(_baseClient))
912
            {
913
                if (!String.IsNullOrWhiteSpace(account))
914
                    client.BaseAddress = GetAccountUrl(account);
915

    
916
                client.PutWithRetry(container, 3);
917
                var expectedCodes = new[] {HttpStatusCode.Created, HttpStatusCode.Accepted, HttpStatusCode.OK};
918
                if (!expectedCodes.Contains(client.StatusCode))
919
                    throw CreateWebException("CreateContainer", client.StatusCode);
920
            }
921
        }
922

    
923
        public void DeleteContainer(string account, string container)
924
        {
925
            if (String.IsNullOrWhiteSpace(container))
926
                throw new ArgumentNullException("container", "The container property can't be empty");
927
            Contract.EndContractBlock();
928

    
929
            using (var client = new RestClient(_baseClient))
930
            {
931
                if (!String.IsNullOrWhiteSpace(account))
932
                    client.BaseAddress = GetAccountUrl(account);
933

    
934
                client.DeleteWithRetry(container, 3);
935
                var expectedCodes = new[] {HttpStatusCode.NotFound, HttpStatusCode.NoContent};
936
                if (!expectedCodes.Contains(client.StatusCode))
937
                    throw CreateWebException("DeleteContainer", client.StatusCode);
938
            }
939

    
940
        }
941

    
942
        /// <summary>
943
        /// 
944
        /// </summary>
945
        /// <param name="account"></param>
946
        /// <param name="container"></param>
947
        /// <param name="objectName"></param>
948
        /// <param name="fileName"></param>
949
        /// <returns></returns>
950
        /// <remarks>This method should have no timeout or a very long one</remarks>
951
        //Asynchronously download the object specified by *objectName* in a specific *container* to 
952
        // a local file
953
        public async Task GetObject(string account, string container, string objectName, string fileName,CancellationToken cancellationToken)
954
        {
955
            if (String.IsNullOrWhiteSpace(container))
956
                throw new ArgumentNullException("container", "The container property can't be empty");
957
            if (String.IsNullOrWhiteSpace(objectName))
958
                throw new ArgumentNullException("objectName", "The objectName property can't be empty");            
959
            Contract.EndContractBlock();
960

    
961
            try
962
            {
963
                //WebClient, and by extension RestClient, are not thread-safe. Create a new RestClient
964
                //object to avoid concurrency errors.
965
                //
966
                //Download operations take a long time therefore they have no timeout.
967
                using(var client = new RestClient(_baseClient) { Timeout = 0 })
968
                {
969
                    if (!String.IsNullOrWhiteSpace(account))
970
                        client.BaseAddress = GetAccountUrl(account);
971

    
972
                    //The container and objectName are relative names. They are joined with the client's
973
                    //BaseAddress to create the object's absolute address
974
                    var builder = client.GetAddressBuilder(container, objectName);
975
                    var uri = builder.Uri;
976

    
977
                    //Download progress is reported to the Trace log
978
                    Log.InfoFormat("[GET] START {0}", objectName);
979
                    client.DownloadProgressChanged += (sender, args) =>
980
                                                      Log.InfoFormat("[GET PROGRESS] {0} {1}% {2} of {3}",
981
                                                                     fileName, args.ProgressPercentage,
982
                                                                     args.BytesReceived,
983
                                                                     args.TotalBytesToReceive);
984

    
985
                    
986
                    //Start downloading the object asynchronously
987
                    await client.DownloadFileTaskAsync(uri, fileName,cancellationToken);
988

    
989
                    //Once the download completes
990
                    //Delete the local client object
991
                }
992
                //And report failure or completion
993
            }
994
            catch (Exception exc)
995
            {
996
                Log.ErrorFormat("[GET] FAIL {0} with {1}", objectName, exc);
997
                throw;
998
            }
999

    
1000
            Log.InfoFormat("[GET] END {0}", objectName);                                             
1001

    
1002

    
1003
        }
1004

    
1005
        public Task<IList<string>> PutHashMap(string account, string container, string objectName, TreeHash hash)
1006
        {
1007
            if (String.IsNullOrWhiteSpace(container))
1008
                throw new ArgumentNullException("container");
1009
            if (String.IsNullOrWhiteSpace(objectName))
1010
                throw new ArgumentNullException("objectName");
1011
            if (hash==null)
1012
                throw new ArgumentNullException("hash");
1013
            if (String.IsNullOrWhiteSpace(Token))
1014
                throw new InvalidOperationException("Invalid Token");
1015
            if (StorageUrl == null)
1016
                throw new InvalidOperationException("Invalid Storage Url");
1017
            Contract.EndContractBlock();
1018

    
1019

    
1020
            //Don't use a timeout because putting the hashmap may be a long process
1021
            var client = new RestClient(_baseClient) { Timeout = 0 };           
1022
            if (!String.IsNullOrWhiteSpace(account))
1023
                client.BaseAddress = GetAccountUrl(account);
1024

    
1025
            //The container and objectName are relative names. They are joined with the client's
1026
            //BaseAddress to create the object's absolute address
1027
            var builder = client.GetAddressBuilder(container, objectName);
1028
            builder.Query = "format=json&hashmap";
1029
            var uri = builder.Uri;
1030

    
1031

    
1032
            //Send the tree hash as Json to the server            
1033
            client.Headers[HttpRequestHeader.ContentType] = "application/octet-stream";
1034
            var jsonHash = hash.ToJson();
1035
                        
1036
            client.Headers.Add("ETag",hash.MD5);
1037
            var uploadTask=client.UploadStringTask(uri, "PUT", jsonHash);
1038
            if (Log.IsDebugEnabled)
1039
                Log.DebugFormat("Hashes:\r\n{0}", jsonHash);
1040
            return uploadTask.ContinueWith(t =>
1041
            {
1042

    
1043
                var empty = (IList<string>)new List<string>();
1044
                
1045

    
1046
                //The server will respond either with 201-created if all blocks were already on the server
1047
                if (client.StatusCode == HttpStatusCode.Created)                    
1048
                {
1049
                    //in which case we return an empty hash list
1050
                    return empty;
1051
                }
1052
                //or with a 409-conflict and return the list of missing parts
1053
                //A 409 will cause an exception so we need to check t.IsFaulted to avoid propagating the exception                
1054
                if (t.IsFaulted)
1055
                {
1056
                    var ex = t.Exception.InnerException;
1057
                    var we = ex as WebException;
1058
                    var response = we.Response as HttpWebResponse;
1059
                    if (response!=null && response.StatusCode==HttpStatusCode.Conflict)
1060
                    {
1061
                        //In case of 409 the missing parts will be in the response content                        
1062
                        using (var stream = response.GetResponseStream())
1063
                        using(var reader=stream.GetLoggedReader(Log))
1064
                        {
1065
                            //We used to have to cleanup the content before returning it because it contains
1066
                            //error content after the list of hashes
1067
                            //
1068
                            //As of 30/1/2012, the result is a proper Json array so we don't need to read the content
1069
                            //line by line
1070
                            
1071
                            var serializer = new JsonSerializer();                            
1072
                            serializer.Error += (sender, args) => Log.ErrorFormat("Deserialization error at [{0}] [{1}]", args.ErrorContext.Error, args.ErrorContext.Member);
1073
                            var hashes = (List<string>)serializer.Deserialize(reader, typeof(List<string>));
1074
                            return hashes;
1075
                        }                        
1076
                    }                    
1077
                    //Any other status code is unexpected and the exception should be rethrown
1078
                    Log.LogError(response);
1079
                    throw ex;
1080
                    
1081
                }
1082

    
1083
                //Any other status code is unexpected but there was no exception. We can probably continue processing
1084
                Log.WarnFormat("Unexcpected status code when putting map: {0} - {1}",client.StatusCode,client.StatusDescription);                    
1085
                
1086
                return empty;
1087
            });
1088

    
1089
        }
1090

    
1091

    
1092
        public async Task<byte[]> GetBlock(string account, string container, Uri relativeUrl, long start, long? end, CancellationToken cancellationToken)
1093
        {
1094
            if (String.IsNullOrWhiteSpace(Token))
1095
                throw new InvalidOperationException("Invalid Token");
1096
            if (StorageUrl == null)
1097
                throw new InvalidOperationException("Invalid Storage Url");
1098
            if (String.IsNullOrWhiteSpace(container))
1099
                throw new ArgumentNullException("container");
1100
            if (relativeUrl == null)
1101
                throw new ArgumentNullException("relativeUrl");
1102
            if (end.HasValue && end < 0)
1103
                throw new ArgumentOutOfRangeException("end");
1104
            if (start < 0)
1105
                throw new ArgumentOutOfRangeException("start");
1106
            Contract.EndContractBlock();
1107

    
1108
            //Don't use a timeout because putting the hashmap may be a long process
1109
            using (var client = new RestClient(_baseClient) {Timeout = 0, RangeFrom = start, RangeTo = end})
1110
            {
1111
                if (!String.IsNullOrWhiteSpace(account))
1112
                    client.BaseAddress = GetAccountUrl(account);
1113

    
1114
                var builder = client.GetAddressBuilder(container, relativeUrl.ToString());
1115
                var uri = builder.Uri;
1116
                
1117
                client.DownloadProgressChanged += (sender, args) =>
1118
                                  Log.DebugFormat("[GET PROGRESS] {0} {1}% {2} of {3}",
1119
                                                 uri.Segments.Last(), args.ProgressPercentage,
1120
                                                 args.BytesReceived,
1121
                                                 args.TotalBytesToReceive);
1122

    
1123

    
1124
                var result = await client.DownloadDataTaskAsync(uri, cancellationToken);
1125
                return result;
1126
            }
1127
        }
1128

    
1129

    
1130
        public async Task PostBlock(string account, string container, byte[] block, int offset, int count,CancellationToken token)
1131
        {
1132
            if (String.IsNullOrWhiteSpace(container))
1133
                throw new ArgumentNullException("container");
1134
            if (block == null)
1135
                throw new ArgumentNullException("block");
1136
            if (offset < 0 || offset >= block.Length)
1137
                throw new ArgumentOutOfRangeException("offset");
1138
            if (count < 0 || count > block.Length)
1139
                throw new ArgumentOutOfRangeException("count");
1140
            if (String.IsNullOrWhiteSpace(Token))
1141
                throw new InvalidOperationException("Invalid Token");
1142
            if (StorageUrl == null)
1143
                throw new InvalidOperationException("Invalid Storage Url");                        
1144
            Contract.EndContractBlock();
1145

    
1146

    
1147
            try
1148
            {
1149

    
1150
                //Don't use a timeout because putting the hashmap may be a long process
1151
                using (var client = new RestClient(_baseClient) { Timeout = 0 })
1152
                {
1153
                    if (!String.IsNullOrWhiteSpace(account))
1154
                        client.BaseAddress = GetAccountUrl(account);
1155

    
1156
                    token.Register(client.CancelAsync);
1157

    
1158
                    var builder = client.GetAddressBuilder(container, "");
1159
                    //We are doing an update
1160
                    builder.Query = "update";
1161
                    var uri = builder.Uri;
1162

    
1163
                    client.Headers[HttpRequestHeader.ContentType] = "application/octet-stream";
1164

    
1165
                    Log.InfoFormat("[BLOCK POST] START");
1166

    
1167
                    client.UploadProgressChanged += (sender, args) =>
1168
                                                    Log.InfoFormat("[BLOCK POST PROGRESS] {0}% {1} of {2}",
1169
                                                                   args.ProgressPercentage, args.BytesSent,
1170
                                                                   args.TotalBytesToSend);
1171
                    client.UploadFileCompleted += (sender, args) =>
1172
                                                  Log.InfoFormat("[BLOCK POST PROGRESS] Completed ");
1173

    
1174
                    var buffer = new byte[count];
1175
                    Buffer.BlockCopy(block, offset, buffer, 0, count);
1176
                    //Send the block
1177
                    await client.UploadDataTaskAsync(uri, "POST", buffer);
1178
                    Log.InfoFormat("[BLOCK POST] END");
1179
                }
1180
            }
1181
            catch (TaskCanceledException )
1182
            {
1183
                Log.Info("Aborting block");
1184
                throw;
1185
            }
1186
            catch (Exception exc)
1187
            {
1188
                Log.ErrorFormat("[BLOCK POST] FAIL with \r{0}", exc);
1189
                throw;
1190
            }
1191
        }
1192

    
1193

    
1194
        public async Task<TreeHash> GetHashMap(string account, string container, string objectName)
1195
        {
1196
            if (String.IsNullOrWhiteSpace(container))
1197
                throw new ArgumentNullException("container");
1198
            if (String.IsNullOrWhiteSpace(objectName))
1199
                throw new ArgumentNullException("objectName");
1200
            if (String.IsNullOrWhiteSpace(Token))
1201
                throw new InvalidOperationException("Invalid Token");
1202
            if (StorageUrl == null)
1203
                throw new InvalidOperationException("Invalid Storage Url");
1204
            Contract.EndContractBlock();
1205

    
1206
            try
1207
            {
1208
                //WebClient, and by extension RestClient, are not thread-safe. Create a new RestClient
1209
                //object to avoid concurrency errors.
1210
                //
1211
                //Download operations take a long time therefore they have no timeout.
1212
                //TODO: Do they really? this is a hashmap operation, not a download
1213
                
1214
                //Start downloading the object asynchronously
1215
                using (var client = new RestClient(_baseClient) { Timeout = 0 })
1216
                {
1217
                    if (!String.IsNullOrWhiteSpace(account))
1218
                        client.BaseAddress = GetAccountUrl(account);
1219

    
1220
                    //The container and objectName are relative names. They are joined with the client's
1221
                    //BaseAddress to create the object's absolute address
1222
                    var builder = client.GetAddressBuilder(container, objectName);
1223
                    builder.Query = "format=json&hashmap";
1224
                    var uri = builder.Uri;
1225

    
1226

    
1227
                    var json = await client.DownloadStringTaskAsync(uri);
1228
                    var treeHash = TreeHash.Parse(json);
1229
                    Log.InfoFormat("[GET HASH] END {0}", objectName);
1230
                    return treeHash;
1231
                }
1232
            }
1233
            catch (Exception exc)
1234
            {
1235
                Log.ErrorFormat("[GET HASH] END {0} with {1}", objectName, exc);
1236
                throw;
1237
            }
1238

    
1239
        }
1240

    
1241

    
1242
        /// <summary>
1243
        /// 
1244
        /// </summary>
1245
        /// <param name="account"></param>
1246
        /// <param name="container"></param>
1247
        /// <param name="objectName"></param>
1248
        /// <param name="fileName"></param>
1249
        /// <param name="hash">Optional hash value for the file. If no hash is provided, the method calculates a new hash</param>
1250
        /// <remarks>>This method should have no timeout or a very long one</remarks>
1251
        public async Task PutObject(string account, string container, string objectName, string fileName, string hash = null, string contentType = "application/octet-stream")
1252
        {
1253
            if (String.IsNullOrWhiteSpace(container))
1254
                throw new ArgumentNullException("container", "The container property can't be empty");
1255
            if (String.IsNullOrWhiteSpace(objectName))
1256
                throw new ArgumentNullException("objectName", "The objectName property can't be empty");
1257
            if (String.IsNullOrWhiteSpace(fileName))
1258
                throw new ArgumentNullException("fileName", "The fileName property can't be empty");
1259
/*
1260
            if (!File.Exists(fileName) && !Directory.Exists(fileName))
1261
                throw new FileNotFoundException("The file or directory does not exist",fileName);
1262
*/
1263
            
1264
            try
1265
            {
1266

    
1267
                using (var client = new RestClient(_baseClient) { Timeout = 0 })
1268
                {
1269
                    if (!String.IsNullOrWhiteSpace(account))
1270
                        client.BaseAddress = GetAccountUrl(account);
1271

    
1272
                    var builder = client.GetAddressBuilder(container, objectName);
1273
                    var uri = builder.Uri;
1274

    
1275
                    string etag = hash ?? CalculateHash(fileName);
1276

    
1277
                    client.Headers.Add("Content-Type", contentType);
1278
                    client.Headers.Add("ETag", etag);
1279

    
1280

    
1281
                    Log.InfoFormat("[PUT] START {0}", objectName);
1282
                    client.UploadProgressChanged += (sender, args) =>
1283
                                                        {
1284
                                                            using (ThreadContext.Stacks["PUT"].Push("Progress"))
1285
                                                            {
1286
                                                                Log.InfoFormat("{0} {1}% {2} of {3}", fileName,
1287
                                                                               args.ProgressPercentage,
1288
                                                                               args.BytesSent, args.TotalBytesToSend);
1289
                                                            }
1290
                                                        };
1291

    
1292
                    client.UploadFileCompleted += (sender, args) =>
1293
                                                      {
1294
                                                          using (ThreadContext.Stacks["PUT"].Push("Progress"))
1295
                                                          {
1296
                                                              Log.InfoFormat("Completed {0}", fileName);
1297
                                                          }
1298
                                                      };                    
1299
                    if (contentType=="application/directory")
1300
                        await client.UploadDataTaskAsync(uri, "PUT", new byte[0]);
1301
                    else
1302
                        await client.UploadFileTaskAsync(uri, "PUT", fileName);
1303
                }
1304

    
1305
                Log.InfoFormat("[PUT] END {0}", objectName);
1306
            }
1307
            catch (Exception exc)
1308
            {
1309
                Log.ErrorFormat("[PUT] END {0} with {1}", objectName, exc);
1310
                throw;
1311
            }                
1312

    
1313
        }
1314
       
1315
        
1316
        private static string CalculateHash(string fileName)
1317
        {
1318
            Contract.Requires(!String.IsNullOrWhiteSpace(fileName));
1319
            Contract.EndContractBlock();
1320

    
1321
            string hash;
1322
            using (var hasher = MD5.Create())
1323
            using(var stream=File.OpenRead(fileName))
1324
            {
1325
                var hashBuilder=new StringBuilder();
1326
                foreach (byte b in hasher.ComputeHash(stream))
1327
                    hashBuilder.Append(b.ToString("x2").ToLower());
1328
                hash = hashBuilder.ToString();                
1329
            }
1330
            return hash;
1331
        }
1332
        
1333
        public void MoveObject(string account, string sourceContainer, string oldObjectName, string targetContainer, string newObjectName)
1334
        {
1335
            if (String.IsNullOrWhiteSpace(sourceContainer))
1336
                throw new ArgumentNullException("sourceContainer", "The container property can't be empty");
1337
            if (String.IsNullOrWhiteSpace(oldObjectName))
1338
                throw new ArgumentNullException("oldObjectName", "The oldObjectName property can't be empty");
1339
            if (String.IsNullOrWhiteSpace(targetContainer))
1340
                throw new ArgumentNullException("targetContainer", "The container property can't be empty");
1341
            if (String.IsNullOrWhiteSpace(newObjectName))
1342
                throw new ArgumentNullException("newObjectName", "The newObjectName property can't be empty");
1343
            Contract.EndContractBlock();
1344

    
1345
            var targetUrl = targetContainer + "/" + newObjectName;
1346
            var sourceUrl = String.Format("/{0}/{1}", sourceContainer, oldObjectName);
1347

    
1348
            using (var client = new RestClient(_baseClient))
1349
            {
1350
                if (!String.IsNullOrWhiteSpace(account))
1351
                    client.BaseAddress = GetAccountUrl(account);
1352

    
1353
                client.Headers.Add("X-Move-From", sourceUrl);
1354
                client.PutWithRetry(targetUrl, 3);
1355

    
1356
                var expectedCodes = new[] {HttpStatusCode.OK, HttpStatusCode.NoContent, HttpStatusCode.Created};
1357
                if (!expectedCodes.Contains(client.StatusCode))
1358
                    throw CreateWebException("MoveObject", client.StatusCode);
1359
            }
1360
        }
1361

    
1362
        public void DeleteObject(string account, string sourceContainer, string objectName)
1363
        {            
1364
            if (String.IsNullOrWhiteSpace(sourceContainer))
1365
                throw new ArgumentNullException("sourceContainer", "The container property can't be empty");
1366
            if (String.IsNullOrWhiteSpace(objectName))
1367
                throw new ArgumentNullException("objectName", "The oldObjectName property can't be empty");
1368
            Contract.EndContractBlock();
1369

    
1370
            var targetUrl = FolderConstants.TrashContainer + "/" + objectName;
1371
            var sourceUrl = String.Format("/{0}/{1}", sourceContainer, objectName);
1372

    
1373
            using (var client = new RestClient(_baseClient))
1374
            {
1375
                if (!String.IsNullOrWhiteSpace(account))
1376
                    client.BaseAddress = GetAccountUrl(account);
1377

    
1378
                client.Headers.Add("X-Move-From", sourceUrl);
1379
                client.AllowedStatusCodes.Add(HttpStatusCode.NotFound);
1380
                Log.InfoFormat("[TRASH] [{0}] to [{1}]",sourceUrl,targetUrl);
1381
                client.PutWithRetry(targetUrl, 3);
1382

    
1383
                var expectedCodes = new[] {HttpStatusCode.OK, HttpStatusCode.NoContent, HttpStatusCode.Created,HttpStatusCode.NotFound};
1384
                if (!expectedCodes.Contains(client.StatusCode))
1385
                    throw CreateWebException("DeleteObject", client.StatusCode);
1386
            }
1387
        }
1388

    
1389
      
1390
        private static WebException CreateWebException(string operation, HttpStatusCode statusCode)
1391
        {
1392
            return new WebException(String.Format("{0} failed with unexpected status code {1}", operation, statusCode));
1393
        }
1394

    
1395

    
1396
/*
1397
        public IEnumerable<ObjectInfo> ListDirectories(ContainerInfo container)
1398
        {
1399
            var directories=this.ListObjects(container.Account, container.Name, "/");
1400
        }
1401
*/
1402

    
1403
        public bool CanUpload(string account, ObjectInfo cloudFile)
1404
        {
1405
            Contract.Requires(!String.IsNullOrWhiteSpace(account));
1406
            Contract.Requires(cloudFile!=null);
1407

    
1408
            using (var client = new RestClient(_baseClient))
1409
            {
1410
                if (!String.IsNullOrWhiteSpace(account))
1411
                    client.BaseAddress = GetAccountUrl(account);
1412

    
1413

    
1414
                var parts = cloudFile.Name.Split('/');
1415
                var folder = String.Join("/", parts,0,parts.Length-1);
1416

    
1417
                var fileUrl=String.Format("{0}/{1}/{2}.pithos.ignore",cloudFile.Container,folder,Guid.NewGuid());
1418

    
1419
                client.Parameters.Clear();
1420
                try
1421
                {
1422
                    client.PutWithRetry(fileUrl, 3, @"application/octet-stream");
1423

    
1424
                    var expectedCodes = new[] { HttpStatusCode.OK, HttpStatusCode.NoContent, HttpStatusCode.Created};
1425
                    var result=(expectedCodes.Contains(client.StatusCode));
1426
                    DeleteObject(account, cloudFile.Container, fileUrl);
1427
                    return result;
1428
                }
1429
                catch
1430
                {
1431
                    return false;
1432
                }
1433
            }
1434
        }
1435
    }
1436

    
1437
    public class ShareAccountInfo
1438
    {
1439
        public DateTime? last_modified { get; set; }
1440
        public string name { get; set; }
1441
    }
1442
}