Added hammock project to debug streaming issues
[pithos-ms-client] / trunk / hammock / src / net35 / Hammock / RestRequest.cs
1 using System;
2 using System.Net;
3 using System.Text;
4 using Hammock.Extensions;
5 using Hammock.Web;
6
7 #if SILVERLIGHT
8 using Hammock.Silverlight.Compat;
9 #endif
10
11 namespace Hammock
12 {
13 #if !Silverlight
14     [Serializable]
15 #endif
16     public class RestRequest : RestBase
17     {
18         private object _entity;
19         private object _expectEntity;
20
21         protected internal virtual Web.WebHeaderCollection ExpectHeaders { get; set; }
22         public virtual HttpStatusCode? ExpectStatusCode { get; set; }
23         public virtual string ExpectStatusDescription { get; set; }
24         public virtual string ExpectContent { get; set; }
25         public virtual string ExpectContentType { get; set; }
26         
27         public RestRequest()
28         {
29             Initialize();
30         }
31
32         private void Initialize()
33         {
34             ExpectHeaders = new Web.WebHeaderCollection();
35         }
36
37         public virtual object Entity
38         {
39             get
40             {
41                 return _entity;
42             }
43             set
44             {
45                 if (_entity != null && _entity.Equals(value))
46                 {
47                     return;
48                 }
49
50                 _entity = value;
51                 OnPropertyChanged("Entity");
52
53                 // [DC] Automatically posts an entity unless put is declared
54                 RequestEntityType = _entity.GetType();
55                 if (_entity != null && (Method != WebMethod.Post && Method != WebMethod.Put))
56                 {
57                     Method = WebMethod.Post;
58                 }
59             }
60         }
61
62         public virtual object ExpectEntity
63         {
64             get
65             {
66                 return _expectEntity;
67             }
68             set
69             {
70                 if (_expectEntity != null && _expectEntity.Equals(value))
71                 {
72                     return;
73                 }
74
75                 _expectEntity = value;
76                 OnPropertyChanged("ExpectEntity");
77             }
78         }
79
80         public virtual Type ResponseEntityType { get; set; }
81         public virtual Type RequestEntityType { get; set; }
82
83         public Uri BuildEndpoint(RestClient client)
84         {
85             var sb = new StringBuilder();
86
87             var path = Path.IsNullOrBlank()
88                            ? client.Path.IsNullOrBlank() ? "" : client.Path
89                            : Path;
90
91             var versionPath = VersionPath.IsNullOrBlank()
92                                   ? client.VersionPath.IsNullOrBlank() ? "" : client.VersionPath
93                                   : VersionPath;
94             var skipAuthority = client.Authority.IsNullOrBlank();
95
96             sb.Append(skipAuthority ? "" : client.Authority);
97             sb.Append(skipAuthority ? "" : client.Authority.EndsWith("/") ? "" : "/");
98             sb.Append(skipAuthority ? "" : versionPath.IsNullOrBlank() ? "" : versionPath);
99             if (!skipAuthority && !versionPath.IsNullOrBlank())
100             {
101                 sb.Append(versionPath.EndsWith("/") ? "" : "/");
102             }
103             sb.Append(path.IsNullOrBlank() ? "" : path.StartsWith("/") ? path.Substring(1) : path);
104
105             Uri uri;
106             Uri.TryCreate(sb.ToString(), UriKind.RelativeOrAbsolute, out uri);
107
108             var queryStringHandling = QueryHandling ?? client.QueryHandling ?? Hammock.QueryHandling.None;
109
110             switch (queryStringHandling)
111             {
112                 case Hammock.QueryHandling.AppendToParameters:
113                     WebParameterCollection parameters;
114                     uri = uri.UriMinusQuery(out parameters);
115                     foreach (var parameter in parameters)
116                     {
117                         Parameters.Add(parameter);
118                     }
119                     break;
120                 default:
121                     break;
122             }
123             
124             return uri;
125         }
126
127         public void ExpectHeader(string name, string value)
128         {
129             ExpectHeaders.Add(name, value);
130         }
131     }
132 }
133
134