87aa5e4a5d0a6e7a6c1a9bf7f86c9e5509ae4f93
[pithos-ms-client] / trunk / Pithos.Network / CloudFilesClient.cs
1 #region\r
2 /* -----------------------------------------------------------------------\r
3  * <copyright file="CloudFilesClient.cs" company="GRNet">\r
4  * \r
5  * Copyright 2011-2012 GRNET S.A. All rights reserved.\r
6  *\r
7  * Redistribution and use in source and binary forms, with or\r
8  * without modification, are permitted provided that the following\r
9  * conditions are met:\r
10  *\r
11  *   1. Redistributions of source code must retain the above\r
12  *      copyright notice, this list of conditions and the following\r
13  *      disclaimer.\r
14  *\r
15  *   2. Redistributions in binary form must reproduce the above\r
16  *      copyright notice, this list of conditions and the following\r
17  *      disclaimer in the documentation and/or other materials\r
18  *      provided with the distribution.\r
19  *\r
20  *\r
21  * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS\r
22  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r
23  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\r
24  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR\r
25  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\r
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\r
27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF\r
28  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED\r
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\r
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN\r
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
32  * POSSIBILITY OF SUCH DAMAGE.\r
33  *\r
34  * The views and conclusions contained in the software and\r
35  * documentation are those of the authors and should not be\r
36  * interpreted as representing official policies, either expressed\r
37  * or implied, of GRNET S.A.\r
38  * </copyright>\r
39  * -----------------------------------------------------------------------\r
40  */\r
41 #endregion\r
42 \r
43 // **CloudFilesClient** provides a simple client interface to CloudFiles and Pithos\r
44 //\r
45 // The class provides methods to upload/download files, delete files, manage containers\r
46 \r
47 \r
48 using System;\r
49 using System.Collections.Generic;\r
50 using System.Collections.Specialized;\r
51 using System.ComponentModel.Composition;\r
52 using System.Diagnostics;\r
53 using System.Diagnostics.Contracts;\r
54 using System.IO;\r
55 using System.Linq;\r
56 using System.Net;\r
57 using System.Net.Http;\r
58 using System.Net.Http.Headers;\r
59 using System.Reflection;\r
60 using System.Threading;\r
61 using System.Threading.Tasks;\r
62 using Newtonsoft.Json;\r
63 using Pithos.Interfaces;\r
64 using Pithos.Network;\r
65 using log4net;\r
66 \r
67 namespace Pithos.Network\r
68 {\r
69 \r
70     [Export(typeof(ICloudClient))]\r
71     public class CloudFilesClient:ICloudClient,IDisposable\r
72     {\r
73         private const string TOKEN_HEADER = "X-Auth-Token";\r
74         private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);\r
75 \r
76         //CloudFilesClient uses *_baseClient* internally to communicate with the server\r
77         //RestClient provides a REST-friendly interface over the standard WebClient.\r
78         private RestClient _baseClient;\r
79 \r
80         private HttpClient _baseHttpClient;\r
81         private HttpClient _baseHttpClientNoTimeout;\r
82         \r
83 \r
84         //During authentication the client provides a UserName \r
85         public string UserName { get; set; }\r
86         \r
87         //and and ApiKey to the server\r
88         public string ApiKey { get; set; }\r
89         \r
90         //And receives an authentication Token. This token must be provided in ALL other operations,\r
91         //in the X-Auth-Token header\r
92         private string _token;\r
93         private readonly string _emptyGuid = Guid.Empty.ToString();\r
94         private readonly Uri _emptyUri = new Uri("",UriKind.Relative);\r
95 \r
96         private HttpClientHandler _httpClientHandler = new HttpClientHandler\r
97                                                            {\r
98                                                                AllowAutoRedirect = true,\r
99                                                                AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,\r
100                                                                UseCookies = true,                                \r
101                                                            };\r
102 \r
103 \r
104         public string Token\r
105         {\r
106             get { return _token; }\r
107             set\r
108             {\r
109                 _token = value;\r
110                 _baseClient.Headers[TOKEN_HEADER] = value;\r
111             }\r
112         }\r
113 \r
114         //The client also receives a StorageUrl after authentication. All subsequent operations must\r
115         //use this url\r
116         public Uri StorageUrl { get; set; }\r
117 \r
118 \r
119         public Uri RootAddressUri { get; set; }\r
120 \r
121 \r
122         public double DownloadPercentLimit { get; set; }\r
123         public double UploadPercentLimit { get; set; }\r
124 \r
125         public string AuthenticationUrl { get; set; }\r
126 \r
127  \r
128         public string VersionPath\r
129         {\r
130             get { return UsePithos ? "v1" : "v1.0"; }\r
131         }\r
132 \r
133         public bool UsePithos { get; set; }\r
134 \r
135 \r
136 \r
137         public CloudFilesClient(string userName, string apiKey)\r
138         {\r
139             UserName = userName;\r
140             ApiKey = apiKey;\r
141         }\r
142 \r
143         public CloudFilesClient(AccountInfo accountInfo)\r
144         {\r
145             if (accountInfo==null)\r
146                 throw new ArgumentNullException("accountInfo");\r
147             Contract.Ensures(!String.IsNullOrWhiteSpace(Token));\r
148             Contract.Ensures(StorageUrl != null);\r
149             Contract.Ensures(_baseClient != null);\r
150             Contract.Ensures(RootAddressUri != null);\r
151             Contract.EndContractBlock();          \r
152 \r
153             _baseClient = new RestClient\r
154             {\r
155                 BaseAddress = accountInfo.StorageUri.ToString(),\r
156                 Timeout = 30000,\r
157                 Retries = 3,\r
158             };\r
159             StorageUrl = accountInfo.StorageUri;\r
160             Token = accountInfo.Token;\r
161             UserName = accountInfo.UserName;\r
162 \r
163             //Get the root address (StorageUrl without the account)\r
164             var storageUrl = StorageUrl.AbsoluteUri;\r
165             var usernameIndex = storageUrl.LastIndexOf(UserName);\r
166             var rootUrl = storageUrl.Substring(0, usernameIndex);\r
167             RootAddressUri = new Uri(rootUrl);\r
168 \r
169             var httpClientHandler = new HttpClientHandler\r
170             {\r
171                 AllowAutoRedirect = true,\r
172                 AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,\r
173                 UseCookies = true,\r
174             };\r
175 \r
176 \r
177             _baseHttpClient = new HttpClient(httpClientHandler)\r
178             {\r
179                 BaseAddress = StorageUrl,\r
180                 Timeout = TimeSpan.FromSeconds(30)\r
181             };\r
182             _baseHttpClient.DefaultRequestHeaders.Add(TOKEN_HEADER, Token);\r
183 \r
184             _baseHttpClientNoTimeout = new HttpClient(httpClientHandler)\r
185             {\r
186                 BaseAddress = StorageUrl,\r
187                 Timeout = TimeSpan.FromMilliseconds(-1)\r
188             };\r
189             _baseHttpClientNoTimeout.DefaultRequestHeaders.Add(TOKEN_HEADER, Token);\r
190 \r
191 \r
192         }\r
193 \r
194 \r
195         private static void AssertStatusOK(HttpResponseMessage response, string message)\r
196         {\r
197             var statusCode = response.StatusCode;\r
198             if (statusCode >= HttpStatusCode.BadRequest)\r
199                 throw new WebException(String.Format("{0} with code {1} - {2}", message, statusCode, response.ReasonPhrase));\r
200         }\r
201 \r
202         public async Task<AccountInfo> Authenticate()\r
203         {\r
204             if (String.IsNullOrWhiteSpace(UserName))\r
205                 throw new InvalidOperationException("UserName is empty");\r
206             if (String.IsNullOrWhiteSpace(ApiKey))\r
207                 throw new InvalidOperationException("ApiKey is empty");\r
208             if (String.IsNullOrWhiteSpace(AuthenticationUrl))\r
209                 throw new InvalidOperationException("AuthenticationUrl is empty");\r
210             Contract.Ensures(!String.IsNullOrWhiteSpace(Token));\r
211             Contract.Ensures(StorageUrl != null);\r
212             Contract.Ensures(_baseClient != null);\r
213             Contract.Ensures(RootAddressUri != null);\r
214             Contract.EndContractBlock();\r
215 \r
216 \r
217             Log.InfoFormat("[AUTHENTICATE] Start for {0}", UserName);\r
218 \r
219             var groups = new List<Group>();\r
220 \r
221             using (var authClient = new HttpClient(_httpClientHandler,false){ BaseAddress = new Uri(AuthenticationUrl),Timeout=TimeSpan.FromSeconds(30) })\r
222             {                \r
223 \r
224                 authClient.DefaultRequestHeaders.Add("X-Auth-User", UserName);\r
225                 authClient.DefaultRequestHeaders.Add("X-Auth-Key", ApiKey);\r
226 \r
227                 string storageUrl;\r
228                 string token;\r
229                 \r
230                 using (var response = await authClient.GetAsyncWithRetries(new Uri(VersionPath, UriKind.Relative),3).ConfigureAwait(false)) // .DownloadStringWithRetryRelative(new Uri(VersionPath, UriKind.Relative), 3);                    \r
231                 {\r
232                     AssertStatusOK(response,"Authentication failed");\r
233                 \r
234                     storageUrl = response.Headers.GetFirstValue("X-Storage-Url");\r
235                     if (String.IsNullOrWhiteSpace(storageUrl))\r
236                         throw new InvalidOperationException("Failed to obtain storage url");\r
237 \r
238                     token = response.Headers.GetFirstValue(TOKEN_HEADER);\r
239                     if (String.IsNullOrWhiteSpace(token))\r
240                         throw new InvalidOperationException("Failed to obtain token url");\r
241 \r
242                 }\r
243 \r
244 \r
245                 _baseClient = new RestClient\r
246                 {\r
247                     BaseAddress = storageUrl,\r
248                     Timeout = 30000,\r
249                     Retries = 3,                    \r
250                 };\r
251 \r
252                 StorageUrl = new Uri(storageUrl);\r
253                 Token = token;\r
254 \r
255                 \r
256 \r
257                                                                \r
258                 //Get the root address (StorageUrl without the account)\r
259                 var usernameIndex=storageUrl.LastIndexOf(UserName);\r
260                 var rootUrl = storageUrl.Substring(0, usernameIndex);\r
261                 RootAddressUri = new Uri(rootUrl);\r
262                 \r
263 \r
264                 _baseHttpClient = new HttpClient(_httpClientHandler,false)\r
265                 {\r
266                     BaseAddress = StorageUrl,\r
267                     Timeout = TimeSpan.FromSeconds(30)\r
268                 };\r
269                 _baseHttpClient.DefaultRequestHeaders.Add(TOKEN_HEADER, token);\r
270 \r
271                 _baseHttpClientNoTimeout = new HttpClient(_httpClientHandler,false)\r
272                 {\r
273                     BaseAddress = StorageUrl,\r
274                     Timeout = TimeSpan.FromMilliseconds(-1)\r
275                 };\r
276                 _baseHttpClientNoTimeout.DefaultRequestHeaders.Add(TOKEN_HEADER, token);\r
277 \r
278                 /* var keys = authClient.ResponseHeaders.AllKeys.AsQueryable();\r
279                 groups = (from key in keys\r
280                             where key.StartsWith("X-Account-Group-")\r
281                             let name = key.Substring(16)\r
282                             select new Group(name, authClient.ResponseHeaders[key]))\r
283                         .ToList();\r
284                     \r
285 */\r
286             }\r
287 \r
288             Log.InfoFormat("[AUTHENTICATE] End for {0}", UserName);\r
289             Debug.Assert(_baseClient!=null);\r
290 \r
291             return new AccountInfo {StorageUri = StorageUrl, Token = Token, UserName = UserName,Groups=groups};            \r
292 \r
293         }\r
294 \r
295         private static void TraceStart(string method, Uri actualAddress)\r
296         {\r
297             Log.InfoFormat("[{0}] {1} {2}", method, DateTime.Now, actualAddress);\r
298         }\r
299 \r
300         private async Task<string> GetStringAsync(Uri targetUri, string errorMessage,DateTimeOffset? since=null)\r
301         {\r
302             TraceStart("GET",targetUri);\r
303             var request = new HttpRequestMessage(HttpMethod.Get, targetUri);            \r
304             if (since.HasValue)\r
305             {\r
306                 request.Headers.IfModifiedSince = since.Value;\r
307             }\r
308             using (var response = await _baseHttpClient.SendAsyncWithRetries(request,3).ConfigureAwait(false))\r
309             {\r
310                 AssertStatusOK(response, errorMessage);\r
311 \r
312                 if (response.StatusCode == HttpStatusCode.NoContent)\r
313                     return String.Empty;\r
314 \r
315                 var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);\r
316                 return content;\r
317             }\r
318         }\r
319 \r
320         public async Task<IList<ContainerInfo>> ListContainers(string account)\r
321         {\r
322 \r
323             var targetUrl = GetTargetUrl(account);\r
324             var targetUri = new Uri(String.Format("{0}?format=json", targetUrl));\r
325             var result = await GetStringAsync(targetUri, "List Containers failed").ConfigureAwait(false);\r
326             if (String.IsNullOrWhiteSpace(result))\r
327                 return new List<ContainerInfo>();\r
328             var infos = JsonConvert.DeserializeObject<IList<ContainerInfo>>(result);\r
329             foreach (var info in infos)\r
330             {\r
331                 info.Account = account;\r
332             }\r
333             return infos;\r
334         }\r
335 \r
336         \r
337         private string GetAccountUrl(string account)\r
338         {\r
339             return RootAddressUri.Combine(account).AbsoluteUri;\r
340         }\r
341 \r
342         public IList<ShareAccountInfo> ListSharingAccounts(DateTime? since=null)\r
343         {\r
344             using (ThreadContext.Stacks["Share"].Push("List Accounts"))\r
345             {\r
346                 if (Log.IsDebugEnabled) Log.DebugFormat("START");\r
347 \r
348                 var targetUri = new Uri(String.Format("{0}?format=json", RootAddressUri), UriKind.Absolute);\r
349                 var content=TaskEx.Run(async ()=>await GetStringAsync(targetUri, "ListSharingAccounts failed", since).ConfigureAwait(false)).Result;\r
350 \r
351                 //If the result is empty, return an empty list,\r
352                 var infos = String.IsNullOrWhiteSpace(content)\r
353                                 ? new List<ShareAccountInfo>()\r
354                             //Otherwise deserialize the account list into a list of ShareAccountInfos\r
355                                 : JsonConvert.DeserializeObject<IList<ShareAccountInfo>>(content);\r
356 \r
357                 Log.DebugFormat("END");\r
358                 return infos;\r
359             }\r
360         }\r
361 \r
362 \r
363         /// <summary>\r
364         /// Request listing of all objects in a container modified since a specific time.\r
365         /// If the *since* value is missing, return all objects\r
366         /// </summary>\r
367         /// <param name="knownContainers">Use the since variable only for the containers listed in knownContainers. Unknown containers are considered new\r
368         /// and should be polled anyway\r
369         /// </param>\r
370         /// <param name="since"></param>\r
371         /// <returns></returns>\r
372         public IList<ObjectInfo> ListSharedObjects(HashSet<string> knownContainers, DateTimeOffset? since)\r
373         {\r
374 \r
375             using (ThreadContext.Stacks["Share"].Push("List Objects"))\r
376             {\r
377                 if (Log.IsDebugEnabled) Log.DebugFormat("START");\r
378                 //'since' is not used here because we need to have ListObjects return a NoChange result\r
379                 //for all shared accounts,containers\r
380 \r
381                 Func<ContainerInfo, string> getKey = c => String.Format("{0}\\{1}", c.Account, c.Name);\r
382 \r
383                 var containers = (from account in ListSharingAccounts()\r
384                                  let conts = ListContainers(account.name).Result\r
385                                  from container in conts\r
386                                  select container).ToList();                \r
387                 var items = from container in containers \r
388                             let actualSince=knownContainers.Contains(getKey(container))?since:null\r
389                             select ListObjects(container.Account , container.Name,  actualSince);\r
390                 var objects=items.SelectMany(r=> r).ToList();\r
391 \r
392                 //For each object\r
393                 //Check parents recursively up to (but not including) the container.\r
394                 //If parents are missing, add them to the list\r
395                 //Need function to calculate all parent URLs\r
396                 objects = AddMissingParents(objects);\r
397                 \r
398                 //Store any new containers\r
399                 foreach (var container in containers)\r
400                 {\r
401                     knownContainers.Add(getKey(container));\r
402                 }\r
403 \r
404 \r
405 \r
406                 if (Log.IsDebugEnabled) Log.DebugFormat("END");\r
407                 return objects;\r
408             }\r
409         }\r
410 \r
411         private List<ObjectInfo> AddMissingParents(List<ObjectInfo> objects)\r
412         {\r
413             //TODO: Remove short-circuit when we decide to use Missing Parents functionality\r
414             //return objects;\r
415 \r
416             var existingUris = objects.ToDictionary(o => o.Uri, o => o);\r
417             foreach (var objectInfo in objects)\r
418             {\r
419                 //Can be null when retrieving objects to show in selective sync\r
420                 if (objectInfo.Name == null)\r
421                     continue;\r
422 \r
423                 //No need to unescape here, the parts will be used to create new ObjectInfos\r
424                 var parts = objectInfo.Name.ToString().Split(new[]{'/'},StringSplitOptions.RemoveEmptyEntries);\r
425                 //If there is no parent, skip\r
426                 if (parts.Length == 1)\r
427                     continue;\r
428                 var baseParts = new[]\r
429                                   {\r
430                                       objectInfo.Uri.Host, objectInfo.Uri.Segments[1].TrimEnd('/'),objectInfo.Account,objectInfo.Container.ToString()\r
431                                   };\r
432                 for (var partIdx = 0; partIdx < parts.Length - 1; partIdx++)\r
433                 {\r
434                     var nameparts = parts.Range(0, partIdx).ToArray();\r
435                     var parentName= String.Join("/", nameparts);\r
436 \r
437                     var parentParts = baseParts.Concat(nameparts);\r
438                     var parentUrl = objectInfo.Uri.Scheme+ "://" + String.Join("/", parentParts);\r
439                     \r
440                     var parentUri = new Uri(parentUrl, UriKind.Absolute);\r
441 \r
442                     ObjectInfo existingInfo;\r
443                     if (!existingUris.TryGetValue(parentUri,out existingInfo))\r
444                     {\r
445                         var h = parentUrl.GetHashCode();\r
446                         var reverse = new string(parentUrl.Reverse().ToArray());\r
447                         var rh = reverse.GetHashCode();\r
448                         var b1 = BitConverter.GetBytes(h);\r
449                         var b2 = BitConverter.GetBytes(rh);\r
450                         var g = new Guid(0,0,0,b1.Concat(b2).ToArray());\r
451                         \r
452 \r
453                         existingUris[parentUri] = new ObjectInfo\r
454                                                       {\r
455                                                           Account = objectInfo.Account,\r
456                                                           Container = objectInfo.Container,\r
457                                                           Content_Type = ObjectInfo.CONTENT_TYPE_DIRECTORY,\r
458                                                           ETag = Signature.MERKLE_EMPTY,\r
459                                                           X_Object_Hash = Signature.MERKLE_EMPTY,\r
460                                                           Name=new Uri(parentName,UriKind.Relative),\r
461                                                           StorageUri=objectInfo.StorageUri,\r
462                                                           Bytes = 0,\r
463                                                           UUID=g.ToString(),                                                          \r
464                                                       };\r
465                     }\r
466                 }\r
467             }\r
468             return existingUris.Values.ToList();\r
469         }\r
470 \r
471         public void SetTags(ObjectInfo target,IDictionary<string,string> tags)\r
472         {\r
473             if (String.IsNullOrWhiteSpace(Token))\r
474                 throw new InvalidOperationException("The Token is not set");\r
475             if (StorageUrl == null)\r
476                 throw new InvalidOperationException("The StorageUrl is not set");\r
477             if (target == null)\r
478                 throw new ArgumentNullException("target");\r
479             Contract.EndContractBlock();\r
480 \r
481             using (ThreadContext.Stacks["Share"].Push("Share Object"))\r
482             {\r
483                 if (Log.IsDebugEnabled) Log.DebugFormat("START");\r
484 \r
485                 using (var client = new RestClient(_baseClient))\r
486                 {\r
487 \r
488                     client.BaseAddress = GetAccountUrl(target.Account);\r
489 \r
490                     client.Parameters.Clear();\r
491                     client.Parameters.Add("update", "");\r
492 \r
493                     foreach (var tag in tags)\r
494                     {\r
495                         var headerTag = String.Format("X-Object-Meta-{0}", tag.Key);\r
496                         client.Headers.Add(headerTag, tag.Value);\r
497                     }\r
498                     \r
499                     client.DownloadStringWithRetryRelative(target.Container, 3);\r
500 \r
501                     \r
502                     client.AssertStatusOK("SetTags failed");\r
503                     //If the status is NOT ACCEPTED we have a problem\r
504                     if (client.StatusCode != HttpStatusCode.Accepted)\r
505                     {\r
506                         Log.Error("Failed to set tags");\r
507                         throw new Exception("Failed to set tags");\r
508                     }\r
509 \r
510                     if (Log.IsDebugEnabled) Log.DebugFormat("END");\r
511                 }\r
512             }\r
513 \r
514 \r
515         }\r
516 \r
517         public void ShareObject(string account, Uri container, Uri objectName, string shareTo, bool read, bool write)\r
518         {\r
519             if (String.IsNullOrWhiteSpace(Token))\r
520                 throw new InvalidOperationException("The Token is not set");\r
521             if (StorageUrl==null)\r
522                 throw new InvalidOperationException("The StorageUrl is not set");\r
523             if (container==null)\r
524                 throw new ArgumentNullException("container");\r
525             if (container.IsAbsoluteUri)\r
526                 throw new ArgumentException("container");\r
527             if (objectName==null)\r
528                 throw new ArgumentNullException("objectName");\r
529             if (objectName.IsAbsoluteUri)\r
530                 throw new ArgumentException("objectName");\r
531             if (String.IsNullOrWhiteSpace(account))\r
532                 throw new ArgumentNullException("account");\r
533             if (String.IsNullOrWhiteSpace(shareTo))\r
534                 throw new ArgumentNullException("shareTo");\r
535             Contract.EndContractBlock();\r
536 \r
537             using (ThreadContext.Stacks["Share"].Push("Share Object"))\r
538             {\r
539                 if (Log.IsDebugEnabled) Log.DebugFormat("START");\r
540                 \r
541                 using (var client = new RestClient(_baseClient))\r
542                 {\r
543 \r
544                     client.BaseAddress = GetAccountUrl(account);\r
545 \r
546                     client.Parameters.Clear();\r
547                     client.Parameters.Add("format", "json");\r
548 \r
549                     string permission = "";\r
550                     if (write)\r
551                         permission = String.Format("write={0}", shareTo);\r
552                     else if (read)\r
553                         permission = String.Format("read={0}", shareTo);\r
554                     client.Headers.Add("X-Object-Sharing", permission);\r
555 \r
556                     var content = client.DownloadStringWithRetryRelative(container, 3);\r
557 \r
558                     client.AssertStatusOK("ShareObject failed");\r
559 \r
560                     //If the result is empty, return an empty list,\r
561                     var infos = String.IsNullOrWhiteSpace(content)\r
562                                     ? new List<ObjectInfo>()\r
563                                 //Otherwise deserialize the object list into a list of ObjectInfos\r
564                                     : JsonConvert.DeserializeObject<IList<ObjectInfo>>(content);\r
565 \r
566                     if (Log.IsDebugEnabled) Log.DebugFormat("END");\r
567                 }\r
568             }\r
569 \r
570 \r
571         }\r
572 \r
573         public async Task<AccountInfo> GetAccountPolicies(AccountInfo accountInfo)\r
574         {\r
575             if (accountInfo==null)\r
576                 throw new ArgumentNullException("accountInfo");\r
577             Contract.EndContractBlock();\r
578 \r
579             using (ThreadContext.Stacks["Account"].Push("GetPolicies"))\r
580             {\r
581                 if (Log.IsDebugEnabled) Log.DebugFormat("START");\r
582 \r
583 /*\r
584                 if (_baseClient == null)\r
585                 {\r
586                     _baseClient = new RestClient\r
587                     {\r
588                         BaseAddress = accountInfo.StorageUri.ToString(),\r
589                         Timeout = 30000,\r
590                         Retries = 3,\r
591                     };\r
592                 }\r
593 \r
594 */                \r
595                 var containerUri = GetTargetUri(accountInfo.UserName);\r
596                 var targetUri = new Uri(String.Format("{0}?format=json", containerUri), UriKind.Absolute);\r
597                 using(var response=await _baseHttpClient.HeadAsyncWithRetries(targetUri,3).ConfigureAwait(false))\r
598                 {\r
599                     \r
600                     var quotaValue=response.Headers.GetFirstValue("X-Account-Policy-Quota");\r
601                     var bytesValue = response.Headers.GetFirstValue("X-Account-Bytes-Used");\r
602                     long quota, bytes;\r
603                     if (long.TryParse(quotaValue, out quota))\r
604                         accountInfo.Quota = quota;\r
605                     if (long.TryParse(bytesValue, out bytes))\r
606                         accountInfo.BytesUsed = bytes;\r
607 \r
608                     return accountInfo;   \r
609                 }\r
610 \r
611 \r
612                 //using (var client = new RestClient(_baseClient))\r
613                 //{\r
614                 //    if (!String.IsNullOrWhiteSpace(accountInfo.UserName))\r
615                 //        client.BaseAddress = GetAccountUrl(accountInfo.UserName);\r
616 \r
617                 //    client.Parameters.Clear();\r
618                 //    client.Parameters.Add("format", "json");                    \r
619                 //    client.Head(_emptyUri, 3);\r
620 \r
621                 //    var quotaValue=client.ResponseHeaders["X-Account-Policy-Quota"];\r
622                 //    var bytesValue= client.ResponseHeaders["X-Account-Bytes-Used"];\r
623 \r
624                 //    long quota, bytes;\r
625                 //    if (long.TryParse(quotaValue, out quota))\r
626                 //        accountInfo.Quota = quota;\r
627                 //    if (long.TryParse(bytesValue, out bytes))\r
628                 //        accountInfo.BytesUsed = bytes;\r
629                     \r
630                 //    return accountInfo;\r
631 \r
632                 //}\r
633 \r
634             }\r
635         }\r
636 \r
637         public void UpdateMetadata(ObjectInfo objectInfo)\r
638         {\r
639             if (objectInfo == null)\r
640                 throw new ArgumentNullException("objectInfo");\r
641             Contract.EndContractBlock();\r
642 \r
643             using (ThreadContext.Stacks["Objects"].Push("UpdateMetadata"))\r
644             {\r
645                 if (Log.IsDebugEnabled) Log.DebugFormat("START");\r
646 \r
647 \r
648                 using(var client=new RestClient(_baseClient))\r
649                 {\r
650 \r
651                     client.BaseAddress = GetAccountUrl(objectInfo.Account);\r
652                     \r
653                     client.Parameters.Clear();\r
654                     \r
655 \r
656                     //Set Tags\r
657                     foreach (var tag in objectInfo.Tags)\r
658                     {\r
659                         var headerTag = String.Format("X-Object-Meta-{0}", tag.Key);\r
660                         client.Headers.Add(headerTag, tag.Value);\r
661                     }\r
662 \r
663                     //Set Permissions\r
664 \r
665                     var permissions=objectInfo.GetPermissionString();\r
666                     client.SetNonEmptyHeaderValue("X-Object-Sharing",permissions);\r
667 \r
668                     client.SetNonEmptyHeaderValue("Content-Disposition",objectInfo.ContendDisposition);\r
669                     client.SetNonEmptyHeaderValue("Content-Encoding",objectInfo.ContentEncoding);\r
670                     client.SetNonEmptyHeaderValue("X-Object-Manifest",objectInfo.Manifest);\r
671                     var isPublic = objectInfo.IsPublic.ToString().ToLower();\r
672                     client.Headers.Add("X-Object-Public", isPublic);\r
673 \r
674 \r
675                     var address = String.Format("{0}/{1}?update=",objectInfo.Container, objectInfo.Name);\r
676                     client.PostWithRetry(new Uri(address,UriKind.Relative),"application/xml");\r
677                     \r
678                     client.AssertStatusOK("UpdateMetadata failed");\r
679                     //If the status is NOT ACCEPTED or OK we have a problem\r
680                     if (!(client.StatusCode == HttpStatusCode.Accepted || client.StatusCode == HttpStatusCode.OK))\r
681                     {\r
682                         Log.Error("Failed to update metadata");\r
683                         throw new Exception("Failed to update metadata");\r
684                     }\r
685 \r
686                     if (Log.IsDebugEnabled) Log.DebugFormat("END");\r
687                 }\r
688             }\r
689 \r
690         }\r
691 \r
692         public void UpdateMetadata(ContainerInfo containerInfo)\r
693         {\r
694             if (containerInfo == null)\r
695                 throw new ArgumentNullException("containerInfo");\r
696             Contract.EndContractBlock();\r
697 \r
698             using (ThreadContext.Stacks["Containers"].Push("UpdateMetadata"))\r
699             {\r
700                 if (Log.IsDebugEnabled) Log.DebugFormat("START");\r
701 \r
702 \r
703                 using(var client=new RestClient(_baseClient))\r
704                 {\r
705 \r
706                     client.BaseAddress = GetAccountUrl(containerInfo.Account);\r
707                     \r
708                     client.Parameters.Clear();\r
709                     \r
710 \r
711                     //Set Tags\r
712                     foreach (var tag in containerInfo.Tags)\r
713                     {\r
714                         var headerTag = String.Format("X-Container-Meta-{0}", tag.Key);\r
715                         client.Headers.Add(headerTag, tag.Value);\r
716                     }\r
717 \r
718                     \r
719                     //Set Policies\r
720                     foreach (var policy in containerInfo.Policies)\r
721                     {\r
722                         var headerPolicy = String.Format("X-Container-Policy-{0}", policy.Key);\r
723                         client.Headers.Add(headerPolicy, policy.Value);\r
724                     }\r
725 \r
726 \r
727                     var uriBuilder = client.GetAddressBuilder(containerInfo.Name,_emptyUri);\r
728                     var uri = uriBuilder.Uri;\r
729 \r
730                     client.UploadValues(uri,new NameValueCollection());\r
731 \r
732 \r
733                     client.AssertStatusOK("UpdateMetadata failed");\r
734                     //If the status is NOT ACCEPTED or OK we have a problem\r
735                     if (!(client.StatusCode == HttpStatusCode.Accepted || client.StatusCode == HttpStatusCode.OK))\r
736                     {\r
737                         Log.Error("Failed to update metadata");\r
738                         throw new Exception("Failed to update metadata");\r
739                     }\r
740 \r
741                     if (Log.IsDebugEnabled) Log.DebugFormat("END");\r
742                 }\r
743             }\r
744 \r
745         }\r
746 \r
747        \r
748 \r
749 \r
750         public IList<ObjectInfo> ListObjects(string account, Uri container, DateTimeOffset? since = null)\r
751         {\r
752             if (container==null)\r
753                 throw new ArgumentNullException("container");\r
754             if (container.IsAbsoluteUri)\r
755                 throw new ArgumentException("container");\r
756             Contract.EndContractBlock();\r
757 \r
758             using (ThreadContext.Stacks["Objects"].Push("List"))\r
759             {\r
760 \r
761                 var containerUri = GetTargetUri(account).Combine(container);\r
762                 var targetUri = new Uri(String.Format("{0}?format=json", containerUri), UriKind.Absolute);\r
763 \r
764                 var content =TaskEx.Run(async ()=>await GetStringAsync(targetUri, "ListObjects failed", since).ConfigureAwait(false)).Result;\r
765 \r
766                 //304 will result in an empty string. Empty containers return an empty json array\r
767                 if (String.IsNullOrWhiteSpace(content))\r
768                      return new[] {new NoModificationInfo(account, container)};\r
769 \r
770                  //If the result is empty, return an empty list,\r
771                  var infos = String.IsNullOrWhiteSpace(content)\r
772                                  ? new List<ObjectInfo>()\r
773                              //Otherwise deserialize the object list into a list of ObjectInfos\r
774                                  : JsonConvert.DeserializeObject<IList<ObjectInfo>>(content);\r
775 \r
776                  foreach (var info in infos)\r
777                  {\r
778                      info.Container = container;\r
779                      info.Account = account;\r
780                      info.StorageUri = StorageUrl;\r
781                  }\r
782                  if (Log.IsDebugEnabled) Log.DebugFormat("END");\r
783                  return infos;\r
784             }\r
785         }\r
786 \r
787         public IList<ObjectInfo> ListObjects(string account, Uri container, Uri folder, DateTimeOffset? since = null)\r
788         {\r
789             if (container==null)\r
790                 throw new ArgumentNullException("container");\r
791             if (container.IsAbsoluteUri)\r
792                 throw new ArgumentException("container");\r
793             Contract.EndContractBlock();\r
794 \r
795             using (ThreadContext.Stacks["Objects"].Push("List"))\r
796             {\r
797                 if (Log.IsDebugEnabled) Log.DebugFormat("START");\r
798 \r
799                 var containerUri = GetTargetUri(account).Combine(container);\r
800                 var targetUri = new Uri(String.Format("{0}?format=json&path={1}", containerUri,folder), UriKind.Absolute);\r
801                 var content = TaskEx.Run(async ()=>await GetStringAsync(targetUri, "ListObjects failed", since).ConfigureAwait(false)).Result;                \r
802 \r
803                 //304 will result in an empty string. Empty containers return an empty json array\r
804                 if (String.IsNullOrWhiteSpace(content))\r
805                     return new[] { new NoModificationInfo(account, container) };\r
806 \r
807 \r
808                 var infos = JsonConvert.DeserializeObject<IList<ObjectInfo>>(content);\r
809                 foreach (var info in infos)\r
810                 {\r
811                     info.Account = account;\r
812                     if (info.Container == null)\r
813                         info.Container = container;\r
814                     info.StorageUri = StorageUrl;\r
815                 }\r
816                 if (Log.IsDebugEnabled) Log.DebugFormat("END");\r
817                 return infos;\r
818 /*\r
819                 using (var client = new RestClient(_baseClient))\r
820                 {\r
821                     if (!String.IsNullOrWhiteSpace(account))\r
822                         client.BaseAddress = GetAccountUrl(account);\r
823 \r
824                     client.Parameters.Clear();\r
825                     client.Parameters.Add("format", "json");\r
826                     client.Parameters.Add("path", folder.ToString());\r
827                     client.IfModifiedSince = since;\r
828                     var content = client.DownloadStringWithRetryRelative(container, 3);\r
829                     client.AssertStatusOK("ListObjects failed");\r
830 \r
831                     if (client.StatusCode==HttpStatusCode.NotModified)\r
832                         return new[]{new NoModificationInfo(account,container,folder)};\r
833 \r
834                     var infos = JsonConvert.DeserializeObject<IList<ObjectInfo>>(content);\r
835                     foreach (var info in infos)\r
836                     {\r
837                         info.Account = account;\r
838                         if (info.Container == null)\r
839                             info.Container = container;\r
840                         info.StorageUri = StorageUrl;\r
841                     }\r
842                     if (Log.IsDebugEnabled) Log.DebugFormat("END");\r
843                     return infos;\r
844                 }\r
845 */\r
846             }\r
847         }\r
848 \r
849  \r
850         public bool ContainerExists(string account, Uri container)\r
851         {\r
852             if (container==null)\r
853                 throw new ArgumentNullException("container", "The container property can't be empty");\r
854             if (container.IsAbsoluteUri)\r
855                 throw new ArgumentException( "The container must be relative","container");\r
856             Contract.EndContractBlock();\r
857 \r
858             using (ThreadContext.Stacks["Containters"].Push("Exists"))\r
859             {\r
860                 if (Log.IsDebugEnabled) Log.DebugFormat("START");\r
861 \r
862                 var targetUri = GetTargetUri(account).Combine(container);\r
863 \r
864                 using (var response = _baseHttpClient.HeadAsyncWithRetries(targetUri, 3).Result)\r
865                 {\r
866 \r
867                     bool result;\r
868                     switch (response.StatusCode)\r
869                     {\r
870                         case HttpStatusCode.OK:\r
871                         case HttpStatusCode.NoContent:\r
872                             result = true;\r
873                             break;\r
874                         case HttpStatusCode.NotFound:\r
875                             result = false;\r
876                             break;\r
877                         default:\r
878                             throw CreateWebException("ContainerExists", response.StatusCode);\r
879                     }\r
880                     if (Log.IsDebugEnabled) Log.DebugFormat("END");\r
881 \r
882                     return result;\r
883                 }\r
884 /*\r
885                 using (var client = new RestClient(_baseClient))\r
886                 {\r
887                     if (!String.IsNullOrWhiteSpace(account))\r
888                         client.BaseAddress = GetAccountUrl(account);\r
889 \r
890                     client.Parameters.Clear();\r
891                     client.Head(container, 3);\r
892                                         \r
893                     bool result;\r
894                     switch (client.StatusCode)\r
895                     {\r
896                         case HttpStatusCode.OK:\r
897                         case HttpStatusCode.NoContent:\r
898                             result=true;\r
899                             break;\r
900                         case HttpStatusCode.NotFound:\r
901                             result=false;\r
902                             break;\r
903                         default:\r
904                             throw CreateWebException("ContainerExists", client.StatusCode);\r
905                     }\r
906                     if (Log.IsDebugEnabled) Log.DebugFormat("END");\r
907 \r
908                     return result;\r
909                 }\r
910 */\r
911                 \r
912             }\r
913         }\r
914 \r
915         private Uri GetTargetUri(string account)\r
916         {\r
917             return new Uri(GetTargetUrl(account),UriKind.Absolute);\r
918         }\r
919 \r
920         private string GetTargetUrl(string account)\r
921         {\r
922             return String.IsNullOrWhiteSpace(account)\r
923                        ? _baseHttpClient.BaseAddress.ToString()\r
924                        : GetAccountUrl(account);\r
925         }\r
926 \r
927         public bool ObjectExists(string account, Uri container, Uri objectName)\r
928         {\r
929             if (container == null)\r
930                 throw new ArgumentNullException("container", "The container property can't be empty");\r
931             if (container.IsAbsoluteUri)\r
932                 throw new ArgumentException("The container must be relative","container");\r
933             if (objectName == null)\r
934                 throw new ArgumentNullException("objectName", "The objectName property can't be empty");\r
935             if (objectName.IsAbsoluteUri)\r
936                 throw new ArgumentException("The objectName must be relative","objectName");\r
937             Contract.EndContractBlock();\r
938 \r
939                 var targetUri=GetTargetUri(account).Combine(container).Combine(objectName);\r
940 \r
941             using (var response = _baseHttpClient.HeadAsyncWithRetries(targetUri, 3).Result)\r
942             {\r
943                 switch (response.StatusCode)\r
944                 {\r
945                     case HttpStatusCode.OK:\r
946                     case HttpStatusCode.NoContent:\r
947                         return true;\r
948                     case HttpStatusCode.NotFound:\r
949                         return false;\r
950                     default:\r
951                         throw CreateWebException("ObjectExists", response.StatusCode);\r
952                 }\r
953             }\r
954 \r
955 /*\r
956             using (var client = new RestClient(_baseClient))\r
957             {\r
958                 if (!String.IsNullOrWhiteSpace(account))\r
959                     client.BaseAddress = GetAccountUrl(account);\r
960 \r
961                 client.Parameters.Clear();\r
962                 client.Head(container.Combine(objectName), 3);\r
963 \r
964                 switch (client.StatusCode)\r
965                 {\r
966                     case HttpStatusCode.OK:\r
967                     case HttpStatusCode.NoContent:\r
968                         return true;\r
969                     case HttpStatusCode.NotFound:\r
970                         return false;\r
971                     default:\r
972                         throw CreateWebException("ObjectExists", client.StatusCode);\r
973                 }\r
974             }\r
975 */\r
976 \r
977         }\r
978 \r
979         public async Task<ObjectInfo> GetObjectInfo(string account, Uri container, Uri objectName)\r
980         {\r
981             if (container == null)\r
982                 throw new ArgumentNullException("container", "The container property can't be empty");\r
983             if (container.IsAbsoluteUri)\r
984                 throw new ArgumentException("The container must be relative", "container");\r
985             if (objectName == null)\r
986                 throw new ArgumentNullException("objectName", "The objectName property can't be empty");\r
987             if (objectName.IsAbsoluteUri)\r
988                 throw new ArgumentException("The objectName must be relative", "objectName");\r
989             Contract.EndContractBlock();\r
990 \r
991             using (ThreadContext.Stacks["Objects"].Push("GetObjectInfo"))\r
992             {\r
993 \r
994                 var targetUri = GetTargetUri(account).Combine(container).Combine(objectName);\r
995                 try\r
996                 {\r
997                     using (var response = await _baseHttpClient.HeadAsyncWithRetries(targetUri, 3))\r
998                     {\r
999                         switch (response.StatusCode)\r
1000                         {\r
1001                             case HttpStatusCode.OK:\r
1002                             case HttpStatusCode.NoContent:\r
1003                                 var tags = response.Headers.GetMeta("X-Object-Meta-");\r
1004                                 var extensions = (from header in response.Headers\r
1005                                                   where\r
1006                                                       header.Key.StartsWith("X-Object-") &&\r
1007                                                       !header.Key.StartsWith("X-Object-Meta-")\r
1008                                                   select new {Name = header.Key, Value = header.Value.FirstOrDefault()})\r
1009                                     .ToDictionary(t => t.Name, t => t.Value);\r
1010 \r
1011                                 var permissions = response.Headers.GetFirstValue("X-Object-Sharing");\r
1012 \r
1013 \r
1014                                 var info = new ObjectInfo\r
1015                                                {\r
1016                                                    Account = account,\r
1017                                                    Container = container,\r
1018                                                    Name = objectName,\r
1019                                                    ETag = response.Headers.ETag.Tag,\r
1020                                                    UUID = response.Headers.GetFirstValue("X-Object-UUID"),\r
1021                                                    X_Object_Hash = response.Headers.GetFirstValue("X-Object-Hash"),\r
1022                                                    Content_Type = response.Headers.GetFirstValue("Content-Type"),\r
1023                                                    Bytes = Convert.ToInt64(response.Content.Headers.ContentLength),\r
1024                                                    Tags = tags,\r
1025                                                    Last_Modified = response.Content.Headers.LastModified,\r
1026                                                    Extensions = extensions,\r
1027                                                    ContentEncoding =\r
1028                                                        response.Content.Headers.ContentEncoding.FirstOrDefault(),\r
1029                                                    ContendDisposition =\r
1030                                                        response.Content.Headers.ContentDisposition.ToString(),\r
1031                                                    Manifest = response.Headers.GetFirstValue("X-Object-Manifest"),\r
1032                                                    PublicUrl = response.Headers.GetFirstValue("X-Object-Public"),\r
1033                                                    StorageUri = StorageUrl,\r
1034                                                };\r
1035                                 info.SetPermissions(permissions);\r
1036                                 return info;\r
1037                             case HttpStatusCode.NotFound:\r
1038                                 return ObjectInfo.Empty;\r
1039                             default:\r
1040                                 throw new WebException(\r
1041                                     String.Format("[FAIL] GetObjectInfo for {0} failed with unexpected status code {1}",\r
1042                                                   objectName, response.StatusCode));\r
1043                         }\r
1044                     }\r
1045                 }\r
1046                 catch (RetryException)\r
1047                 {\r
1048                     Log.WarnFormat("[RETRY FAIL] GetObjectInfo for {0} failed.", objectName);\r
1049                     return ObjectInfo.Empty;\r
1050                 }\r
1051                 catch (WebException e)\r
1052                 {\r
1053                     Log.Error(\r
1054                         String.Format("[FAIL] GetObjectInfo for {0} failed with unexpected status {1}",\r
1055                                       objectName, e.Status), e);\r
1056                     throw;\r
1057                 }\r
1058             } /* using (var client = new RestClient(_baseClient))\r
1059                 {\r
1060                     if (!String.IsNullOrWhiteSpace(account))\r
1061                         client.BaseAddress = GetAccountUrl(account);\r
1062                     try\r
1063                     {\r
1064                         client.Parameters.Clear();\r
1065 \r
1066                         client.Head(container.Combine(objectName), 3);\r
1067 \r
1068                         if (client.TimedOut)\r
1069                             return ObjectInfo.Empty;\r
1070 \r
1071                         switch (client.StatusCode)\r
1072                         {\r
1073                             case HttpStatusCode.OK:\r
1074                             case HttpStatusCode.NoContent:\r
1075                                 var keys = client.ResponseHeaders.AllKeys.AsQueryable();\r
1076                                 var tags = client.GetMeta("X-Object-Meta-");\r
1077                                 var extensions = (from key in keys\r
1078                                                   where key.StartsWith("X-Object-") && !key.StartsWith("X-Object-Meta-")\r
1079                                                   select new {Name = key, Value = client.ResponseHeaders[key]})\r
1080                                     .ToDictionary(t => t.Name, t => t.Value);\r
1081 \r
1082                                 var permissions=client.GetHeaderValue("X-Object-Sharing", true);\r
1083                                 \r
1084                                 \r
1085                                 var info = new ObjectInfo\r
1086                                                {\r
1087                                                    Account = account,\r
1088                                                    Container = container,\r
1089                                                    Name = objectName,\r
1090                                                    ETag = client.GetHeaderValue("ETag"),\r
1091                                                    UUID=client.GetHeaderValue("X-Object-UUID"),\r
1092                                                    X_Object_Hash = client.GetHeaderValue("X-Object-Hash"),\r
1093                                                    Content_Type = client.GetHeaderValue("Content-Type"),\r
1094                                                    Bytes = Convert.ToInt64(client.GetHeaderValue("Content-Length",true)),\r
1095                                                    Tags = tags,\r
1096                                                    Last_Modified = client.LastModified,\r
1097                                                    Extensions = extensions,\r
1098                                                    ContentEncoding=client.GetHeaderValue("Content-Encoding",true),\r
1099                                                    ContendDisposition = client.GetHeaderValue("Content-Disposition",true),\r
1100                                                    Manifest=client.GetHeaderValue("X-Object-Manifest",true),\r
1101                                                    PublicUrl=client.GetHeaderValue("X-Object-Public",true),  \r
1102                                                    StorageUri=StorageUrl,\r
1103                                                };\r
1104                                 info.SetPermissions(permissions);\r
1105                                 return info;\r
1106                             case HttpStatusCode.NotFound:\r
1107                                 return ObjectInfo.Empty;\r
1108                             default:\r
1109                                 throw new WebException(\r
1110                                     String.Format("[FAIL] GetObjectInfo for {0} failed with unexpected status code {1}",\r
1111                                                   objectName, client.StatusCode));\r
1112                         }\r
1113 \r
1114                     }\r
1115                     catch (RetryException)\r
1116                     {\r
1117                         Log.WarnFormat("[RETRY FAIL] GetObjectInfo for {0} failed.",objectName);\r
1118                         return ObjectInfo.Empty;\r
1119                     }\r
1120                     catch (WebException e)\r
1121                     {\r
1122                         Log.Error(\r
1123                             String.Format("[FAIL] GetObjectInfo for {0} failed with unexpected status code {1}",\r
1124                                           objectName, client.StatusCode), e);\r
1125                         throw;\r
1126                     }\r
1127                 } */\r
1128 \r
1129 \r
1130         }\r
1131 \r
1132 \r
1133 \r
1134         public void CreateFolder(string account, Uri container, Uri folder)\r
1135         {\r
1136             if (container == null)\r
1137                 throw new ArgumentNullException("container", "The container property can't be empty");\r
1138             if (container.IsAbsoluteUri)\r
1139                 throw new ArgumentException("The container must be relative","container");\r
1140             if (folder == null)\r
1141                 throw new ArgumentNullException("folder", "The objectName property can't be empty");\r
1142             if (folder.IsAbsoluteUri)\r
1143                 throw new ArgumentException("The objectName must be relative","folder");\r
1144             Contract.EndContractBlock();\r
1145 \r
1146             var folderUri=container.Combine(folder);            \r
1147             var targetUri = GetTargetUri(account).Combine(folderUri);\r
1148             var message = new HttpRequestMessage(HttpMethod.Put, targetUri);\r
1149             \r
1150             message.Headers.Add("Content-Type", ObjectInfo.CONTENT_TYPE_DIRECTORY);\r
1151             message.Headers.Add("Content-Length", "0");\r
1152             using (var response = _baseHttpClient.SendAsyncWithRetries(message, 3).Result)\r
1153             {\r
1154                 if (response.StatusCode != HttpStatusCode.Created && response.StatusCode != HttpStatusCode.Accepted)\r
1155                     throw CreateWebException("CreateFolder", response.StatusCode);\r
1156             }\r
1157 /*\r
1158             using (var client = new RestClient(_baseClient))\r
1159             {\r
1160                 if (!String.IsNullOrWhiteSpace(account))\r
1161                     client.BaseAddress = GetAccountUrl(account);\r
1162 \r
1163                 client.Parameters.Clear();\r
1164                 client.Headers.Add("Content-Type", ObjectInfo.CONTENT_TYPE_DIRECTORY);\r
1165                 client.Headers.Add("Content-Length", "0");\r
1166                 client.PutWithRetry(folderUri, 3);\r
1167 \r
1168                 if (client.StatusCode != HttpStatusCode.Created && client.StatusCode != HttpStatusCode.Accepted)\r
1169                     throw CreateWebException("CreateFolder", client.StatusCode);\r
1170             }\r
1171 */\r
1172         }\r
1173 \r
1174         private Dictionary<string, string> GetMeta(HttpResponseMessage response,string metaPrefix)\r
1175         {\r
1176             if (String.IsNullOrWhiteSpace(metaPrefix))\r
1177                 throw new ArgumentNullException("metaPrefix");\r
1178             Contract.EndContractBlock();\r
1179 \r
1180             var dict = (from header in response.Headers\r
1181                         where header.Key.StartsWith(metaPrefix)\r
1182                          select new { Name = header.Key, Value = String.Join(",", header.Value) })\r
1183                         .ToDictionary(t => t.Name, t => t.Value);\r
1184 \r
1185           \r
1186             return dict;\r
1187         }\r
1188 \r
1189 \r
1190         public ContainerInfo GetContainerInfo(string account, Uri container)\r
1191         {\r
1192             if (container == null)\r
1193                 throw new ArgumentNullException("container", "The container property can't be empty");\r
1194             if (container.IsAbsoluteUri)\r
1195                 throw new ArgumentException("The container must be relative","container");\r
1196             Contract.EndContractBlock();\r
1197 \r
1198             var targetUri = GetTargetUri(account).Combine(container);            \r
1199             using (var response = _baseHttpClient.HeadAsyncWithRetries(targetUri, 3).Result)\r
1200             {\r
1201                 if (Log.IsDebugEnabled)\r
1202                     Log.DebugFormat("ContainerInfo data: {0}\n{1}",response,response.Content.ReadAsStringAsync().Result);\r
1203                 switch (response.StatusCode)\r
1204                 {\r
1205                     case HttpStatusCode.OK:\r
1206                     case HttpStatusCode.NoContent:\r
1207                         var tags = GetMeta(response,"X-Container-Meta-");\r
1208                         var policies = GetMeta(response,"X-Container-Policy-");\r
1209 \r
1210                         var containerInfo = new ContainerInfo\r
1211                                                 {\r
1212                                                     Account = account,\r
1213                                                     Name = container,\r
1214                                                     StorageUrl = StorageUrl.ToString(),\r
1215                                                     Count =long.Parse(response.Headers.GetFirstValue("X-Container-Object-Count")),\r
1216                                                     Bytes = long.Parse(response.Headers.GetFirstValue("X-Container-Bytes-Used")),\r
1217                                                     BlockHash = response.Headers.GetFirstValue("X-Container-Block-Hash"),\r
1218                                                     BlockSize =\r
1219                                                         int.Parse(response.Headers.GetFirstValue("X-Container-Block-Size")),\r
1220                                                     Last_Modified = response.Content.Headers.LastModified,\r
1221                                                     Tags = tags,\r
1222                                                     Policies = policies\r
1223                                                 };\r
1224 \r
1225 \r
1226                         return containerInfo;\r
1227                     case HttpStatusCode.NotFound:\r
1228                         return ContainerInfo.Empty;\r
1229                     default:\r
1230                         throw CreateWebException("GetContainerInfo", response.StatusCode);\r
1231                 }\r
1232             }            \r
1233         }\r
1234 \r
1235         public void CreateContainer(string account, Uri container)\r
1236         {\r
1237             if (container == null)\r
1238                 throw new ArgumentNullException("container", "The container property can't be empty");\r
1239             if (container.IsAbsoluteUri)\r
1240                 throw new ArgumentException("The container must be relative","container");\r
1241             Contract.EndContractBlock();\r
1242 \r
1243             var targetUri=GetTargetUri(account).Combine(container);\r
1244             var message = new HttpRequestMessage(HttpMethod.Put, targetUri);\r
1245             message.Headers.Add("Content-Length", "0");\r
1246             using (var response = _baseHttpClient.SendAsyncWithRetries(message, 3).Result)\r
1247             {            \r
1248                 var expectedCodes = new[] {HttpStatusCode.Created, HttpStatusCode.Accepted, HttpStatusCode.OK};\r
1249                 if (!expectedCodes.Contains(response.StatusCode))\r
1250                     throw CreateWebException("CreateContainer", response.StatusCode);\r
1251             }\r
1252 /*\r
1253             using (var client = new RestClient(_baseClient))\r
1254             {\r
1255                 if (!String.IsNullOrWhiteSpace(account))\r
1256                     client.BaseAddress = GetAccountUrl(account);\r
1257 \r
1258                 client.PutWithRetry(container, 3);\r
1259                 var expectedCodes = new[] {HttpStatusCode.Created, HttpStatusCode.Accepted, HttpStatusCode.OK};\r
1260                 if (!expectedCodes.Contains(client.StatusCode))\r
1261                     throw CreateWebException("CreateContainer", client.StatusCode);\r
1262             }\r
1263 */\r
1264         }\r
1265 \r
1266         public async Task WipeContainer(string account, Uri container)\r
1267         {\r
1268             if (container == null)\r
1269                 throw new ArgumentNullException("container", "The container property can't be empty");\r
1270             if (container.IsAbsoluteUri)\r
1271                 throw new ArgumentException("The container must be relative", "container");\r
1272             Contract.EndContractBlock();\r
1273 \r
1274             await DeleteContainer(account, new Uri(String.Format("{0}?delimiter=/", container), UriKind.Relative)).ConfigureAwait(false);\r
1275         }\r
1276 \r
1277 \r
1278         public async Task DeleteContainer(string account, Uri container)\r
1279         {\r
1280             if (container == null)\r
1281                 throw new ArgumentNullException("container", "The container property can't be empty");\r
1282             if (container.IsAbsoluteUri)\r
1283                 throw new ArgumentException("The container must be relative","container");\r
1284             Contract.EndContractBlock();\r
1285 \r
1286             var targetUri = GetTargetUri(account).Combine(container);\r
1287             var message = new HttpRequestMessage(HttpMethod.Delete, targetUri);\r
1288             using (var response = await _baseHttpClient.SendAsyncWithRetries(message, 3).ConfigureAwait(false))\r
1289             {\r
1290                 var expectedCodes = new[] { HttpStatusCode.NotFound, HttpStatusCode.NoContent };\r
1291                 if (!expectedCodes.Contains(response.StatusCode))\r
1292                     throw CreateWebException("DeleteContainer", response.StatusCode);\r
1293             }\r
1294 \r
1295         }\r
1296 \r
1297         /// <summary>\r
1298         /// \r
1299         /// </summary>\r
1300         /// <param name="account"></param>\r
1301         /// <param name="container"></param>\r
1302         /// <param name="objectName"></param>\r
1303         /// <param name="fileName"></param>\r
1304         /// <param name="cancellationToken"> </param>\r
1305         /// <returns></returns>\r
1306         /// <remarks>This method should have no timeout or a very long one</remarks>\r
1307         //Asynchronously download the object specified by *objectName* in a specific *container* to \r
1308         // a local file\r
1309         public async Task GetObject(string account, Uri container, Uri objectName, string fileName,CancellationToken cancellationToken)\r
1310         {\r
1311             if (container == null)\r
1312                 throw new ArgumentNullException("container", "The container property can't be empty");\r
1313             if (container.IsAbsoluteUri)\r
1314                 throw new ArgumentException("The container must be relative","container");\r
1315             if (objectName == null)\r
1316                 throw new ArgumentNullException("objectName", "The objectName property can't be empty");\r
1317             if (objectName.IsAbsoluteUri)\r
1318                 throw new ArgumentException("The objectName must be relative","objectName");\r
1319             Contract.EndContractBlock();\r
1320                         \r
1321 \r
1322             try\r
1323             {\r
1324                 //WebClient, and by extension RestClient, are not thread-safe. Create a new RestClient\r
1325                 //object to avoid concurrency errors.\r
1326                 //\r
1327                 //Download operations take a long time therefore they have no timeout.\r
1328                 using(var client = new RestClient(_baseClient) { Timeout = 0 })\r
1329                 {\r
1330                     if (!String.IsNullOrWhiteSpace(account))\r
1331                         client.BaseAddress = GetAccountUrl(account);\r
1332 \r
1333                     //The container and objectName are relative names. They are joined with the client's\r
1334                     //BaseAddress to create the object's absolute address\r
1335                     var builder = client.GetAddressBuilder(container, objectName);\r
1336                     var uri = builder.Uri;\r
1337 \r
1338                     //Download progress is reported to the Trace log\r
1339                     Log.InfoFormat("[GET] START {0}", objectName);\r
1340                     /*client.DownloadProgressChanged += (sender, args) =>\r
1341                                                       Log.InfoFormat("[GET PROGRESS] {0} {1}% {2} of {3}",\r
1342                                                                      fileName, args.ProgressPercentage,\r
1343                                                                      args.BytesReceived,\r
1344                                                                      args.TotalBytesToReceive);*/\r
1345                     var progress = new Progress<DownloadProgressChangedEventArgs>(args =>\r
1346                                 {\r
1347                                     Log.InfoFormat("[GET PROGRESS] {0} {1}% {2} of {3}",\r
1348                                                    fileName, args.ProgressPercentage,\r
1349                                                    args.BytesReceived,\r
1350                                                    args.TotalBytesToReceive);\r
1351                                     if (DownloadProgressChanged!=null)\r
1352                                         DownloadProgressChanged(this, new DownloadArgs(args));\r
1353                                 });\r
1354                     \r
1355                     //Start downloading the object asynchronously                    \r
1356                     await client.DownloadFileTaskAsync(uri, fileName, cancellationToken,progress).ConfigureAwait(false);\r
1357 \r
1358                     //Once the download completes\r
1359                     //Delete the local client object\r
1360                 }\r
1361                 //And report failure or completion\r
1362             }\r
1363             catch (Exception exc)\r
1364             {\r
1365                 Log.ErrorFormat("[GET] FAIL {0} with {1}", objectName, exc);\r
1366                 throw;\r
1367             }\r
1368 \r
1369             Log.InfoFormat("[GET] END {0}", objectName);                                             \r
1370 \r
1371 \r
1372         }\r
1373 \r
1374         public async Task<IList<string>> PutHashMap(string account, Uri container, Uri objectName, TreeHash hash)\r
1375         {\r
1376             if (container == null)\r
1377                 throw new ArgumentNullException("container", "The container property can't be empty");\r
1378             if (container.IsAbsoluteUri)\r
1379                 throw new ArgumentException("The container must be relative","container");\r
1380             if (objectName == null)\r
1381                 throw new ArgumentNullException("objectName", "The objectName property can't be empty");\r
1382             if (objectName.IsAbsoluteUri)\r
1383                 throw new ArgumentException("The objectName must be relative","objectName");\r
1384             if (hash == null)\r
1385                 throw new ArgumentNullException("hash");\r
1386             if (String.IsNullOrWhiteSpace(Token))\r
1387                 throw new InvalidOperationException("Invalid Token");\r
1388             if (StorageUrl == null)\r
1389                 throw new InvalidOperationException("Invalid Storage Url");\r
1390             Contract.EndContractBlock();\r
1391 \r
1392             \r
1393 \r
1394             //The container and objectName are relative names. They are joined with the client's\r
1395             //BaseAddress to create the object's absolute address\r
1396 \r
1397             var targetUri = GetTargetUri(account).Combine(container).Combine(objectName);\r
1398   \r
1399 \r
1400             var uri = new Uri(String.Format("{0}?format=json&hashmap",targetUri),UriKind.Absolute);\r
1401 \r
1402             \r
1403             //Send the tree hash as Json to the server            \r
1404             var jsonHash = hash.ToJson();\r
1405             if (Log.IsDebugEnabled)\r
1406                 Log.DebugFormat("Hashes:\r\n{0}", jsonHash);\r
1407 \r
1408             var message = new HttpRequestMessage(HttpMethod.Put, uri)\r
1409             {\r
1410                 Content = new StringContent(jsonHash)\r
1411             };\r
1412             message.Headers.Add("ETag",hash.TopHash.ToHashString());\r
1413             \r
1414             //Don't use a timeout because putting the hashmap may be a long process\r
1415 \r
1416             using (var response = await _baseHttpClientNoTimeout.SendAsyncWithRetries(message, 3).ConfigureAwait(false))\r
1417             {\r
1418                 var empty = (IList<string>)new List<string>();\r
1419                 \r
1420                 switch (response.StatusCode)\r
1421                 {\r
1422                     case HttpStatusCode.Created:\r
1423                         //The server will respond either with 201-created if all blocks were already on the server\r
1424                         return empty;\r
1425                     case HttpStatusCode.Conflict:\r
1426                         //or with a 409-conflict and return the list of missing parts\r
1427                         using (var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false))\r
1428                         using(var reader=stream.GetLoggedReader(Log))\r
1429                         {                            \r
1430                             var serializer = new JsonSerializer();                            \r
1431                             serializer.Error += (sender, args) => Log.ErrorFormat("Deserialization error at [{0}] [{1}]", args.ErrorContext.Error, args.ErrorContext.Member);\r
1432                             var hashes = (List<string>)serializer.Deserialize(reader, typeof(List<string>));\r
1433                             return hashes;\r
1434                         }                        \r
1435                     default:\r
1436                         //All other cases are unexpected\r
1437                         //Ensure that failure codes raise exceptions\r
1438                         response.EnsureSuccessStatusCode();\r
1439                         //And log any other codes as warngings, but continute processing\r
1440                         Log.WarnFormat("Unexcpected status code when putting map: {0} - {1}",response.StatusCode,response.ReasonPhrase);\r
1441                         return empty;\r
1442                 }\r
1443             }\r
1444 \r
1445         }\r
1446 \r
1447 \r
1448         public async Task<byte[]> GetBlock(string account, Uri container, Uri relativeUrl, long start, long? end, CancellationToken cancellationToken)\r
1449         {\r
1450             if (String.IsNullOrWhiteSpace(Token))\r
1451                 throw new InvalidOperationException("Invalid Token");\r
1452             if (StorageUrl == null)\r
1453                 throw new InvalidOperationException("Invalid Storage Url");\r
1454             if (container == null)\r
1455                 throw new ArgumentNullException("container", "The container property can't be empty");\r
1456             if (container.IsAbsoluteUri)\r
1457                 throw new ArgumentException("The container must be relative","container");\r
1458             if (relativeUrl == null)\r
1459                 throw new ArgumentNullException("relativeUrl");\r
1460             if (end.HasValue && end < 0)\r
1461                 throw new ArgumentOutOfRangeException("end");\r
1462             if (start < 0)\r
1463                 throw new ArgumentOutOfRangeException("start");\r
1464             Contract.EndContractBlock();\r
1465 \r
1466 \r
1467             var targetUri = GetTargetUri(account).Combine(container).Combine(relativeUrl);\r
1468             var message = new HttpRequestMessage(HttpMethod.Get, targetUri);\r
1469             message.Headers.Range=new RangeHeaderValue(start,end);\r
1470 \r
1471             //Don't use a timeout because putting the hashmap may be a long process\r
1472 \r
1473             IProgress<DownloadArgs> progress = new Progress<DownloadArgs>(args =>\r
1474                 {\r
1475                     Log.DebugFormat("[GET PROGRESS] {0} {1}% {2} of {3}",\r
1476                                     targetUri.Segments.Last(), args.ProgressPercentage,\r
1477                                     args.BytesReceived,\r
1478                                     args.TotalBytesToReceive);\r
1479 \r
1480                     if (DownloadProgressChanged!=null)\r
1481                         DownloadProgressChanged(this,  args);\r
1482                 });\r
1483 \r
1484 \r
1485             using (var response = await _baseHttpClientNoTimeout.SendAsyncWithRetries(message, 3, HttpCompletionOption.ResponseHeadersRead,\r
1486                                                           cancellationToken).ConfigureAwait(false))\r
1487             using (var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false))\r
1488             using(var targetStream=new MemoryStream())\r
1489             {\r
1490                 \r
1491                 long totalSize = response.Content.Headers.ContentLength ?? 0;\r
1492                 long total = 0;\r
1493                 var buffer = new byte[65536];\r
1494                 int read;\r
1495                 while ((read = await stream.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false)) != 0)\r
1496                 {\r
1497                     total += read;\r
1498                     progress.Report(new DownloadArgs(total, totalSize));\r
1499                     await targetStream.WriteAsync(buffer, 0, read).ConfigureAwait(false);\r
1500                 }\r
1501 \r
1502                 var result = targetStream.ToArray();\r
1503                 return result;\r
1504             }\r
1505        \r
1506         }\r
1507 \r
1508         public event EventHandler<UploadArgs> UploadProgressChanged;\r
1509         public event EventHandler<DownloadArgs> DownloadProgressChanged;\r
1510         \r
1511 \r
1512         public async Task PostBlock(string account, Uri container, byte[] block, int offset, int count,string blockHash,CancellationToken token)\r
1513         {\r
1514             if (container == null)\r
1515                 throw new ArgumentNullException("container", "The container property can't be empty");\r
1516             if (container.IsAbsoluteUri)\r
1517                 throw new ArgumentException("The container must be relative","container");\r
1518             if (block == null)\r
1519                 throw new ArgumentNullException("block");\r
1520             if (offset < 0 || offset >= block.Length)\r
1521                 throw new ArgumentOutOfRangeException("offset");\r
1522             if (count < 0 || count > block.Length)\r
1523                 throw new ArgumentOutOfRangeException("count");\r
1524             if (String.IsNullOrWhiteSpace(Token))\r
1525                 throw new InvalidOperationException("Invalid Token");\r
1526             if (StorageUrl == null)\r
1527                 throw new InvalidOperationException("Invalid Storage Url");                        \r
1528             Contract.EndContractBlock();\r
1529 \r
1530 \r
1531             try\r
1532             {\r
1533                 var containerUri = GetTargetUri(account).Combine(container);\r
1534                 var targetUri = new Uri(String.Format("{0}?update", containerUri));\r
1535 \r
1536 \r
1537                 //Don't use a timeout because putting the hashmap may be a long process\r
1538 \r
1539 \r
1540                 Log.InfoFormat("[BLOCK POST] START");\r
1541 \r
1542 \r
1543                 var progress = new Progress<UploadArgs>(args =>\r
1544                 {\r
1545                     Log.InfoFormat("[BLOCK POST PROGRESS] {0}% {1} of {2}",\r
1546                         args.ProgressPercentage,\r
1547                         args.BytesSent,\r
1548                         args.TotalBytesToSend);\r
1549                     if (UploadProgressChanged != null)\r
1550                         UploadProgressChanged(this,args);\r
1551                 });\r
1552 \r
1553                 var message = new HttpRequestMessage(HttpMethod.Post, targetUri)\r
1554                                   {\r
1555                                       Content = new ByteArrayContentWithProgress(block, offset, count,progress)\r
1556                                   };\r
1557                 message.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(@"application/octet-stream");\r
1558 \r
1559                 //Send the block\r
1560                 using (var response = await _baseHttpClientNoTimeout.SendAsyncWithRetries(message, 3,HttpCompletionOption.ResponseContentRead,token).ConfigureAwait(false))\r
1561                 {                    \r
1562                     Log.InfoFormat("[BLOCK POST PROGRESS] Completed ");\r
1563                     response.EnsureSuccessStatusCode();\r
1564                     var responseHash = await response.Content.ReadAsStringAsync().ConfigureAwait(false);\r
1565                     var cleanHash = responseHash.TrimEnd();\r
1566                     Debug.Assert(blockHash==cleanHash);\r
1567                     if (responseHash.Equals(cleanHash,StringComparison.OrdinalIgnoreCase))\r
1568                         Log.ErrorFormat("Block hash mismatch posting to [{0}]:[{1}], expected [{2}] but was [{3}]",account,container,blockHash,responseHash);\r
1569                 }\r
1570                 Log.InfoFormat("[BLOCK POST] END");               \r
1571             }\r
1572             catch (TaskCanceledException )\r
1573             {\r
1574                 Log.Info("Aborting block");\r
1575                 throw;\r
1576             }\r
1577             catch (Exception exc)\r
1578             {\r
1579                 Log.ErrorFormat("[BLOCK POST] FAIL with \r{0}", exc);\r
1580                 throw;\r
1581             }\r
1582         }\r
1583 \r
1584 \r
1585         public async Task<TreeHash> GetHashMap(string account, Uri container, Uri objectName)\r
1586         {\r
1587             if (container == null)\r
1588                 throw new ArgumentNullException("container", "The container property can't be empty");\r
1589             if (container.IsAbsoluteUri)\r
1590                 throw new ArgumentException("The container must be relative","container");\r
1591             if (objectName == null)\r
1592                 throw new ArgumentNullException("objectName", "The objectName property can't be empty");\r
1593             if (objectName.IsAbsoluteUri)\r
1594                 throw new ArgumentException("The objectName must be relative","objectName");\r
1595             if (String.IsNullOrWhiteSpace(Token))\r
1596                 throw new InvalidOperationException("Invalid Token");\r
1597             if (StorageUrl == null)\r
1598                 throw new InvalidOperationException("Invalid Storage Url");\r
1599             Contract.EndContractBlock();\r
1600 \r
1601             try\r
1602             {\r
1603 \r
1604                 var objectUri = GetTargetUri(account).Combine(container).Combine(objectName);\r
1605                 var targetUri = new Uri(String.Format("{0}?format=json&hashmap", objectUri));\r
1606 \r
1607                 //Start downloading the object asynchronously\r
1608                 var json = await GetStringAsync(targetUri, "").ConfigureAwait(false);\r
1609                 var treeHash = TreeHash.Parse(json);\r
1610                 Log.InfoFormat("[GET HASH] END {0}", objectName);\r
1611                 return treeHash;\r
1612 \r
1613             }\r
1614             catch (Exception exc)\r
1615             {\r
1616                 Log.ErrorFormat("[GET HASH] END {0} with {1}", objectName, exc);\r
1617                 throw;\r
1618             }\r
1619 \r
1620         }\r
1621 \r
1622 \r
1623         /// <summary>\r
1624         /// \r
1625         /// </summary>\r
1626         /// <param name="account"></param>\r
1627         /// <param name="container"></param>\r
1628         /// <param name="objectName"></param>\r
1629         /// <param name="fileName"></param>\r
1630         /// <param name="hash">Optional hash value for the file. If no hash is provided, the method calculates a new hash</param>\r
1631         /// <param name="contentType"> </param>\r
1632         /// <remarks>>This method should have no timeout or a very long one</remarks>\r
1633         public async Task PutObject(string account, Uri container, Uri objectName, string fileName, string hash = Signature.MERKLE_EMPTY, string contentType = "application/octet-stream")\r
1634         {\r
1635             if (container == null)\r
1636                 throw new ArgumentNullException("container", "The container property can't be empty");\r
1637             if (container.IsAbsoluteUri)\r
1638                 throw new ArgumentException("The container must be relative","container");\r
1639             if (objectName == null)\r
1640                 throw new ArgumentNullException("objectName", "The objectName property can't be empty");\r
1641             if (objectName.IsAbsoluteUri)\r
1642                 throw new ArgumentException("The objectName must be relative","objectName");\r
1643             if (String.IsNullOrWhiteSpace(fileName))\r
1644                 throw new ArgumentNullException("fileName", "The fileName property can't be empty");\r
1645             try\r
1646             {\r
1647 \r
1648                 using (var client = new RestClient(_baseClient) { Timeout = 0 })\r
1649                 {\r
1650                     if (!String.IsNullOrWhiteSpace(account))\r
1651                         client.BaseAddress = GetAccountUrl(account);\r
1652 \r
1653                     var builder = client.GetAddressBuilder(container, objectName);\r
1654                     var uri = builder.Uri;\r
1655 \r
1656                     string etag = hash ;\r
1657 \r
1658                     client.Headers.Add("Content-Type", contentType);\r
1659                     if (contentType!=ObjectInfo.CONTENT_TYPE_DIRECTORY)\r
1660                         client.Headers.Add("ETag", etag);\r
1661 \r
1662 \r
1663                     Log.InfoFormat("[PUT] START {0}", objectName);\r
1664                     client.UploadProgressChanged += (sender, args) =>\r
1665                                                         {\r
1666                                                             using (ThreadContext.Stacks["PUT"].Push("Progress"))\r
1667                                                             {\r
1668                                                                 Log.InfoFormat("{0} {1}% {2} of {3}", fileName,\r
1669                                                                                args.ProgressPercentage,\r
1670                                                                                args.BytesSent, args.TotalBytesToSend);\r
1671                                                             }\r
1672                                                         };\r
1673 \r
1674                     client.UploadFileCompleted += (sender, args) =>\r
1675                                                       {\r
1676                                                           using (ThreadContext.Stacks["PUT"].Push("Progress"))\r
1677                                                           {\r
1678                                                               Log.InfoFormat("Completed {0}", fileName);\r
1679                                                           }\r
1680                                                       }; \r
1681                     \r
1682                     if (contentType==ObjectInfo.CONTENT_TYPE_DIRECTORY)\r
1683                         await client.UploadDataTaskAsync(uri, "PUT", new byte[0]).ConfigureAwait(false);\r
1684                     else\r
1685                         await client.UploadFileTaskAsync(uri, "PUT", fileName).ConfigureAwait(false);\r
1686                 }\r
1687 \r
1688                 Log.InfoFormat("[PUT] END {0}", objectName);\r
1689             }\r
1690             catch (Exception exc)\r
1691             {\r
1692                 Log.ErrorFormat("[PUT] END {0} with {1}", objectName, exc);\r
1693                 throw;\r
1694             }                \r
1695 \r
1696         }\r
1697         \r
1698         public void MoveObject(string account, Uri sourceContainer, Uri oldObjectName, Uri targetContainer, Uri newObjectName)\r
1699         {\r
1700             if (sourceContainer == null)\r
1701                 throw new ArgumentNullException("sourceContainer", "The sourceContainer property can't be empty");\r
1702             if (sourceContainer.IsAbsoluteUri)\r
1703                 throw new ArgumentException("The sourceContainer must be relative","sourceContainer");\r
1704             if (oldObjectName == null)\r
1705                 throw new ArgumentNullException("oldObjectName", "The oldObjectName property can't be empty");\r
1706             if (oldObjectName.IsAbsoluteUri)\r
1707                 throw new ArgumentException("The oldObjectName must be relative","oldObjectName");\r
1708             if (targetContainer == null)\r
1709                 throw new ArgumentNullException("targetContainer", "The targetContainer property can't be empty");\r
1710             if (targetContainer.IsAbsoluteUri)\r
1711                 throw new ArgumentException("The targetContainer must be relative","targetContainer");\r
1712             if (newObjectName == null)\r
1713                 throw new ArgumentNullException("newObjectName", "The newObjectName property can't be empty");\r
1714             if (newObjectName.IsAbsoluteUri)\r
1715                 throw new ArgumentException("The newObjectName must be relative","newObjectName");\r
1716             Contract.EndContractBlock();\r
1717 \r
1718             var baseUri = GetTargetUri(account);\r
1719             var targetUri = baseUri.Combine(targetContainer).Combine(newObjectName);\r
1720             var sourceUri = new Uri(String.Format("/{0}/{1}", sourceContainer, oldObjectName),UriKind.Relative);\r
1721 \r
1722             var message = new HttpRequestMessage(HttpMethod.Put, targetUri);\r
1723             message.Headers.Add("X-Move-From", sourceUri.ToString());\r
1724             using (var response = _baseHttpClient.SendAsyncWithRetries(message, 3).Result)\r
1725             {\r
1726                 var expectedCodes = new[] {HttpStatusCode.OK, HttpStatusCode.NoContent, HttpStatusCode.Created};\r
1727                 if (!expectedCodes.Contains(response.StatusCode))\r
1728                     throw CreateWebException("MoveObject", response.StatusCode);\r
1729             }\r
1730         }\r
1731 \r
1732         public void DeleteObject(string account, Uri sourceContainer, Uri objectName, bool isDirectory)\r
1733         {\r
1734             if (sourceContainer == null)\r
1735                 throw new ArgumentNullException("sourceContainer", "The sourceContainer property can't be empty");\r
1736             if (sourceContainer.IsAbsoluteUri)\r
1737                 throw new ArgumentException("The sourceContainer must be relative","sourceContainer");\r
1738             if (objectName == null)\r
1739                 throw new ArgumentNullException("objectName", "The objectName property can't be empty");\r
1740             if (objectName.IsAbsoluteUri)\r
1741                 throw new ArgumentException("The objectName must be relative","objectName");\r
1742             Contract.EndContractBlock();\r
1743 \r
1744             var targetUrl = FolderConstants.TrashContainer + "/" + objectName;\r
1745 /*\r
1746             if (isDirectory)\r
1747                 targetUrl = targetUrl + "?delimiter=/";\r
1748 */\r
1749 \r
1750             var sourceUrl = String.Format("/{0}/{1}", sourceContainer, objectName);\r
1751 \r
1752             using (var client = new RestClient(_baseClient))\r
1753             {\r
1754                 if (!String.IsNullOrWhiteSpace(account))\r
1755                     client.BaseAddress = GetAccountUrl(account);\r
1756 \r
1757                 client.Headers.Add("X-Move-From", sourceUrl);\r
1758                 client.AllowedStatusCodes.Add(HttpStatusCode.NotFound);\r
1759                 Log.InfoFormat("[TRASH] [{0}] to [{1}]",sourceUrl,targetUrl);\r
1760                 client.PutWithRetry(new Uri(targetUrl,UriKind.Relative), 3);\r
1761 \r
1762                 var expectedCodes = new[] {HttpStatusCode.OK, HttpStatusCode.NoContent, HttpStatusCode.Created,HttpStatusCode.NotFound};\r
1763                 if (!expectedCodes.Contains(client.StatusCode))\r
1764                     throw CreateWebException("DeleteObject", client.StatusCode);\r
1765             }\r
1766         }\r
1767 \r
1768       \r
1769         private static WebException CreateWebException(string operation, HttpStatusCode statusCode)\r
1770         {\r
1771             return new WebException(String.Format("{0} failed with unexpected status code {1}", operation, statusCode));\r
1772         }\r
1773 \r
1774 \r
1775         public bool CanUpload(string account, ObjectInfo cloudFile)\r
1776         {\r
1777             Contract.Requires(!String.IsNullOrWhiteSpace(account));\r
1778             Contract.Requires(cloudFile!=null);\r
1779 \r
1780             using (var client = new RestClient(_baseClient))\r
1781             {\r
1782                 if (!String.IsNullOrWhiteSpace(account))\r
1783                     client.BaseAddress = GetAccountUrl(account);\r
1784 \r
1785 \r
1786                 var parts = cloudFile.Name.ToString().Split('/');\r
1787                 var folder = String.Join("/", parts,0,parts.Length-1);\r
1788 \r
1789                 var fileName = String.Format("{0}/{1}.pithos.ignore", folder, Guid.NewGuid());\r
1790                 var fileUri=fileName.ToEscapedUri();                                            \r
1791 \r
1792                 client.Parameters.Clear();\r
1793                 try\r
1794                 {\r
1795                     var relativeUri = cloudFile.Container.Combine(fileUri);\r
1796                     client.PutWithRetry(relativeUri, 3, @"application/octet-stream");\r
1797 \r
1798                     var expectedCodes = new[] { HttpStatusCode.OK, HttpStatusCode.NoContent, HttpStatusCode.Created};\r
1799                     var result=(expectedCodes.Contains(client.StatusCode));\r
1800                     DeleteObject(account, cloudFile.Container, fileUri, cloudFile.IsDirectory);\r
1801                     return result;\r
1802                 }\r
1803                 catch\r
1804                 {\r
1805                     return false;\r
1806                 }\r
1807             }\r
1808         }\r
1809 \r
1810         ~CloudFilesClient()\r
1811         {\r
1812             Dispose(false);\r
1813         }\r
1814 \r
1815         public void Dispose()\r
1816         {\r
1817             Dispose(true);\r
1818             GC.SuppressFinalize(this);\r
1819         }\r
1820 \r
1821         protected virtual void Dispose(bool disposing)\r
1822         {\r
1823             if (disposing)\r
1824             {\r
1825                 if (_httpClientHandler!=null)\r
1826                     _httpClientHandler.Dispose();\r
1827                 if (_baseClient!=null)\r
1828                     _baseClient.Dispose();\r
1829                 if(_baseHttpClient!=null)\r
1830                     _baseHttpClient.Dispose();\r
1831                 if (_baseHttpClientNoTimeout!=null)\r
1832                     _baseHttpClientNoTimeout.Dispose();\r
1833             }\r
1834             _httpClientHandler = null;\r
1835             _baseClient = null;\r
1836             _baseHttpClient = null;\r
1837             _baseHttpClientNoTimeout = null;\r
1838         }\r
1839     }\r
1840 \r
1841     public class ShareAccountInfo\r
1842     {\r
1843         public DateTime? last_modified { get; set; }\r
1844         public string name { get; set; }\r
1845     }\r
1846 }\r