Statistics
| Branch: | Revision:

root / trunk / Pithos.Network / RestClient.cs @ 255f5f86

History | View | Annotate | Download (19.6 kB)

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

    
51

    
52
namespace Pithos.Network
53
{
54
    using System;
55
    using System.Collections.Generic;
56
    using System.Linq;
57
    using System.Text;
58

    
59
    /// <summary>
60
    /// TODO: Update summary.
61
    /// </summary>
62
    public class RestClient:WebClient
63
    {
64
        public int Timeout { get; set; }
65

    
66
        public bool TimedOut { get; set; }
67

    
68
        public HttpStatusCode StatusCode { get; private set; }
69

    
70
        public string StatusDescription { get; set; }
71

    
72
        public long? RangeFrom { get; set; }
73
        public long? RangeTo { get; set; }
74

    
75
        public int Retries { get; set; }
76

    
77
        private readonly Dictionary<string, string> _parameters=new Dictionary<string, string>();
78
        public Dictionary<string, string> Parameters
79
        {
80
            get
81
            {
82
                Contract.Ensures(_parameters!=null);
83
                return _parameters;
84
            }            
85
        }
86

    
87
        private static readonly ILog Log = LogManager.GetLogger("RestClient");
88

    
89

    
90
        [ContractInvariantMethod]
91
        private void Invariants()
92
        {
93
            Contract.Invariant(Headers!=null);    
94
        }
95

    
96
        public RestClient():base()
97
        {
98
            
99
        }
100

    
101
       
102
        public RestClient(RestClient other)
103
            : base()
104
        {
105
            if (other==null)
106
                throw new ArgumentNullException("other");
107
            Contract.EndContractBlock();
108

    
109
            CopyHeaders(other);
110
            Timeout = other.Timeout;
111
            Retries = other.Retries;
112
            BaseAddress = other.BaseAddress;             
113

    
114
            foreach (var parameter in other.Parameters)
115
            {
116
                Parameters.Add(parameter.Key,parameter.Value);
117
            }
118

    
119
            this.Proxy = other.Proxy;
120
        }
121

    
122

    
123
        protected override WebRequest GetWebRequest(Uri address)
124
        {
125
            TimedOut = false;
126
            var webRequest = base.GetWebRequest(address);            
127
            var request = (HttpWebRequest)webRequest;
128
            request.ServicePoint.ConnectionLimit = 50;
129
            if (IfModifiedSince.HasValue)
130
                request.IfModifiedSince = IfModifiedSince.Value;
131
            request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
132
            if(Timeout>0)
133
                request.Timeout = Timeout;
134

    
135
            if (RangeFrom.HasValue)
136
            {
137
                if (RangeTo.HasValue)
138
                    request.AddRange(RangeFrom.Value, RangeTo.Value);
139
                else
140
                    request.AddRange(RangeFrom.Value);
141
            }
142
            return request; 
143
        }
144

    
145
        public DateTime? IfModifiedSince { get; set; }
146

    
147
        //Asynchronous version
148
        protected override WebResponse GetWebResponse(WebRequest request, IAsyncResult result)
149
        {
150
            Log.InfoFormat("ASYNC [{0}] {1}",request.Method, request.RequestUri);
151
            HttpWebResponse response = null;
152

    
153
            try
154
            {
155
                response = (HttpWebResponse)base.GetWebResponse(request, result);
156
            }
157
            catch (WebException exc)
158
            {
159
                if (!TryGetResponse(exc, out response))
160
                    throw;
161
            }
162

    
163
            StatusCode = response.StatusCode;
164
            LastModified = response.LastModified;
165
            StatusDescription = response.StatusDescription;
166
            return response;
167

    
168
        }
169
      
170

    
171
        //Synchronous version
172
        protected override WebResponse GetWebResponse(WebRequest request)
173
        {
174
            HttpWebResponse response = null;
175
            try
176
            {                                
177
                response = (HttpWebResponse)base.GetWebResponse(request);
178
            }
179
            catch (WebException exc)
180
            {
181
                if (!TryGetResponse(exc, out response))
182
                    throw;
183
            }
184

    
185
            StatusCode = response.StatusCode;
186
            LastModified = response.LastModified;
187
            StatusDescription = response.StatusDescription;
188
            return response;
189
        }
190

    
191
        private bool TryGetResponse(WebException exc, out HttpWebResponse response)
192
        {
193
            response = null;
194
            //Fail on empty response
195
            if (exc.Response == null)
196
                return false;
197

    
198
            response = (exc.Response as HttpWebResponse);
199
            //Succeed on allowed status codes
200
            if (AllowedStatusCodes.Contains(response.StatusCode))
201
                return true;
202

    
203
            //Does the response have any content to log?
204
            if (exc.Response.ContentLength > 0)
205
            {
206
                var content = LogContent(exc.Response);
207
                Log.ErrorFormat(content);
208
            }
209
            return false;
210
        }
211

    
212
        private readonly List<HttpStatusCode> _allowedStatusCodes=new List<HttpStatusCode>{HttpStatusCode.NotModified};        
213

    
214
        public List<HttpStatusCode> AllowedStatusCodes
215
        {
216
            get
217
            {
218
                return _allowedStatusCodes;
219
            }            
220
        }
221

    
222
        public DateTime LastModified { get; private set; }
223

    
224
        private static string LogContent(WebResponse webResponse)
225
        {
226
            if (webResponse == null)
227
                throw new ArgumentNullException("webResponse");
228
            Contract.EndContractBlock();
229

    
230
            //The response stream must be copied to avoid affecting other code by disposing of the 
231
            //original response stream.
232
            var stream = webResponse.GetResponseStream();            
233
            using(var memStream=new MemoryStream())
234
            using (var reader = new StreamReader(memStream))
235
            {
236
                stream.CopyTo(memStream);                
237
                string content = reader.ReadToEnd();
238

    
239
                stream.Seek(0,SeekOrigin.Begin);
240
                return content;
241
            }
242
        }
243

    
244
        public string DownloadStringWithRetry(string address,int retries=0)
245
        {
246
            
247
            if (address == null)
248
                throw new ArgumentNullException("address");
249

    
250
            var actualAddress = GetActualAddress(address);
251

    
252
            TraceStart("GET",actualAddress);            
253
            
254
            var actualRetries = (retries == 0) ? Retries : retries;
255

    
256
            var uriString = String.Join("/", BaseAddress.TrimEnd('/'), actualAddress);
257

    
258
            var task = Retry(() =>
259
            {                
260
                var content = base.DownloadString(uriString);
261

    
262
                if (StatusCode == HttpStatusCode.NoContent)
263
                    return String.Empty;
264
                return content;
265

    
266
            }, actualRetries);
267

    
268
            try
269
            {
270
                var result = task.Result;
271
                return result;
272

    
273
            }
274
            catch (AggregateException exc)
275
            {
276
                //If the task fails, propagate the original exception
277
                if (exc.InnerException!=null)
278
                    throw exc.InnerException;
279
                throw;
280
            }
281
        }
282

    
283
        public void Head(string address,int retries=0)
284
        {
285
            AllowedStatusCodes.Add(HttpStatusCode.NotFound);
286
            RetryWithoutContent(address, retries, "HEAD");
287
        }
288

    
289
        public void PutWithRetry(string address, int retries = 0)
290
        {
291
            RetryWithoutContent(address, retries, "PUT");
292
        }
293

    
294
        public void DeleteWithRetry(string address,int retries=0)
295
        {
296
            RetryWithoutContent(address, retries, "DELETE");
297
        }
298

    
299
        public string GetHeaderValue(string headerName,bool optional=false)
300
        {
301
            if (this.ResponseHeaders==null)
302
                throw new InvalidOperationException("ResponseHeaders are null");
303
            Contract.EndContractBlock();
304

    
305
            var values=this.ResponseHeaders.GetValues(headerName);
306
            if (values != null)
307
                return values[0];
308

    
309
            if (optional)            
310
                return null;            
311
            //A required header was not found
312
            throw new WebException(String.Format("The {0}  header is missing", headerName));
313
        }
314

    
315
        public void SetNonEmptyHeaderValue(string headerName, string value)
316
        {
317
            if (String.IsNullOrWhiteSpace(value))
318
                return;
319
            Headers.Add(headerName,value);
320
        }
321

    
322
        private void RetryWithoutContent(string address, int retries, string method)
323
        {
324
            if (address == null)
325
                throw new ArgumentNullException("address");
326

    
327
            var actualAddress = GetActualAddress(address);            
328
            var actualRetries = (retries == 0) ? Retries : retries;
329

    
330
            var task = Retry(() =>
331
            {
332
                var uriString = String.Join("/",BaseAddress ,actualAddress);
333
                var uri = new Uri(uriString);
334
                var request =  GetWebRequest(uri);
335
                request.Method = method;
336
                if (ResponseHeaders!=null)
337
                    ResponseHeaders.Clear();
338

    
339
                TraceStart(method, uriString);
340
                if (method == "PUT")
341
                    request.ContentLength = 0;
342

    
343
                //Have to use try/finally instead of using here, because WebClient needs a valid WebResponse object
344
                //in order to return response headers
345
                var response = (HttpWebResponse)GetWebResponse(request);
346
                try
347
                {
348
                    LastModified = response.LastModified;
349
                    StatusCode = response.StatusCode;
350
                    StatusDescription = response.StatusDescription;
351
                }
352
                finally
353
                {
354
                    response.Close();
355
                }
356
                
357

    
358
                return 0;
359
            }, actualRetries);
360

    
361
            try
362
            {
363
                task.Wait();
364
            }
365
            catch (AggregateException ex)
366
            {
367
                var exc = ex.InnerException;
368
                if (exc is RetryException)
369
                {
370
                    Log.ErrorFormat("[{0}] RETRY FAILED for {1} after {2} retries",method,address,retries);
371
                }
372
                else
373
                {
374
                    Log.ErrorFormat("[{0}] FAILED for {1} with \n{2}", method, address, exc);
375
                }
376
                throw exc;
377

    
378
            }
379
            catch(Exception ex)
380
            {
381
                Log.ErrorFormat("[{0}] FAILED for {1} with \n{2}", method, address, ex);
382
                throw;
383
            }
384
        }
385
        
386
        private static void TraceStart(string method, string actualAddress)
387
        {
388
            Log.InfoFormat("[{0}] {1} {2}", method, DateTime.Now, actualAddress);
389
        }
390

    
391
        private string GetActualAddress(string address)
392
        {
393
            if (Parameters.Count == 0)
394
                return address;
395
            var addressBuilder=new StringBuilder(address);            
396

    
397
            bool isFirst = true;
398
            foreach (var parameter in Parameters)
399
            {
400
                if(isFirst)
401
                    addressBuilder.AppendFormat("?{0}={1}", parameter.Key, parameter.Value);
402
                else
403
                    addressBuilder.AppendFormat("&{0}={1}", parameter.Key, parameter.Value);
404
                isFirst = false;
405
            }
406
            return addressBuilder.ToString();
407
        }
408

    
409
        public string DownloadStringWithRetry(Uri address,int retries=0)
410
        {
411
            if (address == null)
412
                throw new ArgumentNullException("address");
413

    
414
            var actualRetries = (retries == 0) ? Retries : retries;            
415
            var task = Retry(() =>
416
            {
417
                var content = base.DownloadString(address);
418

    
419
                if (StatusCode == HttpStatusCode.NoContent)
420
                    return String.Empty;
421
                return content;
422

    
423
            }, actualRetries);
424

    
425
            var result = task.Result;
426
            return result;
427
        }
428

    
429
      
430
        /// <summary>
431
        /// Copies headers from another RestClient
432
        /// </summary>
433
        /// <param name="source">The RestClient from which the headers are copied</param>
434
        public void CopyHeaders(RestClient source)
435
        {
436
            if (source == null)
437
                throw new ArgumentNullException("source", "source can't be null");
438
            Contract.EndContractBlock();
439
            //The Headers getter initializes the property, it is never null
440
            Contract.Assume(Headers!=null);
441
                
442
            CopyHeaders(source.Headers,Headers);
443
        }
444
        
445
        /// <summary>
446
        /// Copies headers from one header collection to another
447
        /// </summary>
448
        /// <param name="source">The source collection from which the headers are copied</param>
449
        /// <param name="target">The target collection to which the headers are copied</param>
450
        public static void CopyHeaders(WebHeaderCollection source,WebHeaderCollection target)
451
        {
452
            if (source == null)
453
                throw new ArgumentNullException("source", "source can't be null");
454
            if (target == null)
455
                throw new ArgumentNullException("target", "target can't be null");
456
            Contract.EndContractBlock();
457

    
458
            for (int i = 0; i < source.Count; i++)
459
            {
460
                target.Add(source.GetKey(i), source[i]);
461
            }            
462
        }
463

    
464
        public void AssertStatusOK(string message)
465
        {
466
            if (StatusCode >= HttpStatusCode.BadRequest)
467
                throw new WebException(String.Format("{0} with code {1} - {2}", message, StatusCode, StatusDescription));
468
        }
469

    
470

    
471
        private Task<T> Retry<T>(Func<T> original, int retryCount, TaskCompletionSource<T> tcs = null)
472
        {
473
            if (original==null)
474
                throw new ArgumentNullException("original");
475
            Contract.EndContractBlock();
476

    
477
            if (tcs == null)
478
                tcs = new TaskCompletionSource<T>();
479
            Task.Factory.StartNew(original).ContinueWith(_original =>
480
                {
481
                    if (!_original.IsFaulted)
482
                        tcs.SetFromTask(_original);
483
                    else 
484
                    {
485
                        var e = _original.Exception.InnerException;
486
                        var we = (e as WebException);
487
                        if (we==null)
488
                            tcs.SetException(e);
489
                        else
490
                        {
491
                            var statusCode = GetStatusCode(we);
492

    
493
                            //Return null for 404
494
                            if (statusCode == HttpStatusCode.NotFound)
495
                                tcs.SetResult(default(T));
496
                            //Retry for timeouts and service unavailable
497
                            else if (we.Status == WebExceptionStatus.Timeout ||
498
                                (we.Status == WebExceptionStatus.ProtocolError && statusCode == HttpStatusCode.ServiceUnavailable))
499
                            {
500
                                TimedOut = true;
501
                                if (retryCount == 0)
502
                                {                                    
503
                                    Log.ErrorFormat("[ERROR] Timed out too many times. \n{0}\n",e);
504
                                    tcs.SetException(new RetryException("Timed out too many times.", e));                                    
505
                                }
506
                                else
507
                                {
508
                                    Log.ErrorFormat(
509
                                        "[RETRY] Timed out after {0} ms. Will retry {1} more times\n{2}", Timeout,
510
                                        retryCount, e);
511
                                    Retry(original, retryCount - 1, tcs);
512
                                }
513
                            }
514
                            else
515
                                tcs.SetException(e);
516
                        }
517
                    };
518
                });
519
            return tcs.Task;
520
        }
521

    
522
        private HttpStatusCode GetStatusCode(WebException we)
523
        {
524
            if (we==null)
525
                throw new ArgumentNullException("we");
526
            var statusCode = HttpStatusCode.RequestTimeout;
527
            if (we.Response != null)
528
            {
529
                statusCode = ((HttpWebResponse) we.Response).StatusCode;
530
                this.StatusCode = statusCode;
531
            }
532
            return statusCode;
533
        }
534

    
535
        public UriBuilder GetAddressBuilder(string container, string objectName)
536
        {
537
            var builder = new UriBuilder(String.Join("/", BaseAddress, container, objectName));
538
            return builder;
539
        }
540

    
541
        public Dictionary<string, string> GetMeta(string metaPrefix)
542
        {
543
            if (String.IsNullOrWhiteSpace(metaPrefix))
544
                throw new ArgumentNullException("metaPrefix");
545
            Contract.EndContractBlock();
546

    
547
            var keys = ResponseHeaders.AllKeys.AsQueryable();
548
            var dict = (from key in keys
549
                        where key.StartsWith(metaPrefix)
550
                        let name = key.Substring(metaPrefix.Length)
551
                        select new { Name = name, Value = ResponseHeaders[key] })
552
                        .ToDictionary(t => t.Name, t => t.Value);
553
            return dict;
554
        }
555
    }
556

    
557
    public class RetryException:Exception
558
    {
559
        public RetryException()
560
            :base()
561
        {
562
            
563
        }
564

    
565
        public RetryException(string message)
566
            :base(message)
567
        {
568
            
569
        }
570

    
571
        public RetryException(string message,Exception innerException)
572
            :base(message,innerException)
573
        {
574
            
575
        }
576

    
577
        public RetryException(SerializationInfo info,StreamingContext context)
578
            :base(info,context)
579
        {
580
            
581
        }
582
    }
583
}