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
.Internal
18 using System
.Collections
;
19 using System
.Collections
.Specialized
;
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
;
39 /// Indicates if a redirected has been issued
41 protected bool redirected
;
42 private UrlInfo urlInfo
;
45 /// Initializes a new instance of the <see cref="BaseResponse"/> class.
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
;
64 /// Gets or sets the status code.
66 /// <value>The status code.</value>
67 public abstract int StatusCode { get; set; }
70 /// Gets or sets the status code.
72 /// <value>The status code.</value>
73 public abstract string StatusDescription { get; set; }
76 /// Gets or sets the type of the content.
78 /// <value>The type of the content.</value>
79 public abstract string ContentType { get; set; }
82 /// Gets the caching policy (expiration time, privacy,
83 /// vary clauses) of a Web page.
86 public abstract HttpCachePolicy CachePolicy { get; set; }
89 /// Sets the Cache-Control HTTP header to Public or Private.
92 public abstract string CacheControlHeader { get; set; }
95 /// Gets or sets the HTTP character set of the output stream.
98 public abstract string Charset { get; set; }
103 /// <value>The output.</value>
104 public abstract TextWriter Output { get; set; }
107 /// Gets the output stream.
109 /// <value>The output stream.</value>
110 public abstract Stream OutputStream { get; }
113 /// Appends the header.
115 /// <param name="name">The name.</param>
116 /// <param name="value">The value.</param>
117 public abstract void AppendHeader(string name
, string value);
119 /// Writes the buffer to the browser
121 /// <param name="buffer">The buffer.</param>
122 public abstract void BinaryWrite(byte[] buffer
);
124 /// Writes the stream to the browser
126 /// <param name="stream">The stream.</param>
127 public abstract void BinaryWrite(Stream stream
);
129 /// Clears the response (only works if buffered)
131 public abstract void Clear();
133 /// Clears the response content (only works if buffered).
135 public abstract void ClearContent();
137 /// Writes the specified string.
139 /// <param name="s">The string.</param>
140 public abstract void Write(string s
);
142 /// Writes the specified obj.
144 /// <param name="obj">The obj.</param>
145 public abstract void Write(object obj
);
147 /// Writes the specified char.
149 /// <param name="ch">The char.</param>
150 public abstract void Write(char ch
);
152 /// Writes the specified buffer.
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
);
161 /// <param name="fileName">Name of the file.</param>
162 public abstract void WriteFile(string fileName
);
164 /// Gets a value indicating whether the response sent a redirect.
166 /// <value><c>true</c> if was redirected; otherwise, <c>false</c>.</value>
167 public abstract bool WasRedirected { get; }
170 /// Gets a value indicating whether this instance is client connected.
173 /// <c>true</c> if this instance is client connected; otherwise, <c>false</c>.
175 public abstract bool IsClientConnected { get; }
178 /// Redirects to the specified url
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
);
188 /// Redirects to url using referrer.
190 public void RedirectToReferrer()
192 if (referrer
== null)
194 throw new InvalidOperationException("No referrer available");
196 RedirectToUrl(referrer
);
200 /// Redirects to the site root directory (<c>Context.ApplicationPath + "/"</c>).
202 public void RedirectToSiteRoot()
204 RedirectToUrl(currentUrl
.AppVirtualDir
+ "/");
208 /// Redirects to the specified url
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);
217 /// Redirects to the specified url
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)
233 url
+= CommonUtils
.BuildQueryString(serverUtility
, queryStringParameters
, false);
235 RedirectToUrl(url
, false);
239 /// Redirects to the specified url
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)
255 url
+= CommonUtils
.BuildQueryString(serverUtility
, queryStringParameters
, false);
257 RedirectToUrl(url
, false);
261 /// Redirects to the specified url
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
));
271 /// Redirects to the specified url
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
));
281 /// Redirects to another controller's action.
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);
291 /// Redirects to another controller's action with the specified parameters.
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);
303 /// Redirects to another controller's action with the specified parameters.
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);
315 /// Redirects to another controller's action with the specified parameters.
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);
327 /// Redirects to another controller's action in a different area.
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);
338 /// Redirects to another controller's action in a different area with the specified parameters.
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);
351 /// Redirects to another controller's action in a different area with the specified parameters.
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);
364 /// Redirects to another controller's action in a different area with the specified parameters.
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);
377 /// Redirects using a named route.
378 /// The name must exists otherwise a <see cref="MonoRailException"/> will be thrown.
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);
389 /// Redirects using a named route.
390 /// The name must exists otherwise a <see cref="MonoRailException"/> will be thrown.
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);
403 /// Redirects using a named route.
404 /// The name must exists otherwise a <see cref="MonoRailException"/> will be thrown.
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);
417 /// Tries to resolve the target redirect url by using the routing rules registered.
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);
430 /// Tries to resolve the target redirect url by using the routing rules registered.
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);
444 /// Tries to resolve the target redirect url by using the routing rules registered.
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);
457 /// Tries to resolve the target redirect url by using the routing rules registered.
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);
470 /// Tries to resolve the target redirect url by using the routing rules registered.
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);
484 /// Tries to resolve the target redirect url by using the routing rules registered.
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);
498 /// Creates the cookie.
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
);
510 /// Creates the cookie.
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
);
524 /// Creates the cookie.
526 /// <param name="cookie">The cookie.</param>
527 public abstract void CreateCookie(HttpCookie cookie
);
530 /// Removes the cookie.
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
546 /// Gets the urls the request was redirected to.
548 /// <value>The redirected to.</value>
549 public virtual string RedirectedTo
551 get { throw new NotImplementedException(); }
557 /// <value>The output.</value>
558 public virtual string OutputContent
560 get { throw new NotImplementedException(); }
564 /// Gets or sets the URL info.
566 /// <value>The URL info.</value>
567 public UrlInfo UrlInfo
569 get { return urlInfo; }
570 set { urlInfo = value; }
574 /// Gets or sets the URL builder.
576 /// <value>The URL builder.</value>
577 public IUrlBuilder UrlBuilder
579 get { return urlBuilder; }
580 set { urlBuilder = value; }
585 private string SafeAppPath()
587 return currentUrl
.AppVirtualDir
== string.Empty
? "/" : currentUrl
.AppVirtualDir
;