More working tests.
[castle.git] / MonoRail / Castle.MonoRail.Framework / Internal / BaseResponse.cs
blob205ebc78f9dface6bd1d8d14522de4ad43cd8645
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.Internal
17 using System;
18 using System.Collections;
19 using System.Collections.Specialized;
20 using System.IO;
21 using System.Web;
22 using Core;
23 using Helpers;
24 using Routing;
25 using Services;
26 using Test;
28 /// <summary>
29 ///
30 /// </summary>
31 public abstract class BaseResponse : IMockResponse
33 private readonly UrlInfo currentUrl;
34 private readonly IServerUtility serverUtility;
35 private readonly RouteMatch routeMatch;
36 private IUrlBuilder urlBuilder;
37 /// <summary>
38 /// Indicates if a redirected has been issued
39 /// </summary>
40 protected bool redirected;
41 private UrlInfo urlInfo;
43 /// <summary>
44 /// Initializes a new instance of the <see cref="BaseResponse"/> class.
45 /// </summary>
46 /// <param name="currentUrl">The current URL.</param>
47 /// <param name="urlBuilder">The URL builder.</param>
48 /// <param name="serverUtility">The server utility.</param>
49 /// <param name="routeMatch">The route match.</param>
50 protected BaseResponse(UrlInfo currentUrl, IUrlBuilder urlBuilder, IServerUtility serverUtility, RouteMatch routeMatch)
52 this.currentUrl = currentUrl;
53 this.urlBuilder = urlBuilder;
54 this.serverUtility = serverUtility;
55 this.routeMatch = routeMatch;
58 #region abstracts
60 /// <summary>
61 /// Gets or sets the status code.
62 /// </summary>
63 /// <value>The status code.</value>
64 public abstract int StatusCode { get; set; }
66 /// <summary>
67 /// Gets or sets the status code.
68 /// </summary>
69 /// <value>The status code.</value>
70 public abstract string StatusDescription { get; set; }
72 /// <summary>
73 /// Gets or sets the type of the content.
74 /// </summary>
75 /// <value>The type of the content.</value>
76 public abstract string ContentType { get; set; }
78 /// <summary>
79 /// Gets the caching policy (expiration time, privacy,
80 /// vary clauses) of a Web page.
81 /// </summary>
82 /// <value></value>
83 public abstract HttpCachePolicy CachePolicy { get; set; }
85 /// <summary>
86 /// Sets the Cache-Control HTTP header to Public or Private.
87 /// </summary>
88 /// <value></value>
89 public abstract string CacheControlHeader { get; set; }
91 /// <summary>
92 /// Gets or sets the HTTP character set of the output stream.
93 /// </summary>
94 /// <value></value>
95 public abstract string Charset { get; set; }
97 /// <summary>
98 /// Gets the output.
99 /// </summary>
100 /// <value>The output.</value>
101 public abstract TextWriter Output { get; set; }
103 /// <summary>
104 /// Gets the output stream.
105 /// </summary>
106 /// <value>The output stream.</value>
107 public abstract Stream OutputStream { get; }
109 /// <summary>
110 /// Appends the header.
111 /// </summary>
112 /// <param name="name">The name.</param>
113 /// <param name="value">The value.</param>
114 public abstract void AppendHeader(string name, string value);
115 /// <summary>
116 /// Writes the buffer to the browser
117 /// </summary>
118 /// <param name="buffer">The buffer.</param>
119 public abstract void BinaryWrite(byte[] buffer);
120 /// <summary>
121 /// Writes the stream to the browser
122 /// </summary>
123 /// <param name="stream">The stream.</param>
124 public abstract void BinaryWrite(Stream stream);
125 /// <summary>
126 /// Clears the response (only works if buffered)
127 /// </summary>
128 public abstract void Clear();
129 /// <summary>
130 /// Clears the response content (only works if buffered).
131 /// </summary>
132 public abstract void ClearContent();
133 /// <summary>
134 /// Writes the specified string.
135 /// </summary>
136 /// <param name="s">The string.</param>
137 public abstract void Write(string s);
138 /// <summary>
139 /// Writes the specified obj.
140 /// </summary>
141 /// <param name="obj">The obj.</param>
142 public abstract void Write(object obj);
143 /// <summary>
144 /// Writes the specified char.
145 /// </summary>
146 /// <param name="ch">The char.</param>
147 public abstract void Write(char ch);
148 /// <summary>
149 /// Writes the specified buffer.
150 /// </summary>
151 /// <param name="buffer">The buffer.</param>
152 /// <param name="index">The index.</param>
153 /// <param name="count">The count.</param>
154 public abstract void Write(char[] buffer, int index, int count);
155 /// <summary>
156 /// Writes the file.
157 /// </summary>
158 /// <param name="fileName">Name of the file.</param>
159 public abstract void WriteFile(string fileName);
160 /// <summary>
161 /// Gets a value indicating whether the response sent a redirect.
162 /// </summary>
163 /// <value><c>true</c> if was redirected; otherwise, <c>false</c>.</value>
164 public abstract bool WasRedirected { get; }
166 /// <summary>
167 /// Gets a value indicating whether this instance is client connected.
168 /// </summary>
169 /// <value>
170 /// <c>true</c> if this instance is client connected; otherwise, <c>false</c>.
171 /// </value>
172 public abstract bool IsClientConnected { get; }
174 /// <summary>
175 /// Redirects to the specified url
176 /// </summary>
177 /// <param name="url">An relative or absolute URL to redirect the client to</param>
178 /// <param name="endProcess">if set to <c>true</c>, sends the redirect and
179 /// kills the current request process.</param>
180 public abstract void RedirectToUrl(string url, bool endProcess);
182 #endregion
184 /// <summary>
185 /// Redirects to the site root directory (<c>Context.ApplicationPath + "/"</c>).
186 /// </summary>
187 public void RedirectToSiteRoot()
189 RedirectToUrl(currentUrl.AppVirtualDir + "/");
192 /// <summary>
193 /// Redirects to the specified url
194 /// </summary>
195 /// <param name="url">An relative or absolute URL to redirect the client to</param>
196 public void RedirectToUrl(string url)
198 RedirectToUrl(url, false);
201 /// <summary>
202 /// Redirects to the specified url
203 /// </summary>
204 /// <param name="url">An relative or absolute URL to redirect the client to</param>
205 /// <param name="queryStringParameters">The querystring entries</param>
206 public void RedirectToUrl(string url, IDictionary queryStringParameters)
208 if (queryStringParameters != null && queryStringParameters.Count != 0)
210 if (url.IndexOf('?') != -1)
212 url += '&';
214 else
216 url += '?';
218 url += CommonUtils.BuildQueryString(serverUtility, queryStringParameters, false);
220 RedirectToUrl(url, false);
223 /// <summary>
224 /// Redirects to the specified url
225 /// </summary>
226 /// <param name="url">An relative or absolute URL to redirect the client to</param>
227 /// <param name="queryStringParameters">The querystring entries</param>
228 public void RedirectToUrl(string url, NameValueCollection queryStringParameters)
230 if (queryStringParameters != null && queryStringParameters.Count != 0)
232 if (url.IndexOf('?') != -1)
234 url += '&';
236 else
238 url += '?';
240 url += CommonUtils.BuildQueryString(serverUtility, queryStringParameters, false);
242 RedirectToUrl(url, false);
245 /// <summary>
246 /// Redirects to the specified url
247 /// </summary>
248 /// <param name="url">An relative or absolute URL to redirect the client to</param>
249 /// <param name="queryStringParameters">The querystring entries</param>
250 public void RedirectToUrl(string url, params string[] queryStringParameters)
252 RedirectToUrl(url, DictHelper.Create(queryStringParameters));
255 /// <summary>
256 /// Redirects to the specified url
257 /// </summary>
258 /// <param name="url">An relative or absolute URL to redirect the client to</param>
259 /// <param name="queryStringAnonymousDictionary">The querystring entries as an anonymous dictionary</param>
260 public void RedirectToUrl(string url, object queryStringAnonymousDictionary)
262 RedirectToUrl(url, new ReflectionBasedDictionaryAdapter(queryStringAnonymousDictionary));
265 /// <summary>
266 /// Redirects to another controller's action.
267 /// </summary>
268 /// <param name="controller">The controller name to be redirected to.</param>
269 /// <param name="action">The desired action on the target controller.</param>
270 public void Redirect(string controller, string action)
272 RedirectToUrl(urlBuilder.BuildUrl(currentUrl, new UrlBuilderParameters(controller, action)), false);
275 /// <summary>
276 /// Redirects to another controller's action with the specified parameters.
277 /// </summary>
278 /// <param name="controller">The controller name to be redirected to.</param>
279 /// <param name="action">The desired action on the target controller.</param>
280 /// <param name="queryStringParameters">The querystring entries</param>
281 public void Redirect(string controller, string action, NameValueCollection queryStringParameters)
283 RedirectToUrl(urlBuilder.BuildUrl(currentUrl, new UrlBuilderParameters(controller, action).
284 SetQueryString(queryStringParameters)), false);
287 /// <summary>
288 /// Redirects to another controller's action with the specified parameters.
289 /// </summary>
290 /// <param name="controller">The controller name to be redirected to.</param>
291 /// <param name="action">The desired action on the target controller.</param>
292 /// <param name="queryStringParameters">The querystring entries</param>
293 public void Redirect(string controller, string action, IDictionary queryStringParameters)
295 RedirectToUrl(urlBuilder.BuildUrl(currentUrl, new UrlBuilderParameters(controller, action).
296 SetQueryString(queryStringParameters)), false);
299 /// <summary>
300 /// Redirects to another controller's action with the specified parameters.
301 /// </summary>
302 /// <param name="controller">The controller name to be redirected to.</param>
303 /// <param name="action">The desired action on the target controller.</param>
304 /// <param name="queryStringAnonymousDictionary">The querystring entries as an anonymous dictionary</param>
305 public void Redirect(string controller, string action, object queryStringAnonymousDictionary)
307 RedirectToUrl(urlBuilder.BuildUrl(currentUrl, new UrlBuilderParameters(controller, action).
308 SetQueryString(new ReflectionBasedDictionaryAdapter(queryStringAnonymousDictionary))), false);
311 /// <summary>
312 /// Redirects to another controller's action in a different area.
313 /// </summary>
314 /// <param name="area">The area the target controller belongs to.</param>
315 /// <param name="controller">The controller name to be redirected to.</param>
316 /// <param name="action">The desired action on the target controller.</param>
317 public void Redirect(string area, string controller, string action)
319 RedirectToUrl(urlBuilder.BuildUrl(currentUrl, new UrlBuilderParameters(area, controller, action)), false);
322 /// <summary>
323 /// Redirects to another controller's action in a different area with the specified parameters.
324 /// </summary>
325 /// <param name="area">The area the target controller belongs to.</param>
326 /// <param name="controller">The controller name to be redirected to.</param>
327 /// <param name="action">The desired action on the target controller.</param>
328 /// <param name="queryStringParameters">The querystring entries</param>
329 public void Redirect(string area, string controller, string action, IDictionary queryStringParameters)
331 RedirectToUrl(urlBuilder.BuildUrl(currentUrl, new UrlBuilderParameters(area, controller, action).
332 SetQueryString(queryStringParameters)), false);
335 /// <summary>
336 /// Redirects to another controller's action in a different area with the specified parameters.
337 /// </summary>
338 /// <param name="area">The area the target controller belongs to.</param>
339 /// <param name="controller">The controller name to be redirected to.</param>
340 /// <param name="action">The desired action on the target controller.</param>
341 /// <param name="queryStringParameters">The querystring entries</param>
342 public void Redirect(string area, string controller, string action, NameValueCollection queryStringParameters)
344 RedirectToUrl(urlBuilder.BuildUrl(currentUrl, new UrlBuilderParameters(area, controller, action).
345 SetQueryString(queryStringParameters)), false);
348 /// <summary>
349 /// Redirects to another controller's action in a different area with the specified parameters.
350 /// </summary>
351 /// <param name="area">The area the target controller belongs to.</param>
352 /// <param name="controller">The controller name to be redirected to.</param>
353 /// <param name="action">The desired action on the target controller.</param>
354 /// <param name="queryStringAnonymousDictionary">The querystring entries as an anonymous dictionary</param>
355 public void Redirect(string area, string controller, string action, object queryStringAnonymousDictionary)
357 RedirectToUrl(urlBuilder.BuildUrl(currentUrl, new UrlBuilderParameters(area, controller, action).
358 SetQueryString(new ReflectionBasedDictionaryAdapter(queryStringAnonymousDictionary))), false);
361 /// <summary>
362 /// Tries to resolve the target redirect url by using the routing rules registered.
363 /// </summary>
364 /// <param name="controller">The controller name to be redirected to.</param>
365 /// <param name="action">The desired action on the target controller.</param>
366 /// <param name="useCurrentRouteParams">if set to <c>true</c> the current request matching route rules will be used.</param>
367 public void RedirectUsingRoute(string controller, string action, bool useCurrentRouteParams)
369 UrlBuilderParameters @params = new UrlBuilderParameters(controller, action).
370 SetRouteMatch(useCurrentRouteParams, routeMatch);
371 RedirectToUrl(urlBuilder.BuildUrl(currentUrl, @params), false);
374 /// <summary>
375 /// Tries to resolve the target redirect url by using the routing rules registered.
376 /// </summary>
377 /// <param name="area">The area the target controller belongs to.</param>
378 /// <param name="controller">The controller name to be redirected to.</param>
379 /// <param name="action">The desired action on the target controller.</param>
380 /// <param name="useCurrentRouteParams">if set to <c>true</c> the current request matching route rules will be used.</param>
381 public void RedirectUsingRoute(string area, string controller, string action, bool useCurrentRouteParams)
383 UrlBuilderParameters @params = new UrlBuilderParameters(area, controller, action).
384 SetRouteMatch(useCurrentRouteParams, routeMatch);
385 RedirectToUrl(urlBuilder.BuildUrl(currentUrl, @params), false);
388 /// <summary>
389 /// Tries to resolve the target redirect url by using the routing rules registered.
390 /// </summary>
391 /// <param name="controller">The controller name to be redirected to.</param>
392 /// <param name="action">The desired action on the target controller.</param>
393 /// <param name="routeParameters">The routing rule parameters.</param>
394 public void RedirectUsingRoute(string controller, string action, IDictionary routeParameters)
396 UrlBuilderParameters @params = new UrlBuilderParameters(controller, action);
397 @params.RouteParameters = routeParameters;
398 RedirectToUrl(urlBuilder.BuildUrl(currentUrl, @params), false);
401 /// <summary>
402 /// Tries to resolve the target redirect url by using the routing rules registered.
403 /// </summary>
404 /// <param name="controller">The controller name to be redirected to.</param>
405 /// <param name="action">The desired action on the target controller.</param>
406 /// <param name="routeParameters">The routing rule parameters.</param>
407 public void RedirectUsingRoute(string controller, string action, object routeParameters)
409 UrlBuilderParameters @params = new UrlBuilderParameters(controller, action);
410 @params.RouteParameters = routeParameters;
411 RedirectToUrl(urlBuilder.BuildUrl(currentUrl, @params), false);
414 /// <summary>
415 /// Tries to resolve the target redirect url by using the routing rules registered.
416 /// </summary>
417 /// <param name="area">The area the target controller belongs to.</param>
418 /// <param name="controller">The controller name to be redirected to.</param>
419 /// <param name="action">The desired action on the target controller.</param>
420 /// <param name="routeParameters">The routing rule parameters.</param>
421 public void RedirectUsingRoute(string area, string controller, string action, IDictionary routeParameters)
423 UrlBuilderParameters @params = new UrlBuilderParameters(area, controller, action);
424 @params.RouteParameters = routeParameters;
425 RedirectToUrl(urlBuilder.BuildUrl(currentUrl, @params), false);
428 /// <summary>
429 /// Tries to resolve the target redirect url by using the routing rules registered.
430 /// </summary>
431 /// <param name="area">The area the target controller belongs to.</param>
432 /// <param name="controller">The controller name to be redirected to.</param>
433 /// <param name="action">The desired action on the target controller.</param>
434 /// <param name="routeParameters">The routing rule parameters.</param>
435 public void RedirectUsingRoute(string area, string controller, string action, object routeParameters)
437 UrlBuilderParameters @params = new UrlBuilderParameters(area, controller, action);
438 @params.RouteParameters = routeParameters;
439 RedirectToUrl(urlBuilder.BuildUrl(currentUrl, @params), false);
442 /// <summary>
443 /// Creates the cookie.
444 /// </summary>
445 /// <param name="name">The name.</param>
446 /// <param name="cookieValue">The cookie value.</param>
447 public virtual void CreateCookie(string name, string cookieValue)
449 HttpCookie cookie = new HttpCookie(name, cookieValue);
450 cookie.Path = SafeAppPath();
451 CreateCookie(cookie);
454 /// <summary>
455 /// Creates the cookie.
456 /// </summary>
457 /// <param name="name">The name.</param>
458 /// <param name="cookieValue">The cookie value.</param>
459 /// <param name="expiration">The expiration.</param>
460 public virtual void CreateCookie(string name, string cookieValue, DateTime expiration)
462 HttpCookie cookie = new HttpCookie(name, cookieValue);
463 cookie.Expires = expiration;
464 cookie.Path = SafeAppPath();
465 CreateCookie(cookie);
468 /// <summary>
469 /// Creates the cookie.
470 /// </summary>
471 /// <param name="cookie">The cookie.</param>
472 public abstract void CreateCookie(HttpCookie cookie);
474 /// <summary>
475 /// Removes the cookie.
476 /// </summary>
477 /// <param name="name">The name.</param>
478 public virtual void RemoveCookie(string name)
480 HttpCookie cookie = new HttpCookie(name, "");
482 cookie.Expires = DateTime.Now.AddYears(-10);
483 cookie.Path = SafeAppPath();
485 CreateCookie(cookie);
488 #region IMockResponse
490 /// <summary>
491 /// Gets the urls the request was redirected to.
492 /// </summary>
493 /// <value>The redirected to.</value>
494 public virtual string RedirectedTo
496 get { throw new NotImplementedException(); }
499 /// <summary>
500 /// Gets the output.
501 /// </summary>
502 /// <value>The output.</value>
503 public virtual string OutputContent
505 get { throw new NotImplementedException(); }
508 /// <summary>
509 /// Gets or sets the URL info.
510 /// </summary>
511 /// <value>The URL info.</value>
512 public UrlInfo UrlInfo
514 get { return urlInfo; }
515 set { urlInfo = value; }
518 /// <summary>
519 /// Gets or sets the URL builder.
520 /// </summary>
521 /// <value>The URL builder.</value>
522 public IUrlBuilder UrlBuilder
524 get { return urlBuilder; }
525 set { urlBuilder = value; }
528 #endregion
530 private string SafeAppPath()
532 return currentUrl.AppVirtualDir == string.Empty ? "/" : currentUrl.AppVirtualDir;