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
.Adapters
18 using System
.Security
.Principal
;
20 using System
.Collections
;
21 using Castle
.MonoRail
.Framework
;
22 using Castle
.MonoRail
.Framework
.Container
;
25 /// Adapter to expose a valid <see cref="IEngineContext"/>
26 /// implementation on top of <c>HttpContext</c>.
28 public class DefaultEngineContext
: AbstractServiceContainer
, IEngineContext
30 private readonly IMonoRailContainer container
;
31 private readonly HttpContext context
;
32 private readonly UrlInfo urlInfo
;
33 private readonly IServerUtility server
;
34 private readonly IRequest request
;
35 private readonly IResponse response
;
37 private IDictionary session
;
39 private Exception lastException
;
40 // private IDictionary _session;
41 // private String _url;
42 // private ICacheProvider _cache;
43 // private IServiceProvider container;
44 // private bool customSessionSet;
45 private IController currentController
;
46 private IControllerContext controllerContext
;
49 /// Initializes a new instance of the <see cref="DefaultEngineContext"/> class.
51 /// <param name="container">The container.</param>
52 /// <param name="urlInfo">Url information</param>
53 /// <param name="context">The context.</param>
54 /// <param name="server">The server.</param>
55 /// <param name="request">The request.</param>
56 /// <param name="response">The response.</param>
57 /// <param name="trace">The trace.</param>
58 /// <param name="session">The session.</param>
59 public DefaultEngineContext(IMonoRailContainer container
, UrlInfo urlInfo
,
60 HttpContext context
, IServerUtility server
,
61 IRequest request
, IResponse response
, ITrace trace
,
65 this.container
= container
;
66 this.context
= context
;
67 this.urlInfo
= urlInfo
;
68 this.request
= request
;
69 this.response
= response
;
70 this.session
= session
;
76 /// Gets the underlying context of the API being used.
79 public HttpContext UnderlyingContext
81 get { return context; }
85 /// Gets a reference to the MonoRail services.
87 /// <value>The services.</value>
88 public IMonoRailServices Services
90 get { return container; }
94 /// Gets the last exception raised during
95 /// the execution of an action.
98 public Exception LastException
100 get { return lastException; }
101 set { lastException = value; }
105 // /// Gets the request type (GET, POST, etc)
107 // /// <value></value>
108 // public String RequestType
110 // get { return _context.Request.RequestType; }
114 // /// Gets the request URL.
116 // /// <value></value>
119 // get { return _url; }
120 // set { _url = value; }
124 /// Access the session objects.
127 public IDictionary Session
129 get { return session; }
130 set { session = value; }
134 /// Gets the request object.
137 public IRequest Request
139 get { return request; }
143 /// Gets the response object.
146 public IResponse Response
148 get { return response; }
152 /// Gets the trace object.
157 get { return trace; }
161 /// Returns an <see cref="IServerUtility"/>.
164 public IServerUtility Server
166 get { return server; }
170 // /// Access the Cache associated with this
171 // /// web execution context.
173 // /// <value></value>
174 // public ICacheProvider Cache
176 // get { return _cache; }
180 /// Access a dictionary of volative items.
185 get { return flash; }
186 set { flash = value; }
190 // /// Transfer the execution to another resource.
192 // /// <param name="path"></param>
193 // /// <param name="preserveForm"></param>
194 // public void Transfer(String path, bool preserveForm)
196 // _context.Server.Transfer(path, preserveForm);
200 /// Gets or sets the current user.
203 public IPrincipal CurrentUser
205 get { return context.User; }
206 set { context.User = value; }
210 /// Returns the <see cref="UrlInfo"/> of the the current request.
213 public UrlInfo UrlInfo
215 get { return urlInfo; }
219 /// Returns the application virtual path.
222 public String ApplicationPath
228 if (UnderlyingContext
!= null)
230 path
= UnderlyingContext
.Request
.ApplicationPath
;
232 if ("/".Equals(path
))
239 // Huh? That is supposed to be the virtual path
240 path
= AppDomain
.CurrentDomain
.BaseDirectory
;
248 /// Returns the physical application path.
250 public String ApplicationPhysicalPath
256 if (UnderlyingContext
!= null)
258 path
= UnderlyingContext
.Request
.PhysicalApplicationPath
;
262 path
= AppDomain
.CurrentDomain
.BaseDirectory
;
270 /// Returns the Items collection from the current HttpContext.
273 public IDictionary Items
275 get { return UnderlyingContext.Items; }
279 /// Gets or sets the current controller.
281 /// <value>The current controller.</value>
282 public IController CurrentController
284 get { return currentController; }
285 set { currentController = value; }
289 /// Gets or sets the current controller context.
291 /// <value>The current controller context.</value>
292 public IControllerContext CurrentControllerContext
294 get { return controllerContext; }
295 set { controllerContext = value; }
299 // /// If a container is available for the app, this
300 // /// property exposes its instance.
302 // public IServiceProvider Container
304 // get { return container; }