More working tests.
[castle.git] / MonoRail / Castle.MonoRail.Framework / Adapters / RequestAdapter.cs
blob99443d247086272de4275a6f405a05b6a401fe8b
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.MonoRail.Framework.Adapters
17 using System;
18 using System.Collections;
19 using System.Collections.Specialized;
20 using System.Web;
21 using Castle.Components.Binder;
23 /// <summary>
24 /// This class adapts the <c>HttpRequest</c> to a MonoRail <c>IRequest</c>.
25 /// </summary>
26 public class RequestAdapter : IRequest
28 private TreeBuilder treeBuilder = new TreeBuilder();
29 private HttpRequest request;
30 private CompositeNode paramsNode, formNode, queryStringNode;
31 private FileDictionaryAdapter files;
33 /// <summary>
34 /// Initializes a new instance of the <see cref="RequestAdapter"/> class.
35 /// </summary>
36 /// <param name="request">The request.</param>
37 public RequestAdapter(HttpRequest request)
39 this.request = request;
42 /// <summary>
43 /// Gets the request URL.
44 /// </summary>
45 /// <value></value>
46 public string Url
48 get { return RawUrl; }
51 /// <summary>
52 /// Gets the accept header.
53 /// </summary>
54 /// <value>The accept header.</value>
55 public string AcceptHeader
57 get { return request.Headers["Accept"]; }
60 /// <summary>
61 /// Gets the referring URL.
62 /// </summary>
63 /// <value></value>
64 public String UrlReferrer
66 get
68 Uri referrer = request.UrlReferrer;
70 if (referrer != null)
72 return referrer.ToString();
74 else
76 return null;
81 /// <summary>
82 /// Gets a value indicating whether this requeest is from a local address.
83 /// </summary>
84 /// <value><c>true</c> if this instance is local; otherwise, <c>false</c>.</value>
85 public bool IsLocal
87 get { return request.Url.IsLoopback; }
90 /// <summary>
91 /// Gets the request type (GET, POST, etc)
92 /// </summary>
93 /// <value></value>
94 public string RequestType
96 get { return HttpMethod; }
99 /// <summary>
100 /// Gets the HTTP method (GET, POST, etc).
101 /// </summary>
102 /// <value>The HTTP method.</value>
103 public string HttpMethod
105 get { return request.HttpMethod; }
108 /// <summary>
109 /// Gets the URI.
110 /// </summary>
111 /// <value>The URI.</value>
112 public Uri Uri
114 get { return request.Url; }
117 /// <summary>
118 /// Gets additional path information for
119 /// a resource with a URL extension.
120 /// </summary>
121 /// <value>The path info.</value>
122 public String PathInfo
124 get { return request.PathInfo; }
127 /// <summary>
128 /// Gets the raw URL.
129 /// </summary>
130 /// <value>The raw URL.</value>
131 public String RawUrl
133 get { return request.RawUrl; }
136 /// <summary>
137 /// Gets the file path.
138 /// </summary>
139 /// <value>The file path.</value>
140 public String FilePath
142 get { return request.FilePath; }
145 /// <summary>
146 /// Gets the Http headers.
147 /// </summary>
148 /// <value>The Http headers.</value>
149 public NameValueCollection Headers
151 get { return request.Headers; }
154 /// <summary>
155 /// Gets the params which accumulates headers, post, querystring and cookies.
156 /// </summary>
157 /// <value>The params.</value>
158 public NameValueCollection Params
160 get { return request.Params; }
163 /// <summary>
164 /// Gets the query string.
165 /// </summary>
166 /// <value>The query string.</value>
167 public NameValueCollection QueryString
169 get { return request.QueryString; }
172 /// <summary>
173 /// Gets the form.
174 /// </summary>
175 /// <value>The form.</value>
176 public NameValueCollection Form
178 get { return request.Form; }
181 /// <summary>
182 /// Indexer to access <see cref="Params"/> entries.
183 /// </summary>
184 /// <value></value>
185 public string this[string name]
187 get { return request[name]; }
190 // /// <summary>
191 // /// Reads the request data as a byte array.
192 // /// </summary>
193 // /// <param name="count">How many bytes.</param>
194 // /// <returns></returns>
195 // public byte[] BinaryRead(int count)
196 // {
197 // return request.BinaryRead(count);
198 // }
201 /// <summary>
202 /// Gets the <see cref="HttpPostedFile"/> per key.
203 /// </summary>
204 /// <value></value>
205 public IDictionary Files
209 if (files == null)
211 files = new FileDictionaryAdapter(request.Files);
213 return files;
217 /// <summary>
218 /// Gets the user languages.
219 /// </summary>
220 /// <value>The user languages.</value>
221 public String[] UserLanguages
223 get { return request.UserLanguages; }
226 /// <summary>
227 /// Gets the IP host address of the remote client.
228 /// </summary>
229 /// <value>The IP address of the remote client.</value>
230 public string UserHostAddress
232 get { return request.UserHostAddress; }
235 /// <summary>
236 /// Reads the cookie.
237 /// </summary>
238 /// <param name="name">The cookie name.</param>
239 /// <returns></returns>
240 public String ReadCookie(String name)
242 HttpCookie cookie = request.Cookies[name];
243 if (cookie == null)
245 return null;
247 return cookie.Value;
250 /// <summary>
251 /// Lazy initialized property with a hierarchical
252 /// representation of the flat data on <see cref="Controller.Params"/>
253 /// </summary>
254 public CompositeNode ParamsNode
258 if (paramsNode == null)
260 paramsNode = treeBuilder.BuildSourceNode(Params);
261 treeBuilder.PopulateTree(paramsNode, request.Files);
264 return paramsNode;
268 /// <summary>
269 /// Lazy initialized property with a hierarchical
270 /// representation of the flat data on <see cref="IRequest.Form"/>
271 /// </summary>
272 public CompositeNode FormNode
276 if (formNode == null)
278 formNode = treeBuilder.BuildSourceNode(Form);
279 treeBuilder.PopulateTree(formNode, request.Files);
282 return formNode;
286 /// <summary>
287 /// Lazy initialized property with a hierarchical
288 /// representation of the flat data on <see cref="IRequest.QueryString"/>
289 /// </summary>
290 public CompositeNode QueryStringNode
294 if (queryStringNode == null)
296 queryStringNode = treeBuilder.BuildSourceNode(QueryString);
297 treeBuilder.PopulateTree(queryStringNode, request.Files);
300 return queryStringNode;
304 /// <summary>
305 /// This method is for internal use only
306 /// </summary>
307 /// <param name="from"></param>
308 /// <returns></returns>
309 public CompositeNode ObtainParamsNode(ParamStore from)
311 switch (from)
313 case ParamStore.Form:
314 return FormNode;
315 case ParamStore.QueryString:
316 return QueryStringNode;
317 default:
318 return ParamsNode;
322 /// <summary>
323 /// Validates the input.
324 /// </summary>
325 public void ValidateInput()
327 request.ValidateInput();