Revision d15e99b4 trunk/Pithos.Network/CloudFilesClient.cs
b/trunk/Pithos.Network/CloudFilesClient.cs | ||
---|---|---|
8 | 8 |
using System.Net; |
9 | 9 |
using System.Security.Cryptography; |
10 | 10 |
using System.Text; |
11 |
using System.Threading.Algorithms; |
|
11 | 12 |
using System.Threading.Tasks; |
12 | 13 |
using Newtonsoft.Json; |
13 | 14 |
using Pithos.Interfaces; |
... | ... | |
78 | 79 |
|
79 | 80 |
authClient.AssertStatusOK("Authentication failed"); |
80 | 81 |
|
81 |
//var keys = authClient.ResponseHeaders.AllKeys.AsQueryable(); |
|
82 |
|
|
83 | 82 |
string storageUrl = authClient.GetHeaderValue("X-Storage-Url"); |
84 | 83 |
if (String.IsNullOrWhiteSpace(storageUrl)) |
85 | 84 |
throw new InvalidOperationException("Failed to obtain storage url"); |
... | ... | |
91 | 90 |
Token = token; |
92 | 91 |
} |
93 | 92 |
|
94 |
/*_retryPolicy = new RetryPolicy { RetryCount = _retries }; |
|
95 |
_retryPolicy.RetryConditions.Add(new TimeoutRetryCondition());*/ |
|
96 |
|
|
97 | 93 |
_client = new PithosClient{ |
98 | 94 |
BaseAddress = StorageUrl.AbsoluteUri, |
99 | 95 |
Timeout=10000, |
100 | 96 |
Retries=3}; |
101 | 97 |
if (Proxy!=null) |
102 | 98 |
_client.Proxy = new WebProxy(Proxy); |
103 |
//_client.FileProgress += OnFileProgress; |
|
104 | 99 |
|
105 | 100 |
_client.Headers.Add("X-Auth-Token", Token); |
106 |
/*if (UsePithos) |
|
107 |
{ |
|
108 |
_client.AddHeader("X-Auth-User", UserName); |
|
109 |
_client.AddHeader("X-Auth-Key",ApiKey); |
|
110 |
}*/ |
|
111 | 101 |
|
112 | 102 |
Trace.TraceInformation("[AUTHENTICATE] End for {0}", userName); |
113 | 103 |
} |
114 | 104 |
|
115 |
/* private void OnFileProgress(object sender, FileProgressEventArgs e) |
|
116 |
{ |
|
117 |
Trace.TraceInformation("[PROGRESS] {0} {1:p} {2} of {3}",e.FileName,(double)e.BytesWritten/e.TotalBytes, e.BytesWritten,e.TotalBytes); |
|
118 |
}*/ |
|
119 | 105 |
|
120 | 106 |
public IList<ContainerInfo> ListContainers() |
121 | 107 |
{ |
122 |
//Workaround for Hammock quirk: Hammock always |
|
123 |
//appends a / unless a Path is specified. |
|
124 |
|
|
125 |
//Create a request with a complete path |
|
126 |
//var request = new RestRequest { Path = StorageUrl.ToString(), RetryPolicy = _retryPolicy,Timeout = _shortTimeout }; |
|
127 |
//request.AddParameter("format","json"); |
|
128 |
//Create a client clone |
|
129 |
|
|
130 |
/*var url = String.Join("/", new[] { _client.Authority, StorageUrl.ToString() }); |
|
131 |
var builder=new UriBuilder(url); |
|
132 |
builder.Query = "format=json"; |
|
133 |
|
|
134 |
var client= new PithosClient(_client){Timeout=10}; */ |
|
108 |
|
|
135 | 109 |
var content=_client.DownloadStringWithRetry("",3); |
136 | 110 |
_client.Parameters.Clear(); |
137 | 111 |
_client.Parameters.Add("format", "json"); |
... | ... | |
142 | 116 |
var infos = JsonConvert.DeserializeObject<IList<ContainerInfo>>(content); |
143 | 117 |
return infos; |
144 | 118 |
|
145 |
|
|
146 |
/* |
|
147 |
var client = new RestClient{Proxy=Proxy.ToString()}; |
|
148 |
foreach (var header in _client.GetAllHeaders()) |
|
149 |
{ |
|
150 |
client.AddHeader(header.Name,header.Value); |
|
151 |
} |
|
152 |
|
|
153 |
|
|
154 |
|
|
155 |
|
|
156 |
|
|
157 |
var response = client.Request(request); |
|
158 |
|
|
159 |
if (response.StatusCode == HttpStatusCode.NoContent) |
|
160 |
return new List<ContainerInfo>(); |
|
161 |
|
|
162 |
ThrowIfNotStatusOK(response, "List Containers failed"); |
|
163 |
|
|
164 |
|
|
165 |
var infos=JsonConvert.DeserializeObject<IList<ContainerInfo>>(response.Content); |
|
166 |
|
|
167 |
return infos;*/ |
|
168 | 119 |
} |
169 | 120 |
|
170 | 121 |
public IList<ObjectInfo> ListObjects(string container) |
... | ... | |
174 | 125 |
|
175 | 126 |
Trace.TraceInformation("[START] ListObjects"); |
176 | 127 |
|
177 |
//var request = new RestRequest { Path = container, RetryPolicy = _retryPolicy, Timeout = TimeSpan.FromMinutes(1) }; |
|
178 |
//request.AddParameter("format", "json"); |
|
179 |
//var response = _client.Request(request); |
|
180 |
|
|
181 |
|
|
182 |
/* |
|
183 |
var url = String.Join("/", new[] { _client.Authority, container }); |
|
184 |
var builder = new UriBuilder(url) {Query = "format=json"}; |
|
185 |
|
|
186 |
var client = new PithosClient(_client) { Timeout = 60000 }; |
|
187 |
*/ |
|
128 |
|
|
188 | 129 |
_client.Parameters.Clear(); |
189 | 130 |
_client.Parameters.Add("format", "json"); |
190 | 131 |
var content = _client.DownloadStringWithRetry(container, 3); |
... | ... | |
206 | 147 |
|
207 | 148 |
Trace.TraceInformation("[START] ListObjects"); |
208 | 149 |
|
209 |
/* var request = new RestRequest { Path = container,RetryPolicy = _retryPolicy, Timeout = TimeSpan.FromMinutes(1) }; |
|
210 |
request.AddParameter("format", "json"); |
|
211 |
request.AddParameter("path", folder);*/ |
|
150 |
|
|
212 | 151 |
|
213 | 152 |
_client.Parameters.Clear(); |
214 | 153 |
_client.Parameters.Add("format", "json"); |
... | ... | |
218 | 157 |
|
219 | 158 |
var infos = JsonConvert.DeserializeObject<IList<ObjectInfo>>(content); |
220 | 159 |
|
221 |
/* var response = _client.Request(request); |
|
222 |
|
|
223 |
var infos = InfosFromContent(response);*/ |
|
160 |
|
|
224 | 161 |
|
225 | 162 |
Trace.TraceInformation("[END] ListObjects"); |
226 | 163 |
return infos; |
227 | 164 |
} |
228 | 165 |
|
229 |
/* private static IList<ObjectInfo> InfosFromContent(RestResponse response) |
|
230 |
{ |
|
231 |
if (response.TimedOut) |
|
232 |
return new List<ObjectInfo>(); |
|
233 |
|
|
234 |
if (response.StatusCode == 0) |
|
235 |
return new List<ObjectInfo>(); |
|
236 |
|
|
237 |
if (response.StatusCode == HttpStatusCode.NoContent) |
|
238 |
return new List<ObjectInfo>(); |
|
239 |
|
|
240 |
|
|
241 |
var statusCode = (int)response.StatusCode; |
|
242 |
if (statusCode < 200 || statusCode >= 300) |
|
243 |
{ |
|
244 |
Trace.TraceWarning("ListObjects failed with code {0} - {1}", response.StatusCode, response.StatusDescription); |
|
245 |
return new List<ObjectInfo>(); |
|
246 |
} |
|
247 |
|
|
248 |
var infos = JsonConvert.DeserializeObject<IList<ObjectInfo>>(response.Content); |
|
249 |
return infos; |
|
250 |
} |
|
251 |
*/ |
|
166 |
|
|
252 | 167 |
public bool ContainerExists(string container) |
253 | 168 |
{ |
254 | 169 |
if (String.IsNullOrWhiteSpace(container)) |
... | ... | |
256 | 171 |
|
257 | 172 |
_client.Parameters.Clear(); |
258 | 173 |
_client.Head(container,3); |
259 |
//var request = new RestRequest { Path = container, Method = WebMethod.Head, RetryPolicy = _retryPolicy,Timeout = _shortTimeout }; |
|
260 |
//var response = _client.Request(request); |
|
261 |
|
|
174 |
|
|
262 | 175 |
switch (_client.StatusCode) |
263 | 176 |
{ |
264 | 177 |
case HttpStatusCode.OK: |
... | ... | |
278 | 191 |
if (String.IsNullOrWhiteSpace(objectName)) |
279 | 192 |
throw new ArgumentNullException("objectName", "The objectName property can't be empty"); |
280 | 193 |
|
281 |
|
|
282 |
/* |
|
283 |
var request = new RestRequest { Path = container + "/" + objectName, Method = WebMethod.Head,RetryPolicy = _retryPolicy, Timeout = _shortTimeout }; |
|
284 |
var response = _client.Request(request); |
|
285 |
*/ |
|
286 | 194 |
_client.Parameters.Clear(); |
287 | 195 |
_client.Head(container + "/" + objectName, 3); |
288 | 196 |
|
... | ... | |
306 | 214 |
if (String.IsNullOrWhiteSpace(objectName)) |
307 | 215 |
throw new ArgumentNullException("objectName", "The objectName property can't be empty"); |
308 | 216 |
|
309 |
|
|
310 |
/* |
|
311 |
var request = new RestRequest { Path = container + "/" + objectName, Method = WebMethod.Head, RetryPolicy = _retryPolicy,Timeout = _shortTimeout }; |
|
312 |
var response = _client.Request(request); |
|
313 |
*/ |
|
314 | 217 |
try |
315 | 218 |
{ |
316 | 219 |
_client.Parameters.Clear(); |
... | ... | |
375 | 278 |
throw new ArgumentNullException("folder", "The folder property can't be empty"); |
376 | 279 |
|
377 | 280 |
var folderUrl=String.Format("{0}/{1}",container,folder); |
378 |
/* |
|
379 |
var request = new RestRequest { Path = folderUrl, Method = WebMethod.Put, RetryPolicy = _retryPolicy,Timeout = _shortTimeout }; |
|
380 |
request.AddHeader("Content-Type", @"application/directory"); |
|
381 |
request.AddHeader("Content-Length", "0"); |
|
382 |
*/ |
|
383 |
|
|
281 |
|
|
384 | 282 |
_client.Parameters.Clear(); |
385 | 283 |
_client.Headers.Add("Content-Type", @"application/directory"); |
386 | 284 |
_client.Headers.Add("Content-Length", "0"); |
... | ... | |
396 | 294 |
if (String.IsNullOrWhiteSpace(container)) |
397 | 295 |
throw new ArgumentNullException("container", "The container property can't be empty"); |
398 | 296 |
|
399 |
/* |
|
400 |
var request = new RestRequest { Path = container, Method = WebMethod.Head, RetryPolicy = _retryPolicy,Timeout = _shortTimeout }; |
|
401 |
var response = _client.Request(request); |
|
402 |
*/ |
|
403 | 297 |
_client.Head(container); |
404 | 298 |
switch (_client.StatusCode) |
405 | 299 |
{ |
... | ... | |
423 | 317 |
if (String.IsNullOrWhiteSpace(container)) |
424 | 318 |
throw new ArgumentNullException("container", "The container property can't be empty"); |
425 | 319 |
|
426 |
/* |
|
427 |
var request = new RestRequest { Path = container, Method = WebMethod.Put, RetryPolicy = _retryPolicy,Timeout = _shortTimeout }; |
|
428 |
var response = _client.Request(request); |
|
429 |
*/ |
|
430 | 320 |
_client.PutWithRetry(container,3); |
431 | 321 |
var expectedCodes = new[]{HttpStatusCode.Created ,HttpStatusCode.Accepted , HttpStatusCode.OK}; |
432 | 322 |
if (!expectedCodes.Contains(_client.StatusCode)) |
... | ... | |
438 | 328 |
if (String.IsNullOrWhiteSpace(container)) |
439 | 329 |
throw new ArgumentNullException("container", "The container property can't be empty"); |
440 | 330 |
|
441 |
/* |
|
442 |
var request = new RestRequest { Path = container, Method = WebMethod.Delete, RetryPolicy = _retryPolicy,Timeout = _shortTimeout }; |
|
443 |
var response = _client.Request(request); |
|
444 |
*/ |
|
445 | 331 |
_client.DeleteWithRetry(container,3); |
446 | 332 |
var expectedCodes = new[] { HttpStatusCode.NotFound, HttpStatusCode.NoContent}; |
447 | 333 |
if (!expectedCodes.Contains(_client.StatusCode)) |
... | ... | |
463 | 349 |
throw new ArgumentNullException("container", "The container property can't be empty"); |
464 | 350 |
if (String.IsNullOrWhiteSpace(objectName)) |
465 | 351 |
throw new ArgumentNullException("objectName", "The objectName property can't be empty"); |
466 |
/* |
|
467 |
var request = new RestRequest {Path = container + "/" + objectName, Method = WebMethod.Get}; |
|
468 |
|
|
469 |
if (DownloadPercentLimit > 0) |
|
470 |
request.TaskOptions = new TaskOptions<int> { RateLimitPercent = DownloadPercentLimit }; |
|
471 |
*/ |
|
352 |
|
|
472 | 353 |
try |
473 | 354 |
{ |
474 | 355 |
var url = String.Join("/", _client.BaseAddress, container, objectName); |
475 | 356 |
var uri = new Uri(url); |
476 | 357 |
|
477 | 358 |
var client = new PithosClient(_client){Timeout=0}; |
478 |
/* if (!String.IsNullOrWhiteSpace(_client.Proxy)) |
|
479 |
client.Proxy = new WebProxy(_client.Proxy); |
|
480 |
|
|
481 |
CopyHeaders(_client, client);*/ |
|
359 |
|
|
482 | 360 |
|
483 | 361 |
Trace.TraceInformation("[GET] START {0}", objectName); |
484 | 362 |
client.DownloadProgressChanged += (sender, args) => |
... | ... | |
544 | 422 |
client.Headers.Add("Content-Type", "application/octet-stream"); |
545 | 423 |
client.Headers.Add("ETag", etag); |
546 | 424 |
|
547 |
/* |
|
548 |
if(!String.IsNullOrWhiteSpace(_client.Proxy)) |
|
549 |
client.Proxy = new WebProxy(_client.Proxy); |
|
550 |
|
|
551 |
CopyHeaders(_client, client); |
|
552 |
*/ |
|
553 | 425 |
|
554 | 426 |
Trace.TraceInformation("[PUT] START {0}", objectName); |
555 | 427 |
client.UploadProgressChanged += (sender, args) => |
... | ... | |
578 | 450 |
|
579 | 451 |
} |
580 | 452 |
|
581 |
/* |
|
582 |
/// <summary> |
|
583 |
/// Copies headers from a Hammock RestClient to a WebClient |
|
584 |
/// </summary> |
|
585 |
/// <param name="source">The RestClient from which the headers are copied</param> |
|
586 |
/// <param name="target">The WebClient to which the headers are copied</param> |
|
587 |
private static void CopyHeaders(RestClient source, WebClient target) |
|
588 |
{ |
|
589 |
Contract.Requires(source!=null,"source can't be null"); |
|
590 |
Contract.Requires(target != null, "target can't be null"); |
|
591 |
if (source == null) |
|
592 |
throw new ArgumentNullException("source", "source can't be null"); |
|
593 |
if (source == null) |
|
594 |
throw new ArgumentNullException("target", "target can't be null"); |
|
595 |
|
|
596 |
foreach (var header in source.GetAllHeaders()) |
|
597 |
{ |
|
598 |
target.Headers.Add(header.Name, header.Value); |
|
599 |
} |
|
600 |
}*/ |
|
601 |
|
|
602 |
/* /// <summary> |
|
603 |
/// Copies headers from a Hammock RestClient to a WebClient |
|
604 |
/// </summary> |
|
605 |
/// <param name="source">The RestClient from which the headers are copied</param> |
|
606 |
/// <param name="target">The WebClient to which the headers are copied</param> |
|
607 |
private static void CopyHeaders(RestClient source, WebRequest target) |
|
608 |
{ |
|
609 |
Contract.Requires(source!=null,"source can't be null"); |
|
610 |
Contract.Requires(target != null, "target can't be null"); |
|
611 |
if (source == null) |
|
612 |
throw new ArgumentNullException("source", "source can't be null"); |
|
613 |
if (source == null) |
|
614 |
throw new ArgumentNullException("target", "target can't be null"); |
|
615 |
|
|
616 |
foreach (var header in source.GetAllHeaders()) |
|
617 |
{ |
|
618 |
target.Headers.Add(header.Name, header.Value); |
|
619 |
} |
|
620 |
}*/ |
|
621 |
|
|
622 | 453 |
|
623 | 454 |
private static string CalculateHash(string fileName) |
624 | 455 |
{ |
... | ... | |
641 | 472 |
if (String.IsNullOrWhiteSpace(objectName)) |
642 | 473 |
throw new ArgumentNullException("objectName", "The objectName property can't be empty"); |
643 | 474 |
|
644 |
/* |
|
645 |
var request = new RestRequest { Path = container + "/" + objectName, Method = WebMethod.Delete, RetryPolicy = _retryPolicy,Timeout = _shortTimeout }; |
|
646 |
var response = _client.Request(request); |
|
647 |
*/ |
|
648 | 475 |
_client.DeleteWithRetry(container + "/" + objectName,3); |
649 | 476 |
|
650 | 477 |
var expectedCodes = new[] { HttpStatusCode.NotFound, HttpStatusCode.NoContent }; |
... | ... | |
667 | 494 |
var targetUrl = targetContainer + "/" + newObjectName; |
668 | 495 |
var sourceUrl = String.Format("/{0}/{1}", sourceContainer, oldObjectName); |
669 | 496 |
|
670 |
/* |
|
671 |
var request = new RestRequest { Path = targetUrl, Method = WebMethod.Put }; |
|
672 |
request.AddHeader("X-Copy-From",sourceUrl); |
|
673 |
request.AddPostContent(new byte[]{}); |
|
674 |
var response = _client.Request(request); |
|
675 |
*/ |
|
676 |
|
|
677 | 497 |
var client = new PithosClient(_client); |
678 | 498 |
client.Headers.Add("X-Copy-From", sourceUrl); |
679 | 499 |
client.PutWithRetry(targetUrl,3); |
... | ... | |
688 | 508 |
} |
689 | 509 |
|
690 | 510 |
|
691 |
|
|
692 |
/*private string GetHeaderValue(string headerName, WebHeaderCollection headers, IQueryable<string> keys) |
|
693 |
{ |
|
694 |
if (keys.Any(key => key == headerName)) |
|
695 |
return headers[headerName]; |
|
696 |
else |
|
697 |
throw new WebException(String.Format("The {0} header is missing", headerName)); |
|
698 |
}*/ |
|
699 |
|
|
700 |
/* private static void ThrowIfNotStatusOK(RestResponse response, string message) |
|
701 |
{ |
|
702 |
int status = (int)response.StatusCode; |
|
703 |
ThrowIfNotStatusOK(status, message); |
|
704 |
} |
|
705 |
|
|
706 |
private static void ThrowIfNotStatusOK(HttpWebResponse response, string message) |
|
707 |
{ |
|
708 |
int status = (int)response.StatusCode; |
|
709 |
ThrowIfNotStatusOK(status, message); |
|
710 |
} |
|
711 |
|
|
712 |
private static void ThrowIfNotStatusOK(int status, string message) |
|
713 |
{ |
|
714 |
if (status < 200 || status >= 300) |
|
715 |
throw new WebException(String.Format("{0} with code {1}", message, status)); |
|
716 |
} |
|
717 |
*/ |
|
718 | 511 |
private static WebException CreateWebException(string operation, HttpStatusCode statusCode) |
719 | 512 |
{ |
720 | 513 |
return new WebException(String.Format("{0} failed with unexpected status code {1}", operation, statusCode)); |
721 | 514 |
} |
722 | 515 |
|
723 |
/* public static Func<T> Retry<T>(Func<int,T> original, int retryCount,int timeout) |
|
724 |
{ |
|
725 |
return () => |
|
726 |
{ |
|
727 |
while (true) |
|
728 |
{ |
|
729 |
try |
|
730 |
{ |
|
731 |
return original(timeout); |
|
732 |
} |
|
733 |
catch (WebException e) |
|
734 |
{ |
|
735 |
if (e.Status == WebExceptionStatus.Timeout) |
|
736 |
{ |
|
737 |
if (retryCount == 0) |
|
738 |
{ |
|
739 |
throw; |
|
740 |
} |
|
741 |
retryCount--; |
|
742 |
} |
|
743 |
else |
|
744 |
{ |
|
745 |
throw; |
|
746 |
} |
|
747 |
} |
|
748 |
catch (Exception e) |
|
749 |
{ |
|
750 |
throw; |
|
751 |
} |
|
752 |
} |
|
753 |
}; |
|
754 |
} */ |
|
755 |
|
|
756 |
|
|
516 |
|
|
757 | 517 |
} |
758 | 518 |
} |
Also available in: Unified diff