1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
18 using System
.Collections
;
19 using System
.Collections
.Specialized
;
21 using Castle
.Components
.Binder
;
24 /// This class adapts the <c>HttpRequest</c> to a MonoRail <c>IRequest</c>.
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
;
34 /// Initializes a new instance of the <see cref="RequestAdapter"/> class.
36 /// <param name="request">The request.</param>
37 public RequestAdapter(HttpRequest request
)
39 this.request
= request
;
43 /// Gets the request URL.
48 get { return RawUrl; }
52 /// Gets the accept header.
54 /// <value>The accept header.</value>
55 public string AcceptHeader
57 get { return request.Headers["Accept"]; }
61 /// Gets the referring URL.
64 public String UrlReferrer
68 Uri referrer
= request
.UrlReferrer
;
72 return referrer
.ToString();
82 /// Gets a value indicating whether this requeest is from a local address.
84 /// <value><c>true</c> if this instance is local; otherwise, <c>false</c>.</value>
87 get { return request.Url.IsLoopback; }
91 /// Gets the request type (GET, POST, etc)
94 public string RequestType
96 get { return HttpMethod; }
100 /// Gets the HTTP method (GET, POST, etc).
102 /// <value>The HTTP method.</value>
103 public string HttpMethod
105 get { return request.HttpMethod; }
111 /// <value>The URI.</value>
114 get { return request.Url; }
118 /// Gets additional path information for
119 /// a resource with a URL extension.
121 /// <value>The path info.</value>
122 public String PathInfo
124 get { return request.PathInfo; }
128 /// Gets the raw URL.
130 /// <value>The raw URL.</value>
133 get { return request.RawUrl; }
137 /// Gets the file path.
139 /// <value>The file path.</value>
140 public String FilePath
142 get { return request.FilePath; }
146 /// Gets the Http headers.
148 /// <value>The Http headers.</value>
149 public NameValueCollection Headers
151 get { return request.Headers; }
155 /// Gets the params which accumulates headers, post, querystring and cookies.
157 /// <value>The params.</value>
158 public NameValueCollection Params
160 get { return request.Params; }
164 /// Gets the query string.
166 /// <value>The query string.</value>
167 public NameValueCollection QueryString
169 get { return request.QueryString; }
175 /// <value>The form.</value>
176 public NameValueCollection Form
178 get { return request.Form; }
182 /// Indexer to access <see cref="Params"/> entries.
185 public string this[string name
]
187 get { return request[name]; }
191 // /// Reads the request data as a byte array.
193 // /// <param name="count">How many bytes.</param>
194 // /// <returns></returns>
195 // public byte[] BinaryRead(int count)
197 // return request.BinaryRead(count);
202 /// Gets the <see cref="HttpPostedFile"/> per key.
205 public IDictionary Files
211 files
= new FileDictionaryAdapter(request
.Files
);
218 /// Gets the user languages.
220 /// <value>The user languages.</value>
221 public String
[] UserLanguages
223 get { return request.UserLanguages; }
227 /// Gets the IP host address of the remote client.
229 /// <value>The IP address of the remote client.</value>
230 public string UserHostAddress
232 get { return request.UserHostAddress; }
236 /// Reads the cookie.
238 /// <param name="name">The cookie name.</param>
239 /// <returns></returns>
240 public String
ReadCookie(String name
)
242 HttpCookie cookie
= request
.Cookies
[name
];
251 /// Lazy initialized property with a hierarchical
252 /// representation of the flat data on <see cref="Controller.Params"/>
254 public CompositeNode ParamsNode
258 if (paramsNode
== null)
260 paramsNode
= treeBuilder
.BuildSourceNode(Params
);
261 treeBuilder
.PopulateTree(paramsNode
, request
.Files
);
269 /// Lazy initialized property with a hierarchical
270 /// representation of the flat data on <see cref="IRequest.Form"/>
272 public CompositeNode FormNode
276 if (formNode
== null)
278 formNode
= treeBuilder
.BuildSourceNode(Form
);
279 treeBuilder
.PopulateTree(formNode
, request
.Files
);
287 /// Lazy initialized property with a hierarchical
288 /// representation of the flat data on <see cref="IRequest.QueryString"/>
290 public CompositeNode QueryStringNode
294 if (queryStringNode
== null)
296 queryStringNode
= treeBuilder
.BuildSourceNode(QueryString
);
297 treeBuilder
.PopulateTree(queryStringNode
, request
.Files
);
300 return queryStringNode
;
305 /// This method is for internal use only
307 /// <param name="from"></param>
308 /// <returns></returns>
309 public CompositeNode
ObtainParamsNode(ParamStore
from)
313 case ParamStore
.Form
:
315 case ParamStore
.QueryString
:
316 return QueryStringNode
;
323 /// Validates the input.
325 public void ValidateInput()
327 request
.ValidateInput();