Added hammock project to debug streaming issues
[pithos-ms-client] / trunk / hammock / src / net35 / Hammock / RestBase.cs
1 using System;
2 using System.Collections.Generic;
3 using System.IO;
4 using System.Linq;
5 using System.Net;
6 using System.Text;
7 using Hammock.Authentication;
8 using Hammock.Caching;
9 using Hammock.Model;
10 using Hammock.Retries;
11 using Hammock.Serialization;
12 using Hammock.Tasks;
13 using Hammock.Web;
14 using Hammock.Streaming;
15
16 #if SILVERLIGHT
17 using Hammock.Silverlight.Compat;
18 #else
19 using System.Collections.Specialized;
20 #endif
21
22 namespace Hammock
23 {
24 #if !SILVERLIGHT
25     [Serializable]
26 #endif
27     public enum QueryHandling
28     {
29         /// <summary>
30         /// Query strings present in paths are left alone.
31         /// </summary>
32         None,
33         /// <summary>
34         /// Query string pairs present in paths are added to the parameter collection,
35         /// where they are appended back in the case of GET, HEAD, DELETE, and OPTIONS, or added to the
36         /// request body in the case of POST, or PUT.
37         /// </summary>
38         AppendToParameters
39     }
40
41 #if !SILVERLIGHT
42     [Serializable]
43 #endif
44     public abstract class RestBase : PropertyChangedBase
45     {
46         private byte[] _postContent;
47         private TaskOptions _taskOptions;
48         private RetryPolicy _retryPolicy;
49
50         public WebParameterCollection GetAllHeaders()
51         {
52             var headers = new WebParameterCollection();
53
54             var parameters = Headers.AllKeys.Select(key => new WebPair(key, Headers[key]));
55             foreach (var parameter in parameters)
56             {
57                 headers.Add(parameter.Name, parameter.Value);
58             }
59
60             return headers;
61         }
62
63         protected virtual internal NameValueCollection Headers { get; set; }
64         protected virtual internal WebParameterCollection Parameters { get; set; }
65         protected virtual internal WebParameterCollection Cookies { get; set; }
66         protected virtual internal ICollection<HttpPostParameter> PostParameters { get; set; }
67         protected internal virtual bool TraceEnabled { get; set; }
68         
69         protected virtual internal byte[] PostContent
70         {
71             get
72             {
73                 return _postContent;
74             }
75             set
76             {
77                 _postContent = value;
78                 if (value != null && (Method != WebMethod.Post && Method != WebMethod.Put))
79                 {
80                     Method = WebMethod.Post;
81                 }
82             }
83         }
84
85         public virtual Func<RestRequest, RestResponseBase, Type> GetErrorResponseEntityType { get; set; }
86         public virtual string UserAgent { get; set; }
87         public virtual WebMethod? Method { get; set; }
88         public virtual IWebCredentials Credentials { get; set; }
89         public virtual Encoding Encoding { get; set; }
90
91         protected RestBase()
92         {
93             Initialize();
94         }
95
96         private void Initialize()
97         {
98             Headers = new NameValueCollection(0);
99             Parameters = new WebParameterCollection();
100             Cookies = new WebParameterCollection(0);
101             PostParameters = new List<HttpPostParameter>(0);
102         }
103
104 #if !Silverlight
105         public virtual ServicePoint ServicePoint { get; set; }
106         public virtual bool? FollowRedirects { get; set; }
107 #endif
108
109         public virtual QueryHandling? QueryHandling { get; set; }
110         public virtual string Proxy { get; set; }
111         public virtual TimeSpan? Timeout { get; set; }
112         public virtual string VersionPath { get; set; }
113         public virtual ISerializer Serializer { get; set; }
114         public virtual IDeserializer Deserializer { get; set; }
115         public virtual ICache Cache { get; set; }
116         public virtual CacheOptions CacheOptions { get; set; }
117         public virtual RetryPolicy RetryPolicy
118         {
119             get { return _retryPolicy; }
120             set
121             {
122                 if (_retryPolicy == value)
123                 {
124                     return;
125                 }
126                 _retryPolicy = value;
127                 RetryState = new TaskState();
128             }
129         }
130
131         public virtual TaskOptions TaskOptions
132         {
133             get { return _taskOptions; }
134             set
135             {
136                 if (_taskOptions == value)
137                 {
138                     return;
139                 }
140                 _taskOptions = value;
141                 TaskState = new TaskState();
142             }
143         }
144
145         public virtual bool IsFirstIteration
146         {
147             get
148             {
149                 if (RetryState != null)
150                 {
151                     return RetryState.RepeatCount == 0;
152                 }
153                 if (TaskState != null)
154                 {
155                     return TaskState.RepeatCount == 0;
156                 }
157                 return true; 
158             }
159         }
160         
161         public virtual ITaskState TaskState { get; set; }
162         public virtual ITaskState RetryState { get; set; }
163         public virtual StreamOptions StreamOptions { get; set; }
164         public virtual Func<string> CacheKeyFunction { get; set; }
165         public virtual DecompressionMethods? DecompressionMethods { get; set; }
166         public virtual IWebQueryInfo Info { get; set; }
167         public virtual string Path { get; set; }
168         public virtual object Tag { get; set; }
169
170         public virtual void AddHeader(string name, string value)
171         {
172             Headers.Add(name, value);
173         }
174
175         public virtual void AddParameter(string name, string value)
176         {
177             Parameters.Add(name, value);
178         }
179
180         public virtual void AddCookie(string name, string value)
181         {
182             Cookies.Add(new HttpCookieParameter(name, value));
183         }
184
185         public virtual void AddCookie(Uri domain, string name, string value)
186         {
187             Cookies.Add(new HttpCookieParameter(name, value) { Domain = domain });
188         }
189
190         public virtual void AddField(string name, string value)
191         {
192             var field = new HttpPostParameter(name, value);
193             PostParameters.Add(field);
194         }
195
196         public virtual void AddFile(string name, string fileName, string filePath)
197         {
198             AddFile(name, fileName, filePath, "application/octet-stream", "form-data");
199         }
200
201         public virtual void AddFile(string name, string fileName, string filePath, string contentType)
202         {
203             AddFile(name, fileName, filePath, contentType, "form-data");
204         }
205
206         public virtual void AddFile(string name, string fileName, Stream stream)
207         {
208             AddFile(name, fileName, stream, "application/octet-stream", "form-data");
209         }
210
211         public virtual void AddFile(string name, string fileName, Stream stream, string contentType)
212         {
213             AddFile(name, fileName, stream, contentType, "form-data");
214         }
215
216         public virtual void AddFile(string name, string fileName, string filePath, string contentType, string contentDisposition)
217         {
218             var parameter = HttpPostParameter.CreateFile(name, fileName, filePath, contentType, contentDisposition);
219             PostParameters.Add(parameter);
220         }
221
222         public virtual void AddFile(string name, string fileName, Stream stream, string contentType, string contentDisposition)
223         {
224             var parameter = HttpPostParameter.CreateFile(name, fileName, stream, contentType, contentDisposition);
225             PostParameters.Add(parameter);
226         }
227
228         public virtual void AddPostContent(byte[] content)
229         {
230             if (PostContent == null)
231             {
232                 PostContent = content;
233             }
234             else
235             {
236                 var original = PostContent.Length;
237                 var current = content.Length;
238
239                 var final = new byte[current + original];
240                 Array.Copy(PostContent, 0, final, 0, original);
241                 Array.Copy(content, 0, final, original, current);
242
243                 PostContent = final;
244             }
245         }
246     }
247
248     public class RetryEventArgs : EventArgs
249     {
250         public virtual RestClient Client { get; set; }
251         public virtual RestRequest Request { get; set; }
252     }
253
254     public class FileProgressEventArgs : EventArgs
255     {
256         public virtual string FileName { get; set; }
257         public virtual long BytesWritten { get; set; }
258         public virtual long TotalBytes { get; set; }
259     }
260 }