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 using Castle
.MonoRail
.Framework
;
17 namespace Castle
.MonoRail
.Framework
.Adapters
20 using System
.Collections
;
21 using System
.Collections
.Specialized
;
24 using Castle
.Components
.Binder
;
27 /// This class adapts the <c>HttpRequest</c> to a MonoRail <c>IRequest</c>.
29 public class RequestAdapter
: IRequest
31 private TreeBuilder treeBuilder
= new TreeBuilder();
32 private HttpRequest request
;
33 private FileDictionaryAdapter files
;
36 /// Lazy initialized property with a hierarchical
37 /// representation of the flat data on <see cref="Controller.Params"/>
39 protected CompositeNode paramsCompositeNode
;
42 /// Lazy initialized property with a hierarchical
43 /// representation of the flat data on <see cref="IRequest.Form"/>
45 protected CompositeNode formCompositeNode
;
48 /// Lazy initialized property with a hierarchical
49 /// representation of the flat data on <see cref="IRequest.QueryString"/>
51 protected CompositeNode queryStringCompositeNode
;
54 /// Initializes a new instance of the <see cref="RequestAdapter"/> class.
56 /// <param name="request">The request.</param>
57 public RequestAdapter(HttpRequest request
)
59 this.request
= request
;
63 /// Gets the request URL.
68 get { return RawUrl; }
72 /// Gets the accept header.
74 /// <value>The accept header.</value>
75 public string AcceptHeader
77 get { return request.Headers["Accept"]; }
81 /// Gets the referring URL.
84 public String UrlReferrer
88 Uri referrer
= request
.UrlReferrer
;
92 return referrer
.ToString();
102 /// Gets a value indicating whether this requeest is from a local address.
104 /// <value><c>true</c> if this instance is local; otherwise, <c>false</c>.</value>
107 get { return request.Url.IsLoopback; }
111 /// Gets the contents of the incoming HTTP entity body.
114 public Stream InputStream
116 get { return request.InputStream; }
120 /// Gets or sets the MIME content type of the incoming request.
123 public string ContentType
125 get { return request.ContentType; }
126 set { request.ContentType = value; }
130 /// Gets the request type (GET, POST, etc)
133 public string RequestType
135 get { return HttpMethod; }
139 /// Gets the HTTP method (GET, POST, etc).
141 /// <value>The HTTP method.</value>
142 public string HttpMethod
144 get { return request.HttpMethod; }
150 /// <value>The URI.</value>
153 get { return request.Url; }
157 /// Gets additional path information for
158 /// a resource with a URL extension.
160 /// <value>The path info.</value>
161 public String PathInfo
163 get { return request.PathInfo; }
167 /// Gets the raw URL.
169 /// <value>The raw URL.</value>
172 get { return request.RawUrl; }
176 /// Gets the file path.
178 /// <value>The file path.</value>
179 public String FilePath
181 get { return request.FilePath; }
185 /// Gets the Http headers.
187 /// <value>The Http headers.</value>
188 public NameValueCollection Headers
190 get { return request.Headers; }
194 /// Gets the params which accumulates headers, post, querystring and cookies.
196 /// <value>The params.</value>
197 public NameValueCollection Params
199 get { return request.Params; }
203 /// Gets the query string.
205 /// <value>The query string.</value>
206 public NameValueCollection QueryString
208 get { return request.QueryString; }
214 /// <value>The form.</value>
215 public NameValueCollection Form
217 get { return request.Form; }
221 /// Indexer to access <see cref="Params"/> entries.
224 public string this[string name
]
226 get { return request[name]; }
230 // /// Reads the request data as a byte array.
232 // /// <param name="count">How many bytes.</param>
233 // /// <returns></returns>
234 // public byte[] BinaryRead(int count)
236 // return request.BinaryRead(count);
241 /// Gets the <see cref="HttpPostedFile"/> per key.
244 public IDictionary Files
250 files
= new FileDictionaryAdapter(request
.Files
);
257 /// Gets the user languages.
259 /// <value>The user languages.</value>
260 public String
[] UserLanguages
262 get { return request.UserLanguages; }
266 /// Gets the IP host address of the remote client.
268 /// <value>The IP address of the remote client.</value>
269 public string UserHostAddress
271 get { return request.UserHostAddress; }
275 /// Reads the cookie.
277 /// <param name="name">The cookie name.</param>
278 /// <returns></returns>
279 public String
ReadCookie(String name
)
281 HttpCookie cookie
= request
.Cookies
[name
];
290 /// Lazy initialized property with a hierarchical
291 /// representation of the flat data on <see cref="Controller.Params"/>
293 public virtual CompositeNode ParamsNode
297 if (paramsCompositeNode
== null)
299 paramsCompositeNode
= treeBuilder
.BuildSourceNode(Params
);
300 treeBuilder
.PopulateTree(paramsCompositeNode
, request
.Files
);
303 return paramsCompositeNode
;
308 /// Lazy initialized property with a hierarchical
309 /// representation of the flat data on <see cref="IRequest.Form"/>
311 public virtual CompositeNode FormNode
315 if (formCompositeNode
== null)
317 formCompositeNode
= treeBuilder
.BuildSourceNode(Form
);
318 treeBuilder
.PopulateTree(formCompositeNode
, request
.Files
);
321 return formCompositeNode
;
326 /// Lazy initialized property with a hierarchical
327 /// representation of the flat data on <see cref="IRequest.QueryString"/>
329 public virtual CompositeNode QueryStringNode
333 if (queryStringCompositeNode
== null)
335 queryStringCompositeNode
= treeBuilder
.BuildSourceNode(QueryString
);
336 treeBuilder
.PopulateTree(queryStringCompositeNode
, request
.Files
);
339 return queryStringCompositeNode
;
344 /// This method is for internal use only
346 /// <param name="from"></param>
347 /// <returns></returns>
348 public CompositeNode
ObtainParamsNode(ParamStore
from)
352 case ParamStore
.Form
:
354 case ParamStore
.QueryString
:
355 return QueryStringNode
;
362 /// Validates the input.
364 public void ValidateInput()
366 request
.ValidateInput();