Removed untyped contructor from ComponentRegistration and add a protected setter.
[castle.git] / MonoRail / Castle.MonoRail.Framework / Internal / BaseResponse.cs
blob9d0748d276edda97ce5dd66c64dd36f316c502c3
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 readonly string referrer;
37 private IUrlBuilder urlBuilder;
38 /// <summary>
39 /// Indicates if a redirected has been issued
40 /// </summary>
41 protected bool redirected;
42 private UrlInfo urlInfo;
44 /// <summary>
45 /// Initializes a new instance of the <see cref="BaseResponse"/> class.
46 /// </summary>
47 /// <param name="currentUrl">The current URL.</param>
48 /// <param name="urlBuilder">The URL builder.</param>
49 /// <param name="serverUtility">The server utility.</param>
50 /// <param name="routeMatch">The route match.</param>
51 /// <param name="referrer">The referrer.</param>
52 protected BaseResponse(UrlInfo currentUrl, IUrlBuilder urlBuilder, IServerUtility serverUtility, RouteMatch routeMatch, string referrer)
54 this.currentUrl = currentUrl;
55 this.urlBuilder = urlBuilder;
56 this.serverUtility = serverUtility;
57 this.routeMatch = routeMatch;
58 this.referrer = referrer;
61 #region abstracts
63 /// <summary>
64 /// Gets or sets the status code.
65 /// </summary>
66 /// <value>The status code.</value>
67 public abstract int StatusCode { get; set; }
69 /// <summary>
70 /// Gets or sets the status code.
71 /// </summary>
72 /// <value>The status code.</value>
73 public abstract string StatusDescription { get; set; }
75 /// <summary>
76 /// Gets or sets the type of the content.
77 /// </summary>
78 /// <value>The type of the content.</value>
79 public abstract string ContentType { get; set; }
81 /// <summary>
82 /// Gets the caching policy (expiration time, privacy,
83 /// vary clauses) of a Web page.
84 /// </summary>
85 /// <value></value>
86 public abstract HttpCachePolicy CachePolicy { get; set; }
88 /// <summary>
89 /// Sets the Cache-Control HTTP header to Public or Private.
90 /// </summary>
91 /// <value></value>
92 public abstract string CacheControlHeader { get; set; }
94 /// <summary>
95 /// Gets or sets the HTTP character set of the output stream.
96 /// </summary>
97 /// <value></value>
98 public abstract string Charset { get; set; }
100 /// <summary>
101 /// Gets the output.
102 /// </summary>
103 /// <value>The output.</value>
104 public abstract TextWriter Output { get; set; }
106 /// <summary>
107 /// Gets the output stream.
108 /// </summary>
109 /// <value>The output stream.</value>
110 public abstract Stream OutputStream { get; }
112 /// <summary>
113 /// Appends the header.
114 /// </summary>
115 /// <param name="name">The name.</param>
116 /// <param name="value">The value.</param>
117 public abstract void AppendHeader(string name, string value);
118 /// <summary>
119 /// Writes the buffer to the browser
120 /// </summary>
121 /// <param name="buffer">The buffer.</param>
122 public abstract void BinaryWrite(byte[] buffer);
123 /// <summary>
124 /// Writes the stream to the browser
125 /// </summary>
126 /// <param name="stream">The stream.</param>
127 public abstract void BinaryWrite(Stream stream);
128 /// <summary>
129 /// Clears the response (only works if buffered)
130 /// </summary>
131 public abstract void Clear();
132 /// <summary>
133 /// Clears the response content (only works if buffered).
134 /// </summary>
135 public abstract void ClearContent();
136 /// <summary>
137 /// Writes the specified string.
138 /// </summary>
139 /// <param name="s">The string.</param>
140 public abstract void Write(string s);
141 /// <summary>
142 /// Writes the specified obj.
143 /// </summary>
144 /// <param name="obj">The obj.</param>
145 public abstract void Write(object obj);
146 /// <summary>
147 /// Writes the specified char.
148 /// </summary>
149 /// <param name="ch">The char.</param>
150 public abstract void Write(char ch);
151 /// <summary>
152 /// Writes the specified buffer.
153 /// </summary>
154 /// <param name="buffer">The buffer.</param>
155 /// <param name="index">The index.</param>
156 /// <param name="count">The count.</param>
157 public abstract void Write(char[] buffer, int index, int count);
158 /// <summary>
159 /// Writes the file.
160 /// </summary>
161 /// <param name="fileName">Name of the file.</param>
162 public abstract void WriteFile(string fileName);
163 /// <summary>
164 /// Gets a value indicating whether the response sent a redirect.
165 /// </summary>
166 /// <value><c>true</c> if was redirected; otherwise, <c>false</c>.</value>
167 public abstract bool WasRedirected { get; }
169 /// <summary>
170 /// Gets a value indicating whether this instance is client connected.
171 /// </summary>
172 /// <value>
173 /// <c>true</c> if this instance is client connected; otherwise, <c>false</c>.
174 /// </value>
175 public abstract bool IsClientConnected { get; }
177 /// <summary>
178 /// Redirects to the specified url
179 /// </summary>
180 /// <param name="url">An relative or absolute URL to redirect the client to</param>
181 /// <param name="endProcess">if set to <c>true</c>, sends the redirect and
182 /// kills the current request process.</param>
183 public abstract void RedirectToUrl(string url, bool endProcess);
185 #endregion
187 /// <summary>
188 /// Redirects to url using referrer.
189 /// </summary>
190 public void RedirectToReferrer()
192 if (referrer == null)
194 throw new InvalidOperationException("No referrer available");
196 RedirectToUrl(referrer);
199 /// <summary>
200 /// Redirects to the site root directory (<c>Context.ApplicationPath + "/"</c>).
201 /// </summary>
202 public void RedirectToSiteRoot()
204 RedirectToUrl(currentUrl.AppVirtualDir + "/");
207 /// <summary>
208 /// Redirects to the specified url
209 /// </summary>
210 /// <param name="url">An relative or absolute URL to redirect the client to</param>
211 public void RedirectToUrl(string url)
213 RedirectToUrl(url, false);
216 /// <summary>
217 /// Redirects to the specified url
218 /// </summary>
219 /// <param name="url">An relative or absolute URL to redirect the client to</param>
220 /// <param name="queryStringParameters">The querystring entries</param>
221 public void RedirectToUrl(string url, IDictionary queryStringParameters)
223 if (queryStringParameters != null && queryStringParameters.Count != 0)
225 if (url.IndexOf('?') != -1)
227 url += '&';
229 else
231 url += '?';
233 url += CommonUtils.BuildQueryString(serverUtility, queryStringParameters, false);
235 RedirectToUrl(url, false);
238 /// <summary>
239 /// Redirects to the specified url
240 /// </summary>
241 /// <param name="url">An relative or absolute URL to redirect the client to</param>
242 /// <param name="queryStringParameters">The querystring entries</param>
243 public void RedirectToUrl(string url, NameValueCollection queryStringParameters)
245 if (queryStringParameters != null && queryStringParameters.Count != 0)
247 if (url.IndexOf('?') != -1)
249 url += '&';
251 else
253 url += '?';
255 url += CommonUtils.BuildQueryString(serverUtility, queryStringParameters, false);
257 RedirectToUrl(url, false);
260 /// <summary>
261 /// Redirects to the specified url
262 /// </summary>
263 /// <param name="url">An relative or absolute URL to redirect the client to</param>
264 /// <param name="queryStringParameters">The querystring entries</param>
265 public void RedirectToUrl(string url, params string[] queryStringParameters)
267 RedirectToUrl(url, DictHelper.Create(queryStringParameters));
270 /// <summary>
271 /// Redirects to the specified url
272 /// </summary>
273 /// <param name="url">An relative or absolute URL to redirect the client to</param>
274 /// <param name="queryStringAnonymousDictionary">The querystring entries as an anonymous dictionary</param>
275 public void RedirectToUrl(string url, object queryStringAnonymousDictionary)
277 RedirectToUrl(url, new ReflectionBasedDictionaryAdapter(queryStringAnonymousDictionary));
280 /// <summary>
281 /// Redirects to another controller's action.
282 /// </summary>
283 /// <param name="controller">The controller name to be redirected to.</param>
284 /// <param name="action">The desired action on the target controller.</param>
285 public void Redirect(string controller, string action)
287 RedirectToUrl(urlBuilder.BuildUrl(currentUrl, new UrlBuilderParameters(controller, action)), false);
290 /// <summary>
291 /// Redirects to another controller's action with the specified parameters.
292 /// </summary>
293 /// <param name="controller">The controller name to be redirected to.</param>
294 /// <param name="action">The desired action on the target controller.</param>
295 /// <param name="queryStringParameters">The querystring entries</param>
296 public void Redirect(string controller, string action, NameValueCollection queryStringParameters)
298 RedirectToUrl(urlBuilder.BuildUrl(currentUrl, new UrlBuilderParameters(controller, action).
299 SetQueryString(queryStringParameters)), false);
302 /// <summary>
303 /// Redirects to another controller's action with the specified parameters.
304 /// </summary>
305 /// <param name="controller">The controller name to be redirected to.</param>
306 /// <param name="action">The desired action on the target controller.</param>
307 /// <param name="queryStringParameters">The querystring entries</param>
308 public void Redirect(string controller, string action, IDictionary queryStringParameters)
310 RedirectToUrl(urlBuilder.BuildUrl(currentUrl, new UrlBuilderParameters(controller, action).
311 SetQueryString(queryStringParameters)), false);
314 /// <summary>
315 /// Redirects to another controller's action with the specified parameters.
316 /// </summary>
317 /// <param name="controller">The controller name to be redirected to.</param>
318 /// <param name="action">The desired action on the target controller.</param>
319 /// <param name="queryStringAnonymousDictionary">The querystring entries as an anonymous dictionary</param>
320 public void Redirect(string controller, string action, object queryStringAnonymousDictionary)
322 RedirectToUrl(urlBuilder.BuildUrl(currentUrl, new UrlBuilderParameters(controller, action).
323 SetQueryString(new ReflectionBasedDictionaryAdapter(queryStringAnonymousDictionary))), false);
326 /// <summary>
327 /// Redirects to another controller's action in a different area.
328 /// </summary>
329 /// <param name="area">The area the target controller belongs to.</param>
330 /// <param name="controller">The controller name to be redirected to.</param>
331 /// <param name="action">The desired action on the target controller.</param>
332 public void Redirect(string area, string controller, string action)
334 RedirectToUrl(urlBuilder.BuildUrl(currentUrl, new UrlBuilderParameters(area, controller, action)), false);
337 /// <summary>
338 /// Redirects to another controller's action in a different area with the specified parameters.
339 /// </summary>
340 /// <param name="area">The area the target controller belongs to.</param>
341 /// <param name="controller">The controller name to be redirected to.</param>
342 /// <param name="action">The desired action on the target controller.</param>
343 /// <param name="queryStringParameters">The querystring entries</param>
344 public void Redirect(string area, string controller, string action, IDictionary queryStringParameters)
346 RedirectToUrl(urlBuilder.BuildUrl(currentUrl, new UrlBuilderParameters(area, controller, action).
347 SetQueryString(queryStringParameters)), false);
350 /// <summary>
351 /// Redirects to another controller's action in a different area with the specified parameters.
352 /// </summary>
353 /// <param name="area">The area the target controller belongs to.</param>
354 /// <param name="controller">The controller name to be redirected to.</param>
355 /// <param name="action">The desired action on the target controller.</param>
356 /// <param name="queryStringParameters">The querystring entries</param>
357 public void Redirect(string area, string controller, string action, NameValueCollection queryStringParameters)
359 RedirectToUrl(urlBuilder.BuildUrl(currentUrl, new UrlBuilderParameters(area, controller, action).
360 SetQueryString(queryStringParameters)), false);
363 /// <summary>
364 /// Redirects to another controller's action in a different area with the specified parameters.
365 /// </summary>
366 /// <param name="area">The area the target controller belongs to.</param>
367 /// <param name="controller">The controller name to be redirected to.</param>
368 /// <param name="action">The desired action on the target controller.</param>
369 /// <param name="queryStringAnonymousDictionary">The querystring entries as an anonymous dictionary</param>
370 public void Redirect(string area, string controller, string action, object queryStringAnonymousDictionary)
372 RedirectToUrl(urlBuilder.BuildUrl(currentUrl, new UrlBuilderParameters(area, controller, action).
373 SetQueryString(new ReflectionBasedDictionaryAdapter(queryStringAnonymousDictionary))), false);
376 /// <summary>
377 /// Redirects using a named route.
378 /// The name must exists otherwise a <see cref="MonoRailException"/> will be thrown.
379 /// </summary>
380 /// <param name="routeName">Route name.</param>
381 public void RedirectUsingNamedRoute(string routeName)
383 UrlBuilderParameters @params = new UrlBuilderParameters();
384 @params.RouteName = routeName;
385 RedirectToUrl(urlBuilder.BuildUrl(currentUrl, @params), false);
388 /// <summary>
389 /// Redirects using a named route.
390 /// The name must exists otherwise a <see cref="MonoRailException"/> will be thrown.
391 /// </summary>
392 /// <param name="routeName">Route name.</param>
393 /// <param name="routeParameters">The route parameters.</param>
394 public void RedirectUsingNamedRoute(string routeName, object routeParameters)
396 UrlBuilderParameters @params = new UrlBuilderParameters();
397 @params.RouteName = routeName;
398 @params.RouteParameters = routeParameters;
399 RedirectToUrl(urlBuilder.BuildUrl(currentUrl, @params), false);
402 /// <summary>
403 /// Redirects using a named route.
404 /// The name must exists otherwise a <see cref="MonoRailException"/> will be thrown.
405 /// </summary>
406 /// <param name="routeName">Route name.</param>
407 /// <param name="routeParameters">The route parameters.</param>
408 public void RedirectUsingNamedRoute(string routeName, IDictionary routeParameters)
410 UrlBuilderParameters @params = new UrlBuilderParameters();
411 @params.RouteName = routeName;
412 @params.RouteParameters = routeParameters;
413 RedirectToUrl(urlBuilder.BuildUrl(currentUrl, @params), false);
416 /// <summary>
417 /// Tries to resolve the target redirect url by using the routing rules registered.
418 /// </summary>
419 /// <param name="controller">The controller name to be redirected to.</param>
420 /// <param name="action">The desired action on the target controller.</param>
421 /// <param name="useCurrentRouteParams">if set to <c>true</c> the current request matching route rules will be used.</param>
422 public void RedirectUsingRoute(string controller, string action, bool useCurrentRouteParams)
424 UrlBuilderParameters @params = new UrlBuilderParameters(controller, action).
425 SetRouteMatch(useCurrentRouteParams, routeMatch);
426 RedirectToUrl(urlBuilder.BuildUrl(currentUrl, @params), false);
429 /// <summary>
430 /// Tries to resolve the target redirect url by using the routing rules registered.
431 /// </summary>
432 /// <param name="area">The area the target controller belongs to.</param>
433 /// <param name="controller">The controller name to be redirected to.</param>
434 /// <param name="action">The desired action on the target controller.</param>
435 /// <param name="useCurrentRouteParams">if set to <c>true</c> the current request matching route rules will be used.</param>
436 public void RedirectUsingRoute(string area, string controller, string action, bool useCurrentRouteParams)
438 UrlBuilderParameters @params = new UrlBuilderParameters(area, controller, action).
439 SetRouteMatch(useCurrentRouteParams, routeMatch);
440 RedirectToUrl(urlBuilder.BuildUrl(currentUrl, @params), false);
443 /// <summary>
444 /// Tries to resolve the target redirect url by using the routing rules registered.
445 /// </summary>
446 /// <param name="controller">The controller name to be redirected to.</param>
447 /// <param name="action">The desired action on the target controller.</param>
448 /// <param name="routeParameters">The routing rule parameters.</param>
449 public void RedirectUsingRoute(string controller, string action, IDictionary routeParameters)
451 UrlBuilderParameters @params = new UrlBuilderParameters(controller, action);
452 @params.RouteParameters = routeParameters;
453 RedirectToUrl(urlBuilder.BuildUrl(currentUrl, @params), false);
456 /// <summary>
457 /// Tries to resolve the target redirect url by using the routing rules registered.
458 /// </summary>
459 /// <param name="controller">The controller name to be redirected to.</param>
460 /// <param name="action">The desired action on the target controller.</param>
461 /// <param name="routeParameters">The routing rule parameters.</param>
462 public void RedirectUsingRoute(string controller, string action, object routeParameters)
464 UrlBuilderParameters @params = new UrlBuilderParameters(controller, action);
465 @params.RouteParameters = routeParameters;
466 RedirectToUrl(urlBuilder.BuildUrl(currentUrl, @params), false);
469 /// <summary>
470 /// Tries to resolve the target redirect url by using the routing rules registered.
471 /// </summary>
472 /// <param name="area">The area the target controller belongs to.</param>
473 /// <param name="controller">The controller name to be redirected to.</param>
474 /// <param name="action">The desired action on the target controller.</param>
475 /// <param name="routeParameters">The routing rule parameters.</param>
476 public void RedirectUsingRoute(string area, string controller, string action, IDictionary routeParameters)
478 UrlBuilderParameters @params = new UrlBuilderParameters(area, controller, action);
479 @params.RouteParameters = routeParameters;
480 RedirectToUrl(urlBuilder.BuildUrl(currentUrl, @params), false);
483 /// <summary>
484 /// Tries to resolve the target redirect url by using the routing rules registered.
485 /// </summary>
486 /// <param name="area">The area the target controller belongs to.</param>
487 /// <param name="controller">The controller name to be redirected to.</param>
488 /// <param name="action">The desired action on the target controller.</param>
489 /// <param name="routeParameters">The routing rule parameters.</param>
490 public void RedirectUsingRoute(string area, string controller, string action, object routeParameters)
492 UrlBuilderParameters @params = new UrlBuilderParameters(area, controller, action);
493 @params.RouteParameters = routeParameters;
494 RedirectToUrl(urlBuilder.BuildUrl(currentUrl, @params), false);
497 /// <summary>
498 /// Creates the cookie.
499 /// </summary>
500 /// <param name="name">The name.</param>
501 /// <param name="cookieValue">The cookie value.</param>
502 public virtual void CreateCookie(string name, string cookieValue)
504 HttpCookie cookie = new HttpCookie(name, cookieValue);
505 cookie.Path = SafeAppPath();
506 CreateCookie(cookie);
509 /// <summary>
510 /// Creates the cookie.
511 /// </summary>
512 /// <param name="name">The name.</param>
513 /// <param name="cookieValue">The cookie value.</param>
514 /// <param name="expiration">The expiration.</param>
515 public virtual void CreateCookie(string name, string cookieValue, DateTime expiration)
517 HttpCookie cookie = new HttpCookie(name, cookieValue);
518 cookie.Expires = expiration;
519 cookie.Path = SafeAppPath();
520 CreateCookie(cookie);
523 /// <summary>
524 /// Creates the cookie.
525 /// </summary>
526 /// <param name="cookie">The cookie.</param>
527 public abstract void CreateCookie(HttpCookie cookie);
529 /// <summary>
530 /// Removes the cookie.
531 /// </summary>
532 /// <param name="name">The name.</param>
533 public virtual void RemoveCookie(string name)
535 HttpCookie cookie = new HttpCookie(name, "");
537 cookie.Expires = DateTime.Now.AddYears(-10);
538 cookie.Path = SafeAppPath();
540 CreateCookie(cookie);
543 #region IMockResponse
545 /// <summary>
546 /// Gets the urls the request was redirected to.
547 /// </summary>
548 /// <value>The redirected to.</value>
549 public virtual string RedirectedTo
551 get { throw new NotImplementedException(); }
554 /// <summary>
555 /// Gets the output.
556 /// </summary>
557 /// <value>The output.</value>
558 public virtual string OutputContent
560 get { throw new NotImplementedException(); }
563 /// <summary>
564 /// Gets or sets the URL info.
565 /// </summary>
566 /// <value>The URL info.</value>
567 public UrlInfo UrlInfo
569 get { return urlInfo; }
570 set { urlInfo = value; }
573 /// <summary>
574 /// Gets or sets the URL builder.
575 /// </summary>
576 /// <value>The URL builder.</value>
577 public IUrlBuilder UrlBuilder
579 get { return urlBuilder; }
580 set { urlBuilder = value; }
583 #endregion
585 private string SafeAppPath()
587 return currentUrl.AppVirtualDir == string.Empty ? "/" : currentUrl.AppVirtualDir;