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 IUrlBuilder urlBuilder
;
38 /// Indicates if a redirected has been issued
40 protected bool redirected
;
41 private UrlInfo urlInfo
;
44 /// Initializes a new instance of the <see cref="BaseResponse"/> class.
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
;
61 /// Gets or sets the status code.
63 /// <value>The status code.</value>
64 public abstract int StatusCode { get; set; }
67 /// Gets or sets the status code.
69 /// <value>The status code.</value>
70 public abstract string StatusDescription { get; set; }
73 /// Gets or sets the type of the content.
75 /// <value>The type of the content.</value>
76 public abstract string ContentType { get; set; }
79 /// Gets the caching policy (expiration time, privacy,
80 /// vary clauses) of a Web page.
83 public abstract HttpCachePolicy CachePolicy { get; set; }
86 /// Sets the Cache-Control HTTP header to Public or Private.
89 public abstract string CacheControlHeader { get; set; }
92 /// Gets or sets the HTTP character set of the output stream.
95 public abstract string Charset { get; set; }
100 /// <value>The output.</value>
101 public abstract TextWriter Output { get; set; }
104 /// Gets the output stream.
106 /// <value>The output stream.</value>
107 public abstract Stream OutputStream { get; }
110 /// Appends the header.
112 /// <param name="name">The name.</param>
113 /// <param name="value">The value.</param>
114 public abstract void AppendHeader(string name
, string value);
116 /// Writes the buffer to the browser
118 /// <param name="buffer">The buffer.</param>
119 public abstract void BinaryWrite(byte[] buffer
);
121 /// Writes the stream to the browser
123 /// <param name="stream">The stream.</param>
124 public abstract void BinaryWrite(Stream stream
);
126 /// Clears the response (only works if buffered)
128 public abstract void Clear();
130 /// Clears the response content (only works if buffered).
132 public abstract void ClearContent();
134 /// Writes the specified string.
136 /// <param name="s">The string.</param>
137 public abstract void Write(string s
);
139 /// Writes the specified obj.
141 /// <param name="obj">The obj.</param>
142 public abstract void Write(object obj
);
144 /// Writes the specified char.
146 /// <param name="ch">The char.</param>
147 public abstract void Write(char ch
);
149 /// Writes the specified buffer.
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
);
158 /// <param name="fileName">Name of the file.</param>
159 public abstract void WriteFile(string fileName
);
161 /// Gets a value indicating whether the response sent a redirect.
163 /// <value><c>true</c> if was redirected; otherwise, <c>false</c>.</value>
164 public abstract bool WasRedirected { get; }
167 /// Gets a value indicating whether this instance is client connected.
170 /// <c>true</c> if this instance is client connected; otherwise, <c>false</c>.
172 public abstract bool IsClientConnected { get; }
175 /// Redirects to the specified url
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
);
185 /// Redirects to the site root directory (<c>Context.ApplicationPath + "/"</c>).
187 public void RedirectToSiteRoot()
189 RedirectToUrl(currentUrl
.AppVirtualDir
+ "/");
193 /// Redirects to the specified url
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);
202 /// Redirects to the specified url
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)
218 url
+= CommonUtils
.BuildQueryString(serverUtility
, queryStringParameters
, false);
220 RedirectToUrl(url
, false);
224 /// Redirects to the specified url
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)
240 url
+= CommonUtils
.BuildQueryString(serverUtility
, queryStringParameters
, false);
242 RedirectToUrl(url
, false);
246 /// Redirects to the specified url
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
));
256 /// Redirects to the specified url
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
));
266 /// Redirects to another controller's action.
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);
276 /// Redirects to another controller's action with the specified parameters.
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);
288 /// Redirects to another controller's action with the specified parameters.
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);
300 /// Redirects to another controller's action with the specified parameters.
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);
312 /// Redirects to another controller's action in a different area.
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);
323 /// Redirects to another controller's action in a different area with the specified parameters.
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);
336 /// Redirects to another controller's action in a different area with the specified parameters.
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);
349 /// Redirects to another controller's action in a different area with the specified parameters.
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);
362 /// Tries to resolve the target redirect url by using the routing rules registered.
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);
375 /// Tries to resolve the target redirect url by using the routing rules registered.
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);
389 /// Tries to resolve the target redirect url by using the routing rules registered.
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);
402 /// Tries to resolve the target redirect url by using the routing rules registered.
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);
415 /// Tries to resolve the target redirect url by using the routing rules registered.
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);
429 /// Tries to resolve the target redirect url by using the routing rules registered.
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);
443 /// Creates the cookie.
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
);
455 /// Creates the cookie.
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
);
469 /// Creates the cookie.
471 /// <param name="cookie">The cookie.</param>
472 public abstract void CreateCookie(HttpCookie cookie
);
475 /// Removes the cookie.
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
491 /// Gets the urls the request was redirected to.
493 /// <value>The redirected to.</value>
494 public virtual string RedirectedTo
496 get { throw new NotImplementedException(); }
502 /// <value>The output.</value>
503 public virtual string OutputContent
505 get { throw new NotImplementedException(); }
509 /// Gets or sets the URL info.
511 /// <value>The URL info.</value>
512 public UrlInfo UrlInfo
514 get { return urlInfo; }
515 set { urlInfo = value; }
519 /// Gets or sets the URL builder.
521 /// <value>The URL builder.</value>
522 public IUrlBuilder UrlBuilder
524 get { return urlBuilder; }
525 set { urlBuilder = value; }
530 private string SafeAppPath()
532 return currentUrl
.AppVirtualDir
== string.Empty
? "/" : currentUrl
.AppVirtualDir
;