using System.Net;
using System.Security.Cryptography;
using System.Text;
+using System.Threading.Algorithms;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Pithos.Interfaces;
authClient.AssertStatusOK("Authentication failed");
- //var keys = authClient.ResponseHeaders.AllKeys.AsQueryable();
-
string storageUrl = authClient.GetHeaderValue("X-Storage-Url");
if (String.IsNullOrWhiteSpace(storageUrl))
throw new InvalidOperationException("Failed to obtain storage url");
Token = token;
}
- /*_retryPolicy = new RetryPolicy { RetryCount = _retries };
- _retryPolicy.RetryConditions.Add(new TimeoutRetryCondition());*/
-
_client = new PithosClient{
BaseAddress = StorageUrl.AbsoluteUri,
Timeout=10000,
Retries=3};
if (Proxy!=null)
_client.Proxy = new WebProxy(Proxy);
- //_client.FileProgress += OnFileProgress;
_client.Headers.Add("X-Auth-Token", Token);
- /*if (UsePithos)
- {
- _client.AddHeader("X-Auth-User", UserName);
- _client.AddHeader("X-Auth-Key",ApiKey);
- }*/
Trace.TraceInformation("[AUTHENTICATE] End for {0}", userName);
}
- /* private void OnFileProgress(object sender, FileProgressEventArgs e)
- {
- Trace.TraceInformation("[PROGRESS] {0} {1:p} {2} of {3}",e.FileName,(double)e.BytesWritten/e.TotalBytes, e.BytesWritten,e.TotalBytes);
- }*/
public IList<ContainerInfo> ListContainers()
{
- //Workaround for Hammock quirk: Hammock always
- //appends a / unless a Path is specified.
-
- //Create a request with a complete path
- //var request = new RestRequest { Path = StorageUrl.ToString(), RetryPolicy = _retryPolicy,Timeout = _shortTimeout };
- //request.AddParameter("format","json");
- //Create a client clone
-
- /*var url = String.Join("/", new[] { _client.Authority, StorageUrl.ToString() });
- var builder=new UriBuilder(url);
- builder.Query = "format=json";
-
- var client= new PithosClient(_client){Timeout=10}; */
+
var content=_client.DownloadStringWithRetry("",3);
_client.Parameters.Clear();
_client.Parameters.Add("format", "json");
var infos = JsonConvert.DeserializeObject<IList<ContainerInfo>>(content);
return infos;
-
-/*
- var client = new RestClient{Proxy=Proxy.ToString()};
- foreach (var header in _client.GetAllHeaders())
- {
- client.AddHeader(header.Name,header.Value);
- }
-
-
-
-
-
- var response = client.Request(request);
-
- if (response.StatusCode == HttpStatusCode.NoContent)
- return new List<ContainerInfo>();
-
- ThrowIfNotStatusOK(response, "List Containers failed");
-
-
- var infos=JsonConvert.DeserializeObject<IList<ContainerInfo>>(response.Content);
-
- return infos;*/
}
public IList<ObjectInfo> ListObjects(string container)
Trace.TraceInformation("[START] ListObjects");
- //var request = new RestRequest { Path = container, RetryPolicy = _retryPolicy, Timeout = TimeSpan.FromMinutes(1) };
- //request.AddParameter("format", "json");
- //var response = _client.Request(request);
-
-
-/*
- var url = String.Join("/", new[] { _client.Authority, container });
- var builder = new UriBuilder(url) {Query = "format=json"};
-
- var client = new PithosClient(_client) { Timeout = 60000 };
-*/
+
_client.Parameters.Clear();
_client.Parameters.Add("format", "json");
var content = _client.DownloadStringWithRetry(container, 3);
Trace.TraceInformation("[START] ListObjects");
- /* var request = new RestRequest { Path = container,RetryPolicy = _retryPolicy, Timeout = TimeSpan.FromMinutes(1) };
- request.AddParameter("format", "json");
- request.AddParameter("path", folder);*/
+
_client.Parameters.Clear();
_client.Parameters.Add("format", "json");
var infos = JsonConvert.DeserializeObject<IList<ObjectInfo>>(content);
- /* var response = _client.Request(request);
-
- var infos = InfosFromContent(response);*/
+
Trace.TraceInformation("[END] ListObjects");
return infos;
}
- /* private static IList<ObjectInfo> InfosFromContent(RestResponse response)
- {
- if (response.TimedOut)
- return new List<ObjectInfo>();
-
- if (response.StatusCode == 0)
- return new List<ObjectInfo>();
-
- if (response.StatusCode == HttpStatusCode.NoContent)
- return new List<ObjectInfo>();
-
-
- var statusCode = (int)response.StatusCode;
- if (statusCode < 200 || statusCode >= 300)
- {
- Trace.TraceWarning("ListObjects failed with code {0} - {1}", response.StatusCode, response.StatusDescription);
- return new List<ObjectInfo>();
- }
-
- var infos = JsonConvert.DeserializeObject<IList<ObjectInfo>>(response.Content);
- return infos;
- }
-*/
+
public bool ContainerExists(string container)
{
if (String.IsNullOrWhiteSpace(container))
_client.Parameters.Clear();
_client.Head(container,3);
- //var request = new RestRequest { Path = container, Method = WebMethod.Head, RetryPolicy = _retryPolicy,Timeout = _shortTimeout };
- //var response = _client.Request(request);
-
+
switch (_client.StatusCode)
{
case HttpStatusCode.OK:
if (String.IsNullOrWhiteSpace(objectName))
throw new ArgumentNullException("objectName", "The objectName property can't be empty");
-
-/*
- var request = new RestRequest { Path = container + "/" + objectName, Method = WebMethod.Head,RetryPolicy = _retryPolicy, Timeout = _shortTimeout };
- var response = _client.Request(request);
-*/
_client.Parameters.Clear();
_client.Head(container + "/" + objectName, 3);
if (String.IsNullOrWhiteSpace(objectName))
throw new ArgumentNullException("objectName", "The objectName property can't be empty");
-
-/*
- var request = new RestRequest { Path = container + "/" + objectName, Method = WebMethod.Head, RetryPolicy = _retryPolicy,Timeout = _shortTimeout };
- var response = _client.Request(request);
-*/
try
{
_client.Parameters.Clear();
throw new ArgumentNullException("folder", "The folder property can't be empty");
var folderUrl=String.Format("{0}/{1}",container,folder);
-/*
- var request = new RestRequest { Path = folderUrl, Method = WebMethod.Put, RetryPolicy = _retryPolicy,Timeout = _shortTimeout };
- request.AddHeader("Content-Type", @"application/directory");
- request.AddHeader("Content-Length", "0");
-*/
-
+
_client.Parameters.Clear();
_client.Headers.Add("Content-Type", @"application/directory");
_client.Headers.Add("Content-Length", "0");
if (String.IsNullOrWhiteSpace(container))
throw new ArgumentNullException("container", "The container property can't be empty");
-/*
- var request = new RestRequest { Path = container, Method = WebMethod.Head, RetryPolicy = _retryPolicy,Timeout = _shortTimeout };
- var response = _client.Request(request);
-*/
_client.Head(container);
switch (_client.StatusCode)
{
if (String.IsNullOrWhiteSpace(container))
throw new ArgumentNullException("container", "The container property can't be empty");
-/*
- var request = new RestRequest { Path = container, Method = WebMethod.Put, RetryPolicy = _retryPolicy,Timeout = _shortTimeout };
- var response = _client.Request(request);
-*/
_client.PutWithRetry(container,3);
var expectedCodes = new[]{HttpStatusCode.Created ,HttpStatusCode.Accepted , HttpStatusCode.OK};
if (!expectedCodes.Contains(_client.StatusCode))
if (String.IsNullOrWhiteSpace(container))
throw new ArgumentNullException("container", "The container property can't be empty");
-/*
- var request = new RestRequest { Path = container, Method = WebMethod.Delete, RetryPolicy = _retryPolicy,Timeout = _shortTimeout };
- var response = _client.Request(request);
-*/
_client.DeleteWithRetry(container,3);
var expectedCodes = new[] { HttpStatusCode.NotFound, HttpStatusCode.NoContent};
if (!expectedCodes.Contains(_client.StatusCode))
throw new ArgumentNullException("container", "The container property can't be empty");
if (String.IsNullOrWhiteSpace(objectName))
throw new ArgumentNullException("objectName", "The objectName property can't be empty");
- /*
- var request = new RestRequest {Path = container + "/" + objectName, Method = WebMethod.Get};
-
- if (DownloadPercentLimit > 0)
- request.TaskOptions = new TaskOptions<int> { RateLimitPercent = DownloadPercentLimit };
- */
+
try
{
var url = String.Join("/", _client.BaseAddress, container, objectName);
var uri = new Uri(url);
var client = new PithosClient(_client){Timeout=0};
- /* if (!String.IsNullOrWhiteSpace(_client.Proxy))
- client.Proxy = new WebProxy(_client.Proxy);
-
- CopyHeaders(_client, client);*/
+
Trace.TraceInformation("[GET] START {0}", objectName);
client.DownloadProgressChanged += (sender, args) =>
client.Headers.Add("Content-Type", "application/octet-stream");
client.Headers.Add("ETag", etag);
-/*
- if(!String.IsNullOrWhiteSpace(_client.Proxy))
- client.Proxy = new WebProxy(_client.Proxy);
-
- CopyHeaders(_client, client);
-*/
Trace.TraceInformation("[PUT] START {0}", objectName);
client.UploadProgressChanged += (sender, args) =>
}
-/*
- /// <summary>
- /// Copies headers from a Hammock RestClient to a WebClient
- /// </summary>
- /// <param name="source">The RestClient from which the headers are copied</param>
- /// <param name="target">The WebClient to which the headers are copied</param>
- private static void CopyHeaders(RestClient source, WebClient target)
- {
- Contract.Requires(source!=null,"source can't be null");
- Contract.Requires(target != null, "target can't be null");
- if (source == null)
- throw new ArgumentNullException("source", "source can't be null");
- if (source == null)
- throw new ArgumentNullException("target", "target can't be null");
-
- foreach (var header in source.GetAllHeaders())
- {
- target.Headers.Add(header.Name, header.Value);
- }
- }*/
-
- /* /// <summary>
- /// Copies headers from a Hammock RestClient to a WebClient
- /// </summary>
- /// <param name="source">The RestClient from which the headers are copied</param>
- /// <param name="target">The WebClient to which the headers are copied</param>
- private static void CopyHeaders(RestClient source, WebRequest target)
- {
- Contract.Requires(source!=null,"source can't be null");
- Contract.Requires(target != null, "target can't be null");
- if (source == null)
- throw new ArgumentNullException("source", "source can't be null");
- if (source == null)
- throw new ArgumentNullException("target", "target can't be null");
-
- foreach (var header in source.GetAllHeaders())
- {
- target.Headers.Add(header.Name, header.Value);
- }
- }*/
-
private static string CalculateHash(string fileName)
{
if (String.IsNullOrWhiteSpace(objectName))
throw new ArgumentNullException("objectName", "The objectName property can't be empty");
-/*
- var request = new RestRequest { Path = container + "/" + objectName, Method = WebMethod.Delete, RetryPolicy = _retryPolicy,Timeout = _shortTimeout };
- var response = _client.Request(request);
-*/
_client.DeleteWithRetry(container + "/" + objectName,3);
var expectedCodes = new[] { HttpStatusCode.NotFound, HttpStatusCode.NoContent };
var targetUrl = targetContainer + "/" + newObjectName;
var sourceUrl = String.Format("/{0}/{1}", sourceContainer, oldObjectName);
-/*
- var request = new RestRequest { Path = targetUrl, Method = WebMethod.Put };
- request.AddHeader("X-Copy-From",sourceUrl);
- request.AddPostContent(new byte[]{});
- var response = _client.Request(request);
-*/
-
var client = new PithosClient(_client);
client.Headers.Add("X-Copy-From", sourceUrl);
client.PutWithRetry(targetUrl,3);
}
-
- /*private string GetHeaderValue(string headerName, WebHeaderCollection headers, IQueryable<string> keys)
- {
- if (keys.Any(key => key == headerName))
- return headers[headerName];
- else
- throw new WebException(String.Format("The {0} header is missing", headerName));
- }*/
-
- /* private static void ThrowIfNotStatusOK(RestResponse response, string message)
- {
- int status = (int)response.StatusCode;
- ThrowIfNotStatusOK(status, message);
- }
-
- private static void ThrowIfNotStatusOK(HttpWebResponse response, string message)
- {
- int status = (int)response.StatusCode;
- ThrowIfNotStatusOK(status, message);
- }
-
- private static void ThrowIfNotStatusOK(int status, string message)
- {
- if (status < 200 || status >= 300)
- throw new WebException(String.Format("{0} with code {1}", message, status));
- }
- */
private static WebException CreateWebException(string operation, HttpStatusCode statusCode)
{
return new WebException(String.Format("{0} failed with unexpected status code {1}", operation, statusCode));
}
- /* public static Func<T> Retry<T>(Func<int,T> original, int retryCount,int timeout)
- {
- return () =>
- {
- while (true)
- {
- try
- {
- return original(timeout);
- }
- catch (WebException e)
- {
- if (e.Status == WebExceptionStatus.Timeout)
- {
- if (retryCount == 0)
- {
- throw;
- }
- retryCount--;
- }
- else
- {
- throw;
- }
- }
- catch (Exception e)
- {
- throw;
- }
- }
- };
- } */
-
-
+
}
}