Merge branch 'master' of \\\pk2010\Pithos\
[pithos-ms-client] / trunk / Pithos.Network / RestClient.cs
index fd4a5f3..d196752 100644 (file)
@@ -1,6 +1,37 @@
 // -----------------------------------------------------------------------
-// <copyright file="RestClient.cs" company="Microsoft">
-// TODO: Update copyright text.
+// <copyright file="RestClient.cs" company="GRNet">
+// Copyright 2011-2012 GRNET S.A. All rights reserved.
+// 
+// Redistribution and use in source and binary forms, with or
+// without modification, are permitted provided that the following
+// conditions are met:
+// 
+//   1. Redistributions of source code must retain the above
+//      copyright notice, this list of conditions and the following
+//      disclaimer.
+// 
+//   2. Redistributions in binary form must reproduce the above
+//      copyright notice, this list of conditions and the following
+//      disclaimer in the documentation and/or other materials
+//      provided with the distribution.
+// 
+// THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
+// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
+// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+// USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+// AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+// POSSIBILITY OF SUCH DAMAGE.
+// 
+// The views and conclusions contained in the software and
+// documentation are those of the authors and should not be
+// interpreted as representing official policies, either expressed
+// or implied, of GRNET S.A.
 // </copyright>
 // -----------------------------------------------------------------------
 
@@ -11,6 +42,8 @@ using System.IO;
 using System.Net;
 using System.Runtime.Serialization;
 using System.Threading.Tasks;
+using log4net;
+
 
 namespace Pithos.Network
 {
@@ -40,7 +73,20 @@ namespace Pithos.Network
         private readonly Dictionary<string, string> _parameters=new Dictionary<string, string>();
         public Dictionary<string, string> Parameters
         {
-            get { return _parameters; }            
+            get
+            {
+                Contract.Ensures(_parameters!=null);
+                return _parameters;
+            }            
+        }
+
+        private static readonly ILog Log = LogManager.GetLogger("RestClient");
+
+
+        [ContractInvariantMethod]
+        private void Invariants()
+        {
+            Contract.Invariant(Headers!=null);    
         }
 
         public RestClient():base()
@@ -52,6 +98,10 @@ namespace Pithos.Network
         public RestClient(RestClient other)
             : base()
         {
+            if (other==null)
+                throw new ArgumentNullException("other");
+            Contract.EndContractBlock();
+
             CopyHeaders(other);
             Timeout = other.Timeout;
             Retries = other.Retries;
@@ -65,11 +115,13 @@ namespace Pithos.Network
             this.Proxy = other.Proxy;
         }
 
+
         protected override WebRequest GetWebRequest(Uri address)
         {
             TimedOut = false;
-            var webRequest = base.GetWebRequest(address);
-            var request = webRequest as HttpWebRequest;
+            var webRequest = base.GetWebRequest(address);            
+            var request = (HttpWebRequest)webRequest;
+            request.ServicePoint.ConnectionLimit = 50;
             if (IfModifiedSince.HasValue)
                 request.IfModifiedSince = IfModifiedSince.Value;
             request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
@@ -88,58 +140,106 @@ namespace Pithos.Network
 
         public DateTime? IfModifiedSince { get; set; }
 
+        //Asynchronous version
         protected override WebResponse GetWebResponse(WebRequest request, IAsyncResult result)
         {
-            var response = (HttpWebResponse) base.GetWebResponse(request, result);            
-            StatusCode=response.StatusCode;
-            StatusDescription=response.StatusDescription;
-            return response;
-        }
+            Log.InfoFormat("ASYNC [{0}] {1}",request.Method, request.RequestUri);
+            HttpWebResponse response = null;
 
+            try
+            {
+                response = (HttpWebResponse)base.GetWebResponse(request, result);
+            }
+            catch (WebException exc)
+            {
+                if (!TryGetResponse(exc, out response))
+                    throw;
+            }
 
+            StatusCode = response.StatusCode;
+            LastModified = response.LastModified;
+            StatusDescription = response.StatusDescription;
+            return response;
+
+        }
+      
 
+        //Synchronous version
         protected override WebResponse GetWebResponse(WebRequest request)
         {
+            HttpWebResponse response = null;
             try
-            {                
-                var response = (HttpWebResponse)base.GetWebResponse(request);
-                StatusCode = response.StatusCode;
-                LastModified=response.LastModified;                
-                StatusDescription = response.StatusDescription;                
-                return response;
+            {                                
+                response = (HttpWebResponse)base.GetWebResponse(request);
             }
             catch (WebException exc)
-            {                
-                if (exc.Response!=null)
-                {
-                    var response = (exc.Response as HttpWebResponse);
-                    if (response.StatusCode == HttpStatusCode.NotModified)
-                        return response;
-                    if (exc.Response.ContentLength > 0)
-                    {
-                        string content = GetContent(exc.Response);
-                        Trace.TraceError(content);
-                    }
-                }
-                throw;
+            {
+                if (!TryGetResponse(exc, out response))
+                    throw;
             }
+
+            StatusCode = response.StatusCode;
+            LastModified = response.LastModified;
+            StatusDescription = response.StatusDescription;
+            return response;
+        }
+
+        private bool TryGetResponse(WebException exc, out HttpWebResponse response)
+        {
+            response = null;
+            //Fail on empty response
+            if (exc.Response == null)
+                return false;
+
+            response = (exc.Response as HttpWebResponse);
+            //Succeed on allowed status codes
+            if (AllowedStatusCodes.Contains(response.StatusCode))
+                return true;
+
+            //Does the response have any content to log?
+            if (exc.Response.ContentLength > 0)
+            {
+                var content = LogContent(exc.Response);
+                Log.ErrorFormat(content);
+            }
+            return false;
+        }
+
+        private readonly List<HttpStatusCode> _allowedStatusCodes=new List<HttpStatusCode>{HttpStatusCode.NotModified};        
+
+        public List<HttpStatusCode> AllowedStatusCodes
+        {
+            get
+            {
+                return _allowedStatusCodes;
+            }            
         }
 
         public DateTime LastModified { get; private set; }
 
-        private static string GetContent(WebResponse webResponse)
+        private static string LogContent(WebResponse webResponse)
         {
-            string content;
-            using (var stream = webResponse.GetResponseStream())
-            using (var reader = new StreamReader(stream))
+            if (webResponse == null)
+                throw new ArgumentNullException("webResponse");
+            Contract.EndContractBlock();
+
+            //The response stream must be copied to avoid affecting other code by disposing of the 
+            //original response stream.
+            var stream = webResponse.GetResponseStream();            
+            using(var memStream=new MemoryStream())
+            using (var reader = new StreamReader(memStream))
             {
-                content = reader.ReadToEnd();
+                stream.CopyTo(memStream);                
+                string content = reader.ReadToEnd();
+
+                stream.Seek(0,SeekOrigin.Begin);
+                return content;
             }
-            return content;
         }
 
         public string DownloadStringWithRetry(string address,int retries=0)
         {
+            
             if (address == null)
                 throw new ArgumentNullException("address");
 
@@ -148,12 +248,11 @@ namespace Pithos.Network
             TraceStart("GET",actualAddress);            
             
             var actualRetries = (retries == 0) ? Retries : retries;
-            
 
-            
+            var uriString = String.Join("/", BaseAddress.TrimEnd('/'), actualAddress);
+
             var task = Retry(() =>
-            {
-                var uriString = String.Join("/", BaseAddress.TrimEnd('/'), actualAddress);                
+            {                
                 var content = base.DownloadString(uriString);
 
                 if (StatusCode == HttpStatusCode.NoContent)
@@ -162,12 +261,24 @@ namespace Pithos.Network
 
             }, actualRetries);
 
-            var result = task.Result;
-            return result;
+            try
+            {
+                var result = task.Result;
+                return result;
+
+            }
+            catch (AggregateException exc)
+            {
+                //If the task fails, propagate the original exception
+                if (exc.InnerException!=null)
+                    throw exc.InnerException;
+                throw;
+            }
         }
 
         public void Head(string address,int retries=0)
         {
+            AllowedStatusCodes.Add(HttpStatusCode.NotFound);
             RetryWithoutContent(address, retries, "HEAD");
         }
 
@@ -181,13 +292,27 @@ namespace Pithos.Network
             RetryWithoutContent(address, retries, "DELETE");
         }
 
-        public string GetHeaderValue(string headerName)
+        public string GetHeaderValue(string headerName,bool optional=false)
         {
+            if (this.ResponseHeaders==null)
+                throw new InvalidOperationException("ResponseHeaders are null");
+            Contract.EndContractBlock();
+
             var values=this.ResponseHeaders.GetValues(headerName);
-            if (values == null)
-                throw new WebException(String.Format("The {0}  header is missing", headerName));
-            else
+            if (values != null)
                 return values[0];
+
+            if (optional)            
+                return null;            
+            //A required header was not found
+            throw new WebException(String.Format("The {0}  header is missing", headerName));
+        }
+
+        public void SetNonEmptyHeaderValue(string headerName, string value)
+        {
+            if (String.IsNullOrWhiteSpace(value))
+                return;
+            Headers.Add(headerName,value);
         }
 
         private void RetryWithoutContent(string address, int retries, string method)
@@ -208,10 +333,22 @@ namespace Pithos.Network
                     ResponseHeaders.Clear();
 
                 TraceStart(method, uriString);
+                if (method == "PUT")
+                    request.ContentLength = 0;
 
+                //Have to use try/finally instead of using here, because WebClient needs a valid WebResponse object
+                //in order to return response headers
                 var response = (HttpWebResponse)GetWebResponse(request);
-                StatusCode = response.StatusCode;
-                StatusDescription = response.StatusDescription;                
+                try
+                {
+                    LastModified = response.LastModified;
+                    StatusCode = response.StatusCode;
+                    StatusDescription = response.StatusDescription;
+                }
+                finally
+                {
+                    response.Close();
+                }
                 
 
                 return 0;
@@ -226,81 +363,25 @@ namespace Pithos.Network
                 var exc = ex.InnerException;
                 if (exc is RetryException)
                 {
-                    Trace.TraceError("[{0}] RETRY FAILED for {1} after {2} retries",method,address,retries);
+                    Log.ErrorFormat("[{0}] RETRY FAILED for {1} after {2} retries",method,address,retries);
                 }
                 else
                 {
-                    Trace.TraceError("[{0}] FAILED for {1} with \n{2}", method, address, exc);
+                    Log.ErrorFormat("[{0}] FAILED for {1} with \n{2}", method, address, exc);
                 }
-                throw;
+                throw exc;
 
             }
             catch(Exception ex)
             {
-                Trace.TraceError("[{0}] FAILED for {1} with \n{2}", method, address, ex);
+                Log.ErrorFormat("[{0}] FAILED for {1} with \n{2}", method, address, ex);
                 throw;
             }
         }
         
-        /*private string RetryWithContent(string address, int retries, string method)
-        {
-            if (address == null)
-                throw new ArgumentNullException("address");
-
-            var actualAddress = GetActualAddress(address);            
-            var actualRetries = (retries == 0) ? Retries : retries;
-
-            var task = Retry(() =>
-            {
-                var uriString = String.Join("/",BaseAddress ,actualAddress);
-                var uri = new Uri(uriString);
-                
-                var request =  GetWebRequest(uri);
-                request.Method = method;                
-
-                if (ResponseHeaders!=null)
-                    ResponseHeaders.Clear();
-
-                TraceStart(method, uriString);
-
-                var getResponse = request.GetResponseAsync();
-                
-                var setStatus= getResponse.ContinueWith(t =>
-                {
-                    var response = (HttpWebResponse)t.Result;                    
-                    StatusCode = response.StatusCode;
-                    StatusDescription = response.StatusDescription;                
-                    return response;
-                });
-
-                var getData = setStatus.ContinueWith(t =>
-                {
-                    var response = t.Result;
-                    return response.GetResponseStream()
-                        .ReadAllBytesAsync();
-                }).Unwrap();
-
-                var data = getData.Result;
-                var content=Encoding.UTF8.GetString(data);
-
-//                var response = (HttpWebResponse)GetWebResponse(request);
-                
-                
-/*
-                StatusCode = response.StatusCode;
-                StatusDescription = response.StatusDescription;                
-#1#
-                
-
-                return content;
-            }, actualRetries);
-
-            return task.Result;
-        }*/
-
         private static void TraceStart(string method, string actualAddress)
         {
-            Trace.WriteLine(String.Format("[{0}] {1} {2}", method, DateTime.Now, actualAddress));
+            Log.InfoFormat("[{0}] {1} {2}", method, DateTime.Now, actualAddress);
         }
 
         private string GetActualAddress(string address)
@@ -348,9 +429,12 @@ namespace Pithos.Network
         /// <param name="source">The RestClient from which the headers are copied</param>
         public void CopyHeaders(RestClient source)
         {
-            Contract.Requires(source != null, "source can't be null");
             if (source == null)
                 throw new ArgumentNullException("source", "source can't be null");
+            Contract.EndContractBlock();
+            //The Headers getter initializes the property, it is never null
+            Contract.Assume(Headers!=null);
+                
             CopyHeaders(source.Headers,Headers);
         }
         
@@ -361,12 +445,12 @@ namespace Pithos.Network
         /// <param name="target">The target collection to which the headers are copied</param>
         public static void CopyHeaders(WebHeaderCollection source,WebHeaderCollection 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 (target == null)
                 throw new ArgumentNullException("target", "target can't be null");
+            Contract.EndContractBlock();
+
             for (int i = 0; i < source.Count; i++)
             {
                 target.Add(source.GetKey(i), source[i]);
@@ -382,6 +466,10 @@ namespace Pithos.Network
 
         private Task<T> Retry<T>(Func<T> original, int retryCount, TaskCompletionSource<T> tcs = null)
         {
+            if (original==null)
+                throw new ArgumentNullException("original");
+            Contract.EndContractBlock();
+
             if (tcs == null)
                 tcs = new TaskCompletionSource<T>();
             Task.Factory.StartNew(original).ContinueWith(_original =>
@@ -408,12 +496,12 @@ namespace Pithos.Network
                                 TimedOut = true;
                                 if (retryCount == 0)
                                 {                                    
-                                    Trace.TraceError("[ERROR] Timed out too many times. \n{0}\n",e);
+                                    Log.ErrorFormat("[ERROR] Timed out too many times. \n{0}\n",e);
                                     tcs.SetException(new RetryException("Timed out too many times.", e));                                    
                                 }
                                 else
                                 {
-                                    Trace.TraceError(
+                                    Log.ErrorFormat(
                                         "[RETRY] Timed out after {0} ms. Will retry {1} more times\n{2}", Timeout,
                                         retryCount, e);
                                     Retry(original, retryCount - 1, tcs);
@@ -429,6 +517,8 @@ namespace Pithos.Network
 
         private HttpStatusCode GetStatusCode(WebException we)
         {
+            if (we==null)
+                throw new ArgumentNullException("we");
             var statusCode = HttpStatusCode.RequestTimeout;
             if (we.Response != null)
             {
@@ -437,6 +527,27 @@ namespace Pithos.Network
             }
             return statusCode;
         }
+
+        public UriBuilder GetAddressBuilder(string container, string objectName)
+        {
+            var builder = new UriBuilder(String.Join("/", BaseAddress, container, objectName));
+            return builder;
+        }
+
+        public Dictionary<string, string> GetMeta(string metaPrefix)
+        {
+            if (String.IsNullOrWhiteSpace(metaPrefix))
+                throw new ArgumentNullException("metaPrefix");
+            Contract.EndContractBlock();
+
+            var keys = ResponseHeaders.AllKeys.AsQueryable();
+            var dict = (from key in keys
+                        where key.StartsWith(metaPrefix)
+                        let name = key.Substring(metaPrefix.Length)
+                        select new { Name = name, Value = ResponseHeaders[key] })
+                        .ToDictionary(t => t.Name, t => t.Value);
+            return dict;
+        }
     }
 
     public class RetryException:Exception