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
18 using System
.Collections
;
20 using System
.Web
.SessionState
;
26 public abstract class BaseHttpHandler
: IHttpHandler
31 protected readonly IController controller
;
33 /// The controller context
35 protected readonly IControllerContext controllerContext
;
37 /// The engine context
39 protected readonly IEngineContext engineContext
;
40 private readonly bool sessionless
;
43 /// Initializes a new instance of the <see cref="BaseHttpHandler"/> class.
45 /// <param name="engineContext">The engine context.</param>
46 /// <param name="controller">The controller.</param>
47 /// <param name="controllerContext">The controller context.</param>
48 /// <param name="sessionless">if set to <c>true</c> then we wont have a session to work.</param>
49 protected BaseHttpHandler(IEngineContext engineContext
,
50 IController controller
, IControllerContext controllerContext
, bool sessionless
)
52 this.controller
= controller
;
53 this.controllerContext
= controllerContext
;
54 this.engineContext
= engineContext
;
55 this.sessionless
= sessionless
;
63 /// <param name="context"></param>
64 public void ProcessRequest(HttpContext context
)
72 public bool IsReusable
80 /// Performs the base work of MonoRail. Extracts
81 /// the information from the URL, obtain the controller
82 /// that matches this information and dispatch the execution
85 /// <param name="context">The context.</param>
86 public virtual void Process(HttpContext context
)
88 BeforeControllerProcess(context
);
92 engineContext
.Services
.ExtensionManager
.RaisePreProcessController(engineContext
);
94 controller
.Process(engineContext
, controllerContext
);
96 engineContext
.Services
.ExtensionManager
.RaisePostProcessController(engineContext
);
100 HttpResponse response
= context
.Response
;
102 if (response
.StatusCode
== 200)
104 response
.StatusCode
= 500;
107 engineContext
.LastException
= ex
;
109 engineContext
.Services
.ExtensionManager
.RaiseUnhandledError(engineContext
);
111 throw new MonoRailException("Error processing MonoRail request. Action " +
112 controllerContext
.Action
+ " on controller " + controllerContext
.Name
, ex
);
116 AfterControllerProcess();
121 /// Handles MonoRail's actions afters the cotroller action finished processing
123 protected void AfterControllerProcess()
130 PersistCustomSession();
132 ReleaseController(controller
);
136 /// Handles MonoRail's actions before the controller action started processing
138 /// <param name="context">The context.</param>
139 protected void BeforeControllerProcess(HttpContext context
)
143 // Now we have a session
144 engineContext
.Session
= ResolveSession(context
);
147 IDictionary session
= engineContext
.Session
;
153 flash
= new Flash((Flash
) session
[Flash
.FlashKey
]);
160 engineContext
.Flash
= flash
;
162 // items added to be used by the test context
163 context
.Items
["mr.controller"] = controller
;
164 context
.Items
["mr.flash"] = engineContext
.Flash
;
165 context
.Items
["mr.propertybag"] = controllerContext
.PropertyBag
;
166 context
.Items
["mr.session"] = context
.Session
;
168 AcquireCustomSession();
172 /// Resolves the session.
174 /// <param name="context">The context.</param>
175 /// <returns></returns>
176 protected virtual IDictionary
ResolveSession(HttpContext context
)
180 if (context
.Items
["AspSession"] != null)
182 // Windows and Testing
183 session
= context
.Items
["AspSession"];
188 session
= context
.Session
;
191 if (session
is HttpSessionState
)
193 return new SessionAdapter(session
as HttpSessionState
);
197 return (IDictionary
)session
;
202 /// Acquires the custom session from the custom session.
204 protected virtual void AcquireCustomSession()
206 engineContext
.Services
.ExtensionManager
.RaiseAcquireRequestState(engineContext
);
210 /// Persists the custom session to the custom session.
212 protected virtual void PersistCustomSession()
214 engineContext
.Services
.ExtensionManager
.RaiseReleaseRequestState(engineContext
);
217 private void ReleaseController(IController controller
)
219 engineContext
.Services
.ControllerFactory
.Release(controller
);
222 private void PersistFlashItems()
224 Flash currentFlash
= engineContext
.Flash
;
226 if (currentFlash
== null) return;
228 currentFlash
.Sweep();
230 if (currentFlash
.HasItemsToKeep
)
232 engineContext
.Session
[Flash
.FlashKey
] = currentFlash
;
234 else if (engineContext
.Session
.Contains(Flash
.FlashKey
))
236 engineContext
.Session
.Remove(Flash
.FlashKey
);